POJ2115 C Looooops ——模线性方程(扩展gcd)

题目链接:http://poj.org/problem?id=2115


C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27838 Accepted: 7930

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



题解:

A要跳到B的位置,那么它跳了n次后(绕了若干圈),可以跳到B的位置(这时可以理解为A在这一圈内,要从B的位置开始继续跳,当然循环已经结束)。

可知:( A + n*C ) % len = B。其中len = 1<<k;

那么:A + n*C = m*len + B,即: n*C - m*len = B - A。 由于m、n是任意常数,所以将其符号全部转为正。

即得出一个模线性方程: n*C + m*len = B - A。

标准形式为:a*x + b*y = c,其中 a = C, b = len, c = B-A。

然后就是扩展欧几里得的应用。




代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e5+10;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(a==0 &&b==0) return -1;
    if(b==0) {x=1; y=0; return a;}
    LL d = exgcd(b,a%b,y,x);
    y -= a/b*x;
    return d;
}

int main()
{
#ifdef LOCAL
    freopen("123", "r", stdin);
//      freopen("output.txt", "w", stdout);
#endif
    LL a, b, c, k;
    while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k) && (a || b||c ||k))
    {
        LL len = (1LL<<k);
        LL x, y;
        LL d = exgcd(c, len, x,y);
        LL C = b-a; //C为扩展gcd等式右边的常数项
        if(C%d) //如果常数项不能被最大公约数除尽,则无解
        {
            printf("FOREVER\n");
            continue;
        }

        x = (x*(C/d))%len;  //C/d为倍数
        x = (x%(len/d)+(len/d))%(len/d);   //(len/d)相当于x的通解的斜率k(必为正数),(x+k)%k即得到最小正整数解
        printf("%lld\n",x);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值