POJ 2115 C Looooops【數論】

來源:互聯網
上載者:User

標籤:io   os   for   資料   sp   問題   c   amp   r   

很容易看出來一個同餘式,說到底是解一個線性同餘方程,電腦解通常有拓展歐幾裡得和歐拉定理兩種演算法,參照去年的NOIP水題,問題是這題資料範圍是2^32所以要int64 TAT

#include<cstdio>

#include<iostream>

#include<string.h>

#include<math.h>

using namespace std;

__int64 exgcd(__int64 a,__int64 b,__int64&x,__int64 &y)

{

    if(b==0)

    {

       x=1;y=0;return a;

    }

   else

    {

       __int64 r=exgcd(b,a %b,y,x);

       y-=x*(a/b);

       return r;

    }

}

__int64 lme(__int64 a,__int64 b,__int64n)//ax=b(mod n)

{

   __int64 x,y;

   __int64 d=exgcd(a,n,x,y);

   if(b%d!=0)return -1;

   __int64 e=x*(b/d)%n+n;

   return e%(n/d);

}

 

int main()

{

   __int64 a,b,c,k;

   scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);

   while(1)

    {

       __int64 d=lme(c,b-a,1LL<<k);

       if (d==-1)

       {

           printf("FOREVER\n");

       }

       else

       {

           printf("%I64d\n",d);

       }

       scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);

       if(a==0 && b==0 && c==0 && k==0) break;

    }

   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.