nhjcacmt / acm

NHJC-ACM队信息站
6 stars 1 forks source link

中国剩余定理 #9

Open UNICKCHENG opened 6 years ago

UNICKCHENG commented 6 years ago

[任务] 求出方程x≡ai(mod mi) (0<=i<n)的解x,其中m1,m2,...,mn-1两两互质

int CRT(int a[],int m[],int n)
{
    int M=1;
    for(int i=0;i<n;i++) M*=m[i];
    int ret=0;
    for(int i=0;i<n;i++)
    {
        int x,y;
        int tm=M/m[i];
        extend_gcd(tm,m[i],x,y);
        ret=(ret+tm*x*a[i])%M;
    }
    return (ret+M)%M;
}

待完善