Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8193   Accepted: 2448

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.

要考虑0的情况,注意输入,,看清题意

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; __int64 Ex_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if(b==)
{
x=;
y=;
return a;
}
__int64 g=Ex_gcd(b,a%b,x,y);
__int64 hxl=x-(a/b)*y;
x=y;
y=hxl;
return g;
} __int64 gcd(__int64 a,__int64 b)
{
if(b==)
return a;
return gcd(b,a%b);
} int main()
{
__int64 k,m1,m2,r1,r2,x,y,t,i,d,c;
__int64 sum1=,sum2;
bool flag;
while(scanf("%I64d",&k)>)
{
scanf("%I64d%I64d",&m1,&r1);
sum1=m1;sum2=m1;
flag=false;
for(i=;i<=k;i++)
{
scanf("%I64d%I64d",&m2,&r2); if(flag==true) continue;
sum1=sum1*m2;
sum2=gcd(sum2,m2);
d=Ex_gcd(m1,m2,x,y);
c=r2-r1;
if(c%d)
{
flag=true;
continue;
}
x=c/d*x;
t=m2/d;
x=(x%t +t)%t;
r1=m1*x+r1;
m1=m1*m2/d;
}
if(flag==true)
{
printf("-1\n");
continue;
}
if(r1==)
{
r1=sum1/sum2;
}
printf("%I64d\n",r1);
}
return ;
}

poj Strange Way to Express Integers 中国剩余定理的更多相关文章

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

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

  2. POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  3. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  4. Day7 - E - Strange Way to Express Integers POJ - 2891

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

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

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

  6. poj——2891 Strange Way to Express Integers

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

  7. [POJ 2891] Strange Way to Express Integers

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

  8. poj 2891 Strange Way to Express Integers (扩展gcd)

    题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...

  9. POJ.2891.Strange Way to Express Integers(扩展CRT)

    题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...

随机推荐

  1. CentOS中vsftpd的主动和被动方式

    网址http://blog.csdn.net/nyunyuzhao/article/details/5734978,学习了. FTP是File Transfer Protocol(文件传输协议)的缩写 ...

  2. SQL注入不简单?那是你没有懂它的原理~

    我们真的了解SQL注入吗? 不管用什么语言编写的Web应用,它们都用一个共同点,具有交互性并且多数是数据库驱动.在网络中,数据库驱动的Web应用随处可见,由此而存在的SQL注入是影响企业运营且最具破坏 ...

  3. 爬虫5:beautifulsoup

      灵活方便的网页解析库,处理高效,支持多种解析器,利用它不用编写正则表达式即可方便的实现网页信息的提取     一. BeautifulSoup的几种解析库   解析器 使用方法 优势 劣势 Pyt ...

  4. Linux中的任务调度

    1.crond,linux中的任务调度器 crond的概念和crontab是不可分割的.crontab是一个命令,常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入 ...

  5. Vue 不睡觉教程1-从最土开始

    目标最近在学习vue的过程中发现网上的vue教程总有些不同的问题,有的教程上来只说语法,有的教程上来就用vue-cli来建项目,但是vue-cli是整合了webpack等多个插件的工具,不利于我们学习 ...

  6. 【kuangbin】计算几何部分最新模板

    二维几何部分 // `计算几何模板` ; const double inf = 1e20; const double pi = acos(-1.0); ; //`Compares a double t ...

  7. PXE+Kickstart 批量安装CentOS 7

    安装之前 将需要安装 CentOS 的服务器与 PXE 服务器通过网络设备连接:PXE 服务器安装 CentOS,并且关闭firewalld.selinux,设置静态IP地址,本例设为10.0.0.1 ...

  8. tomcat startup.brt启动一闪而过

    今天启动皕杰自带的tomcat的时候,直接点startup.brt控制台窗口一闪而过. 改用cmd命令启动: 切换到 E:\皕杰报表\皕杰报表3.5\DemoServer\bin目录 运行startu ...

  9. hiho# 1394最小路径覆盖 网络流拆点

    题目传送门 思路: 观察到路径上除了终点起点以外的每个点出度和入度都为1,和网络流的拆点很像,所以就把每个点都拆成两个点,若存在一条路径$(u,v)$,则建一条$(u,v+n,1)$的边,然后求出最大 ...

  10. Eclipse打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素的解决方法

    把有问题的 devices.xml 文件删除,再在sdk 里面 tools\lib 下找到devices.xml 文件,将这个文件拷贝到你删除的那个文件夹里,重启 eclipse 就 OK 啦!