POJ 2115 C Looooops(拡張ユークリッド)拡張ユークリッド詳細


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 < 2
k) modulo 2
k.
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 < 2
k) 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

題意によりサイクル数を求め、デッドサイクル出力FOEVER
構想:公式の変形によってC*x=(B-A)%2^kがモードに転化する線形方程式を得ることができる
</pre><pre name="code" class="cpp">#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
LL x,y;
LL exgcd(LL a,LL n)
{
    LL t,d;
    if(!n)//    a*x+b*y=gcd(a,b){       };  n-->b
    {
        x=1;y=0;// n==0,a*x=gcd(a,0)=a;  x=1,y=0;
        return a;
    }//      
    d=exgcd(n,a%n);//      gcd(a,n)=gcd(n,a%n),      1     
    //b*x1+(a%b)*y1==b*x1+(a-(a/b)*b)*y1==a*y1+b*(x1-(a/b)*y1),    ax+by==a*y1+b*(x1-(a/b)*y1)
    t=x;//     x=y1(y),y1(y)=x1-(a/b)*y1
    x=y;
    y=t-a/n*y;
    return d;
}
int main()
{
    LL n,k,i,j,A,B,C,a,b,d;
    while(~scanf("%lld%lld%lld%lld",&A,&B,&C,&k))
    {
        if(A==0&&B==0&&C==0&&k==0)//            ax+by=gcd(a,b);
            break;
        n=(1LL<<k);//   1  LL WA- -!
        a=C;b=B-A;//    ,    C*x=(A-B)%2^k;          a*x=b%n;
        d=exgcd(a,n);
        if(b%d)//             b%(gcd(a,n))==0
        {
            puts("FOREVER");
            continue;
        }//      
        x=x*(b/d)%n;//  a*x=b%n    
        x=(x%(n/d)+(n/d))%(n/d);// x%(n/gcd(a,n))      -n/gcd(a,n)~n/gcd(a,n),
        //   n/gcd(a,n)     0~2*n/gcd(a,n),
//   n/gcd(a,n),         
        printf("%lld
",x); } return 0; }

構想:拡張ユークリッドはax+by=c=>一、ax+by=GCD(a,b)であり、この問題二、Cx+my=B-Aを通じて、同時に公式Cx+A+y 2^k=BすなわちCx=(B-A)%2^kを得ることができる.
私たちはb=(B-A);ex拡張GCD式によりxを求めるが、ここでのxは式二Cx+my=bのxではないことに注意し、式一の押し出し式二から式一を同時にb/GCD(C,2^k)に乗じることができる.
これがx=x*(b/GCD()の理由である.またx取得範囲の問題,(x 0+k*(m/GCD()),y 0+k*(C/GCD()));(T_Tやっと分かった!!!!)
#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
#define LL __int64
#define inf 0x3f3f3f3f
const double PI=acos(-1.0);
using namespace std;

void ex(LL a,LL b,LL &d,LL &x,LL &y){
    if(!b){
        d=a,x=1,y=0;
    }
    else{
        ex(b,a%b,d,y,x); y-=x*(a/b);
    }
}
int main(){
    LL b,B,A,C,n,k,d,x,y;
    while(~scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)){
        if(!A&&!B&&!C&&!k) break;
        n=(1LL<<k);
        b=B-A;
        ex(C,n,d,x,y);
        if(b%d==0){
            x=x*(b/d)%n;
            x=(x%(n/d)+n/d)%(n/d);
            printf("%I64d
",x); } else{ puts("FOREVER"); } } return 0; }