【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)} $$ 这样我们依次合并下去即可得到答案啦~(话说代码里那段处理的过程我还没看懂……

代码:(copy自http://www.cnblogs.com/Missa/archive/2013/06/01/3112536.html

 1 Source Code
 2 Problem: 2891        User: sdfzyhy
 3 Memory: 676K        Time: 0MS
 4 Language: G++        Result: Accepted
 5
 6     Source Code
 7
 8     //POJ 2891
 9     #include<vector>
10     #include<cstdio>
11     #include<cstring>
12     #include<cstdlib>
13     #include<iostream>
14     #include<algorithm>
15     #define rep(i,n) for(int i=0;i<n;++i)
16     #define F(i,j,n) for(int i=j;i<=n;++i)
17     #define D(i,j,n) for(int i=j;i>=n;--i)
18     using namespace std;
19     typedef long long LL;
20     inline LL getLL(){
21         LL r=1,v=0; char ch=getchar();
22         for(;!isdigit(ch);ch=getchar()) if(ch==‘-‘)r=-1;
23         for(; isdigit(ch);ch=getchar()) v=v*10+ch-‘0‘;
24         return r*v;
25     }
26     const int N=1e5+10,INF=~0u>>2;
27     /******************template*********************/
28     LL a[N],r[N],n;
29     void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
30         if (!b){d=a;x=1;y=0;}
31         else{ exgcd(b,a%b,d,y,x);y-=(a/b)*x;}
32     }
33     LL ex_CRT(LL *m,LL *r,int n){
34         LL M=m[1],R=r[1],x,y,d;
35         F(i,2,n){
36             exgcd(M,m[i],d,x,y);
37             if ((r[i]-R)%d) return -1;
38             x = (r[i] - R) / d * x % (m[i] / d);
39             R += x * M;
40             M = M / d * m[i];
41             R %= M;
42         }
43         return R > 0 ? R :R + M;
44     }
45     int main(){
46     #ifndef ONLINE_JUDGE
47         freopen("2891.in","r",stdin);
48         freopen("2891.out","w",stdout);
49     #endif
50         while(scanf("%lld",&n)!=EOF){
51             F(i,1,n) a[i]=getLL(),r[i]=getLL();
52             printf("%lld\n",ex_CRT(a,r,n));
53         }
54         return 0;
55     }

时间: 2024-10-22 13:17:23

【POJ】【2891】Strange Way to Express Integers的相关文章

【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

【POJ2891】Strange Way to Express Integers(拓展CRT)

[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream> #include<cstdio> using namespace std; #define ll long long #define MAX 111111 ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b){x=1,y=0;return a;

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 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 (非互质的中国剩余定理)

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

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 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

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

Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2891 Appoint description:  System Crawler  (2015-04-27) Description Elina is reading a book written by Rujia Liu,

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.

POJ2891——Strange Way to Express Integers(模线性方程组)

Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:Choose k different positive integers a1, a2, …, ak. For some n