题目链接

扩展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 (扩展中国剩余定理)的更多相关文章

  1. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  2. [poj2891]Strange Way to Express Integers(扩展中国剩余定理)

    题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...

  3. POJ.2891.Strange Way to Express Integers(扩展CRT)

    题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...

  4. poj 2891 Strange Way to Express Integers (扩展gcd)

    题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...

  5. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  6. poj 2891 Strange Way to Express Integers【扩展中国剩余定理】

    扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...

  7. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  8. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  9. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

随机推荐

  1. SourceTree的基本使用---基本介绍/本地开发

    转载自https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 wind ...

  2. hadoop linux 杂记

    切换到root        su    修改sudo sudo + 命令 --> root权限 + 命令        su root        vim /etc/sudoers      ...

  3. loadrunner之脚本篇——代理录制

    版本:Loadruner 11.0 A.PC端录制Web应用程序 步骤1:根据实际情况,选择对应的协议 本例中选择Web(HTTP/HTML),如下 步骤2:找到代理设置界面 点击 Start Rec ...

  4. 【Flask】sqlalchemy 排序

    ### 排序:1. order_by:可以指定根据这个表中的某个字段进行排序,如果在前面加了一个-,代表的是降序排序.2. 在模型定义的时候指定默认排序:有些时候,不想每次在查询的时候都指定排序的方式 ...

  5. linux下ipython的安装

    第一种:ipython源码安装ipython的源码下载页面为:https://pypi.python.org/pypi/ipython 或者是到git页面下载:https://github.com/i ...

  6. js中对象的类型

    js中的类型分为三种,"内部对象"."宿主对象"."自定义对象" 1."内部对象"有Date.Function.Arra ...

  7. UVA 11029 || Lightoj 1282 Leading and Trailing 数学

    Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...

  8. AVFoundation之录音及播放

    录音 在开始录音前,要把会话方式设置成AVAudioSessionCategoryPlayAndRecord //设置为播放和录音状态,以便可以在录制完之后播放录音 AVAudioSession *s ...

  9. localstorage存储对象

    之前在客户端存储数据一直用的是cookie,由于有大小等限制,随着html5时代的到来,现在大多数用的是localstorage存储数据: 例如: localStorage.setItem(" ...

  10. underscore中template的使用Demo

    在客户端渲染数据时,一般可通过underscore中的template对数据模板进行渲染,例如: 定义模板,需要把type类型设置为“text/template” <script type=&q ...