poj 2115 C Looooops——exgcd模板

题目:http://poj.org/problem?id=2115

exgcd裸题。注意最后各种%b。注意打出正确的exgcd板子。就是别忘了/=g。

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
ll a,b,x,y,r,A,B,C,k,g;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void exgcd(ll a,ll b,ll &x,ll &y)
{
  if(!b){x=1;y=0;return;}
  exgcd(b,a%b,y,x);y-=a/b*x;
}
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);r=B-A;
      ll g=gcd(a,b);
      if(r%g){printf("FOREVER\n");continue;}
      a/=g;b/=g;r/=g;/////////
      exgcd(a,b,x,y);
      printf("%lld\n",(x%b*r%b+b)%b);//
    }
}

猜你喜欢

转载自www.cnblogs.com/Narh/p/9264926.html