POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)
扩展CRT模板题,原理及证明见传送门(引用)
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N=1e5+;
ll n,m[N],c[N];
void exgcd(ll a,ll b,ll& x,ll& y,ll& g) {
if(!b)x=,y=,g=a;
else exgcd(b,a%b,y,x,g),y-=x*(a/b);
}
bool CRT(ll& c1,ll& m1,ll c2,ll m2) {
ll y1,y2,g;
exgcd(m1,m2,y1,y2,g);
if((c1-c2)%g)return ;
y1*=(c1-c2)/g,c1-=y1%(m2/g)*m1,m1*=m2/g,c1=(c1+m1)%m1;
return ;
}
ll solve() {
ll C=,M=;
for(ll i=; i<n; ++i)if(!CRT(C,M,c[i],m[i]))return -;
return C;
} int main() {
while(scanf("%lld",&n)==) {
for(ll i=; i<n; ++i)scanf("%lld%lld",&m[i],&c[i]);
printf("%lld\n",solve());
}
return ;
}
POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)的更多相关文章
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- [poj2891]Strange Way to Express Integers(扩展中国剩余定理)
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...
- POJ.2891.Strange Way to Express Integers(扩展CRT)
题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...
- poj 2891 Strange Way to Express Integers (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
随机推荐
- 曾经跳过的坑----jQuery mouseover与mouseenter,mouseout与mouseleave的区别
mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...
- requirejs源码分析: requirejs 方法–1. 主入口
该方法是 主要的入口点 也是最常用的方法. req = requirejs = function (deps, callback, errback, optional) { //Find the ri ...
- 20160419 while练习,复习
10 一.while和if题目练习 . 二.知识拓展 1. C#中的委托是什么?事件是不是一种委托? 答 : 委托可以把一个方法作为参数代入另一个方法.委托可以理解为指向一个函数的引用. ...
- 键盘没有Home键和End键的完美解决办法
最近新入手一个笔记本,发现键盘没有Home/End,这两个键虽然不是必用,但也是用顺手了,特别是选择一行,到行首,行尾的时候甚是方便 作为一枚程序员,怎么能够妥协? 于是开始研究 方案一 通过观察笔记 ...
- delphi 中 unicode 转汉字 函数
近期用到这个函数,无奈没有找到 delphi 自带的,网上找了下 有类似的,没有现成的,我需要的是 支持 “\u4f00 ” 这种格式的,即前面带标准的 “\u” 于是改造了一下. 下面是 解码 函 ...
- 在IOS开发中,项目的目录结构如何搭建?
网上有很多关于IOS开发的学习资料.然而却很少有关于设计一个项目时,如何设计其目录结构?这对于自学IOS的程序猿们,无疑有诸多不利.接下来,我就简单的谈下真正在公司中,项目的目录结构如何搭建: 以上为 ...
- 1.python基本数据类型
1.数值类型(4种) 数值类型都是不可变的 1)整型(int) example: a = 0 b = -1 诸如此类,都为整型 2)浮点型(float) example: f = 1.1 f = -1 ...
- Qt如何重写虚函数
eg:QWidget的有个虚函数,KeyPressEvent,当它的子类获得焦点的时候,如果有任何按键按下,就会触发这个虚函数. 1.在mainwindow.h中声明此虚函数 protected:vo ...
- maven 简介 —— maven权威指南学习笔记(一)
maven是什么?有什么用? Maven是一个项目管理工具,它包含了 一个项目对象模型 (Project Object Model), 一组标准集合, 一个项目生命周期(ProjectLifecycl ...
- AppDomain.CurrentDomain.BaseDirectory
在winform中的OnPaint事件中,AppDomain.CurrentDomain.BaseDirectory得到的是下面这个路径 C:\Program Files (x86)\Microsof ...