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 ...
随机推荐
- jquery怎么获取当前标签下的子标签
1.var num1=$("ul > a:eq(0)").attr("ID");2.var num2=$("ul").children ...
- 最权威的json自定义格式
1.封装result,作为返回的对象 public class Result<T> { private int code; private String msg; ...
- swoole 父子进程间通信
<?php /** * 场景: * 监控订单表状态 父子进程通信 * 一个主进程 两个子进程实现 */ //设置主进程名 echo '主进程id:' . posix_getpid() . PHP ...
- CentOS 7 搭建Cobbler实现自动化安装系统
1.安装软件包 # yum -y install epel-release #安装EPEL源 # yum -y install cobbler dhcp pykickstart 2.启动cob ...
- Django 学习之Xadmin
一.xadmin的特点 1.基于Bootstrap3:Xadmin使用Bootstrap3.0框架精心打造.基于Bootstrap3,Xadmin天生就支持在多种屏幕上无缝浏览,并完全支持Bootst ...
- 从零开始-建站前的准备之django数据库创建的问题
稍微熟悉了一下django里面对于数据的操作,发现遇见了好多的问题. django对数据的操作是代码式的操作. 一开始在models里面开始为某个表创建参数,像username,password这样的 ...
- 修改gho打造个性ghostxp光盘
一.更换OEM信息(更改用户名.公司名与电脑名的方法 1.用软碟通提取出ghostxp中的gho镜像文件. 2.,用GHOSTEXP .EXE打开这个GHO文件,提取sysprep目录下的syspre ...
- (C#)Image.FromFile 方法会锁住文件的原因及可能的解决方法
Image.FromFile 一旦使用后,对应的文件在一直调用其生成的Image对象被Disponse前都不会被解除锁定,这就造成了一个问题,就是在这个图形被解锁前无法对图像进行操作(比如删除,修改等 ...
- 四、java基础-面向过程_对象_类中可出现的因素
1.面向过程和面向对象区别: 1)面向过程:开发一个应用程序.一个项目,必须先了解整个过程,了解各个步骤.模块间的因果关系,使的面向过程方式去开发程序时,代码和代码之间的关联程度是非常强.所以其中任何 ...
- video-editing
1. 视频编辑 2. 视频编辑软件列表 3. 视频编辑软件比较 4. 视频转换 1. 视频编辑 https://zh.wikipedia.org/wiki/视频编辑 2. 视频编辑软件列表 https ...