C Looooops

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23700   Accepted: 6550

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

由题意易得(a+cx)%2^k==b,求x最小值。可得同余方程c*x=(b-a)mod2^k。

 //2016.8.17
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long using namespace std; ll ex_gcd(ll a, ll b, ll& x, ll& y)//扩展欧几里得
{
if(b==)
{
x = ;
y = ;
return a;
}
ll ans = ex_gcd(b, a%b, x, y);
ll tmp = x;
x = y;
y = tmp-(a/b)*y;
return ans;
} int main()
{
ll a, b, c, x, y, res, n;
int k;
while(scanf("%lld%lld%lld%d", &a, &b, &c, &k)!=EOF)
{
if(!a&&!b&&!c&&!k)
break;
n = (ll)<<k;
res = ex_gcd(c, n, x, y);
cout<<res<<endl<<x<<endl;
if((b-a)%res!=)cout<<"FOREVER"<<endl;
else
{
x = x*(b-a)/res%n;//方程ax=b-a(mod n)的最小解
ll tmp = n/res;
x = (x%tmp+tmp)%tmp;//最小正数解
printf("%lld\n", x);
}
} return ;
}
 #include <iostream>
#define ll long long using namespace std; ll ex_gcd(ll a, ll b, ll& x, ll& y){
if(b == ){
x = ;
y = ;
return a;
}
ll ans = ex_gcd(b, a%b, x, y);
ll tmpx = x;
x = y;
y = tmpx-a/b*y;
return ans;
} int main()
{
int a, b, c, k;
while(cin>>a>>b>>c>>k){
if(!a&&!b&&!c&&!k)break;
ll x, y;
ll A = c;
ll B = b-a;
ll n = 1LL<<k;
ll gcd = ex_gcd(A, n, x, y);
if(B%gcd != )
cout<<"FOREVER"<<endl;
else{
x = (x*(B/gcd))%n;
x = (x%(n/gcd)+n/gcd)%(n/gcd);
cout<<x<<endl;
}
}
return ;
}

POJ2115(扩展欧几里得)的更多相关文章

  1. POJ2115 C Looooops 模线性方程(扩展欧几里得)

    题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...

  2. POJ2115 - C Looooops(扩展欧几里得)

    题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...

  3. 【扩展欧几里得】poj2115 C Looooops

    题意大概是让你求(A+Cx) mod 2^k = B的最小非负整数解. 若(B-A) mod gcd(C,2^k) = 0,就有解,否则无解. 式子可以化成Cx + 2^k*y = B - A,可以用 ...

  4. poj2115 C Looooops——扩展欧几里得

    题目:http://poj.org/problem?id=2115 就是扩展欧几里得呗: 然而忘记除公约数... 代码如下: #include<iostream> #include< ...

  5. POJ1061:青蛙的约会+POJ2115C Looooops+UVA10673Play with Floor and Ceil(扩展欧几里得)

    http://poj.org/problem?id=1061 第一遍的写法: #include <iostream> #include <stdio.h> #include & ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  7. UVA 12169 Disgruntled Judge 枚举+扩展欧几里得

    题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...

  8. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  9. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

随机推荐

  1. iframe与父页面的js通信

    1.父页面调用iframe中的函数: document.getElementById('myframe').contentWidow.fun1(); 2.在iframe中调用父页面中的函数: wind ...

  2. 编写一个python脚本功能-备份

    版本一 解决方案当我们基本完成程序的设计,我们就可以编写代码了,它是对我们的解决方案的实施.版本一例10.1 备份脚本——版本一 #!/usr/bin/python # Filename: backu ...

  3. lwip移植到stm32上-enc28j60,103mcu(2)

    前面小玩了一下ucos和lwip,但是都还不是真正的网络多任务,真正的网络多任务应该是什么样子的呢?应该是有一个专门的任务负责网络的通讯,他负责将数据发送出去,将数据接收回来,而其他的需要用到网络的任 ...

  4. html5 js控制音乐播放

      <!DOCTYPE HTML><html><head><meta charset="UTF-8"><script lang ...

  5. 详解Objective-C的meta-class 分类: ios相关 ios技术 2015-03-07 15:41 51人阅读 评论(0) 收藏

    比较简单的一篇英文,重点是讲解meta-class.翻译下,加深理解. 原文标题:What is a meta-class in Objective-C? 原文地址:http://www.cocoaw ...

  6. Zynq和microblaze的区别

    Zynq钩中PS端的外设之后不需要初始化过程,但是如果在microblaze中连接外设之后需要有初始化过程.

  7. CoordinatorLayout学习笔记

    CoordinatorLayout是一个增强型的FrameLayout.它的作用有两个 作为一个布局的根布局 最为一个为子视图之间相互协调手势效果的一个协调布局 代码如下: <?xml vers ...

  8. php 模式

    设计模式1.单例模式类的计划生育1.让该类在外界无法造对象2.让外界可以造一个对象,做一个静态方法返回对象3.在类里面通过静态变量控 class Dog { static $dx; public $t ...

  9. js执行js字符串函数的方法

    <script> var jsText = 'return function(){alert(1+1)}' var jscode = new Function(jsText)(); jsc ...

  10. bitmap资源回收

    这个问题哎,困扰本宫一天! bitmap不完全解决method: http://blog.csdn.net/hahahacff/article/details/8540942 http://blog. ...