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

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

Source

POJ Monthly--2006.07.30, Static

POJ2891

还是洛谷的好,题意就是同模方程组(模数不互质)

洛谷POJ区别就是POJ有判断误解和多组数据(POJ传统,好多多组数据)

这种最重要的就是推导过程了

网上还是很多的,我就再赘述一遍

x = r1(% a1)

x = r2(%a2)

即x = k1*a1*+r1 = k2*a2+r2

移项k1*a1 = k2*a2+r2-r1(%a2)

同模a2,即k1*a1 = r2-r1(%a2)

这里用exgcd,求出k1通解即k1 = k0+a2/gcd(a,b)*t

带入,也就是x = a1*k1+r1 = a1*(k0+a2/gcd(a,b)*t)+r1 = a1*k0+lcm(a,b)+r1

同模一个lcm(a1,a2)即a1*k0+r1 = x(%lcm(a1,a2))

这样我们就每次用一个exgcd可以把方程合并了(复杂度应该是nlogn)

同模方程组无解就是exgcd无解,判断下就好了

注意:

1.发现无解之后不可以break,因为数据还没有读完就进行下一组,肯定是错的

2.POJ此题没有数据范围,也就是说本题最好不要离线,只要先读入一个数,然后在线搞即可(时间相差不大,只是快了一点但是省内存,原来蒟蒻的我2.27MB,现在还是蒟蒻的我在线搞0.86MB)

好了上代码

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 typedef long long ll;
 6 ll n,a,r,x,y;bool flag=0;
 7 inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
 8 inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 9 ll ksc(ll a,ll b,ll mod)
10 {
11     ll fina=0;int kk=1;
12     if(a<0)a=-a,kk=-kk;
13     if(b<0)b=-b,kk=-kk;
14     while(b)
15     {
16         if(b%2)fina=(fina+a)%mod;
17         a=(a+a)%mod,b=b/2;
18     }
19     return fina%mod*kk;
20 }
21 void exgcd(ll a,ll b)
22 {
23     if(!b)
24         x=1,y=0;
25     else
26     {
27         exgcd(b,a%b);
28         ll tt=x;
29         x=y,y=tt-a/b*x;
30     }
31 }
32 int main()
33 {
34     while(scanf("%lld",&n)==1 && n)
35     {
36         flag=0;
37         scanf("%lld%lld",&a,&r);
38         for(register ll i=1,a1,r1,gc,aa,bb,cc;i<n;i++)
39         {
40             a1=a,r1=r;
41             scanf("%lld%lld",&a,&r);
42             aa=a1,bb=a,cc=r-r1,gc=gcd(aa,bb);
43             if(cc%gc){flag=1;continue;}
44             aa/=gc,bb/=gc,cc/=gc;
45             exgcd(aa,bb);
46             r=(ksc(x,cc,bb)+bb)%bb*a1+r1;
47             a=lcm(a1,a);
48         }
49         if(flag){puts("-1");continue;}
50         ll aa=1,bb=a,cc=r;
51         exgcd(aa,bb);
52         printf("%lld\n",(ksc(x,cc,bb)+bb)%bb);
53     }
54     return 0;
55 }

原文地址:https://www.cnblogs.com/Qin-Wei-Kai/p/10079603.html

时间: 2024-10-01 23:11:30

【EXCRT模板】POJ2891/LuoGu4777Strange 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(模线性方程组)

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

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

POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 15898 418373 161478614 149488440 1748022751 21618619576 810918992 241779667 1772616743 1953316358 125248280 2273149397 3849022001 2509433771 3885219405 35

POJ 2891 Strange Way to Express Integers 中国剩余定理

裸题,上模版,,嘿嘿 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<map> using namespace std; #define ll __in

模板——中国剩余定理

1 //n个方程,x=a[i](mod m[i]) 2 3 LL china(int n, int *a, int *m) { 4 LL M = 1, d, y, x = 0; 5 for(int i = 0; i < n; i++) M *= m[i]; 6 for(int i = 0; i < n' i++) { 7 LL w = M / m[i]; 8 exgcd(m[i], w, d, d, y); 9 x = (x + y*w*a[i]) % M; 10 } 11 return (x

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