POJ 2891 Strange Way to Express Integers(CRT非互质)
来源:互联网 发布:陕西大数据集团官网 编辑:程序博客网 时间:2024/05/19 03:45
题目点我点我点我
题目大意:给出k个模方程组:x mod ai = ri。求x的最小正值。如果不存在这样的x,那么输出-1.
解题思路:a[i]与a[j]不一定互质,不能直接用CRT求解。
运用合并的思想:
X mod r1=a1
X mod r2=a2
...
...
...
X mod rn=an
即可得同余方程x = r[i] (mod a[i])
易知 x = k1 * a1 + r1,x = k2 * a2 + r2。即k1 * a1 + r1=k2 * a2 + r2。
化简可得:k1 * a1= (r2 – r1) + k2 * a2 —— (1)
因为k2 * a2 % a2 = 0,所以k1 * a1 = (r2 – r1) (mod a2) ——(2)。
根据(1)可以变成
k1 * a1 - k2 * a2= (r2 – r1) ,即AX+BY=C方程组的形式,
要想方程组有解则C必须是gcd(A,B)的整数倍,
故k1 * a1 - k2 * a2= (r2 – r1) 有解的条件就是(r2 – r1)/gcd(a1,a2)= 0,否则无解,即输出-1.
设d=gcd(a1,a2),那么(2)可以化为
k1* a1/d = (r2 – r1) /d (mod a2/d)
再变形k1 = c/d * (a1/d)^(-1) (mod a2 / d),
其中,(a1/d)^(-1)表示(a1/d)对于(a2/d)的乘法逆元。
(a1/d)^(-1)可以通过扩展欧几里德求得。
令K=c/d * (a1/d)^(-1) ,则k1=K (mod a2/d),即k1 = y * a2/d + K,代入 x = k1 * a1 + r1 可得x = (a1 * K + r1) (mod a1 * a2 / d),此时a1*a2/d其实就是a1、a2的最小公倍数,
即LCM(a1,a2),
令r=(a1 * K + r1),即x=r(mod LCM(a1,a2)),合并完成!
/* ***********************************************┆ ┏┓ ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃ ┃ ┆┆┃ ━ ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃ ┃ ┆┆┃ ┻ ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆ ┃ 勒 ┃ ┆ ┆ ┃ 戈 ┗━━━┓ ┆┆ ┃ 壁 ┣┓┆┆ ┃ 的草泥马 ┏┛┆┆ ┗┓┓┏━┳┓┏┛ ┆┆ ┃┫┫ ┃┫┫ ┆┆ ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}//#pragma comment(linker, "/STACK:102400000,102400000")LL extend_gcd(LL a,LL b,LL &x,LL &y){ if(b==0) { x = 1, y = 0; return a; } LL p = extend_gcd(b,a%b,x,y); LL temp = x; x = y; y = temp - (a / b) * y; return p;}LL CRT(int n){ LL a1,a2,r1,r2,d,x,y,t; int flag = 1; a1 = read(), r1 = read(); for(int i=2;i<=n;i++) { a2 = read(), r2 = read(); d = extend_gcd(a1,a2,x,y); if((r2-r1)%d)flag = -1; t = a2 / d; x = ( (x * (r2-r1)/d)%t+t ) % t; //x为a2/d的逆元 r1 = x * a1 + r1; a1 = a1/d*a2; //a1为a1、a2的最小公倍数 r1 = (r1 % a1 + a1) % a1; } if(flag==-1)return -1; return r1;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);int n;while(~scanf("%d",&n)) { printf("%I64d\n",CRT(n)); }return 0;}
- POJ 2891 Strange Way to Express Integers(CRT非互质)
- poj 2891 Strange Way to Express Integers 【CRT】
- Poj 2891 Strange Way to Express Integers【crt】
- 【POJ】2891 - Strange Way to Express Integers(CRT)
- 【POJ 2891】Strange Way to Express Integers 【 CRT 证明详解】
- POJ 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- poj 2891 Strange Way to Express Integers
- poj 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- poj 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- Poj 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- poj 2891Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- POJ 3126 Prime Path(首相的无厘头要求,这个是不是华盛顿,BFS)
- ubuntu14.04安装wine
- 从数组中获取最值
- CreateProcess创建新的进程
- Opencv【7】---IplImage中四字节对其问题
- POJ 2891 Strange Way to Express Integers(CRT非互质)
- Studio中xml布局的design不显示“ Rendering Problems Exception raised during rendering: com/android/ut”,解决方案!
- 生活、工作点滴(二)
- HDU 5763 Another Meaning(KMP+DP)
- HDU 5775 Bubble Sort (树状数组或线段树)
- 使用Flying Saucer生成pdf
- 基于select模型的TCP服务器
- 随堂笔记7.29:对象,接口,构造方法,引用数据类型,抽象类,抽象方法-------1
- Codeforces 9C Hexadecimal's Numbers