poj 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 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.
要考虑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 中国剩余定理的更多相关文章
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- 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 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- 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 ...
- poj 2891 Strange Way to Express Integers (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- POJ.2891.Strange Way to Express Integers(扩展CRT)
题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...
随机推荐
- PHP中关于foreach的笔试题
1,php与C++的不同之处是PHP中变量没有局部作用域,只有函数作用域和全局作用域.如下函数,在php中,$name的作用域是函数test():在C++中$name的作用域是for循环体,for循环 ...
- elasticsearch.net search入门使用指南中文版(翻译)
elasticsearch.net search入门使用指南中文版,elasticsearch.Net是一个非常底层且灵活的客户端,它不在意你如何的构建自己的请求和响应.它非常抽象,因此所有的elas ...
- P4842 城市旅行
题目链接 题意分析 首先存在树上的删边连边操作 所以我们使用\(LCT\)维护 然后考虑怎么维护答案 可以发现 对于一条链 我们编号为\(1,2,3,...,n\) 那么期望就是 \[\frac{a_ ...
- 丢用lamp手动安装apache php mysql
Centos7环境下. 使用lamp环境无法正常显示出thinkphp站点的内容,一气之下,选择手动安装 第一步: 安装apache php 和php连接数据库的工具php-mysql [root@ ...
- QuantLib 金融计算——随机过程之 Heston 过程
目录 QuantLib 金融计算--随机过程之 Heston 过程 Heston 过程 参考文献 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--随机过程之 H ...
- HAL库PWM
1.占空比控制 a.在CUBMX的TIM下选择时钟源,选择通道为模式PWM Generation ch1 ,设置分频系数,初始值,不需要自动重装载,选择PWM模式1或2,设置比较值pulse,设置初始 ...
- HDU - 6315 吉司机线段树
题意:给出a,b数组,区间上两种操作,给\(a[L,R]\)+1s,或者求\(\sum_{i=l}^{r}a_i/b_i\) 一看就知道是吉司机乱搞型线段树(低配版),暴力剪枝就好 维护区间a的最大值 ...
- Mac 10.12安装PDF浏览工具Foxit Reader
说明:永久没费的跨平台PDF浏览工具. 下载: (链接: https://pan.baidu.com/s/1pLEAoXH密码: is5j)
- 终于解决了贴吧手机版的一个重大BUG
终于解决了贴吧手机版的一个重大BUG 别诧异虽然同一个域名,但是,PC 和手机打开完全不一样的体验 http://tieba.yunxunmi.com/ 吃点夜校准备做梦去!! 发现 我云贴吧 一个 ...
- 在MVC程序中使用MongoDB数据库
.net项目中混搭数据库 安装和部署 C#中使用MongoDB MongoDb与关系型数据库区别 开机自启动 关系型数据库和非关系型数据口可以混搭 MongoDB是一个高性能,开源,无模式的,基于分布 ...