题目:http://poj.org/problem?id=2115
就是扩展欧几里得呗;
然而忘记除公约数...
代码如下:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; ll A,B,C,k,a,b,x,y,g,s; ll gcd(ll a,ll b){return a%b?gcd(b,a%b):b;} void exgcd(ll a,ll b,ll &x,ll &y) { if(!b){x=1; y=0; return;} exgcd(b,a%b,x,y);//最近总是忘写这个啊...无力... ll t=x; x=y; y=t-a/b*y; } int main() { while(1) { scanf("%lld%lld%lld%lld",&A,&B,&C,&k); if(!A&&!B&&!C&&!k)return 0; a=C; b=1ll<<k; g=gcd(a,b); s=(B-A+b)%b; if(s%g){printf("FOREVER "); continue;} a/=g;//() b/=g;//!! s/=g;//! exgcd(a,b,x,y);//ax+by=g x=(x*s%b+b)%b; printf("%lld ",x); } }