日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C Looooops POJ - 2115

發(fā)布時間:2023/12/3 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C Looooops POJ - 2115 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。