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 ...
随机推荐
- jvm之gc日志
ava GC日志可以通过 +PrintGCDetails开启 以ParallelGC为例 YoungGC日志解释如下 FullGC:
- dns之缓存。
1.浏览器缓存.这里以chrome为例.在chrome上输入:chrome://net-internals/#dns 可以查看chrome浏览器的dns缓存信息. 这样. 2.windows系统缓存. ...
- WebService—CXF—实现接口发布和客户端调用
(一)接口发布的几种方式 定义接口: @WebService(targetNamespace="http://www.itfad.net/queryUser") public in ...
- HttpClient4.x 使用cookie保持会话
HttpClient4.x可以自带维持会话功能,只要使用同一个HttpClient且未关闭连接,则可以使用相同会话来访问其他要求登录验证的服务(见TestLogin()方法中的“执行get请求”部分) ...
- SharePoint研究之表单登录配置
本文将演示SharePoint怎样配置表单(Form)登录,后续文章将研究 无密码登录.编程添加用户组.编程添加用户.编程添加文件夹.编程分享文件夹(权限分配)等. 知识点:SharePoint.Sq ...
- php 添加时间戳
<?php $tomorrow = mktime(,,,date(,date("Y")); echo "Tomorrow is ".date(" ...
- Yarn资源调度过程详细
在MapReduce1.0中,我们都知道也存在和HDFS一样的单点故障问题,主要是JobTracker既负责资源管理,又负责任务分配. Yarn中可以添加多种计算框架,Hadoop,Spark,Map ...
- <<网络是怎样连接的>>笔记第2章用电信号传输Tcp/ip数据
创建套接字: 协议栈的内部结构.套接字的实体,以及创建套接字的操作过程 连接服务器: “连接”具体是进行怎样的操作,在这个过程中协议栈到底是如何工作的, 以及客户端和服务器是如何进行交互的. 收发数据 ...
- git 重写历史
重写最后一次提交的commit git commit --amend 修改多个历史 git rebase -i HEAD~3 命令执行后结果如下: pick f7f3f6d changed my na ...
- STL空间配置器那点事
STL简介 STL(Standard Template Library,标准模板库),从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其 ...