C Looooops POJ - 2115
C Looooops POJ - 2115
題目:
A Compiler Mystery: We are given a C-language style for loop of type
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
題意:
初始值為A,每次可以增加C,值要mod2k,問mod后的值如果等于B,增加了幾次C,如果無法等于B輸出FOREVER
題解:
看一下我的推導(dǎo):
你會發(fā)現(xiàn)其實(shí)就是擴(kuò)展歐幾里得的模板題,并求出最小正整數(shù)解
我們可以用 (x0 % b1 + b1 ) % b1得到它的最小正整數(shù)解了
此處x0= x * c / gcd(a,b)
詳細(xì)證明看下面博客
擴(kuò)展歐幾里得講解
代碼:
#include<iostream> #include<vector> #include<string> #include<cmath> #include<algorithm> #include<cstdio> #include<cstring> #include<list> using namespace std; typedef long long ll; ll exgcd(ll a,ll b,ll &x,ll &y)//擴(kuò)展歐幾里得算法 {if(b==0){x=1;y=0;return a; //到達(dá)遞歸邊界開始向上一層返回}ll gcd=exgcd(b,a%b,x,y);ll y1=y; //把x y變成上一層的ll x1=x;y=x1-(a/b)*y1;x=y1;return gcd; //得到a b的最大公因數(shù) }int main() {ll A,B,C,K;while(cin>>A>>B>>C>>K){if(A==0&&B==0&&C==0&&K==0)break;ll x,y;ll a=C;ll b=(ll)1<<K;ll c=B-A;ll gcd=exgcd(a,b,x,y);if(c%gcd!=0){cout<<"FOREVER"<<endl;}else {x=(x*(c/gcd))%b;x=(x%(b/gcd)+b/gcd)%(b/gcd);cout<<x<<endl;}}return 0; }總結(jié)
以上是生活随笔為你收集整理的C Looooops POJ - 2115的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三维GIS笔记
- 下一篇: 一起开心暑假集训第一周限时训练 2020