中国剩余定理

 

51nod1079

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 typedef unsigned long long ULL;
 5 int p[11],r[11];
 6 
 7 ULL Find(ULL x, int y)
 8 {
 9      ULL ret;
10      for(int i = 1; i < y; ++i)
11      {
12          if((ret = i*x) % y == 1) {
13              break;
14          }
15      }
16      return ret;
17 }
18 
19 int main()
20 {
21     int n;
22     ULL Mul = 1;
23     cin>>n;
24     for(int i = 1; i <= n; ++i)
25     {
26         scanf("%d%d",&p[i],&r[i]);
27         Mul *= p[i];
28     }
29     ULL ans = 0;
30     for(int i = 1; i <= n; ++i)
31     {
32         ULL k = Find(Mul/p[i],p[i]);
33         for(int j = 1; j <= r[i]; ++j)
34         {
35             ans = (ans + k)%Mul;
36         }
37     }
38     printf("%llu\n",ans);
39     return 0;
40 }

 

posted @ 2016-05-28 15:44  PosProteus  阅读(241)  评论(0编辑  收藏  举报