有一个比较简单的思路
定义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;
else
cout << n/a1*g << ends << a1*g << endl;
}
return 0;
}
还有一件事 就是在Poj 是RE的
试了很多数据都是对的
自己没找出错在哪??求帮忙??