Monkey Tradition(中国剩余定理)
Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
Description
In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows:
1) There are N monkeys who play this game and there are N bamboos of equal heights. Let the height be L meters.
2) Each monkey stands in front of a bamboo and every monkey is assigned a different bamboo.
3) When the whistle is blown, the monkeys start climbing the bamboos and they are not allowed to jump to a different bamboo throughout the game.
4) Since they are monkeys, they usually climb by jumping. And in each jump, the ith monkey can jump exactly pi meters (pi is a prime). After a while when a monkey finds that he cannot jump because one more jump may get him out of the bamboo, he reports the remaining length ri that he is not able to cover.
5) And before the game, each monkey is assigned a distinct pi.
6) The monkey, who has the lowest ri, wins.
Now, the organizers have found all the information of the game last year, but unluckily they haven't found the height of the bamboo. To be more exact, they know N, all pi and corresponding ri, but notL. So, you came forward and found the task challenging and so, you want to find L, from the given information.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 12). Each of the next n lines contains two integers pi (1 < pi < 40, pi is a prime) and ri (0 < ri < pi). All pi will be distinct.
Output
For each case, print the case number and the minimum possible value of L that satisfies the above conditions. If there is no solution, print 'Impossible'.
Sample Input
2
3
5 4
7 6
11 3
4
2 1
3 2
5 3
7 1
Sample Output
Case 1: 69
Case 2: 113
题解:
用扩展GCD求;剩下的就是模版;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
LL p[], r[];
void ex_gcd(LL a, LL b, LL &x, LL &y){
if(!b){
x = ;
y = ;
return;
}
ex_gcd(b, a%b, x, y);
LL temp = x;
x = y;
y = temp - a/b * y;
}
int main(){
int T, n, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
LL MOD = ;
for(int i = ; i < n; i++){
scanf("%lld%lld", &p[i], &r[i]);
MOD *= p[i];
}
LL x, y;
LL ans = ;
for(int i = ; i < n; i++){
ex_gcd(MOD/p[i], p[i], x, y);
ans = (ans + MOD/p[i]*x*r[i] + MOD) % MOD;
}
printf("Case %d: %lld\n",++kase, (ans + MOD) % MOD);
}
return ;
}
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(中国剩余定理)
题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...
- 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 ...
- poj1006生理周期(中国剩余定理)
/* 中国剩余定理可以描述为: 若某数x分别被d1..….dn除得的余数为r1.r2.….rn,则可表示为下式: x=R1r1+R2r2+…+Rnrn+RD 其中R1是d2.d3.….dn的公倍数,而 ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
随机推荐
- Windows7 32位机上,OpenCV中配置GPU操作步骤
1. 查看本机配置,查看显卡类型是否支持NVIDIA GPU: 2. 从http://www.nvidia.cn/Download/index.aspx?lang=cn下载最新驱动并安装: 3. ...
- Unity发送短信
闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码! 1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧! 2,界面极其 ...
- CSS 定位元素之 relative
1. relative 和 absolute relative 会限制 absolute. absolute 会根据 父级的的定位元素来定位. 2. overflow 和 absolue 当overf ...
- 程序员的家!我终于拥有自己的blog了!!!
经过多次提交诚恳的家园申请,终于得到了审核通过!今天就开始了我的.net成长之路!!!
- (转)ie -ms-interpolation-mode: bicubic 属性详解
ie -ms-interpolation-mode: bicubic 属性详解 img { -ms-interpolation-mode: bicubic; } 这个在做实时缩放图片.缩略图的时候用 ...
- silverlight visifire控件图表制作——silverlight 静态页面xaml
一.silverlight 静态页面 1. 时间控件:DatePicker ,添加引用: xmlns:sdk="clr-namespace:System.Windows.Controls;a ...
- js 计算两个日期之间的周数
//返回两个日期相差的周数 function WeeksBetw(date1, date2) { //这里的date1,date2都是Date对象 var d1 = new Date(date1); ...
- 费马小定理&欧拉定理
在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...
- Mac系统cocos2dx + android 开发环境配置
Mac系统cocos2dx + android 开发环境配置 /****************************************************** 这遍文章主要转载自:htt ...
- MSSQL-资源地址
1.SQL-SERVER客户端管理工具.下载地址 https://msdn.microsoft.com/en-us/library/mt238290.aspx?f=255&MSPPError= ...