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 ...
随机推荐
- setInterval(),setTimeout(),location.reload(true)
1,setInterval() setInterval()方法可以按照指定的周期来调用函数或表达式,他会不停地调用函数,直到调用clearInterval()方法或窗口关闭.由setInterval( ...
- robot framework学习笔记之二———变量
Robot Framework的变量分为标量, 列表和字典, 分别使用语法格式 ${SCALAR}, @{LIST} 和 &{DICT} 来定义. 此外, 环境变量可以直接使用语法 %{ENV ...
- CentOS6.5下telnet服务
00×0 本文介绍Telnet搭建,以及展示这是一个不安全的远程服务. 00×1 服务准备工作 [root@localhost ~]# yum install xinetd telnet-server ...
- day06 --class --home
# -*- coding: utf-8 -*-# @Time : 2018/12/26 20:29# @Author : Endless-cloud# @Site : # @File : day 06 ...
- iOS学习笔记(5)——显示简单的TableView
1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...
- 代码 | 自适应大邻域搜索系列之(2) - ALNS算法主逻辑结构解析
00 前言 在上一篇推文中,教大家利用了ALNS的lib库求解了一个TSP问题作为实例.不知道你萌把代码跑起来了没有.那么,今天咱们再接再厉.跑完代码以后,小编再给大家深入讲解具体的代码内容.大家快去 ...
- Java方法区
方法区 在一个jvm实例的内部,类型信息被存储在一个称为方法区的内存逻辑区中.类型信息是由类加载器在类加载时从类文件中提取出来的.类(静态)变量也存储在方法区中. jvm实现的设计者决定了类型信息的内 ...
- Markdown数学公式速查记录
参考: Markdown数学公式语法 markdown最全数学公式速查 行内与独行 行内公式:将公式插入到本行内,符号:$公式内容$,如:$xyz$ 独行公式:将公式插入到新的一行内,并且居中,符号: ...
- 基于CentOS7系统一键配置Aria2 实现服务器离线下载工具
我们有些网友购买的海外VPS主机并不是用来做网站的,而是用来作为下载资源工具使用的.确实用这样的工具搭建之后是比本地下载速度快,因为有些资源.软件等是海外资源,而且挂载在服务器上不占用本地的资源.在这 ...
- c# timer使用
C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Timers.Timer 这三个Timer我想大家对S ...