poj 2891Strange Way to Express Integers 中国剩余定理(不互质) 数据没读完就结束了程序居然是runtime error。
来源:互联网 发布:c语言入门书籍下载 编辑:程序博客网 时间:2024/04/28 20:33
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
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
#include<cstdio>#include<iostream>using namespace std;#define ll long longll g;void exgcd(ll a,ll b,ll& x,ll& y){ if(b==0) { g=a; x=1; y=0; } else { exgcd(b,a%b,y,x); y-=x*(a/b); }}int main(){ ll n,a,m,mt,att,k1,k2,c; while(cin>>n) { int flag=0; cin>>m>>a; for(int i=1; i<n; i++) { cin>>mt>>att; if(flag) continue; c=att-a; exgcd(m,mt,k1,k2); if(c%g) { flag=1; cout<<-1<<endl; continue; } k1=k1*c/g; k1=(k1+mt/g)%(mt/g); a=k1*m+a; m=m*mt/g; } if(!flag) { a=(a+m)%m; if(a) cout<<a<<endl; else cout<<m<<endl; } } return 0;}
- poj 2891Strange Way to Express Integers 中国剩余定理(不互质) 数据没读完就结束了程序居然是runtime error。
- 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-2981 Strange Way to Express Integers(中国剩余定理)
- poj 2891 Strange Way to Express Integers (中国剩余定理不互质)
- Strange Way to Express Integers-----不互质的中国剩余定理
- Strange Way to Express Integers(中国剩余定理+不互质)
- POJ2891 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)--中国剩余定理&&扩展欧几里得
- [Unity&C#]获取某个方法执行的时间
- maven修改远程和本地仓库地址
- web程序员之到底什么是架构师
- Spark开发语言Scala语言
- 【数据结构_链表_List_0952】单链表插入实践的操作
- poj 2891Strange Way to Express Integers 中国剩余定理(不互质) 数据没读完就结束了程序居然是runtime error。
- Incorrect string value: '\xE9\x98\xBF\xE6\x96\xAF...' for column 'name' at row 1 form表单提交乱码
- 03-上下文切换
- 习题 4-1 象棋
- MQTT入门级服务器搭建及简单通信源码
- PAT-B 1023. 组个最小数 (20)
- 遍历内核驱动模块
- hiho #1474 : 拆字游戏(dfs)@
- 数据结构之顺序表——C++模板类实现