其实我没看懂题不如说根本没看……都说是excrt板子那就写个板子吧

注意开long long

#include<iostream>
#include<cstdio>
using namespace std;
const long long N=100005;
long long n,r[N],m[N];
void exgcd(long long a,long long b,long long &d,long long &x,long long &y)
{
if(b==0)
{
d=a,x=1,y=0;
return;
}
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
long long excrt()
{
long long M=m[1],R=r[1],x,y,d;
for(long long i=2;i<=n;i++)
{
exgcd(M,m[i],d,x,y);
if((r[i]-R)%d)
return -1;
x=(r[i]-R)/d*x%(m[i]/d);
R=R+x*M;
M=M/d*m[i];
R%=M;
}
return R>0?R:R+M;
}
int main()
{
while(~scanf("%lld",&n))
{
for(long long i=1;i<=n;i++)
scanf("%lld%lld",&m[i],&r[i]);
printf("%lld\n",excrt());
}
return 0;
}

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

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

  2. POJ2891 Strange Way to Express Integers【扩展中国剩余定理】

    题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...

  3. POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】

    求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...

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

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

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

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

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

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

  7. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

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

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

  9. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

随机推荐

  1. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  2. [luoguP1040] 加分二叉树(DP)

    传送门 区间DP水题 代码 #include <cstdio> #include <iostream> #define N 41 #define max(x, y) ((x) ...

  3. noip模拟赛 收集果子

    分析:显然的,树形dp,状态也很好想到:f[i][j]表示以i为根的子树收集到j个果子的方案数.转移的话就相当于是背包问题,每个子节点可以选或不选.如果不选子节点k的话,那么以k为根的子树的边无论断不 ...

  4. 【KMP+最小循环节】F. Cyclic Nacklace

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/F [题意] 给定一个字符串,问在字符串后最少添加多少个字母,得到的新字符串能是前 ...

  5. SPOJ SUMPRO(数学)

    题意: 给出一个数N,问所有满足n/x=y(此处为整除)的所有x*y的总和是多少.对答案mod(1e9+7). 1 <= T <= 500. 1 <= N <= 1e9. 分析 ...

  6. codeforces 762E(cdq分治)

    题意: n个电台,每个电台有三个属性xi, ri, fi.分别代表电台的坐标,电台的播报范围,以及播报的频率. 对于一对电台i, j,若min(ri, rj) >= |xi - xj|,那么他们 ...

  7. dtrace-conf 2016

    https://www.joyent.com/about/events/2016/dtrace-conf

  8. DTRACE简介(1)

    https://blogs.oracle.com/swan/entry/dtrace%E7%AE%80%E4%BB%8B By samwan on 三月 20, 2007 记得几年前看过一部美国大片叫 ...

  9. springboot application.properties

    verify if you have this items: @Bean public CommonsMultipartResolver multipartResolver() { CommonsMu ...

  10. linux man 1,2,3 命令

    原文: http://blog.sina.com.cn/s/blog_969c52730101c0p7.html ------------------------------------------- ...