C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19536   Accepted: 5204

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

题意是问在

for (variable = A; variable != B; variable += C)

这样的情况下,循环多少次。

当中全部的数要mod 2的k次方。所以方程就是(A+C*x)%(2^k)=B,变换一下就是-C*x+(2^k)*y=A-B。解这个方程的最小正数x就可以。

又是扩展欧几里德。



代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; long long yue; void ex_gcd(long long a,long long b, long long &xx,long long &yy)
{
if(b==0)
{
xx=1;
yy=0;
yue=a;
}
else
{
ex_gcd(b,a%b,xx,yy); long long t=xx;
xx=yy;
yy=t-(a/b)*yy;
}
} int main()
{
long long A,B,C,k,k2,xx,yy; while(scanf_s("%lld%lld%lld%lld",&A,&B,&C,&k))
{
if(!A&&!B&&!C&&!k)
break; k2=(1LL<<k);
ex_gcd(-C,k2,xx,yy); if((A-B)%yue)
{
cout<<"FOREVER"<<endl;
}
else
{
xx=xx*((A-B)/yue);
long long r=k2/yue;
if(r<0)
xx=(xx%r-r)%r;
else
xx=(xx%r+r)%r;
printf("%lld\n",xx);
}
}
return 0;
}

POJ 2115:C Looooops的更多相关文章

  1. 【poj 2115】C Looooops(数论--拓展欧几里德 求解同余方程 模版题)

    题意:有一个在k位无符号整数下的模型:for (variable = A; variable != B; variable += C)  statement; 问循环的次数,若"永不停息&q ...

  2. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

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

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  6. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  7. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

  8. poj 2115 Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23637   Accepted: 6528 Descr ...

  9. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

随机推荐

  1. ZH奶酪:PHP 执行时间Fatal error: Maximum execution time of...

    来源:http://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-excee ...

  2. JMeter运行通过Chrome打开的website

    部分website在chrome上运行正常,但在IE环境运行会存在问题.而是用 JMeter运行通过chrome打开的website时候,需要处理一下. 可以参考下面几篇文章: http://ninj ...

  3. 微软BI 之SSRS 系列 - 如何实现报表标签的本地化 - 中文和英文的互换

    SSRS 中并没有直接提供本地化的配置方式,因此在 SSRS 中实现本地化,比如有英文标题还有可选的中文标题,就需要通过其它的方式来解决. 比如默认是这样的英文标题 - 但是本地中方用户可能比较喜欢看 ...

  4. 定时执行任务FluentScheduler

    private void Form1_Load(object sender, EventArgs e) { Registry registry = new Registry(); registry.S ...

  5. spring boot-mybatis三种动态sql(5)

    脚本sql XML配置方式的动态SQL我就不讲了,有兴趣可以自己了解,下面是用<script>的方式把它照搬过来,用注解来实现.适用于xml配置转换到注解配置 @Select(" ...

  6. sqlalchemy 获取计数 count

    from sqlalchemy import func message_count = self.db.query(func.count(Message.uid)).filter(Message.ui ...

  7. 一个简单的 JSON 生成/解析库

    这是一个单文件的,适用于C语言的, JSON 读写库. 先说明,不想造轮子,代码是从这里拿来的: https://www.codeproject.com/Articles/887604/jWrite- ...

  8. ES6学习笔记六:迭代

    一:迭代器 它是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). ES6创造了一种新的遍历命令for. ...

  9. Android玩转百度地图Sha1获取正确姿势?

    场景一 由于近期项目钟要用到定位功能因此肯定须要用到地图以及地位功能,相信大家也知道眼下国内比較出名的地图像百度.高德.腾讯等这些还是用到比較多的.于是思考了一下决定还是用百度,相信老司机们都知道的哈 ...

  10. 【shell】创建长目录,目录存在则忽略,缺失则创建

    有时候,我们需要创建一个空目录树,如果给定路径包含目录,那么还必须检查这些目录是否存在: mkdir –p /qinys/oliver/tmp/ 执行上述命令即可创建长目录,并且有则忽略,无则创建原则 ...