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

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 ≤ ik) 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 ≤ ik).

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

同余问题:
关于欧几里得扩展的代码模板:
 void exgcd(int a,int b,int * x,int  *y,int  * d)
{
if(b==)
{
x=,y=,d=a;
}
else
{
exgcd(b,a%b,x,y,d);
int temp=x;
x=y,y=temp-a/b*y;
}
}

解一元多项式时的代码:

 int sove()
{
scanf("%d%d",&a1,&r1);
for(i=;i<n;i++)
{
scanf("%d%d",&a2,r2);
a=a1,b=a2,c=r2-r1;
exgcd(a,b,x,y,d);
if(c%d!=)
{
ifhave=;
}
int t=b/d;
x=(x*(c/d)%t+t)%t;
r1=r1+x*a1;
a1=a1*(a2/d);
}
if(!ifhave)
{
r1=-;
}
return r1; //即为解的个数
}
代码:
 #include<stdio.h>
#define LL long long
LL x,y,q;
void exgcd(LL a,LL b)
{
if(b==)
{
x=,y=,q=a;
}
else
{
exgcd(b,a%b);
LL temp=x;
x=y,y=temp-a/b*y;
}
} int main()
{
LL a1,r1,a2,r2,n,i;
bool ifhave;
while(scanf("%I64d",&n)!=EOF)
{ scanf("%I64d%I64d",&a1,&r1);
ifhave=true;
for(i=;i<n;i++)
{
scanf("%I64d%I64d",&a2,&r2);
exgcd(a1,a2);
if((r2-r1)%q)
{
ifhave=false;
}
LL t=a2/q;
x=(x*((r2-r1)/q)%t+t)%t;
r1+=a1*x;
a1*=(a2/q);
}
if(!ifhave) r1=-;
printf("%I64d\n",r1);
}
return ;
}

POJ--Strange Way to Express Integers的更多相关文章

  1. poj Strange Way to Express Integers 中国剩余定理

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

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

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

  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. poj——2891 Strange Way to Express Integers

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

  5. [POJ 2891] Strange Way to Express Integers

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

  6. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  7. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  8. Strange Way to Express Integers

    I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​ ...

  9. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  10. 数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

    F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format: ...

随机推荐

  1. VMware Workstation 14 Pro 激活密钥

    VMware Workstation 14 Pro 激活密钥 CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3WK-AFXEK-488JP-A7MQX-XL8YF AC5XK-0ZD ...

  2. [转]linux下完全备份数据库mysql

    #配置参数 USER=vimer_admin #数据库用户名 PASSWORD=dreamfly123 #数据库用户密码 DATABASE=vimer #数据库名称 WEBMASTER=@qq.com ...

  3. Flume-NG一些注意事项(转)

    原文链接:记Flume-NG一些注意事项 这里只考虑flume本身的一些东西,对于JVM.HDFS.HBase等得暂不涉及.... 一.关于Source: 1.spool-source:适合静态文件, ...

  4. iOS:NSBundle的具体介绍

    NSBundle介绍:它是一个单例类,用来加载资源 bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-i ...

  5. go语言string、int、int64互相转换

    #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 6 ...

  6. C# 使用Newtonsoft.Json序列化自定义类型

    Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Linq To JSON可以快速的读写Json,通过JsonSerializ ...

  7. SqlServer2008安装时系统配置检查器重新启动计算机失败

    系统:win7 64 旗舰版   软件版本:sqlserver 2008 在安装前执行:系统配置检查器 的时候报错 ‘重新启动计算机失败’,如下图 处理办法: RebootRequiredCheck ...

  8. (转)unity3d中脚本生命周期(MonoBehaviour lifecycle)

    自:http://blog.csdn.net/qitian67/article/details/18516503 最近在做一个小示例,发现类继承于MonoBehaviour的类,有很多个方法,于是乎必 ...

  9. 1-2Html与CSS的关系

    Html和CSS的关系 基于微信的后台开发须要掌握的基础知识包括有:HTML.CSS.JavaScript语言. 以下我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容 ...

  10. springboot securyt 默认的用户名和密码

    用户名:user 密码在控制台,每次启动的时候会生成一个随机的密码,如下: