POJ 2115 C Looooops(擴充歐幾裡得應用)

來源:互聯網
上載者:User

標籤:acm   c語言   編程   演算法   擴充gcd   

題目地址:POJ 2115

水題。。公式很好推。最直接的公式就是a+n*c==b+m*2^k.然後可以變形為模線性方程的樣子,就是

n*c+m*2^k==b-a.即求n*c==(b-a)mod(2^k)的最小解。(真搞不懂為什麼訓練的時候好多人把青蛙的約會都給做出來了,這題卻一直做不出來。。。。。這兩道不都是推公式然後變形嗎。。。。。)

代碼如下:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64LL X, Y;LL exgcd(LL a,LL b){    if(b==0)    {        X=1;        Y=0;        return a;    }    LL r=exgcd(b,a%b);    LL t=X;    X=Y;    Y=t-a/b*Y;    return r;}int main(){    LL a, b, c, k, d, L, z;    while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF&&k)    {        z=pow(2,k);        d=exgcd(c,z);        L=b-a;        if(L%d)        {            printf("FOREVER\n");            continue ;        }        else        {            LL ans=X*L/d;            LL s=z/d;            if(ans<0)            {                ans=ans%s+s;            }            else            {                ans=ans%s;            }            printf("%I64d\n",ans);        }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.