【poj2891】Strange Way to Express Integers
题意:
给出n个模方程x=a(mod r) 求x的最小解
题解:
这就是个线性模方程组的模版题- - 但是有一些要注意的地方
extgcd算出来的解x可能负数 要让x=(x%mo+mo)%mo
而且mo不是等于lcm(r1,r2) 而是r2/gcd(r1,r2)
代码:
#include <cstdio>
typedef long long ll;
ll n,a,r;
ll extgcd(ll &x,ll &y,ll a,ll b){
if (!b){
x=,y=;
return a;
}else{
ll res=extgcd(x,y,b,a%b);
ll t=x;
x=y;
y=t-a/b*y;
return res;
}
}
int main(){
while (scanf("%I64d",&n)!=EOF){
scanf("%I64d%I64d",&r,&a);
ll x,y,a1,r1;
bool bo=;
for (ll i=;i<=n;i++){
scanf("%I64d%I64d",&r1,&a1);
ll gc=extgcd(x,y,r,r1);
if ((a1-a)%gc) bo=;
ll mo=r1/gc;
x=(x*(a1-a)/gc%mo+mo)%mo;
a+=r*x;
r*=r1/gc;
}
if (!bo) puts("-1");
else printf("%I64d\n",a);
}
}
【poj2891】Strange Way to Express Integers的更多相关文章
- 【POJ2891】Strange Way to Express Integers(拓展CRT)
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...
- 【poj2891】 Strange Way to Express Integers
http://poj.org/problem?id=2891 (题目链接) 题意 求解线性同余方程组,不保证模数一定两两互质. Solotion 一般模线性方程组的求解,详情请见:中国剩余定理 细节 ...
- 【POJ】【2891】Strange Way to Express Integers
中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} ...
- 一本通1635【例 5】Strange Way to Express Integers
1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...
- 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- 【POJ 2891】 Strange Way to Express Integers
[题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...
- 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】
题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...
- 1635:【例 5】Strange Way to Express Integers
#include<bits/stdc++.h> #define ll long long using namespace std; ll n,m,a,lcm,now; bool flag; ...
- 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)
题意:Elina看一本刘汝佳的书(O_O*),里面介绍了一种奇怪的方法表示一个非负整数 m .也就是有 k 对 ( ai , ri ) 可以这样表示--m%ai=ri.问 m 的最小值. 解法:拓展欧 ...
随机推荐
- 用户自定义结构数据与VARIANT转换 .
用户自定义结构数据与VARIANT转换 cheungmine 将用户自定义的C结构数据存储成VARIANT类型,需要时再将VARIANT类型转为用户自定义的结构数据,有十分现实的意义,既然我们不想为这 ...
- HDU1412
大水题.. 求集合的并 /* */ #include<algorithm> #include<iostream> #include<string.h> #inclu ...
- POJ3034+DP
题意:给定一个N*N的矩阵, 然后在这个矩阵的每个格子在任意时间会出现一个鼹鼠,这个出现 出现鼹鼠的时间是输出信息所确定的. 现在你手里有一把锤子能够去锤这些鼹鼠. 你能 够移动锤子, ...
- android 设置gridView item的高度
public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater ...
- [杂题]FZU2190 非提的救赎
中文题,题意不多说. 本来感觉很像dp 其实只要从上到下维护单调性就好了 坑是......这个oj......用cin很容易TLE...... //#include <bits/stdc++.h ...
- javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)
function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFun ...
- 【mysql的设计与优化专题(5)】慢查询详解
查询mysql的操作信息 show status -- 显示全部mysql操作信息 show status like "com_insert%"; -- 获得mysql的插入次数; ...
- 面试大总结之二:Java搞定面试中的二叉树题目
package BinaryTreeSummary; import java.util.ArrayList; import java.util.Iterator; import java.util.L ...
- redmine一键安装包下载链接
windows版本一键安装包:<bitnami-redmine-3.1.1-1-windows-installer.exe> 下载地址:http://pan.baidu.com/s/19D ...
- Cobalt Strike
http://www.77169.com/hack/201512/222080.shtm