[模板] 數學

來源:互聯網
上載者:User

標籤:crt   else   方程組   用途   線性同餘方程   不定方程   turn   [1]   同餘方程   

數論exgcd用途

解不定方程 $ ax+by = c $

代碼
ll exgcd(ll a,ll b,ll &x,ll &y){    if(b==0){x=1,y=0;return a;}    ll tmp=exgcd(b,a%b,y,x);    y-=a/b*x;    return tmp;}int main(){    ll a,b,c;    ll m,x,y;    m=exgcd(a,b,x,y);    if(c%m!=0)cout<<"No\n"; //無解    else{        c/=m,x*=c,y*=c,a/=m,b/=m;//令x取最小非負整數        x1=x%b;if(x1<0)x1+=abs(b);        y1=(c-a*x1)/b;//令y取最小非負整數        y1=y%a;if(y1<0)y1+=abs(a);//(y>=0?y%a:y%a+abs(a));        x1=(c-b*y1)/a;    }}
excrt用途

解線性同餘方程組 $ x \equiv a_i \pmod{m_i} $

代碼
ll excrt(ll *a,ll *m,ll n){    ll a0=a[1],m0=m[1],x,y,g;    rep(i,2,n){        g=exgcd(m0,m[i],x,y);        if((a[i]-a0)%g!=0)return -1;        x=(a[i]-a0)/g*x%(m[i]/g);        a0+=x*m0;        m0=m0/g*m[i];        a0%=m0;     }    return a0<0?(a0%m0+m0):(a0%m0);}

[模板] 數學

聯繫我們

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