POJ 2115 C Looooops

~~~题目链接~~~


题目大意:根据题中的c语言for循环表达式, 判断这个循环要几次结束, 或永远不结束, 当内存达到上限时又从0开始。


思路: 与POJ 1061一样, 根据表达式(a+c*x)%1<<k = b%1<<k  推出  c*x+(1<<k)*y = b-a;

Ps:要用(__int64)1<<k ,估计默认情况下1为带符号的32位

code:

#include <stdio.h>
#include <math.h>
void gcd(__int64 s1, __int64 s2, __int64 &d, __int64 &x, __int64 &y)
{
    if(!s2){d = s1;x = 1;y = 0;}
    else {gcd(s2, s1%s2, d, y, x);y -= x*(s1/s2);}
}
int main()
{
    __int64  a = 0, b = 0, c = 0, d = 0, k = 0, s1 = 0, s2 = 0, x = 0, y = 0, r = 0;
    while(scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &k) , a+b+c+k != 0)
    {
        gcd(c, (__int64)1<<k, d, x, y);
        if((b-a)%d)
            printf("FOREVER\n");
        else
        {
            x *= (b-a)/d;
            r = ((__int64)1<<k)/d;
            printf("%I64d\n", (x%r+r)%r);
        }
    }
    return 0;
}


你可能感兴趣的:(POJ 2115 C Looooops)