Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3
Problem 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
 
 #include<iostream>
//#include<cstdio>
using namespace std;
long long x,y,q;
void exgcd( long long a,long long b)
{
if(b==)
{
x=,y=,q=a;
}
else
{
exgcd(b,a%b);
long long temp=x;
x=y,y=temp-a/b*y;
}
} int main()
{
long long a,b,c,k;
long long sum;
while(cin>>a>>b>>c>>k,a+b+c+k)
{
//sum=1<<k;
sum=;
for(int i=;i<k;i++)
{
sum*=;
}
exgcd(c,sum);
q;
if((b-a)%q)
{
cout<<"FOREVER"<<endl;
}
else
{
long long temp=sum/q;
cout<<(((b-a)/q*x%temp+temp)%temp)<<endl;
}
}
return ;
}

HDUOJ----专题训练的更多相关文章

  1. DP专题训练之HDU 2955 Robberies

    打算专题训练下DP,做一道帖一道吧~~现在的代码风格完全变了~~大概是懒了.所以.将就着看吧~哈哈 Description The aspiring Roy the Robber has seen a ...

  2. dp专题训练

    ****************************************************************************************** 动态规划 专题训练 ...

  3. bryce1010专题训练——LCT&&树链剖分

    LCT&&树链剖分专题 参考: https://blog.csdn.net/forever_wjs/article/details/52116682

  4. DP专题训练之HDU 1087 Super Jumping!

    Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is ve ...

  5. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  7. DP专题训练之HDU 1864 最大报销额

    做DP一定要注意数组的大小,嗯,就是这样~ Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过10 ...

  8. bzoj专题训练

    //http://blog.csdn.net/PoPoQQQ/article/category/2542243

  9. 算法专题训练 搜索a-T3 Ni骑士(ni)

    搞了半天八数码弄不出来就只好来打题解  这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...

  10. 图论专题训练1-D(K步最短路,矩阵连乘)

    题目链接 /* *题目大意: *求出从i到j,刚好经过k条边的最短路; * *矩阵乘法的应用之一(国家队论文): *矩阵乘法不满足交换律,矩阵乘法满足结合律; *给定一个有向图,问从A点恰好走k步(允 ...

随机推荐

  1. 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSTEM

    ylbtech-Oracle:数据库实例: STOREBOOK  >  表空间  >  编辑 表空间: SYSTEM  表空间  >  编辑 表空间: SYSTEM 1. 一般信息返 ...

  2. OTL翻译(8) -- otl_long_string/otl_long_unicode_string类

    otl_long_string/olt_long_unicode_string 这两个类主要用来处理大对象数据.从OTL4.0版本开始,otl_long_string还可以处理任何类型的RAW/BIA ...

  3. Javascript模块化工具require.js教程

    转自:http://www.w3cschool.cc/w3cnote/requirejs-tutorial-1.html, http://www.w3cschool.cc/w3cnote/requir ...

  4. JS计算本周一和本周五的日期

    代码不长: var today=new Date();var weekday=today.getDay();    var monday=new Date(1000*60*60*24*(1-weekd ...

  5. ACM-尼姆博弈之取(m堆)石子游戏——hdu2176

    取(m堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. windows server 2012 IE增强的安全配置如何关闭

    http://jingyan.baidu.com/article/6181c3e076ac0b152ff15354.html 打开左下角的 服务端 关闭这个就可以了

  7. 【CSWS2014 Summer School】互联网广告中的匹配和排序算法-蒋龙(下)

    [CSWS2014 Summer School]互联网广告中的匹配和排序算法-蒋龙(上) Fig19,用到了矩阵,这个我没有听太明白,蒋博士也没有详细说明.不过可以明确的一点就是,我们常说的K-mea ...

  8. 如何设置qt creator的快捷键

    如何设置qt creator快捷键 以设置设置edit - select encoding的快捷键为例 1. tools - options - environment - keyborad 2.在搜 ...

  9. TQ2440 LCD试验失败经验教训

    试验环境:TQ2440开发板(配套TQ4.3寸屏) 试验参考书目:<ARM处理器裸机开发实战--机制而非策略>(以下简称<裸机开发实战>) <裸机开发实战>第13章 ...

  10. flume hdfs一些简单配置记忆

    ############################################ # producer config ##################################### ...