C Looooops

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 20128 Accepted: 5405

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

Source

CTU Open 2004

看了题解还是不太懂,有时间去请教一下大神

#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI cos(-1.0)
#define RR freopen("input.txt","r",stdin) using namespace std; typedef long long LL; LL ExtendedEuclid(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL d=ExtendedEuclid(b,a%b,x,y);
LL xt=x;
x=y;
y=xt-a/b*y;
return d;
} int main()
{
LL A,B,C,K;
while(cin>>A>>B>>C>>K)
{
if(!A&&!B&&!C&&!K)
{
break;
}
LL a=C;
LL b=B-A;
LL n=(LL)1<<K;
LL x,y;
LL d=ExtendedEuclid(a,n,x,y);
if(b%d!=0)
{
cout<<"FOREVER"<<endl;
}
else
{
x=(x*(b/d))%n;
x=(x%(n/d)+n/d)%(n/d);
cout<<x<<endl;
}
}
return 0;
}

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

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

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

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

    http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...

  3. POJ2115 C Looooops[扩展欧几里得]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Descr ...

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

    辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a ...

  5. POJ 2115 C Looooops扩展欧几里得

    题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...

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

  7. POJ - 2115C Looooops 扩展欧几里得(做的少了无法一眼看出)

    题目大意&&分析: for (variable = A; variable != B; variable += C) statement;这个循环式子表示a+c*n(n为整数)==b是 ...

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

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

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

随机推荐

  1. IntelliJ IDEA 显示行号方法

    设置方法如下:   File->Settings->Editor->General->Appearence->Show Line Number  

  2. PNG图片去除额外透明区域

    bitmapdata.getColorBoundsRect(0xFF000000,0x00000000,false) http://www.cnblogs.com/shinings/archive/2 ...

  3. java系统时间的调用和格式转换

    java在java.text   java.util   java.lang包中查找 import java.util.*; import java.text.*; public class Text ...

  4. 【Origin】工仕途中

    -脚步翩跹,随蝶起舞,翩翩不知所往 晨起脚步催, 蝴蝶迎面飞; 正是春意浓, 三月好风景. -作于二零一六年三月二十八日

  5. VS2012离线安装Xamarin (含破解补丁)

    Xamarin离线安装包 来源于 忘忧草 特此感谢! 离线安装不成功:参考源 http://www.cnblogs.com/zjoch/p/3937014.html  / http://www.cnb ...

  6. 夺命雷公狗ThinkPHP项目之----企业网站1之快速搭建后台

    我们还是老规矩照老方法,将框架里面多余的东西都干掉,然后在index.php里面将框架搭建起来 <?php //定义项目目录 define('APP_PATH','./WEB/'); //开启调 ...

  7. NOIP200505谁拿了最多的奖学金

    NOIP200505谁拿了最多的奖学金 Description 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1)      院士奖学金,每人8000元,期 ...

  8. Openstack的HA解决方案【mysql集群配置】

    使用mysql的galera做多主集群配置,galera的集群优势网络上面有对比,这里不在叙述. 1. 新建3台虚拟机(centos6.5) node1:172.17.44.163 node2:172 ...

  9. Unable to resolve target 'android-19'

    修改两个地方,解决上面的问题

  10. 天气预报接口api(中国天气网)

    中国天气weather.comhttp://m.weather.com.cn/data/101110101.html(六天预报) http://www.weather.com.cn/data/sk/1 ...