poj 2891 中国剩余定理

Strange Way to Express Integers

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 20640   Accepted: 6946

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 a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (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

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

题意 给出k个同余方程组:x mod ai = ri。求x的最小正值。如果不存在这样的x,那么输出-1.不满足所有的ai互质。

解析 https://blog.csdn.net/litble/article/details/75807726 中国扩展剩余定理

 1 #include<stdio.h>
 2 #define pb push_back
 3 #define mp make_pair
 4 #define fi first
 5 #define se second
 6 #define all(a) (a).begin(), (a).end()
 7 #define fillchar(a, x) memset(a, x, sizeof(a))
 8 #define huan printf("\n");
 9 #define debug(a,b) cout<<a<<" "<<b<<" "<<endl;
10 using namespace std;
11 const int maxn=1e5+10,inf=0x3f3f3f3f;
12 typedef long long ll;
13 ll chu[maxn],yu[maxn];
14 ll exgcd(ll a,ll b,ll &x,ll &y)   //&引用符号,修改x,y,函数返回的是a,b的最大公约数
15 {
16     if(b==0)
17     {
18         x=1;y=0;
19         return a;
20     }
21     ll gcd=exgcd(b,a%b,x,y);            //递归下去
22     ll temp=x;
23     x=y;y=temp-a/b*y;
24     return gcd;
25 }
26 ll CRT(int n)//中国剩余定理 除数互质
27 {
28     ll lcm=1,x,y,ans=0;
29     for(int i=0;i<n;i++)
30         lcm*=chu[i];   //除数互质
31     for(int i=0;i<n;i++)
32     {
33         ll temp=lcm/chu[i];
34         ll gcd=exgcd(temp,chu[i],x,y); //互质所以gcd肯定是1 x*temp%chu[i]=1 <=>x*temp+y*chu[i]=1
35         x=(x%chu[i]+chu[i])%chu[i];  //最小正整数解
36         ans=(ans+yu[i]*x*temp)%lcm;
37     }
38     return ans;
39 }
40 ll EXCRT(int n)//扩展中国剩余定理除数互不互质都可以
41 {
42     ll temp=chu[0],k=yu[0],x,y,t;
43     for(int i=1;i<n;i++)
44     {
45         ll gcd=exgcd(temp,chu[i],x,y);
46         if((yu[i]-k)%gcd)return -1;//无解
47         x*=(yu[i]-k)/gcd,t=chu[i]/gcd,x=(x%t+t)%t;
48         k=temp*x+k,temp=temp/gcd*chu[i],k%=temp;
49     }
50     k=(k%temp+temp)%temp;
51     return k;
52 }
53 int main()
54 {
55     int n;
56     while(scanf("%d",&n)!=EOF)
57     {
58         for(int i=0;i<n;i++)
59             scanf("%lld%lld",&chu[i],&yu[i]);
60         printf("%lld\n",CRT(n));
61     }
62     return 0;
63 }

原文地址:https://www.cnblogs.com/stranger-/p/9688516.html

时间: 2024-10-12 02:49:08

poj 2891 中国剩余定理的相关文章

POJ 1006 中国剩余定理

[题意]: 给定p,e,i,d,求解 (x + d) % 23 = p (x + d) % 28 = e(x + d) % 33 = i x最小正整数值 [知识点]: 中国剩余定理 [题解]: 典型的 xmodmi = ai模型,其中mi间两两互素.但该题式子较少,也可以直接自己化简带入值. [代码]: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #inc

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso

Biorhythms POJ - 1006 中国剩余定理

定理证明:https://blog.csdn.net/d_x_d/article/details/48466957 https://blog.csdn.net/lyy289065406/article/details/6648551 关键思路在于构造一个每部分膜另外一个不包含在部分内的部分的余数是1  然后把各部分分别乘以对应的余数 其中题目给出的第一天天数  就是  (n+d)%xn 的余数 不用处理 1 2 #include<iostream> 3 using namespace std;

POJ 1006:Biorhythms 中国剩余定理

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

acm数论之旅--中国剩余定理

中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这个答案 5*7*inv(5*7,  3) % 3  =  1 3*7*inv(3*7,  5) % 5  =  1 3*5*inv(3*5,  7) % 7  =  1 这3个式子对不对,别告诉我逆元你忘了(*′?`*),忘了的人请翻阅前几章复习 然后两边同乘你需要的数 2 * 5*7*inv(5*

poj 1006(剩余定理)

生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111426   Accepted: 34702 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the

gcd,扩展欧几里得,中国剩余定理

1.gcd: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题:有一正整数ans,对于每一对数,都有:(ans-a[i])mod m[i]=0.求此数最小为多少. 输入样例: 1 10 2 3 1 2 3 2 3 5 8 1 2 3 4 5 6 7 8 97 89 67 61 59 53 47 88 12 1 2 3 4 5 6 7 8 9

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mod a[i] = b[i], - (0 < a[i] <= 10). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include