POJ 2891 Strange Way to Express Integers (扩展欧几里得)
来源:互联网 发布:数据库进销存管理系统 编辑:程序博客网 时间:2024/06/07 08:32
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
28 711 9
Sample Output
31
题意
给出一组
思路
因为除数之间不满足两两互质,所以就不能直接套用中国剩余定理的模板咯!
这里我们采用合并 不定方程 的方法,即先合并前两个方程,然后用合并的结果与第三个方程进行合并,最终合并完之后计算得到的
我们假设
联立可得
扩展欧几里得算法中说:存在整数对
于是我们可以根据上面联立得出的式子构造出
通过
于是便有
带回原来的式子可以解得
上式可转化成:
于是这两个方程便合并啦~
合并完所有的方程以后,输出最小的正整数
关于代码中求
即
通解为
于是在所有解中
所以
AC 代码
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<cmath>#include<iostream>using namespace std;#include<queue>#include<map>#define INF (1<<25)typedef __int64 LL;#define maxn 10005LL m[maxn],r[maxn];int n;LL ex_gcd(LL a, LL b, LL &x, LL &y){ LL d = a; if(b != 0) { d = ex_gcd(b, a % b, y, x); y -= (a/b) * x; } else x = 1, y = 0; return d;}LL solve(){ LL M=m[0],R=r[0],gcd,x,y; for(int i=1; i<n; i++) { gcd=ex_gcd(M,m[i],x,y); // 扩展欧几里得求系数 x y gcd if((r[i]-R)%gcd)return -1; // 如果无法整除即无解 LL k=(r[i]-R)/gcd*x%(m[i]/gcd); // 求k0 LL X=k*M+R; // 代回原式求X M=M/gcd*m[i]; // M变为lcm R=(X+M)%M; // R变为此时最小的X } return R;}int main(){ while (~scanf("%d",&n)) { for (int i = 0; i < n; ++i) scanf("%I64d%I64d", &m[i], &r[i]); printf("%I64d\n",solve()); } return 0;}
- 【POJ 2891】Strange Way to Express Integers(扩展欧几里得)
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- poj 2891 Strange Way to Express Integers 扩展欧几里得运用
- POJ Strange Way to Express Integers 2891(扩展欧几里得)
- POJ-----2891---Strange Way to Express Integers扩展欧几里得
- POJ 2891 Strange Way to Express Integers(扩展欧几里得)
- 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(扩展GCD)
- POJ 2891-Strange Way to Express Integers(扩展欧几里德)
- POJ 2891 Strange Way to Express Integers 扩展gcd
- 【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
- 腾讯2017实习生招聘在线笔试编程题(技术类-PC客户端开发)
- UML建模、数据库设计和UI设计工具
- 数学之美番外篇:平凡而又神奇的贝叶斯方法
- 并查集
- 中位数
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- java字符串之split
- Ubuntu16.04配置apache2+CGI
- 题目1124:Digital Roots 九度OJ
- 数据权限实现(Mybatis拦截器+JSqlParser)
- raise IOError, 'Not a gzipped file'
- 常用计算机远程控制软件介绍
- JS 调取摄像头
- C++构造函数初始化列表与赋值