POJ 2891 Strange Way to Express Integers(扩展GCD)
来源:互联网 发布:python 移动文件 编辑:程序博客网 时间:2024/06/06 07:07
题意就是给你k组数,每组是m,a.有一个数字x对每组的m取余得到的余数为a。让你求出满足条件的最小的x。
分析假设只有一组数据那最小的就是余数a。如果有两组的话:x%m1 = a1,x%m2 = a2. m1*y+a1 = m2*z+a2。化简得到:m1*y-m2*z=a2-a1.通过扩展GCD可以得到一组解y,z。所以可以得到一个x = y*m1+a1。满足条件。如果x=(y*m1+a1)+k*LCM(m1,m2)(m1,m2的最小公倍数).可以满足所有的解集。所以这两个公式可以合并为一个公式:T%LCM(m1,m2) = y*m1+a1.这里的T是x=(y*m1+a1)+k*LCM(m1,m2)中的某一个数字。
Description
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
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 10001000//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 101000;using namespace std;LL x, y;LL exit_gcd(LL a, LL b){ if(b == 0) { x = 1; y = 0; return a; } LL p = exit_gcd(b, a%b); LL t = x; x = y; y = t-a/b*y; return p;}LL find_x(LL a, LL b, LL m){ LL d; d = exit_gcd(a, m); if(b%d) return -1; return ((x*(b/d)%m)+m)%m;}int main(){ int k; LL m1, m2, a1, a2; LL y; while(cin >>k) { cin >>m1>>a1; int flag = 0; k--; while(k--) { cin >>m2>>a2; if(flag) continue; y = find_x(m2, a1-a2, m1); if(y == -1) { flag = 1; continue; } m1 = m1*m2/__gcd(m1,m2);//求出最小公倍数 a1 = a2+m2*y;//算出之前满足条件的x,即为现在的余数 a1 = (a1%m1+m1)%m1;//防止有越界的可能 } if(flag) cout<<"-1"<<endl; else cout<<a1<<endl; } return 0;}
- POJ 2891 Strange Way to Express Integers(扩展GCD)
- 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 Strange Way to Express Integers 2891(扩展欧几里得)
- 【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 2891 Strange Way to Express Integers
- POJ 2891 Strange Way to Express Integers
- 跟着BOY学习--史上最通俗易懂的连连看算法--cocos2d-x 环境下开发
- 零散的命令随笔
- 基于UKey的安全网盘设计
- 用Powershell发邮件的两种方法
- XML解析的4种方式详解
- POJ 2891 Strange Way to Express Integers(扩展GCD)
- C++转C#重要方法记录
- 让员工更积极的六种关键因素
- 为MyEclipse添加自定义注释
- 转载_比较全面的gdb调试命令
- script:获取某张表上所有的DML操作
- Android中AlertDialog控件的基本使用和定制方法
- 昆明火车站“3·01”严重暴力恐怖案成功告破
- 深入 Java 调试体系: 第 1 部分,JPDA 体系概览