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 ...
随机推荐
- 使用win32com操作woord的方法记录
CSDN博客平台中有众多的 win32com 库操作word 的说明,对于通用的内容将一笔带过,主要介绍目前看来独一无二的内容. import win32com from win32com.clien ...
- 笔记-mongodb数据操作
笔记-mongodb数据操作 1. 数据操作 1.1. 插入 db.COLLECTION_NAME.insert(document) 案例: db.inventory.insertOn ...
- 在abp core中出现运行项目时EF获取到的appsetting.json或者appsettings.Production.json中的连接字符串为空
原因:有可能是生成的bin或者debug文件夹下没有将appsetting.json或者appsettings.Production.json文件生成过去 解决方法:手动拷贝过去,或者设置成自动生成过 ...
- 吴裕雄--天生自然PYTHON爬虫:使用Selenium爬取大型电商网站数据
用python爬取动态网页时,普通的requests,urllib2无法实现.例如有些网站点击下一页时,会加载新的内容,但是网页的URL却没有改变(没有传入页码相关的参数),requests.urll ...
- Java基础 -2.6
String字符串 在任何语言里面 都没有提供所谓的字符串这种基本数据类型,但是从实际的使用上来讲呢,各个编程语言 为了方便程序的开发,也都会提供有字符串的相应描述 在进行字符串变量使用的时候也可以使 ...
- Python学习第十八课——继承,接口继承等
1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...
- 标准盒模型、怪异盒模型(box-sizing)
CSS中Box model是分为两种: W3C标准(标准盒模型) 和 IE标准盒子模型(怪异盒模型).大多数浏览器采用W3C标准模型,而IE中则采用Microsoft自己的标准. 重要的一个属性是bo ...
- Day1-C-CF-1144A
简述:给你一串字符,判断是否由连续字母构成且每个字符只出现一次 思路:用set直接储存,判断size和初末位置字母与size的关系即可 代码: #include<iostream> #in ...
- 认识iOS系统架构
关于本文: 文章主要介绍iOS系统架构中的四层结构的内容.常用的框架.大致的功能,然后对iOS开发人员的发展提出自己的一些拙见. 一.iOS系统是基于UNIX系统,所有从系统稳定性上来说的确比其他操作 ...
- CSS相关(1)
CSS: 字体: 网页默认字体16px; 网站通用字体大小14px 最小是12px,最大无限大 单位换算:1em=16px 选择器:标签选择器:选择页面中所有指定标签,权重为1 通配符选择器:选择所有 ...