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. php 缓存之 APC 和apcu

    php opcode 缓存 apc. 其实,我自己的理解, php apc 缓存其实分两部分, 一部分是 缓存 类似于 java 编译的中间的 字节码, 不同于c 语言编译之后的二进制的机器码. ph ...

  2. USACO Section 1.1 Your Ride Is Here 解题报告

    题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...

  3. angular中控制器之间的通讯方式

    1, 利用作用域的继承方式 由于作用域的继承是基于js的原型继承方式,所以这里分为两种情况,当作用域上面的值为基本类型的时候,修改父作用域上面的值会 影响到子作用域,反之,修改子作用域只会影响子作用域 ...

  4. (简单) POJ 1502 MPI Maelstrom,Dijkstra。

    Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odysse ...

  5. Spark1.3.0安装

    之前在用Hadoop写ML算法的时候就隐约感觉Hadoop实在是不适合ML这些比较复杂的算法.记得当时写完kmeans后,发现每个job完成后都需要将结果放在HDFS中,然后下次迭代的时候再从文件中读 ...

  6. Android面试题随笔1

    1.如何让一个应用在手机上产生两个或多个图标? 在清单文件中的activity节点下配置如下:[5,7行代码] <activity android:name=".MainActivit ...

  7. Android studio开多个窗口引起的问题

    1.clean 的时候,intermediates删不掉 2.出现:app:compile_DebugJavaWithJavac 没有具体错误 出现以上问题的时候只要把多余的删除,记得只留一个在当前窗 ...

  8. X-002 Exyson4412芯片启动过程分析

    移植u-boot到FriendlyARM Tiny4412开发板上,首先我们需要对Samsung Exyson4412芯片的启动方式.系统时钟初始化.串口初始化.内存初始化以及开发板的内存地址空间分配 ...

  9. sql语句:创建事物

    BEGIN TRAN Tran_Money --开始事务 DECLARE @tran_error int; ; BEGIN TRY WHERE Name = '刘备'; SET @tran_error ...

  10. Compilation err ororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatException

    严重: Compilation errororg.eclipse.jdt.internal.compiler.classfmt.ClassFormatExceptionat org.eclipse.j ...