C Looooops

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 10   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
 题解:
输出lld老是wa,改成I64就过了。。。还有(LL)1<<k,也要加LL,否则还会wa
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a;
x=;
y=;
}
else{
e_gcd(b,a%b,d,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
}
}
void cal(LL a,LL b,LL c){
LL d,x,y;
e_gcd(a,b,d,x,y);
if(c%d!=){
puts("FOREVER");
return;
}
x*=c/d;
b/=d;
if(b<)b=-b;
x%=b;
if(x<)x+=b;
printf("%I64d\n",x);
return;
}
int main(){
LL A,B,C,k;
while(~scanf("%lld%lld%lld%lld",&A,&B,&C,&k),A|B|C|k){
//C*x+2^ky=B-A;
cal(C,(LL)<<k,B-A);
}
return ;
}

C Looooops(扩展欧几里德)的更多相关文章

  1. poj 2115 C Looooops 扩展欧几里德

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23616   Accepted: 6517 Descr ...

  2. poj2115-C Looooops(扩展欧几里德算法)

    本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...

  3. POJ2115 C Looooops 扩展欧几里德

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...

  4. poj2115 Looooops 扩展欧几里德的应用

    好开心又做出一道,看样子做数论一定要先看书,认认真真仔仔细细的看一下各种重要的性质 及其用途,然后第一次接触的题目 边想边看别人的怎么做的,这样做出第一道题目后,后面的题目就完全可以自己思考啦 设要+ ...

  5. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  6. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

  7. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  8. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  9. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

随机推荐

  1. poj2311

    博弈论——sg,mex sg性质:1.在末态的状态点为N态. 2.P态的下一步有一个是N态 3.N态的下一步全部是P态. 当然这是对于单点一个游戏的情形,也相当于NIM只有一堆石子. mex(mini ...

  2. mlpack:可伸缩C++机器学习库(转)

    摘要:mlpack是一个可伸缩C++机器学习库,它的目的是让新用户通过简单.一致的API使用机器学习,同时为专业用户提供C++的高性能和最大灵活性. mlpack是一个直观.快速.可伸缩的C++机器学 ...

  3. Linux学习之chage命令

    功能:修改帐号和密码的有效期限用法:chage[-l][-m mindays][-M maxdays][-I inactive][-E expiredate][-W warndays][-d last ...

  4. 《JavaScript+DOM编程艺术》的摘要(一)---基本知识点

    保持良好的编程习惯:在同一脚本中,保持引号的一致性,都用单引 var mood = "don\"t worry";alert(mood); 数组:var arr=Arra ...

  5. 深刻理解一句js

    最近有点小懒,不想写东西,也不想看东西,看东西还老跑神,这是肿么了呢? 盯着显示器某一刻能把它看透,就是不想挪下眼睛 无意中看到了这行代码 [].forEach.call($$("*&quo ...

  6. The basic introduction to MIX language and machine

    reference: The MIX Computer, The MIX Introduction sets, The basic info storage unit in MIX computer ...

  7. Linux 查看文件

    1: cat file_full_name  这种方法适合查看小文件 cat /etc/services cat -n file_full_name -n 可以加行号! 2: tac file_ful ...

  8. ICT测试原理

    在线测试,ICT,In-Circuit Test,是通过对在线元器件的电性能及电气连接进行测试来检查生产制造缺陷及元器件不良的一种标准测试手段.它主要检查在线的单个元器件以及各电路网络的开.短路情况, ...

  9. Netbeans搭建Android环境

    原文:Netbeans搭建Android环境 Netbeans环境的搭建主要依赖于NBAndroid插件,项目地址: http://www.nbandroid.org/p/installation.h ...

  10. ubuntu 常用生产环境部署配置测试调优

    1,ubuntu monogdb 安装配置 2,ubuntu jdk1.7,tomcat7安装 3,ubuntu LAMP部署 4,mongodb 远程热备份及恢复 使用自带的mongodump和mo ...