AmazingCounters.com

POJ 2115 C Looooops【数论】

很容易看出来一个同余式,说到底是解一个线性同余方程,计算机解通常有拓展欧几里得和欧拉定理两种算法,参照去年的NOIP水题,问题是这题数据范围是2^32所以要int64 TAT

#include<cstdio>

#include<iostream>

#include<string.h>

#include<math.h>

using namespace std;

__int64 exgcd(__int64 a,__int64 b,__int64&x,__int64 &y)

{

    if(b==0)

    {

       x=1;y=0;return a;

    }

   else

    {

       __int64 r=exgcd(b,a %b,y,x);

       y-=x*(a/b);

       return r;

    }

}

__int64 lme(__int64 a,__int64 b,__int64n)//ax=b(mod n)

{

   __int64 x,y;

   __int64 d=exgcd(a,n,x,y);

   if(b%d!=0)return -1;

   __int64 e=x*(b/d)%n+n;

   return e%(n/d);

}

 

int main()

{

   __int64 a,b,c,k;

   scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);

   while(1)

    {

       __int64 d=lme(c,b-a,1LL<<k);

       if (d==-1)

       {

           printf("FOREVER\n");

       }

       else

       {

           printf("%I64d\n",d);

       }

       scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);

       if(a==0 && b==0 && c==0 && k==0) break;

    }

   return 0;

}

posted @ 2014-10-05 13:35  philippica  阅读(184)  评论(0编辑  收藏  举报