首页 > 代码库 > poj 2115 C Looooops
poj 2115 C Looooops
C Looooops
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28251 | Accepted: 8079 |
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 < 2k) are the parameters of the loop.
The input is finished by a line containing four zeros.
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 163 7 2 167 3 2 163 4 2 160 0 0 0
Sample Output
0232766FOREVER
Source
CTU Open 2004
/** @Author: Lyucheng* @Date: 2017-08-07 08:57:28* @Last Modified by: Lyucheng* @Last Modified time: 2017-08-09 16:10:54*//* 题意:给你一个简单的循环for(int i=A;i!=B;i+=C),i是16位得数,问你循环结束的次数,如果是死循环的话,就输出死循环 思路:刚开始想简单的模拟一下,但是超时至少模拟2^k次循环,扩展欧几里得问题完美解决这个问题 题意循环可以转化成公式 ( A + x*C ) mod (1<<K) = B 因为 题意中的A 肯定是小于(1<<k)的所以化简为 x*C mod (1<<k) = B - A 这个就是扩展欧几里得定理求膜线性方程 令D = B - A , n= (1<<k) 得 x*C mod n = D 则 x*C - y*n =D 这就是 a*x+b*y=c的扩展欧几里得定理解不定方程了*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#define LL long longusing namespace std;LL A,B,C,K;LL x,y;LL GCD;LL tol;void exgcd(LL a,LL b,LL &x,LL &y,LL &d){ if(!b){ d=a; x=1; y=0; } else { exgcd(b,a%b,y,x,d); y-=x*(a/b); }}int main(){ // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&K)!=EOF&&(A||B||C||K)){ if(A==B){ puts("0"); continue; } B-=A; tol=(1LL<<K); exgcd(tol,C,x,y,GCD); if(B%GCD!=0){//无解 puts("FOREVER"); continue; }else{ x*=(B/GCD); y*=(B/GCD); y=(y%(tol/GCD)+tol/GCD)%(tol/GCD); printf("%I64d\n", y); } } return 0;}
poj 2115 C Looooops
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。