zoj Simple Equation 数论

时间:2021-03-28 07:00:36

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5176

AX+BY = XY  => (X-B)*(Y-A)= A*B

对A*B因式分解,这里不要乘起来,分A,B因式分解

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<queue> #define MAXN 105000
#define PI acos(-1.0)
#define LL long long
#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,t) for(int i=s; i<=t; i++)
#define show(x) { cerr<<">>>"<<#x<<" = "<<x<<endl; }
#define showtwo(x,y) { cerr<<">>>"<<#x<<"="<<x<<" "<<#y<<" = "<<y<<endl; }
using namespace std; LL A,B,X,Y,ansX,ansY,M;
int prime[MAXN],cnt; //生成质数表
LL sum_AB;
struct Factor
{
int p,k; //p^k;
}a[];
int pv; void get_prime()
{
bool flag[MAXN];
memset(flag,,sizeof(flag));
cnt = ; for(int i=; i<MAXN; i++)
{
if(!flag[i])
{
prime[cnt++] = i;
for(int j=i+i; j<MAXN; j+=i) flag[j] = true;
}
}
} void factor_analysis(int c,int d)
{
pv = ;
for(int i=; i<cnt && (c||d); i++)
{
if(c%prime[i] == || d%prime[i] == )
{
a[pv].p = prime[i];
a[pv].k = ;
while(c % prime[i] == ) a[pv].k++,c /= prime[i];
while(d % prime[i] == ) a[pv].k++,d /= prime[i];
pv++;
}
}
if(c != ) a[pv].p = c,a[pv].k = ,pv++;
if(d != ) a[pv].p = d,a[pv].k = ,pv++;
} void dfs(int pos,long long mul)
{
if(pos == pv && sum_AB % mul == )
{
X = mul + B;
Y = sum_AB / mul + A;
if(X >= M && (ansX+ansY > X+Y || (ansX+ansY == X+Y && ansX > X)))
ansX = X, ansY = Y;
return;
}
long long accu = ;
for(int i=; i<=a[pos].k; i++)
{
dfs(pos+,mul*accu);
accu *= a[pos].p;
}
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
get_prime();
while(scanf("%lld %lld %lld",&A,&B,&M) == )
{
sum_AB = A*B;
factor_analysis(A,B);
ansX = 1e18+, ansY = ;
dfs(,); if(ansX == 1e18+ ) printf("No answer\n");
else printf("%lld %lld\n",ansX,ansY);
}
}