poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9472 | Accepted: 2873 |
Description
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:
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.
Source
//156K 16MS C++ 1362B 2014-06-13 12:36:23
#include<stdio.h>
__int64 gcd(__int64 a,__int64 b)
{
return b?gcd(b,a%b):a;
}
__int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if(b==){
x=;y=;
return a;
}
__int64 d=extend_euclid(b,a%b,x,y);
__int64 t=x;
x=y;
y=t-a/b*y;
return d;
}
__int64 inv(__int64 a,__int64 n)
{
__int64 x,y;
__int64 t=extend_euclid(a,n,x,y);
if(t!=) return -;
return (x%n+n)%n;
}
bool merge(__int64 a1,__int64 n1,__int64 a2,__int64 n2,__int64 &a3,__int64 &n3)
{
__int64 d=gcd(n1,n2);
__int64 c=a2-a1;
if(c%d) return false;
c=(c%n2+n2)%n2;
c/=d;
n1/=d;
n2/=d;
c*=inv(n1,n2);
c%=n2;
c*=n1*d;
c+=a1;
n3=n1*n2*d;
a3=(c%n3+n3)%n3;
return true;
}
__int64 china_reminder2(int len,__int64 *a,__int64 *n)
{
__int64 a1=a[],n1=n[];
__int64 a2,n2;
for(int i=;i<len;i++){
__int64 aa,nn;
a2=a[i],n2=n[i];
if(!merge(a1,n1,a2,n2,aa,nn)) return -;
a1=aa;
n1=nn;
}
return (a1%n1+n1)%n1;
}
int main(void)
{
int n;
__int64 a[],b[];
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%I64d %I64d",&a[i],&b[i]);
printf("%I64d\n",china_reminder2(n,b,a));
}
return ;
}
poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)的更多相关文章
- 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 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】
题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...
随机推荐
- jquery easy ui combox
$(document).ready(function() { $.ajax({ type: 'POST', dataType: "json", url:'menu/getAll', ...
- Application Loader上传app程序
提示:如果您安装了XCode开发环境.在/Applications/XCode.app/Contents/Applications目录中可以找到Application Loader(右键XCode选择 ...
- Linux下实现获取远程机器文件
创建公钥秘钥实现无密码登录后即可获取到文件内容了!! A:xxx.xxx.6.xxx B:xxx.xxx.xxx.x 一.创建 A机器 ssh-keygen -t rsa 二.拷贝——将生成的公钥复制 ...
- 利用硬链接和truncate降低drop table对线上环境的影响
众所周知drop table会严重的消耗服务器IO性能,如果被drop的table容量较大,甚至会影响到线上的正常. 首先,我们看一下为什么drop容量大的table会影响线上服务 直接执行drop ...
- 使用springMVC实现文件上传和下载之环境配置与上传
最近的项目中用到了文件的上传和下载功能,任务分配给了其他的同时完成.如今项目结束告一段落,我觉着这个功能比较重要,因此特意把它提取出来自己进行了尝试. 一. 基础配置: maven导包及配置pom.x ...
- access remote libvirtd
访问远程libvirtd服务因为是在一个可信环境中运行,所以可以忽略安全方面的操作,步骤如下:(1)更改libvirtd配置 1.1 更改/ect/sysconfig/libvirtd文件,打开 ...
- StringIO学习
StringIO StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO.一个简单的例子,让你对StringIO有一 ...
- NBU AIX ORACLE10G RAC恢复到AIX单实例(表空间恢复)
ln -s /usr/openv/netbackup/bin/libobk.a64 /oraclev3/product/10.2.0/lib/libobk.a不建软连接会报如下错误: using ta ...
- (LinkedList)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- windows下使用VS2010编译jpeglib
1.下载源代码下载地址:http://www.ijg.org/files/, 选择最新版本的windows版本压缩包,进行下载. jpegsr9a.zip 1042 Kb Su ...