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 a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (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 (非互质的中国剩余定理)的更多相关文章

  1. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  2. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  3. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  4. 「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 ...

  5. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  6. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  7. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  8. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  9. 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' ...

随机推荐

  1. JVM byte code

    http://blog.jamesdbloom.com/JVMInternals.html#constant_pool http://grepcode.com/file/repository.grep ...

  2. python编码-1

    help帮助系统,一个好的方法是直接看自带的帮助,尽量不用baidu help()是进入交互式帮助界面 quit是退出交互式帮助界面 [root@kvm1 python]# python Python ...

  3. SocketTcpClient

    public class SocketTcpClient { public static string ErrorMsg = string.Empty; private static Socket _ ...

  4. Spring中的ApplicationContext事件机制

    ApplicationContext的事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListerner接口来实现. 1. 创建EmailEvent pu ...

  5. 【redis】 redis 创建集群时,Waiting for the cluster to join.... 一直等待

    redis 搭建集群时,一直join.... ./redis-trib.rb  create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1: ...

  6. ssl_error_rx_record_too_long

    Linux下安装SSL 当使用https访问时出现: SSL 接收到一个超出最大准许长度的记录. (错误码: ssl_error_rx_record_too_long) 网上说修改虚拟机 vi /et ...

  7. 使用新浪云(SAE)实现基于mySql和微信公众平台的关键字请求响应服务

    本例是作者初次尝试微信公众平台开发之作,实现传统的关键字请求响应功能.即:用户发关键字,公众号通过关键字进行检索, 在mysql数据库中读取与关键字相关的信息,并返回给用户.本例在微信订阅号(开发者模 ...

  8. 使用Maven构建多模块项目

    [转] 使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问层).service(业务 ...

  9. Json数据处理

    1.字符串转换为Json数组:取json对象属性值. String st="[{"tradeDate":"2016-09-27","trad ...

  10. vmware workstation LINUX磁盘扩容

    1.edit virtual machine settings -> 选中硬盘->右侧utilities->expand(虚拟机不能存在镜像),输入要扩容到的大小 2.扩容之后进入系 ...