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. AOP 在javascript 中的使用

    AOP(Aspect Oriented Programming) 意为面向切面编程 可以在不修改原有代码的情况下增加新功能,利用AOP可以对业务逻辑各个部分进行隔离,从而使得业务逻辑各部分的耦合度降低 ...

  2. word 书签排序算法

    直接上代码 /// <summary> /// 通过计算插入引文的位置格式化合适的引文序号 /// </summary> /// <returns></ret ...

  3. HDU 2859 Phalanx

    简单二维dp.o(n^3)效率过的.不知道有没有o(n^2)的解法. 为了方便点,先左右交换一下. dp[i][j]表示以[i,j]为左上角的最大对称矩阵长度 那么dp[i][j]=min(Max,d ...

  4. DNS相关配置文件

    我们晓得主机名对应到 IP 有两种方法,早期的方法是直接写在档案里面来对应, 后来比较新的方法则是透过 DNS 架构!那么这两种方法分别使用什么配置文件?可不可以同时存在? 若同时存在时,那个方法优先 ...

  5. (简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。

    Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...

  6. python------unicode字符串转换为其他类型

    问题描述: 一下字符串转换为json类型 {u'src': u'crawl', u'cid': u'Ctengbangguoji', u'datatype': u'ItemBase', u'times ...

  7. POJ3169差分约束系统

    题意:有n头牛,编号为1到n,对于关系好的ml头牛,al和bl之间的距离不大于dl,关系差的md头牛,ad和bd之间的距离不大于dd,求第1头牛和第n头牛之间的距离 分析:这是一道差分约束系统的题目, ...

  8. MySQL的MyISAM和InnoDB

    1.概述 MySQL数据库其中一个特性是它的存储引擎是插件式的.用户可以根据应用需要选择存储引擎.Mysql默认支持多种存储引擎,以适用各种不同的应用需要. 默认情况下,创建表不指定表的存储引擎,则新 ...

  9. ANSI标准

    NSI:美国国家标准学会(AMERICAN NATIONAL STANDARDS INSTITUTE: ANSI)成立于1918年.当时,美国的许多企业和专业技术团体,已开始了标准化工作,但因彼此间没 ...

  10. uos事件控制块与任务同步

    Ucos为了任务之间的通讯定义了信号量,互斥性信号量,消息对象 消息队列等结构以及api,为了统一的管理这些同步,定义了一个结构叫做时间控制块OS_EVENT,如下 typedef struct os ...