LightOJ 1319 Monkey Tradition(中国剩余定理)
题目链接: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(中国剩余定理)的更多相关文章
- (light oj 1319) Monkey Tradition 中国剩余定理(CRT)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...
- LightOJ 1319 - Monkey Tradition CRT除数互质版
本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...
- 1319 - Monkey Tradition
1319 - Monkey Tradition PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- Monkey Tradition(中国剩余定理)
Monkey Tradition Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submi ...
- Monkey Tradition---LightOj1319(中国剩余定理模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- 中国剩余定理(Chinese Remainder Theorem)
我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times \cdots \tim ...
- 51nod1079(中国剩余定理)
题目链接: http://www.51nod.com/onlineJudge/user.html#!userId=21687 题意: 中文题诶~ 思路: 本题就是个中国剩余定理模板题,不过模拟也可以过 ...
- HDU 5446 中国剩余定理+lucas
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
随机推荐
- excel换行
在excel的单元格中换行 1. windows alt + enter 2. mac command + alt + enter
- SAS数据步与过程步,数据步语句
SAS数据步与过程步,数据步语句http://www.biostatistic.net/thread-2045-1-1.html ---转载---原文作者:biostar(出处: 生物统计家园) 数 ...
- WinForm二三事(三)Control.Invoke&Control.BeginInvoke
http://www.cnblogs.com/yuyijq/archive/2010/01/11/1643802.html 这个系列从2009年写到2010年,差点又成太监文.随着WPF/Silver ...
- (转)IOS 的一些资源汇总
UI界面类项目: Panoramagl —— 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressH ...
- struts的status属性
struts2 <s:iterator> status属性 转载▼ iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用标签的时候有三个属性值得我 ...
- [转]标准C++中的string类的用法总结
原文地址:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常 ...
- kle 日志收集系统维护之清理索引及索引优化脚本
logstash每天往es建好索引,按天生成,就目前的需求,需要清理不需要的数据,以保证最新日志的速度展示,哈哈,瞎搞了这个脚本,路过的大神批评. #!/usr/bin/env python # co ...
- java多线程机制2(安全问题)
线程状态图: ================================================================================= /* * 线程安全问题 ...
- .net中的lock
lock锁 //定义一个私有成员变量,用于Lock的锁定标志 private static object lockobj = new object(); void DoSomething() { l ...
- MySQL性能优化之道
1.in和not in子查询优化 not in 是不能命中索引的,所以以下子查询性能很低. 如果是确定且有限的集合时,可以使用.如 IN (0,1,2). 用 exists或 notexists代替 ...