POJ2891 Strange Way to Express Integers
题意
|
Language:Default
Strange Way to Express Integers
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.
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 POJ Monthly--2006.07.30, Static
|
分析
excrt裸题,直接套模板即可。
时间复杂度\(O(n \log n)\)
代码
#include<iostream>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll;
ll exgcd(ll a,ll b,ll&x,ll&y){
if(!b){
x=1,y=0;
return a;
}
ll d=exgcd(b,a%b,x,y);
ll z=x;x=y;y=z-a/b*y;
return d;
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
int n;
while(~scanf("%d",&n)){
ll a,b,x,y,k;
read(a),read(b);
ll lcm=a,ans=b;
bool flag=1;
--n;
while(n--){
read(a),read(b);
b=(b-ans%a+a)%a;
ll d=exgcd(lcm,a,x,y);
if(b%d) flag=0;
else k=x*(b/d)%a;
ans+=k*lcm;
lcm=lcm/d*a;
ans=(ans%lcm+lcm)%lcm;
}
printf("%lld\n",flag?ans:-1);
}
return 0;
}
POJ2891 Strange Way to Express Integers的更多相关文章
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers
P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...
- POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...
- POJ2891 - Strange Way to Express Integers(模线性方程组)
题目大意 求最小整数x,满足x≡a[i](mod m[i])(没有保证所有m[i]两两互质) 题解 中国剩余定理显然不行....只能用方程组两两合并的方法求出最终的解,刘汝佳黑书P230有讲~~具体证 ...
- POJ2891 Strange Way to Express Integers [中国剩余定理]
不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...
- POJ2891 Strange Way to Express Integers【扩展中国剩余定理】
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...
- [poj2891]Strange Way to Express Integers(扩展中国剩余定理)
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...
- poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组
怎样求同余方程组?如: \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \cdots \\ x \equ ...
随机推荐
- MyBatis—mybatis-config.xml配置介绍
在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码 说明: 收藏代码 1. <bean id="sqlSessionFactory" ...
- hdu5072 容斥+枚举
这题说的是给了 n 个数字 每个数值大于1 小于100000,n小于100000 ,找出满足下面要求的三人组有多少种 比如abc ( (ab)==(bc)==(ac) ==1 )||( (ab)!=1 ...
- Python中的MySQL接口:PyMySQL & MySQLdb
MySQLdb模块只支持MySQL-3.23到5.5之间的版本,只支持Python-2.4到2.7之间的版本 PyMySQL支持 Python3.0以后的版本 PyMySQL https://pypi ...
- 分布式ID方案有哪些以及各自的优势
1. 背景 在分布式系统中,经常需要对大量的数据.消息.http请求等进行唯一标识.例如:在分布式系统之间http请求需要唯一标识,调用链路分析的时候需要使用这个唯一标识.这个时候数据自增主键已 ...
- extern--C#调用C++等其他非托管代码
例如: https://www.cnblogs.com/sosoft/p/extern.html
- Migrating from Spring 3 to Spring 4 - org.springframework.scheduling.quartz.CronTriggerBean
I'm trying to migrate from spring 3.0.5 to spring 4.1.X . Spring 3 has Class named as "org.spri ...
- LA 7277 Landscaping(最小割)
https://vjudge.net/problem/UVALive-7277 题意: 给出一个n*m的地图,.代表低坡,#代表高坡. 现在有n+m辆车分别从上端和左端出发,如果在行驶的过程中需要转换 ...
- python 将类属性转为字典
class dictObj(object): def __init__(self): self.x = 'red' self.y = 'Yellow' self.z = 'Green' def do_ ...
- Mongo Plugin插件(编辑器PyCharm的Mongo插件安装与使用)
博主接触到MongoDB数据库.用普通的Navicat工具 是不支持的 正准备重新安装一款对应的可视化工具.刚好发现在PyCharm编辑中有连接mongoDB数据的插件 Mongo Plugin 这里 ...
- Rails 5 Test Prescriptions 倒数第2章spring gem 如何让测试变快。分离rails(只有原理)
Spring speeds up development by keeping your application running in the background Rails程序自动增加:sprin ...