POJ-2115 C Looooops (模线性方程)

C Looooops

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24380   Accepted: 6793

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

CTU Open 2004

首先列出基本方程组 A+C*x=B+(2^k)*y   →   C*x+(2^k)*y=B-A  这样就满足了 ax+by=1 的模型,所以通过扩展欧几里得求出即可。

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4 typedef long long LL;
 5 LL A,B,C,k;
 6 LL a,b,c,d,e,f,x,y;
 7 LL exgcd(LL a,LL b,LL &x,LL &y){
 8     if (b==0)
 9     {x=1;
10      y=0;
11      return a;
12     }
13     LL d=exgcd(b,a%b,x,y);
14     LL t=x;
15     x=y;
16     y=t-(a/b)*y;
17     return d;
18 }
19 int main(){
20     freopen ("c.in","r",stdin);
21     freopen ("c.out","w",stdout);
22     int i,j;
23     while (1)
24     {scanf("%lld%lld%lld%lld",&A,&B,&C,&k);
25      if (A==0 && B==0 && C==0 && k==0)
26       break;
27      a=C,b=1ll<<k,c=B-A;
28      d=exgcd(a,b,x,y);
29      if (c==0)
30      {puts("0");
31       continue;
32      }
33      if (c%d!=0)
34      {puts("FOREVER");
35       continue;
36      }
37      e=x*(c/d);
38      f=(e%(b/d)+b/d)%(b/d);
39      printf("%lld\n",f);
40     }
41     return 0;
42 }
时间: 2024-10-12 08:35:19

POJ-2115 C Looooops (模线性方程)的相关文章

POJ 2115 简单的模线性方程求解

简单的扩展欧几里得题 这里 2^k 不能自作聪明的用 1<<k来写 , k >= 31时就爆int了 , 即使定义为long long 也不能直接这样写 后来老老实实 for(int i=1 ; i<=k ; i++) bb = bb*2; 才过了= = 1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 #define ll long long 6 ll ex_gcd(ll

POJ 2115 C Looooops(扩展欧几里得应用)

题目地址:POJ 2115 水题..公式很好推.最直接的公式就是a+n*c==b+m*2^k.然后可以变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod(2^k)的最小解.(真搞不懂为什么训练的时候好多人把青蛙的约会都给做出来了,这题却一直做不出来.....这两道不都是推公式然后变形吗.....) 代码如下: #include <iostream> #include <cstdio> #include <string> #incl

POJ 2115 C Looooops(模线性方程)

http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思路: 根据题意原题可化成c * x = b - a mod (2 ^ k),然后解这个模线性方程. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio>

poj 2115 C Looooops (解模线性方程)

链接:poj 2115 题意:对于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 分析:设对

POJ 2115 C Looooops(Exgcd)

[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, 其中变量为k比特无符号整数. [题解] 题目等价于求解Cx=(B–A)(mod 2^k),利用扩展欧几里得算法可以求解该问题 [代码] #include <algorithm> #include <cstring> #include <cstdio> using name

poj 2115 C Looooops

C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18799   Accepted: 4924 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 w

POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转化一下形式. 上式可以用同余方程表示为  a + k*c = (b) % (1<<d)   <-->  k*c = (b-a) % (1<<d)(中间应该是全等号,打不出来…).这就是我们想要的同余方程,根据我的个人习惯,我把它转化为线性方程的形式. -->   c*

POJ 2115 C Looooops (线性同余方程)

C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19141   Accepted: 5049 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 w

POJ - 2115 - C Looooops (扩展欧几里得)

C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19826   Accepted: 5299 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 w