Problem E:C Looooops(扩展欧几里德)

POJ2115http://poj.org/problem?id=2115

扩展欧几里德+根据题意写方程

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

Source Code

#include<iostream>
#include<cmath>
#define ll long long
using namespace std;

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    ll temp;
    ll gcd=exgcd(b,a%b,x,y);
    temp=x;
    x=y;
    y=temp-a/b*y;
    return gcd;
}
int main()
{
    ll A,B,C,k;
    while(cin>>A>>B>>C>>k)
    {
        ll a,b,c;
        if(A==0&&B==0&&C==0&&k==0)break;
        if(B==A){cout<<0<<endl;continue;}
        ll x,y;
        //if(a==0&&b==0&&c==0&&d==0)return 0;
        a=C;
        b=pow(2,k);
        c=B-A;
        ll gcd=exgcd(a,b,x,y);
        if(c%gcd!=0)cout<<"FOREVER"<<endl;
        else
        {
            x*=c/gcd;
            //y*=(b/gcd);
            x=(x%(b/gcd)+b/gcd)%(b/gcd);
            cout<<x<<endl;
        }
    }
    return 0;
}

 

扩展欧几里德算法求模逆元可以用来解决一类问题,即对于给定的整数a和模数m,求出一个整数x使得ax ≡ 1 (mod m)。下面是用C语言实现扩展欧几里德算法求模逆元的代码: ```c #include <stdio.h> // 求最大公约数 int gcd(int a, int b, int *x, int *y) { if (b == 0) { *x = 1; *y = 0; return a; } int r = gcd(b, a % b, x, y); int t = *x; *x = *y; *y = t - a / b * *y; return r; } // 求模逆元 int mod_inv(int a, int m) { int x, y; int g = gcd(a, m, &x, &y); if (g != 1) { printf("No solution!\n"); return -1; } else { return (x % m + m) % m; } } // 测试 int main() { int a = 5, m = 7; int inv = mod_inv(a, m); if (inv >= 0) { printf("%d 的模 %d 逆元是 %d\n", a, m, inv); } return 0; } ``` 在上面的代码中,我们首先定义了一个函数`gcd`,用来求解最大公约数。这个函数的输入参数包括两个整数a和b,以及两个指向整数的指针x和y。函数返回值是a和b的最大公约数。在函数中,我们采用了递归的方式来求解最大公约数,并利用指针x和y保存了扩展欧几里德算法中每次迭代后的结果。 接下来,我们定义了一个函数`mod_inv`,用来求解模逆元。这个函数的输入参数包括两个整数a和m,表示要求解的模逆元是a关于模数m的逆元。函数返回值是a关于模数m的逆元,如果不存在,则返回-1。在函数中,我们首先调用了函数`gcd`来求解a和m的最大公约数,并利用指针x和y保存了扩展欧几里德算法中每次迭代后的结果。如果a和m的最大公约数不为1,则说明不存在模逆元,函数返回-1。否则,我们利用扩展欧几里德算法的结果来计算模逆元,并返回结果。 最后,在主函数中,我们定义了两个整数a和m,并调用函数`mod_inv`来求解a关于模数m的逆元。如果求解成功,则输出结果,否则输出错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值