poj 2115 C Looooops

这个题就是一个扩展欧几里得的运用;

B≡(A+K*C)%L 即 B = A + K*C + H*L ;B - A = KC + HL ;即线性方程为 a*x + b*y = c;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

#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 Mod = ExGcd( b, a%b ,x ,y );

   LL temp = x;

   x = y;

   y = temp - ( a/b )*y;

   return Mod;

}

int main(  )

{

    LL A,B,C,x,y,k;

    while( scanf( "%I64d %I64d %I64d %I64d",&A,&B,&C,&k ) , A||B||C||k )

    {

        LL L = 1LL << k,p = B -A;

        LL Mod = ExGcd( C , L , x, y );

        if( p % Mod )

        {

           puts( "FOREVER" );    

        }

        else

        {

            L /= Mod ; p /= Mod;

            LL temp = ( p * x )%L;

            while( temp < 0 )

                   temp += L;

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

        }

    }

    //system( "pause" );

    return 0;

}

你可能感兴趣的:(oop)