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 non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
    Line 1: Contains the integer k.
    Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2
8 7
11 9
Sample Output
31

题目大意:

    给定T个(a,b) 求解最小整数 X,使X满足 X mod a = b

解题思路:

    http://www.cnblogs.com/Enumz/p/4063477.html

Code:

 1 /*************************************************************************
 2     > File Name: poj2891.cpp
 3     > Author: Enumz
 4     > Mail: [email protected]
 5     > Created Time: 2014年10月28日 星期二 02时50分07秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 100000
23 #define LL long long
24 using namespace std;
25 LL extended_gcd(LL a,LL b,LL &x,LL &y) //返回值为gcd(a,b)
26 {
27     LL ret,tmp;
28     if (b==0)
29     {
30         x=1,y=0;
31         return a;
32     }
33     ret=extended_gcd(b,a%b,x,y);
34     tmp=x;
35     x=y;
36     y=tmp-a/b*y;
37     return ret;
38 }
39 int main()
40 {
41     LL N;
42     while (cin>>N)
43     {
44         long long a1,m1;
45         long long a2,m2;
46         cin>>a1>>m1;
47         if (N==1)
48             printf("%lld\n",m1);
49         else
50         {
51             bool flag=0;
52             for (int i=2;i<=N;i++)
53             {
54                 cin>>a2>>m2;
55                 if (flag==1) continue;
56                 long long x,y;
57                 LL ret=extended_gcd(a1,a2,x,y);
58                 if ((m2-m1)%ret!=0)
59                     flag=1;
60                 else
61                 {
62                     long long ans1=(m2-m1)/ret*x;
63                     ans1=ans1%(a2/ret);
64                     if (ans1<0) ans1+=(a2/ret);
65                     m1=ans1*a1+m1;
66                     a1=a1*a2/ret;
67                 }
68             }
69             if (!flag)
70                 cout<<m1<<endl;
71             else
72                 cout<<-1<<endl;
73         }
74     }
75     return 0;
76 }
时间: 2024-10-26 20:55:46

POJ2891——Strange Way to Express Integers(模线性方程组)的相关文章

解题报告 之 POJ2891 Strange Way to Express Integers

解题报告 之 POJ2891 Strange Way to Express Integers Description Elina 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, 

POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 互质就简单,但是不互质就有些麻烦,到现在我还是不大懂. 具体证明可以求教大佬,如果我懂了,会更新的. 代码 #include <cstring> #include <cstdio> #include <algorithm> #in

POJ2891 Strange Way to Express Integers【一元线性同余方程组】

题目链接: http://poj.org/problem?id=2891 题目大意: 选择k个不同的正整数a1.a2.-.ak,对于某个整数m分别对ai求余对应整数ri,如果 适当选择a1.a2.-.ak,那么整数m可由整数对组合(ai,ri)唯一确定. 若已知a1.a2.-.ak以及m,很容易确定所有的整数对(ai,ri),但是题目是已知a1. a2.-.ak以及所有的整数对(ai,ri),求出对应的非负整数m的值. 思路: 题目可以转换为给定一系列的一元线性方程 x ≡ r1( mod a1

poj2891 Strange Way to Express Integers

扩展欧几里得,注意防溢出. http://poj.org/problem?id=2891 1 #include <cstdio> 2 using namespace std; 3 typedef __int64 LL; 4 const int maxn = 1e5 + 10; 5 6 LL a[maxn], r[maxn]; 7 int n; 8 LL egcd(LL a, LL b, LL& x, LL& y){ 9 if(!b){ 10 x = 1, y = 0; 11 r

【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 (扩展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)整除时无解. 怎么推出的看了好

【EXCRT模板】POJ2891/LuoGu4777Strange Way to Express Integers拓展中国剩余定理

这道题需要exgcd的基础 POJ的题干描述十分恶心 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 21217 Accepted: 7120 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negati

数论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.

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