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. 【转】Java四种线程池的使用

    Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...

  2. javascrip与C#高级语言的程序设计

    javascript特点:解释型的.基于对象的脚本语言一.简单性:变量类型是采用弱类型,并未使用严格的数据类型.二.动态性:基于事件驱动三.跨平台性:依赖于浏览器本身,与操作环境无关.四.安全性:不允 ...

  3. Lanczos Algorithm and it's Parallelization Stragegy

    由于写了降维的一个系列算法分析,本来以为对这个Lanczos算法会理解一点,但是还是不知道讲了什么,最多的就是会如何调用,然后出结果,所以就翻译官网的相关内容.本篇翻译来自Dimensional Re ...

  4. 1051: 手机(MOBILE)

    #include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...

  5. PHP MVC单入口

    ThinkPHP去除url中的index.php 看到ThinkPHP路径中没有index.php会很诧异,怎么实现的?其实很简单,使用了apache的url重写功能. ThinPHP URL去ind ...

  6. WIP 001 - design the applicant screen

    In this item, you only need to design the screen

  7. CPU profiling

    http://gernotklingler.com/blog/gprof-valgrind-gperftools-evaluation-tools-application-level-cpu-prof ...

  8. 基于py3和pymysql的数据库查询,查询某几列的数据

    #python3 #xiaodeng #基于py3和pymysql的数据库查询,查询某几列的数据 import pymysql conn=pymysql.connect(....) cur=conn. ...

  9. PL/SQL12中文版

    PLSQL12,界面优化很不错,更加美观,直接集成中文语言!,更加艳丽 01.界面 02.注册码 注意:有能力的还是购买付费版的,付费是对软件的高度认可和支持 12.0.3.1821 64bit: 注 ...

  10. 一颗可靠的时间胶囊:苹果AirPort Time Capsule测评

    http://sspai.com/24181/ 如何从 Time Machine 备份恢复数据? AirPort Time Capsule能轻松完成备份,自然也少不了方便地恢复备份.一般常见的恢复备份 ...