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 ...
随机推荐
- VS2010/MFC编程入门之二十六(常用控件:滚动条控件Scroll Bar)
回顾上一节,鸡啄米讲的是组合框控件Combo Box的使用.本节详解滚动条控件Scroll Bar的相关内容. 滚动条控件简介 滚动条大家也很熟悉了,Windows窗口中很多都有滚动条.前面讲的列表框 ...
- react 项目微信端 签名失败 原因
用SPA做微信h5,调用微信jssdk的页面,安卓微信上木有问题,ios微信报当前url未注册 经过调试,是ios微信版本问题导致页面跳转url未变化,导致验签失败 所以我们大致的思想就是:在ios微 ...
- excel文件与txt文件互转,并且把excel里的数据导入到oracle中
一.excel文件转换成txt文件的步骤 a.首先要把excel文件转换成txt文件 1.Excel另存为中已经包含了TXT格式,所以我们可以直接将Excel表格另存为TXT格式,但是最后的效果好像不 ...
- 20145315 《Java程序设计》第四周学习总结
20145315 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承与多态 6.1何谓继承 6.1.1继承共同行为 把相同的程序代码提升为父类 private String ...
- Linux点亮一个灯
一 文件及其驱动程序 1.解压linux 压缩包 使用命令: tar xzvf linux-3.0.8-20140925.tgz ( tar xvf ------.tar tar xzvf------ ...
- 【大型web架构】一个大型web系统架构设计和技术选型的讨论摘录
1.数据库压力问题 所有的压力最终都会反映到数据库方面,一定要对数据库有一个整体的规划. 可以按照业务.区域等等特性对数据库进行配置,可以考虑分库.使用rac.分区.分表等等策略,确保数据库能正常的进 ...
- cf 429 B Working out
B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- [笔记] SQL性能优化 - 常用语句(二)
1.查询CPU开销大的语句 total_worker_time/execution_count AS avg_cpu_cost, plan_handle, execution_count, ( , ( ...
- UVa 11270 铺放骨牌(轮廓线DP)
https://vjudge.net/problem/UVA-11270 题意: 用1×2骨牌覆盖n×m棋牌,有多少种方法? 思路: 这道题目是典型的轮廓线DP题. 所谓轮廓线DP,就是以整行整列为状 ...
- 使用IDEA创建基于Gradle构建的JavaWeb项目
环境配置 jdk: 1.8 Gradle: 4.4.1 Tomcat: 9.0.0 One Step! 创建项目,初始化项目结构. 打开我们的IDEA,进入创建项目的界面,勾选Java,Web两个选项 ...