POJ2115——C Looooops(擴充歐幾裡德+求解模線性方程)

來源:互聯網
上載者:User

標籤:des   style   blog   io   color   os   ar   for   sp   

C Looooops

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

題目大意:

    對於C的for(i=A ; i!=B ;i +=C)迴圈語句,問在k位儲存系統中迴圈幾次才會結束。

    若在有限次內結束,則輸出迴圈次數。

    否則輸出死迴圈。

解題思路:

    有題意可知:設進行X次迴圈 A%2^K+(C*X)%2^K=B%2^K

      -->(C*X)%(2^K)=(B-A)%(2^K)

      -->模線性方程 ax=b mod n的解。 其中a=C,b=B-A,n=2^K.

演算法:

    演算法導論上給出了 求解a=b (mod n)的虛擬碼 其中a,n為正整數,b為任意整數。

    MODULAR_LINEAR_EQUATION_SOLVER(a,b,n)

    (d,x‘,y‘)=EXTENDED_EUCLID(a,n)

    if (d|b)

      x0=x‘(b/d) mod n

      for i=0 to d-1

        print (x0+i(n/d)) mod n

    else

      print "no solutions"

    其中x0+i(n/d)為所有可以的解,輸出其最小正整數解即可。

Code:

 1 /************************************************************************* 2     > File Name: poj2115.cpp 3     > Author: Enumz 4     > Mail: [email protected] 5     > Created Time: 2014年10月29日 星期三 13時13分46秒 6  ************************************************************************/ 7  8 #include<iostream> 9 #include<cstdio>10 #include<cstdlib>11 #include<string>12 #include<cstring>13 #include<list>14 #include<queue>15 #include<stack>16 #include<map>17 #include<set>18 #include<algorithm>19 #include<cmath>20 #include<bitset>21 #include<climits>22 #define MAXN 10000023 #define LL long long24 using namespace std;25 LL extended_gcd(LL a,LL b,LL &x,LL &y)26 {27     LL ret,tmp;28     if (b==0)29     {30         x=1,y=0;31         return a;32     }33     ret=extended_gcd(b,a%b,x,y);34     tmp=x;35     x=y;36     y=tmp-a/b*y;37     return ret;38 }39 40 int main()41 {42     LL A,B,C,k;43     while (cin>>A>>B>>C>>k)44     {45         if (A==0&&B==0&&C==0&&k==0) break;46         LL mod=1;47         while (k--)48             mod*=2;49         LL x,y;50         LL a=C,b=B-A;51         LL d=extended_gcd(a,mod,x,y);52         //cout<<a<<" "<<x<<" "<<mod<<" "<<y<<endl;53         //cout<<d<<endl;54         if (b/d*d==b)55         {56             LL x0=x*(b/d)%mod;57             if(x0<0) x0+=mod;58             //cout<<x0<<endl;59             long long Min=x0;60             for (int i=0;i<=d-1;i++)61                 if ((x0+i*(mod/d))%mod>=0)62                     Min=min(Min,(x0+i*(mod/d))%mod);63             cout<<Min<<endl;64         }65         else66             printf("FOREVER\n");67     }68     return 0;69 }

 

    

POJ2115——C Looooops(擴充歐幾裡德+求解模線性方程)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.