http://poj.org/problem?id=2891

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 16849   Accepted: 5630

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.

敲模板磨时间、、

 #include <algorithm>
#include <cstdio> using namespace std; #define LL long long
const int N();
LL n,a[N],m[N]; LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b)
{
x=; y=;
return a;
}
LL ret=exgcd(b,a%b,x,y),tmp=x;
x=y; y=tmp-a/b*y;
return ret;
}
LL CRT()
{
LL ret=m[],tot=a[];
for(int i=;i<=n;i++)
{
LL x,y,tt=a[i],c=m[i]-ret;
LL gcd=exgcd(tot,tt,x,y);
if(c%gcd) return -;
x=x*c/gcd;
LL mod=tt/gcd;
x=(x%mod+mod)%mod;
ret+=x*tot; tot*=mod;
}
if(!ret) ret+=tot;
return ret;
} int main()
{
for(;~scanf("%lld",&n);)
{
for(int i=;i<=n;i++)
scanf("%lld%lld",a+i,m+i);
printf("%lld\n",CRT());
}
return ;
}

POJ——T 2891 Strange Way to Express Integers的更多相关文章

  1. 【POJ】2891 Strange Way to Express Integers

    http://poj.org/problem?id=2891 题意:求最小的$x$使得$x \equiv r_i \pmod{ a_i }$. #include <cstdio> #inc ...

  2. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

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

  3. poj——2891 Strange Way to Express Integers

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

  4. [POJ 2891] Strange Way to Express Integers

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

  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 中国剩余定理 数论 exgcd

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

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

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

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

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

随机推荐

  1. 一次AIX LVM PV重复PVID故障处理记录

    故障背景:客户需要把AIX 5.3.10上的一些VG做两台存储之间的LVM级别的Mirror,存储使用的是两台EMC DMX3,但是由于两套SAN存储之前是使用EMC的软件做存储级别的Mirror,所 ...

  2. CsGL着色的三角形

    转自NeHe教程 public override void Draw() { // Here's Where We Do All The Drawing glClear(GL_COLOR_BUFFER ...

  3. java中hashmap和hashtable和hashset的区别

    hastTable和hashMap的区别:(1)Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现.(2)这个不同即是最重要的一点:Ha ...

  4. 面对即将终止支持的server你还能做些什么

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXF1c2hp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  5. XMPP使用简单介绍--登录

    在现阶段的通信服务中,各种标准都有.因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互 ...

  6. scikit-learn:3.2. Grid Search: Searching for estimator parameters

    參考:http://scikit-learn.org/stable/modules/grid_search.html GridSearchCV通过(蛮力)搜索參数空间(參数的全部可能组合).寻找最好的 ...

  7. 七牛用户搭建c# sdk的图文讲解

    Qiniu 七牛问题解答 问题描写叙述:非常多客户属于小白类型. 可是请不要随便喷七牛的文档站.由于须要一点http的专业知识才干了解七牛的api文档.如今我给大家弄个c# sdk的搭建步骤 问题解决 ...

  8. [BZOJ4826][HNOI2017]影魔 可持久化线段树

    链接 题意:给你 \(1\) 到 \(n\) 的排列 \(k_1,k_2,\dots,k_n\) ,对 \(i,j (i<j)\)来说,若不存在 \(k_s (i<s<j)\) 大于 ...

  9. DECLARE CURSOR (Transact-SQL)

    Defines the attributes of a Transact-SQL server cursor, such as its scrolling behavior and the query ...

  10. dropify,不错的图片上传预览插件

    引言 传统的图片上传,很丑.点击选择之后,还无法预览. 有一种方案是传到服务器,然后返回地址,然后显示,比较麻烦. 用这个dropify,就可以解决之歌问题. 看效果 用法 1.引入文件,需要jque ...