您的位置:首页 > 其它

Poj2429 inverse of LCM&GCD

2015-08-31 12:34 288 查看
有一个比较简单的思路定义N=l/g,所求x,y ;将N素数分解得到N=p1^q1 * p2^q2 * ..... * pk^qk;再定义数组a[k],其中a[i]=pi^qi;定义x1=x/g  y1=y/g  由数论知识知道 x1 和 y1 互素 而且 lcd(x,y)=l所以 a[i] 只能属于x1 和 y1 中一个 再用 dfs() 找出和最小就ok了代码很短哦#include <iostream>#include <cstdio>#include <string>#include <algorithm>#include <vector>#pragma warning(disable:4996)using namespace std;typedef long long LL;const LL INF = 0x3ffffffffffffff;vector<LL> a;LL n;LL a1;void dfs(LL i, LL len, LL sum, LL &d){if (i == len- 1){if (sum + n / sum < d)d = sum + n / sum, a1 = sum;if (sum*a[i] + n / (sum*a[i])<d)d = sum*a[i] + n / (sum*a[i]),a1=sum*a[i];return;}dfs(i + 1, len, sum, d);dfs(i + 1, len, sum*a[i], d);}int main(){LL g, l;while (scanf("%lld%lld", &g, &l)!=EOF){a1 = 0;a.clear();n = l / g;LL tmp = n;for (LL i = 2; i*i <= tmp; ++i){LL t = 1;while (tmp%i == 0){t *= i;tmp /= i;}a.push_back(t);}if (tmp != 1)a.push_back(tmp);LL len = a.size();LL d = INF;LL mul = 1;dfs(0, len, mul, d);if (a1<n/a1)cout << a1*g <<ends<< n / a1*g<< endl;elsecout << n/a1*g << ends << a1*g << endl;}return 0;}
还有一件事 就是在Poj 是RE的
试了很多数据都是对的
自己没找出错在哪??求帮忙??
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: