IT民工
加油!

被虐得很惨的一道题,WA了无数次,根据题意推出 C * x + (2 ^ k) * y = B - A,用拓展欧几里德求出x的最小值。如果无解则是死循环!

 

/*Accepted    164K    0MS    C++    882B    2012-07-19 12:03:12*/

#include<cstdio>
#include<cstring>
#include<cstdlib>
#define LL long long


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;
}

int main()
{
    LL a, b, c, e, n;
    int k;
    LL x, y;
    while( scanf("%lld%lld%lld%d", &a, &b, &c, &k) == 4)
    {
        if( a == 0 && b == 0 && c == 0 && k == 0)
            break;
        n = 1LL << k ;
        e = extgcd( c, n, x, y);
        LL t = b - a ;
        if( t % e != 0)
        {
            printf( "FOREVER\n");
        }
        else{
            x = t / e * x; // x = ((t / e) * x) % n + n
            x = ( x % (n / e) + (n / e)) % (n / e); //x = x % (n / e);
            printf( "%lld\n", x) ;
        }
    }
    return 0;
}

 

 

 

 

posted on 2012-07-19 12:10  找回失去的  阅读(195)  评论(0编辑  收藏  举报