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. ural1439 Battle with You-Know-Who

    Battle with You-Know-Who Time limit: 2.0 secondMemory limit: 64 MB Rooms of the Ministry of Magic ar ...

  2. CodeForces 614A Link/Cut Tree

    #include<cstdio> #include<cstring> #include<cmath> #include<stack> #include& ...

  3. iOS之UIColloctionView

    iOS--UICollectionView(滚动视图)入门 UICollectionView @interface UICollectionView : UIScrollView UICollecti ...

  4. 加载window事件

    $(document).ready()和window.onload的区别 发表于 2012-08-29 由 admin 以浏览器装载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM ...

  5. CentOS7 开源跳板机(堡垒机) Jumpserver

    开源跳板机(堡垒机)Jumpserver 环境 CentOS 7   x64       关闭 selinux  firewalld jumpserver: 172.24.0.14 testserve ...

  6. html5新增标签集锦

    <keygen></keygen><meter low="69" high="80" max="100" op ...

  7. ice grid 完整部署过程

    待补充 一 理论准备 一个IceGrid集群有一个registry(注册表,用于定位)和多个node组成. IceGrid配置包括集群配置和应用配置: config.grid是集群配置,配置Regis ...

  8. The account '...' is no team with ID '...'

    iOS升到9.2之后,有一个大坑,原先真机调试的开发者账号(未付费),连不了Xcode了,会弹出一个提示框提示你, The account '...' is no team with ID '...' ...

  9. Android如何调用第三方SO库(转)

    源:Android如何调用第三方SO库 问题描述:Android如何调用第三方SO库:已知条件:SO库为Android版本连接库(*.so文件),并提供了详细的接口说明:已了解解决方案:1.将SO文件 ...

  10. UVA - 129 Krypton Factor (困难的串)(回溯法)

    题意:求由字母表前L个字母组成的字典序第n小的困难串.(如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的串".) 分析:回溯时,检 ...