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 ...
随机推荐
- Windows系统设置临时环境变量
path f:\newtest;%path% ---在Path内容中增加一个新的可执行文件搜索路径 从菜鸟到高手,CMD命令行修改临时环境变量:path
- [JS] 屏蔽右键
if (window.Event) document.captureEvents(Event.MOUSEUP); function nocontextmenu() { event.cancelBubb ...
- 【css】—— inline-block 4px 和图片底部 2px bug
首先我们观察一组案例: HTML结构很简单: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- ubuntu下apache2使用的简单总结
一. 修改apache2原80端口为90端口 1. 修改/etc/apache2/ports.conf, 将端口80改为90,443,改为444 2. 修改/etc/apache2/sites ...
- Android-获取手机已经安装的程序
有时候我们会查询手机里面是否安装了某个程序,或者获取已经安装软件名称的集合. android这边提供了相应的接口. [java] view plaincopy final PackageManager ...
- 2016级算法期末模拟练习赛-A.wuli51和京导的毕业旅行
1063 wuli51和京导的毕业旅行 思路 中等题,二分+贪心. 简化题意,将m+1个数字分成n份,ans为这n段中每段数字和的最大值,求ans最小值及其方案. 对于这种求最小的最大值,最常用的方法 ...
- 【性能测试】:LR插入mysql数据库数据,脚本参数化问题
一,今天准备脚本做mysql数据库的铺地数据,脚本内容不赘述,在批量执行insert语句时候,出现一个问题: // sprintf(chQuery, "insert into table ( ...
- 关于京东首页的制作以及切图软件fireworks推荐
这几天跟随老师给的视频学习制作京东首页 学到了很多 以下是相关代码和截图 html部分 <!DOCTYPE html><html lang="en">< ...
- ArduinoNano卡在上传,无法烧录
卡在“上传...”.过了很久被告知失败. 上午在开发版管理器中将Arduino AVR Boards从1.6.20升级到1.6.22,出现这个问题. 再安装回1.6.20,问题未被解决. 查阅资料无果 ...
- centos 7 上安装 testlink 1.9.15/1.9.16/1.9.17/1.9.18 (mysql/php/httpd)
1.9.18 的System Requirements - server.注意,适用于 1.9.15 及以后. Server environment should consist of: web-se ...