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 ...
随机推荐
- STM32外部中断具体解释
一.基本概念 ARM Coetex-M3内核共支持256个中断,当中16个内部中断,240个外部中断和可编程的256级中断优先级的设置.STM32眼下支持的中断共84个(16个内部+68个外部), ...
- 阿里云 RDS 怎么保证高可用性
RDS在系统构建之初,从网络到硬件再到系统都是基于高可用设计,他可以做到故障30秒无缝转移,对应用完全透明. 当一个数据库实例发生故障时, 另一个会立即顶上,而且对应用是完全透明的.你无需变更应用的连 ...
- (转).net程序员转战android第二篇---牛刀小试
上篇说道如何搭建android的开发环境,这一篇我们将牛刀小试一下, 完成我们第一个android APP应用. 我就从新建项目说起吧. 首先打开Eclipse,选择顶部的File(文件)——new( ...
- ASP.NET MVC请求处理过程
- 为自己的Android应用添加广告
平时也写了不少Android小应用,但是都是做练习之用,从来没有想过添加广告赚钱这一说. 个人是非常反感在应用内添加广告这种行为的,非常影响用户体验,不小心点到广告的话,手机流量哗哗地就没了··· 但 ...
- C#调用C++的DLL 数据类型转换
/C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsig ...
- 一致性哈希与java实现
一致性哈希算法是分布式系统中常用的算法.比如,一个分布式的存储系统,要将数据存储到具体的节点上,如果采用普通的hash方法,将数据映射到具体的节点上,如key%N,key是数据的key,N是机器节点数 ...
- unity绘制线和绘制面
绘制线条代码,其实就是指定至少两个点,然后赋予贴图即可,不废话,上代码: using UnityEngine; using System.Collections; public class LineT ...
- C/C++中的虚析构函数和私有析构函数的使用
代码: #include <iostream> using namespace std; class A{ public: A(){ cout<<"construct ...
- C++中的函数指针用法
代码: #include <iostream> #include <cstdio> typedef void (*FUN)(); using namespace std; vo ...