码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2891 Strange Way to Express Integers

时间:2014-05-31 19:06:39      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   tar   

http://poj.org/problem?id=2891

这道题的题意是:给你多个模性方程组:m mod ai=ri 求最小的m;

中国剩余定理

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define ll long long
 5 using namespace std;
 6 
 7 ll gcd(ll a,ll b,ll &x,ll &y)
 8 {
 9     if(!b)
10     {
11         x=1;
12         y=0;
13         return a;
14     }
15     ll d=gcd(b,a%b,y,x);
16     y-=x*(a/b);
17     return d;
18 }
19 
20 int main()
21 {
22     ll a1,t,r1,x,y,a2,r2;
23     while(scanf("%lld",&t)!=EOF)
24     {
25         scanf("%lld%lld",&a1,&r1);
26         t--;
27         bool flag=true;
28         while(t--)
29         {
30             scanf("%lld%lld",&a2,&r2);
31             if(!flag) continue;
32             ll d1=gcd(a1,a2,x,y);
33             ll c=r2-r1;
34             if(c%d1)
35             {
36                 flag=false;
37                 continue;
38             }
39             ll t1=a2/d1;
40             x=(c/d1*x%t1+t1)%t1;
41             r1=(a1*x+r1);
42             a1=a1*a2/d1;    
43         }
44         if(!flag) printf("-1\n");
45         else printf("%lld\n",r1);
46     }
47     return 0;
48 }
View Code

 

poj 2891 Strange Way to Express Integers,布布扣,bubuko.com

poj 2891 Strange Way to Express Integers

标签:c   style   class   blog   code   tar   

原文地址:http://www.cnblogs.com/fanminghui/p/3762418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!