题目链接: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. Spring Boot系列教程二:创建第一个web工程 hello world

    一.创建工程 创建名称为"springboot_helloworld"的spring boot工程, new->Spring Starter Project,直接上图     ...

  2. Git入门指南

    git学习资源: Pro Git(中文版) Learn Git in your browser for free with Try Git. Git常用命令 Reference 常用 Git 命令清单 ...

  3. Web之CGIC的介绍与使用20171229

    一.CGIC简介 1.CGI简介 CGI(Common Gateway Interface)是外部应用扩展应用程序与WWW服务器交互的一个标准接口.按照CGI标准编写的外部扩展应用程序可以处理客户端浏 ...

  4. 杨辉三角之c实现任意行输出

    #include<stdio.h> #include<stdlib.h> int** fmalloc(int n){ int** array; //二维指针 int i; ar ...

  5. 「Django」rest_framework学习系列-用户登录

    用户POST登录-->后台验证用户名密码-->验证正确返回TOKEN-->验证错误返回错误信息 class UserAPI(APIView): #用户登录类 def post(sel ...

  6. 题解 P1345 【[USACO5.4]奶牛的电信Telecowmunication】

    P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...

  7. Tensorflow Batch normalization函数

    Tensorflow Batch normalization函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 stackoverflow上tensorflow实现BN的不同函数的 ...

  8. Shiro实战教程(一)

    Shiro完整架构图 Shiro认证过程 Shiro授权的内部处理机制 Shiro 支持三种方式的授权 1.编程式:通过写if/else 授权代码块完成: Subject subject = Secu ...

  9. Dubbo+Zookeeper+SpringMVC+Maven整合实现微服务项目

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  10. 【BZOJ】1741: [Usaco2005 nov]Asteroids 穿越小行星群

    [题意]给定n*n网格,有k个物品,每次可以消灭一行或一列,求消灭掉所有物品的最少操作次数. [算法]二分图最小覆盖 [题解]此题是最小覆盖模型的出处. 将物品的x-y连边建立二分图. 最小覆盖:选择 ...