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. Installing Apache Spark on Ubuntu 16.04

    Santosh Srinivas on 07 Nov 2016, tagged onApache Spark, Analytics, Data Minin I've finally got to a ...

  2. 【BZOJ】【1007】【HNOI2008】水平可见直线

    计算几何初步 其实是维护一个类似下凸壳的东西?画图后发现其实斜率是单调递增的,交点的横坐标也是单调递增的,所以排序一下搞个单调栈来做就可以了…… 看了hzwer的做法…… /************* ...

  3. 【12c】root container 和 pdb 的一些差别

      Where\what ? root pdb 备注 Control files and redo log files Y belongs to the CDB and not to a spec ...

  4. LinkedList剖析

    第1部分 LinkedList介绍 LinkedList简介 LinkedList 是一个继承于AbstractSequentialList的双向链表.它也可以被当作堆栈.队列或双端队列进行操作. D ...

  5. 遇到Elements in iteration expect to have 'v-bind:key' directives.' 这个错误

    解决方式一:更改VS Code编辑器的vetur配置 错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind: ...

  6. Informatica 常用组件Lookup之六 查询

    PowerCenter 基于您在查找转换中配置的端口和属性来查询查找.当第一行输入到查找转换时,PowerCenter 运行一个默认的 SQL 语句.如果使用关系查找,您可以在"查找 SQL ...

  7. 第二章 Javac编译原理

    注:本文主要记录自<深入分析java web技术内幕>"第四章 javac编译原理" 1.javac作用 将*.java源代码文件转化为*.class文件 2.编译流程 ...

  8. iOS开发-多线程NSOperation和NSOperationQueue

    上一篇文章稍微提及了一下NSThread的使用,NSThread能直观地控制线程对象,不过需要自己管理线程的生命周期,线程同步,用起来比较繁琐,而且比较容易出错.不过Apple给出了自己的解决方案NS ...

  9. Java基础(十):封装

    在面向对象程式设计方法中,封装(英语:Encapsulation)是指一种将抽象性函式接口的实现细节部份包装.隐藏起来的方法.封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访 ...

  10. Eclipse 创建文件快捷菜单、避免格式化时自动换行、.properties文件中文乱码、在线安装FreeMarker

    创建文件快捷菜单设置 打开窗口“Customize Perspective - Java EE”,切换选项卡到“Shortcuts”: 进行一下配置: “Generate”:如上图勾选方式 " ...