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

原题

题目链接

题目分析

题意很明确,这里的模数是不互质的,因此不能直接套中国剩余定理.这里稍微讲一下中国剩余定理的扩展,假设前i个方程的特解为ans(i),通解为x(i)=ans(i)+k*lcm(前i个模数).把x(i)代入到第i+1个方程,用扩展欧几里得定理求解k=k0,ans(i+1)=ans(i)+k0*lcm(前i个模数),则x(i+1)=ans(i+1)+lcm(前i+1个模数).大概思路就是这样,但有些细节需要注意一下,求出来的k0,ans(i+1)需要用求模的方法缩小一下.

代码

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <utility>
 6 #include <ctime>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <set>
14 #include <map>
15
16 using namespace std;
17 typedef unsigned long long ULL;
18 typedef __int64 LL;
19 typedef long double LB;
20 const int INF_INT=0x3f3f3f3f;
21 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
22
23 vector<LL> a,m;
24
25 LL exgcd(LL a,LL b,LL &x,LL &y)
26 {
27     if(!b)
28     {
29         x=1,y=0;
30         return a;
31     }
32     LL g=exgcd(b,a%b,y,x); //简化写法
33     y-=a/b*x; //y从下一个方程回溯回来时是下一个方程的x
34     return g;
35 }
36
37 LL solve(LL n)
38 {
39     LL ans=0,lcm=1,x,y;
40     for(int i=0;i<n;i++)
41     {
42         if(!i) ans=a[i],lcm*=m[i];
43         else
44         {
45             LL g=exgcd(lcm,m[i],x,y),c=a[i]-ans;
46             if(c%g)
47             {
48                 ans=-1;
49                 break;
50             }
51             x*=c/g;
52             LL t=m[i]/g;
53             x=(x%t+t)%t;
54             ans+=x*lcm;
55             lcm*=m[i]/g;
56             ans=((ans%lcm)+lcm)%lcm;
57         }
58     }
59     return ans;
60 }
61
62 int main()
63 {
64 //    freopen("std.in","r",stdin);
65 //    freopen("std.out","w",stdout);
66     LL k;
67     while(~scanf("%lld",&k))
68     {
69         m.clear(),a.clear();
70         LL x,y;
71         for(int i=0;i<k;i++) scanf("%lld %lld",&x,&y),m.push_back(x),a.push_back(y);
72         printf("%lld\n",solve(k));
73     }
74     return 0;
75 }

原文地址:https://www.cnblogs.com/VBEL/p/11437896.html

时间: 2024-11-08 04:36:21

Strange Way to Express Integers POJ 2891(中国剩余定理扩展)的相关文章

POJ 2891 中国剩余定理(不互素)

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 17877   Accepted: 6021 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

POJ 2891 中国剩余定理的非互质形式

中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利用扩展欧几里得算法求出k k = k0(mod n/(n,m)) = k0 + h*n/(n,m) x = km+a = k0*m+a+h*n*m/(n,m) = k0*m+a (mod n*m/(n,m)) #include <cstdio> #include <cstring> #

poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   Accepted: 2873 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

poj 2891 Strange Way to Express Integers

http://poj.org/problem?id=2891 这道题的题意是:给你多个模性方程组:m mod ai=ri 求最小的m: 中国剩余定理 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(!

poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 11970   Accepted: 3788 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express no

poj——2891 Strange Way to Express Integers

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839   Accepted: 5625 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

poj 2891 Strange Way to Express Integers (扩展gcd)

题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2联立得:a1*x-a2*y=r2-r1;设r=r2-r2; 互质的模线性方程组m=r[i](mod a[i]).两个方程可以合并为一个,新的a1为lcm(a1,a2), 新的r为关于当前两个方程的解m,然后再和下一个方程合并--.(r2-r1)不能被gcd(a1,a2)整除时无解. 怎么推出的看了好

【POJ 2891】Strange Way to Express Integers(扩展欧几里得)

[POJ 2891]Strange Way to Express Integers(扩展欧几里得) Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 12934   Accepted: 4130 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative in

【POJ】【2891】Strange Way to Express Integers

中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} \\ m \equiv r_2 \pmod{a_2} \end{cases} \] 我们可以列出式子 $$ a_1x+r_1=a_2y+r_2 $$ 利用扩展欧几里得解出一个可行解$M'$.那么我们就可以将两个限制条件合为一个: $$ m \equiv M' \pmod{ lcm(a_1,a_2)}