POJ 2891 Strange Way to Express Integers(中国剩余定理)

题目链接

虽然我不懂...

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <map>

 4 #include <cmath>

 5 using namespace std;

 6 #define LL __int64

 7 LL p[1001],o[1001];

 8 LL x,y;

 9 LL ext_eulid(LL a,LL b)

10 {

11     LL t,d;

12     if(b == 0)

13     {

14         x = 1;

15         y = 0;

16         return a;

17     }

18     d = ext_eulid(b,a%b);

19     t = x;

20     x = y;

21     y = t - (a/b)*y;

22     return d;

23 }

24 LL gcd(LL a,LL b)

25 {

26     return b == 0?a:gcd(b,a%b);

27 }

28 int main()

29 {

30     int i,n,z;

31     LL p1,o1,d,c,mod;

32     while(scanf("%d",&n)!=EOF)

33     {

34         for(i = 0;i < n;i ++)

35         {

36             scanf("%I64d%I64d",&p[i],&o[i]);

37         }

38         p1 = p[0];

39         o1 = o[0];

40         z = 1;

41         for(i = 1;i < n&&z;i ++)

42         {

43             d = ext_eulid(p1,p[i]);

44             c = o[i] - o1;

45             if(c%d) z = 0;

46             mod = p[i]/d;

47             x = ((c/d*x)%mod+mod)%mod;

48             o1 = p1*x + o1;

49             p1 = p1*mod;

50         }

51         if(!z)

52         printf("-1\n");

53         else

54         printf("%I64d\n",o1);

55     }

56     return 0;

57 }

 

你可能感兴趣的:(Integer)