POJ--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 ≤ 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
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的更多相关文章
- poj Strange Way to Express Integers 中国剩余定理
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8193 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- 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 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- Strange Way to Express Integers
I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- 数论F - Strange Way to Express Integers(不互素的的中国剩余定理)
F - Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format: ...
随机推荐
- numpy转换
csv2npy cccsv=numpy.genfromtxt('/root/c.csv', delimiter = ',') buf2npy imga=numpy.frombuffer(buf,num ...
- react random key generator;react如何产生随机不重复的key
1.<div key={+new Date() + Math.random()}> 2.使用数组的索引 3.使用uuid:https://www.npmjs.com/package/uui ...
- Spark一个简单案例
Spark是一个类似Map-Reduce的集群计算框架,用于快速进行数据分析. 在这个应用中,我们以统计包含"the"字符的行数为案例,.为建立这个应用,我们使用 Spark 1. ...
- OpenCV学习(27) 直方图(4)
我们可以利用OpenCV的直方图,backproject直方图和meanshift算法来跟踪物体.下面通过简单的例子来说明如何实现跟踪算法,我们有两幅狒狒的图片,如下图所示:我们首先在左图中框选狒狒的 ...
- go语言基础之复合类型
1.分类 类型 名称 长度 默认值 说明 pointer 指针 nil array 数组 0 slice 切片 nil 引⽤类型 map 字典 nil 引⽤类型 struct 结构体 2.指针 指针是 ...
- 介绍两个非常好用的Javascript内存泄漏检测工具
内存泄漏对开发者来说一般很难检测因为它们是由一些大量代码中的意外的错误引起的,但它在系统内存不足前并不影响程序的功能.这就是为什么会有人在很长时间的测试期中收集应用程序性能指标来测试性能. 最简单的检 ...
- JavaScript数组与字符串常用方法总结
先来一段代码引子: var str='hello world'; alert(str.charAt());//通过下标查找值: alert(str.indexOf());//通过值查找字符串下标:没有 ...
- 关于一道JS面试题的思考
题目: ; i < ; i++) { setTimeout(function() { console.log(new Date, i); }, ); } console.log(new Date ...
- php -- 取路径:getcwd()、__DIR__、__FILE__ 的区别
getcwd() :显示是 在哪个文件里调用此文件 的目录 __DIR__ :当前内容写在哪个文件就显示这个文件目录 __FILE__ : 当前内容写在哪个文件就显示这个文件目录+文件名
- java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not retrieve transation read-only status server
解决过程: 查看mysql的事物隔离级别 SHOW VARIABLES LIKE '%iso%'; 返回结果: REPEATABLE-READ 把这个改成:READ-COMMITTED 就好了: SE ...