集训队专题(8)1009 C Looooopsz
来源:互联网 发布:5g网络的股票 编辑:程序博客网 时间:2024/06/15 10:58
C Looooops
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 15
Problem Description
A Compiler Mystery: We are given a C-language style for loop of type
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.
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 < 2<sup>k</sup>) are the parameters of the loop. <br> <br>The input is finished by a line containing four zeros. <br>
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. <br>
Sample Input
3 3 2 163 7 2 167 3 2 163 4 2 160 0 0 0
Sample Output
0232766FOREVER
Source
PKU
这题小编做的时候看的是别人的题解做的,在看的时候发现了一篇写的相当细致的文章,小编这里就不再自己写题解,直接献上大神的作品
http://blog.csdn.net/lyy289065406/article/details/6648546
#include <cstdio>#define LL __int64LL exGcd(LL a,LL b,LL &x,LL &y){if(b == 0){x = 1;y = 0;return a;}LL d = exGcd(b,a%b,x,y);LL tmp = x;x = y;y = tmp-a/b*y;return d;}int main(){LL A,B,C,k;while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)&&(A||B||C||k)){LL a = C, b = B-A, n = (LL)1<<k;LL x,y;LL d = exGcd(a,n,x,y);if(b%d != 0) printf("FOREVER\n");else{x = (x*(b/d))%n;x = (x%(n/d)+n/d)%(n/d);printf("%I64d\n",x);}}return 0;}
0 0
- 集训队专题(8)1009 C Looooopsz
- 集训队专题(1)1009 T9
- 集训队专题(6)1009 过山车
- 集训队专题(8)1001 Biorhythms
- 集训队专题(8)1002 X问题
- 集训队专题(8)1007 Remainder
- 集训队专题(3)1009 Choose the best route
- 集训队专题(5.2)1009 University Entrace Examination
- 集训队专题(8)1003 Chinese remainder theorem again
- 集训队专题(8)1006 Yet Another Multiple Problem
- 集训队专题(8)1008 The Evaluation of Determinant
- 集训队专题(1)1001 Immediate Decodability
- 集训队专题(1)1002 统计难题
- 集训队专题(1)1003 Phone List
- 集训队专题(1)1005 Ancient Printer
- 集训队专题(1)1006 单词数
- 集训队专题(1)1008 Repository
- 集训队专题(2)1001 Fibonacci
- 1006. Sign In and Sign Out (25)
- android 数据库(一)
- java Script 介绍
- Git 出现SSL certificate problem: self signed certificate的解决方法
- 对视频马赛克的调研学习报告
- 集训队专题(8)1009 C Looooopsz
- [疯狂Java]AWT:位图传送(内存镜像/缓冲)
- Android基础(2)——Android Studio建项目
- 远程管理 KVM 虚机 - 每天5分钟玩转 OpenStack(5)
- Ruby on Rails 网站大型化之静态资源 CDN 架构
- poj2082单调栈
- HDOJ 2149 Public Sale 巴什博奕
- Faster R-CNN 翻译
- 自定义控件——带底部监听的RecyclerView