fzu 1402 中国剩余定理 http://acm.fzu.edu.cn/problem.php?pid=1402

//两两互质。
#include<stdio.h>
#include<string.h>
#define in __int64
in num1[15];
in num2[15];
in ext_Euclid(in a,in b,in &x,in &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    in d=ext_Euclid(b,a%b,x,y);
    in tem=x;
    x=y;
    y=tem-a/b*y;
    return d;
}
in China_Reminder(int len,in* n,in* a)
{
    in result=0;
    in N=1;
    int i,j;
    for(i=0; i<len; i++)
        N=N*n[i];
    for(j=0; j<len; j++)
    {
        in m=N/n[j];
        in x,y;
        ext_Euclid(m,n[j],x,y);
        //   printf("x=%I64d\n",x);
        x=(x%n[j]+n[j])%n[j];
        result = (result + m*a[j]*x%N)%N;
    }
    return result;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%I64d%I64d",&num1[i],&num2[i]);
        }
        printf("%I64d\n",China_Reminder(n,num1,num2));
    }
    return 0;
}


你可能感兴趣的:(fzu 1402 中国剩余定理 http://acm.fzu.edu.cn/problem.php?pid=1402)