POJ 2115-C Looooops(基礎一元線性同餘方程)

來源:互聯網
上載者:User

標籤:

題意:

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

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

否則輸出FOREVER

思路:

易列出同餘方程:

x*C+y*2^k = B-A用拓展gcd求解即可

//148K0MSC++808B#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;ll A,B,C;ll x,y;int k;ll e_gcd(ll a,ll b,ll &x,ll &y){    ll ans;    if(b==0)    {        ans=a;        x=1,y=0;    }    else    {        ans=e_gcd(b,a%b,y,x);        y-=(a/b)*x;    }    return ans;}int main(){    while(scanf("%I64d%I64d%I64d%d",&A,&B,&C,&k),(A+B+C+k))    {        ll m=(ll)1<<k;        ll r=(B-A+m)%m;        ll d=e_gcd(C,m,x,y);        if(r%d) puts("FOREVER");        else        {            ll ans=x*(r/d);            ll t=m/d;            ans=(ans%t+t)%t;            printf("%I64d\n",ans);        }    }    return 0;}



POJ 2115-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.