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+Cx=B(mod2^k), 化简有C*x - 2^k*y = B-A, 注意代入的时候带正的2^k,因为求的是x的最小正整数解,和青蛙不一样?(+1s?)
void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d) {
if(!b) {
d = a, x = , y = ;
} else {
ex_gcd(b, a%b, y, x, d);
y -= x * (a / b);
}
} int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
LL a, b, c, k;
while(cin >> a >> b >> c >> k && a+b+c+k) {
LL x, y, d;
if(b-a == ) {
cout << "0\n";
continue;
}
LL MOD = 1LL << k;
ex_gcd(c, MOD, x, y, d);
if((b-a) % d != ) {
cout << "FOREVER\n";
continue;
}
x = x *(b-a) / d;
LL B = MOD / d;
x = (x % B + B) % B;
cout << x << "\n";
}
return ;
}
												

Day7 - F - C Looooops POJ - 2115的更多相关文章

  1. C Looooops POJ - 2115 (exgcd)

    一个编译器之谜:我们被给了一段C++语言风格的循环 for(int i=A;i!=B;i+=C) 内容; 其中所有数都是k位二进制数,即所有数时膜2^k意义下的.我们的目标时球出 内容 被执行了多少次 ...

  2. C Looooops POJ - 2115 拓展gcd 有一个定理待补()

    补算法导论P564 MODULAR-LINEAR-EQUATION-SOLVER算法(P564)

  3. D - C Looooops POJ - 2115 欧几里德拓展

    题意:就是看看for(; ;)多久停止. 最让我蛋疼的是1L和1LL的区别!让我足足wa了12发! 1L 是long类型的, 1LL为long long类型的! 思路: 这就是欧几里德扩展的标准式子了 ...

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

    题目链接:https://cn.vjudge.net/contest/276376#problem/B 题目大意:for( int  i= A ; i != B; i+ = c ),然后给你A,B,C ...

  5. R - C Looooops POJ - 2115 (exgcd)

    题目大意:很好理解,一个for循环语句,从a开始到b结束,步长是c,模数是pow(2,k) 问,最少循环多少次,才能到达b,如果永远都到不了b,输出FOREVER 题解:其实就是求一个线性方程,cx= ...

  6. C Looooops POJ - 2115

    数论好题.. 香! 首先我们看到这一题, 题意是 \[a + c * x \equiv b (mod \ \ 2 ^ k) \] 对此式移一下项, 得 \[c * x \equiv b - a (mo ...

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

  8. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

  9. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

随机推荐

  1. Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)

    日期:2020.01.27 博客期:135 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备](本期博客) b.[云图制作+数据导入] ...

  2. MVC 拦截器

    https://www.cnblogs.com/blosaa/archive/2011/06/02/2067632.html

  3. 文本输入框UITextField和UITextView

    本文概要 1.简介 2.介绍TextField控件 3.介绍TextView控件 4.键盘的打开和关闭 5.关闭和大开键盘的通知 6.键盘的种类 详情 1.简介 与Label一样,TextField和 ...

  4. Servlet+Spring+Mybatis初试

    1.导入相关的jar包 druid mybatis mybatis-spring pageHelper mysql驱动包 spring-context-support spring-aspect sp ...

  5. FTP、SFTP、SCP的区别

    转载于>> FTP(File Transfer Protocol) 是TCP/IP网络上两台计算机传送文件的协议,FTP是在TCP/IP网络和INTERNET上最早使用的协议之一,它属于网 ...

  6. Java 调用系统系统可执行文件

    public class Test { public static Map<String, String> executeCmd(String cmd) { Runtime rt = Ru ...

  7. ToString 奇淫技巧

    int和float同样结果 decimal decTemp = 2.1m; Console.WriteLine(decTemp.ToString("#0.00")); //输出2. ...

  8. JS - false 的 六种类型

    document.write("--------------");document.write(!false);document.write("------------- ...

  9. 「luogu4366」最短路

    「luogu4366」最短路 传送门 直接连边显然不行,考虑优化. 根据异或的结合律和交换律等优秀性质,我们每次只让当前点向只有一位之别的另一个点连边,然后就直接跑最短路. 注意点数会很多,所以用配对 ...

  10. ubuntu14 安装Node.js

    @brief ubuntu  安装Node.js @date 2018-06-28 @see Ubuntu 上安装 Node.js(https://www.runoob.com/nodejs/node ...