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 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

分析:

扩展欧几里得:

对于ax+by=c,求最小整数解

d=gcd(a,b)

1.先求ax0+by0=d的x0,y0。然后两边同乘d得a(x0*c/d)+b(y0*c/d)=gcd(a,b)*c/d=c,求出x=x0*c/d,y=y0*c/d

2.求最小解:由上求出特解x,最小解=(x%(b/d)+b/d)%(b/d)

参考学习网站:http://www.cnblogs.com/comeon4mydream/archive/2011/07/18/2109060.html

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string>
 5 #include<cstring>
 6 #include<vector>
 7 using namespace std;
 8 typedef long long ll;
 9 ll exgcd(ll a,ll b,ll &x,ll &y){//扩展欧几里得算法
10     if(b==0){
11         x=1;
12         y=0;
13         return a;
14     }
15     ll d=exgcd(b,a%b,x,y);
16     ll t=y;
17     y=x-a/b*y;
18     x=t;
19     return d;
20 }
21 int main(){
22     ll A, B, C, k;
23     while(cin>>A>>B>>C>>k&&(A||B||C||k)){
24         ll b=1ll<<k,x,y;
25         ll c=B-A,a=C;
26         ll d=exgcd(a,b,x,y);
27         if(c%d){
28             cout<<"FOREVER"<<endl;
29         }
30         else{
31             b=b/d;//如果少了这歩,最终求得的就可能不是最小整数解,而是某一特解。可以仔细想一想为什么b要除以d
32             //cout<<d<<endl;
33             cout<<((x*c/d%b+b)%b)<<endl;//求最小整数解
34         }
35     }
36     return 0;
37 }
时间: 2024-10-29 04:37:19

poj 2115 C Looooops的相关文章

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 (解模线性方程)

链接: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(模线性方程)

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(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: 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

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

辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } 扩展欧几里得算法 时间复杂度和欧几里得算法相同 int extgcd(int a, int b, int& x, int& y) { int d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x;

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 statem

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*