[数论]POJ 2115/HOJ 1787 C Looooops 扩展欧几里得算法

来源:互联网 发布:手机淘宝页面设计 编辑:程序博客网 时间:2024/05/20 00:14

传送门:C Looooops

C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16096 Accepted: 4138

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 163 7 2 167 3 2 163 4 2 160 0 0 0

Sample Output

0232766FOREVER

Source

CTU Open 2004

解题报告:

鸣谢:小優YoU

大致题意:对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。若在有限次内结束,则输出循环次数。否则输出死循环。 解题思路:题意不难理解,只是利用了 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>using namespace std;//d=ax+by,其中最大公约数d=gcd(a,n),x、y为方程系数,返回值为d、x、ylong long ext_gcd(long long a,long long b,long long &x,long long &y){    if(b==0){        x=1;y=0; return a; //d=a,x=1,y=0,此时等式d=ax+by成立    }    long long d=ext_gcd(b,a%b,x,y);    long long xt=x;    x=y;    y=xt-a/b*y; //系数x、y的取值是为满足等式d=ax+by    return d;}int main(){    long long A,B,C,K;    while(scanf("%lld%lld%lld%lld",&A,&B,&C,&K)==4){        if(!A&&!B&&!C&&!K)            break;        long long a=C;        long long b=B-A;        long long n=(long long)1<<K;  //2^k        long long x,y;        long long d=ext_gcd(a,n,x,y); //求d=gcd(a,n)和方程d=ax+by系数x,y        if(b%d!=0) //方程ax=b(mod n)无解            printf("FOREVER\n");        else{            x=(x*(b/d))%n; //方程ax=b(nod n)的最小解            x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最整数小解            printf("%lld\n",x);        }    }    return 0;}



0 0
原创粉丝点击