题目链接:https://vjudge.net/contest/28079#problem/U

题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n)。

解题思路:就是解同余方程组:比如第一组样例:

3

5 4

7 6

11 3

就是要我们求一个x满足下列同余方程组:

x≡4(mod5)

x≡6(mod7)

x≡11(mod3)

然后自己看这里吧写了解释http://www.cnblogs.com/fu3638/p/7453419.html,是CRT裸题

代码:

 #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=;
LL m[N],a[N],n;
//扩展欧几里得
void exgcd(LL a,LL b,LL &x,LL &y){
if(!b){
x=;
y=;
}
else{
exgcd(b,a%b,y,x);
y-=(a/b)*x;
}
}
//中国剩余余定理
LL CRT(){
LL M=;
for(int i=;i<=n;i++)
M*=m[i];
LL ans=;
for(int i=;i<=n;i++){
LL x,y,Mi;
Mi=M/m[i];
exgcd(Mi,m[i],x,y);
ans=(ans+a[i]*Mi*x)%M;
}
if(ans<) ans+=M;
return ans;
} int main(){
int T;
scanf("%d",&T);
int cas=;
while(T--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld%lld",&m[i],&a[i]);
}
printf("Case %d: %lld\n",++cas,CRT());
}
}

LightOJ 1319 Monkey Tradition(中国剩余定理)的更多相关文章

  1. (light oj 1319) Monkey Tradition 中国剩余定理(CRT)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...

  2. LightOJ 1319 - Monkey Tradition CRT除数互质版

    本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...

  3. 1319 - Monkey Tradition

    1319 - Monkey Tradition   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  4. Monkey Tradition(中国剩余定理)

    Monkey Tradition Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submi ...

  5. Monkey Tradition---LightOj1319(中国剩余定理模板)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...

  6. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  7. 中国剩余定理(Chinese Remainder Theorem)

    我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times  \cdots  \tim ...

  8. 51nod1079(中国剩余定理)

    题目链接: http://www.51nod.com/onlineJudge/user.html#!userId=21687 题意: 中文题诶~ 思路: 本题就是个中国剩余定理模板题,不过模拟也可以过 ...

  9. HDU 5446 中国剩余定理+lucas

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. The meterprter basic commonds

    Using Meterpeter commands Since the Meterpreter provides awhole new environment, we will cover some ...

  2. 解题:BZOJ 4808 马

    题面 以前写过的题,翻出来学习网络流写二分图匹配,因为复杂度更优秀,$Dinic$是$O(sqrt(n)m)$哒~ 原点向左部点连流量为$1$的边,左部点向对应右部点连流量为$1$的边,右部点向汇点连 ...

  3. Rearrangement inequality

    摘抄自:  https://en.wikipedia.org/wiki/Rearrangement_inequality#Proof In mathematics, the rearrangement ...

  4. php整理

    linux上安装php配套环境有些繁琐 推荐使用一键安装: https://lnmp.org/install.html phalcon框架环境: centos7下php7.0.x安装phalcon框架

  5. Gogent相关问题的解决(不断更新)

    1:今天早上打开推特,发现进不去了,google浏览器一直提示404……找不到网址,真心郁闷.后来,查了查,才知道,最近google在北京的主干服务器被xx了,某些省就上不了了…… ……乱七八糟的不说 ...

  6. spring 添加controller返回值绑定

    @EnableWebMvc @Configuration public class Config { @Autowired private RequestMappingHandlerAdapter h ...

  7. 【CodeForces】713 C. Sonya and Problem Wihtout a Legend

    [题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...

  8. 【洛谷 P3705】 [SDOI2017]新生舞会(费用流,01分数规划)

    题目链接 看到这题我想到了以前做过的一题,名字记不清了,反正里面有"矩阵"二字,然后是道二分图匹配的题. 经典的行列连边网络流. 第\(i\)行和第\(j\)列连边,费用为\(b[ ...

  9. css各种姿势的水平居中

    首先是最常用的,利用margin属性的auto来进行水平居中 margin: 0 auto; 其中0是指上下的margin,auto是左右的margin,margin这个属性的简写是按顺时针走的,也就 ...

  10. 【leetcode 简单】第二十三题 二叉树的最大深度

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...