Day7 - E - Strange Way to Express Integers POJ - 2891
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.
思路:解同余方程,中国剩余定理,由于每个余数不一定互质,就需要两两合并方程组,给出代码与参考博客:
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; LL r[maxm], a[maxm]; void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d) {
if(!b) {
d = a, x = , y = ;
} else {
ex_gcd(b, a%b, y, x, d);
y -= x*(a/b);
}
} LL inv(LL t, LL p) {
LL x, y, d;
ex_gcd(t, p, x, y, d);
return d == ?(x%p+p)%p:-;
} LL gcd(LL a, LL b) {
return b?gcd(b, a%b):a;
} PLL linear(LL r[], LL a[], int n) { // x = r[i] (moda[i])
LL x = , m = ;
for(int i = ; i < n; ++i) {
LL A = m, B = r[i] - x, d = gcd(a[i], A);
if(B % d != ) return PLL(, -);
LL t = B/d * inv(A/d, a[i]/d) % (a[i]/d);
x = x + m*t;
m *= a[i]/d;
}
x = (x % m + m) % m;
return PLL(x, m);
} int main() {
int n;
while(scanf("%d", &n) != EOF) {
for(int i = ; i < n; ++i) {
scanf("%lld%lld", &a[i], &r[i]);
}
PLL ans = linear(r, a, n);
if(ans.second == -) printf("-1\n");
else printf("%lld\n", ans.first);
}
return ;
}
Day7 - E - Strange Way to Express Integers POJ - 2891的更多相关文章
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- poj Strange Way to Express Integers 中国剩余定理
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8193 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- Strange Way to Express Integers
I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
随机推荐
- word2vec 构建中文词向量
词向量作为文本的基本结构——词的模型,以其优越的性能,受到自然语言处理领域研究人员的青睐.良好的词向量可以达到语义相近的词在词向量空间里聚集在一起,这对后续的文本分类,文本聚类等等操作提供了便利,本文 ...
- JS获取CHECKBOX的值 AND 两个CHECKBOX 循环选中
获取多选按钮的值 var chk_value = ''; $('input[data-action="checkRole"]:checked').each(function(){ ...
- PCF8591 AD/DA模块使用详解
I2C PCF8591 8位AD/DA转换 BCM2835 Library 1.PCF8591T简述 PCF8591器件图如下: PCF8591是一个8位的CMOS数据采集器件,具有4个模拟输入(其中 ...
- 06. Z字型变换
题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...
- matplotlib常见操作
import osimport numpy as npfrom PIL import Imageimport matplotlib.pyplot as plt img = Image.open(os. ...
- idea右键新建选项没有类和包的创建方式
Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure
- 「AT2021」キャンディーとN人の子供 / Children and Candies
前言 今天练习赛出了这道题,由于我太菜没有在考场上做出来. 翻了题解后,感觉题解讲的并不是十分直观,所以自己写一篇. 题目大意 太长了,不讲了. 数据范围: \(1\leq N\leq 400\) \ ...
- JS回弹原理-高级
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 如何在django-filter中用choice field 的 value 值过滤对象
如果我们有这样一个model: class IPInfoModel(models.Model): TYPE_INTRANET = 1 TYPE_INTERNET = 2 IP_TYPES = ( (T ...
- target信息异常
当工程的编译target信息异常的时候,可以删除YourProjectName.xcodedeprij/xcuserdate目录. 该目录存有当前用户的各种工程状态信息,删除后重启Xcode,Xcod ...