http://poj.org/problem?id=2891 (题目链接)

题意

  求解线性同余方程组,不保证模数一定两两互质。

Solotion

  一般模线性方程组的求解,详情请见:中国剩余定理

细节

  注意当最后发现方程无解直接退出时,会导致有数据没有读完,然后就会Re,所以先用数组将所有数据存下来。

代码

// poj2891
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
LL a[maxn],r[maxn],n; void exgcd(LL a,LL b,LL &d,LL &x,LL &y) {
if (b==0) {d=a;x=1;y=0;return;};
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
LL CRT() {
LL M=a[1],R=r[1];
LL d,x,y;
for (int i=2;i<=n;i++) {
LL mm=a[i],rr=r[i];
exgcd(M,mm,d,x,y); //M*x+R=mm*y+rr
if ((rr-R)%d!=0) return -1;
x=((rr-R)/d*x%(mm/d)+mm/d)%(mm/d); //求出最小正整数x
R+=M*x; //把整个M*x+R当做余数
M*=mm/d; //lcm(M,m)
}
return R;
}
int main() {
while (scanf("%lld",&n)!=EOF) {
for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i],&r[i]);
printf("%lld\n",CRT());
}
return 0;
}

  

  

【poj2891】 Strange Way to Express Integers的更多相关文章

  1. 【POJ2891】Strange Way to Express Integers(拓展CRT)

    [POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream ...

  2. 【poj2891】Strange Way to Express Integers

    题意: 给出n个模方程x=a(mod r) 求x的最小解 题解: 这就是个线性模方程组的模版题- - 但是有一些要注意的地方 extgcd算出来的解x可能负数  要让x=(x%mo+mo)%mo 而且 ...

  3. 【POJ】【2891】Strange Way to Express Integers

    中国剩余定理/扩展欧几里得 题目大意:求一般模线性方程组的解(不满足模数两两互质) solution:对于两个方程 \[ \begin{cases} m \equiv r_1 \pmod {a_1} ...

  4. 一本通1635【例 5】Strange Way to Express Integers

    1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...

  5. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  6. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  7. 「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 ...

  8. 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; ...

  9. 【poj 2891】Strange Way to Express Integers(数论--拓展欧几里德 求解同余方程组 模版题)

    题意:Elina看一本刘汝佳的书(O_O*),里面介绍了一种奇怪的方法表示一个非负整数 m .也就是有 k 对 ( ai , ri ) 可以这样表示--m%ai=ri.问 m 的最小值. 解法:拓展欧 ...

随机推荐

  1. 每日学习心得:UEditor样式被过滤无法显示问题

    前言: 上周开发中有用到开源的富文本编辑器UEditor,在使用的过程中遇到了样式被过滤无法显示问题,经过一番折腾终解决,此外,还有一些关于获取前台界面元素的一些总结. 1. UEditor样式被过滤 ...

  2. Renderer.materials

    修改方法 meshBody.renderer.materials[].mainTexture= clothes[]; meshBody.renderer.materials[]=maters[]; 以 ...

  3. Jenkins学习七:Jenkins的授权和访问控制

    默认的Jenkins不包含任何的安全检查,任何人可以修改Jenkins设置,job和启动build等.显然地在大规模的公司需要多个部门一起协调工作的时候,没有任何安全检查会带来很多的问题. 在系统管理 ...

  4. sublime text2 常用快捷键

    1. ctrl+方向键  按单词移动 2. ctrl+shift + 方向键  按单词选取 3. ctrl + F3 查找选定的或光标所在单词 4. F3 查找特定的单词(一般查找的流程是先ctrl+ ...

  5. NSURLSession学习笔记

    NSURLSession学习笔记(一)简介 一.URL Session的基本概念 1.三种工作模式: 默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁 ...

  6. android中scrollTo和scrollBy的理解

    protected   int  mScrollX;    //该视图内容相当于视图起始坐标的偏移量   , X轴 方向 protected   int  mScrollY;    //该视图内容相当 ...

  7. IBatis.Net学习笔记七--日志处理

    IBatis.Net中提供了方便的日志处理,可以输出sql语句等调试信息. 常用的有两种:1.输出到控制台:   <configSections>    <sectionGroup  ...

  8. 结合C++和GDAL实现shapefile(shp)文件的创建和写入

    工具:vs2012+GDAL 2.0 包含头文件: #include "ogrsf_frmts.h" int main() { const char *pszDriverName ...

  9. Java NIO框架Mina、Netty、Grizzly介绍与对比(zz)

    Mina:Mina(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用 ...

  10. [MetaHook] R_SparkStreaks

    By hzqst void R_SparkStreaks(vec_t *pos, int count, int velocityMin, int velocityMax) { int i; parti ...