POJ2115(数论)

#include <stdio.h>
#include <iostream>
#include <string.h>


using namespace std;


typedef long long ll;
ll extgcd( ll a , ll b, ll & x, ll & y)
{
    if( b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }


    ll d = extgcd(b, a%b ,x ,y);
    ll t = x;
    x = y;
    y = t - a/b*y;
    return d;
}




ll modeq( ll a , ll b, ll n)
{
    ll e ,i ,d ,x, y, f;
    d = extgcd(a, n, x, y);
    if(b % d)
     return -1;
    else
    {
        f = n/d < 0 ? ( -1 *n /d) : n/d;
        e = ( x *(b/d)%f + f) %f;
        d = d < 0 ? -1*d : d;
        return e;
    }
}


int main()
{
    ll a, b, c, k, m, tmp, flag;
    while(scanf("%I64d %I64d %I64d %I64d",&a, &b, &c, &k)!=EOF,a+b+c+k)
    {
        m = 1LL << k;
        tmp = b - a;
        flag = modeq(c, tmp, m);
        if(flag == -1)
         printf("FOREVER\n");
        else
         printf("%I64d\n",flag);
    }
  return 0;
}

你可能感兴趣的:(POJ2115(数论))