http://poj.org/problem?id=2115
作为一个数论白痴 遇到数论题我就有一种想死的冲动
经过一个上午的奋斗 最终有一个地方还是看不懂
问世间数论何物,只叫我生不如死。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define LL long long
using namespace std;
LL exgcd(LL a,LL b,LL *x,LL *y)
{
if(b==0)
{
*x=1;
*y=0;
return a;
}
LL k=exgcd(b,a%b,x,y);
LL temp=*x;
*x=*y;
*y=temp-a/b*(*y);
return k;
}
int main()
{
LL A,B,C,k;
while(cin>>A>>B>>C>>k)
{
if(!A&&!B&&!C&&!k)
break;
LL M=(LL)1<<k;//注意移位需要把1先转换成 long long
LL x,y;
LL K=exgcd(C,M,&x,&y);
if((B-A)%K!=0)
{
printf("FOREVER\n");
}else
{
x=(x*(B-A)/K)%M;
x=(x%(M/K)+(M/K))%(M/K);
cout<<x<<endl;
}
}
return 0;
}
浙公网安备 33010602011771号