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 < 2 k) modulo 2 k


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
< 2 k) 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

注意基础知识:http://blog.csdn.net/u014665013/article/details/50858292

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
LL exgcd(LL a,LL b,LL &x,LL &y) ///返回最大公约数
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL r=exgcd(b,a%b,x,y);
// cout<<"x="<<x<<" y="<<y<<endl;
LL t=x;
x=y;
y=t-a/b*y;
return r;
}
int main (){ LL A,B,C,k;
while(~scanf("%I64I64d%I64d%I64d%I64d",&A,&B,&C,&k)&&!(A==0&&B==0&&C==0&&k==0)){
LL mmax=1LL<<k;
LL a=C,b=mmax,c=(B-A+mmax)%mmax; ///注意这里的b能通过是-mmax,因为这样到后面再求大于0的最小值就不好求了 但是也可以,下面的就要改了如下注释
LL x,y;
LL gcd = exgcd(a,b,x,y);
if(c==0) {
printf("%d\n",0);
continue;
}
if(c%gcd==0){
x=x*(c/gcd);
LL r = b/gcd;
x=(x%r+r)%r; ///最小的正值 <span style="font-family: Arial, Helvetica, sans-serif;">x=-(x%r-r)%r</span>
printf("%I64d\n",x);
}
else{
printf("FOREVER\n");
}
}
return 0;
}

POJ C Looooops的更多相关文章

  1. poj 2115 Looooops

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

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

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

  3. POJ 2115 C Looooops

    扩展GCD...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Mem ...

  4. Poj 2115 C Looooops(exgcd变式)

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

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

  6. POJ 2115:C Looooops

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

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

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

  8. 【题解】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 ...

  9. POJ 2115 C Looooops(Exgcd)

    [题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...

随机推荐

  1. Hadoop 2.2.0 4结点集群安装 非HA

    总体介绍 虚拟机4台,分布在1个物理机上,配置基于hadoop的集群中包括4个节点: 1个 Master, 3个 Salve,i p分布为: 10.10.96.33 hadoop1 (Master) ...

  2. JVM-类加载机制

    虚拟机类加载机制  虚拟机把描述的类的数据从class文件加载到内存后,并对数据进行校验,转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制.  类加载的时机 类被 ...

  3. HDFS中Java的API使用测试

    import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.Scanner ...

  4. Nodejs连接mysql

    1.首先需要安装nodejs 的mysql包 npm install mysql 2.编写nodejs与mysql交互的代码 var mysql = require('mysql'); var TES ...

  5. 寻找研究基于NS2研究覆盖网络的小伙伴:)

    如题,本人菜鸟刚刚入门,想找些基于NS2研究覆盖网络方面的小伙伴,具体点是关于覆盖网络中QoS服务调度方法方面的,有的小伙伴可以留下联系方式,或者加我QQ:245939069  :P:P:P

  6. TCP 状态机

    TCP 状态机 TCP 协议的操作可以使用一个具有 11 种状态的有限状态机( Finite State Machine )来表示,图 3-12 描述了 TCP 的有限状态机,图中的圆角矩形表示状态, ...

  7. .Net内存泄露原因及解决办法

    .Net内存泄露原因及解决办法 1.    什么是.Net内存泄露 (1).NET 应用程序中的内存 您大概已经知道,.NET 应用程序中要使用多种类型的内存,包括:堆栈.非托管堆和托管堆.这里我们需 ...

  8. H5版俄罗斯方块(5)---需求演进和产品迭代

    前言: 产品的形态是不断迭代的, 从粗糙到精致, 从简易到立体. 有了最初的技术积累和时间思考后, 终于明确了该游戏的方向. 我想说的是: 技术不是重点, 产品/用户体验才是核心议题. 结合朋友的游戏 ...

  9. c++类的声明和对象的定义---10

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 类是创建对象的模板,一个类可以创建多个对象,每个对象都是类类型的一个变量:创建对象的过程也叫类的实例化. ...

  10. [vijos P1880]ファーラの力

    据说这是一道 JOI 的题?反正我觉着挺好的喵~ 题目看起来十分可怕,但是代码还是很短的 显而易见的,ans 因分为3个部分:1.中途增加光压的时间 2.中途减少光压的时间 3. 所有路程的总时间 发 ...