POJ_2115_扩展欧几里德

C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23673   Accepted: 6540

Description

A Compiler Mystery: We are given a C-language style for loop of type 
for (variable = A; variable != B; variable += C)

statement;

I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k

Input

The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2k) are the parameters of the loop. 

The input is finished by a line containing four zeros. 

Output

The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate. 

Sample Input

3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0

Sample Output

0
2
32766
FOREVER

参考题解:http://blog.csdn.net/lyy289065406/article/details/6648546

 

利用了 k位存储系统 的数据特性进行循环。

例如int型是16位的,那么int能保存2^16个数据,即最大数为65535(本题默认为无符号),

当循环使得i超过65535时,则i会返回0重新开始计数

如i=65534,当i+=3时,i=1

其实就是 i=(65534+3)%(2^16)=1

 

有了这些思想,设对于某组数据要循环x次结束,那么本题就很容易得到方程:

x=[(B-A+2^k)%2^k] /C

Cx=(B-A)(mod 2^k)  此方程为 模线性方程,本题就是求X的值。

 

下面将结合《算法导论》第2版进行简述,因此先把上面的方程变形,统一符号。

令a=C  

  b=B-A 

  n=2^k

那么原模线性方程变形为:

 ax=b (mod n)

该方程有解的充要条件为 gcd(a,n) | b ,即 b% gcd(a,n)==0

令d=gcd(a,n)

有该方程的 最小整数解为 x = e (mod n/d)

其中e = [x0 mod(n/d) + n/d] mod (n/d) ,x0为方程的最小解

那么原题就是要计算b% gcd(a,n)是否为0,若为0则计算最小整数解,否则输出FOREVER

 

当有解时,关键在于计算最大公约数 d=gcd(a,n) 与 最小解x0

参考《算法导论》,引入欧几里得扩展方程  d=ax+by ,

通过EXTENDED_EUCLID算法(P571)求得d、x、y值,其中返回的x就是最小解x0,求d的原理是辗转相除法(欧几里德算法)

再利用MODULAR-LINEAR-EQUATION-SOLVER算法(P564)通过x0计算x值。注意x0可能为负,因此要先 + n/d 再模n/d。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define LL long long

LL x,y;
LL e_gcd(LL a,LL b)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL r=e_gcd(b,a%b);
    LL t=x;
    x=y;
    y=t-a/b*y;
    return r;
}


int main()
{
    LL A,B,C,k;
    while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)!=EOF)
    {
        LL a=C;
        LL n=1ll;
        //cout<<n<<endl;
        LL b=B-A;
        if(A+B+C+k==0)
            break;
        for(int i=0; i<k; i++)
            n<<=1;
        LL g=e_gcd(a,n);
        if(b%g)
        {
            printf("FOREVER\n");
            continue;
        }
        LL t=b/g;
        x*=t;
        x=(x%(n/g)+(n/g))%(n/g);
            printf("%I64d\n",x);
        }
    return 0;
}

 

posted on 2016-08-16 15:00  JASONlee3  阅读(218)  评论(0编辑  收藏  举报

导航