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

Submit Status

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(中国剩余定理)的更多相关文章

  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(中国剩余定理)

    题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...

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

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

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

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

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

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

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

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

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

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

  8. poj1006生理周期(中国剩余定理)

    /* 中国剩余定理可以描述为: 若某数x分别被d1..….dn除得的余数为r1.r2.….rn,则可表示为下式: x=R1r1+R2r2+…+Rnrn+RD 其中R1是d2.d3.….dn的公倍数,而 ...

  9. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

随机推荐

  1. Oracle 中按条件过滤重复记录

    在数据处理中,经常会遇到类似这样的情况:数据库中存在多条记录,其中某些字段值相同,其他字段值不同.实际的业务需要针对这样的情况,只保留一条数据,其他数据删除.如何做到呢?在sql中有top关键字相对容 ...

  2. MySql索引原理与使用大全

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 一.索引介绍 索引是对数据库表中一列或多列的值进行排序的一种结构.在关系数据库中,索引是一种 ...

  3. CentOS 6.3中安装OpenCV2.3.1

    下面为自己测试可用的OpenCV在Linux下的安装步骤 .检查并安装相关程序,确保gtk安装成功,否则无法显示图片 yum install gcc-c++ yuminstall gtk-devel. ...

  4. 【InversionCount 逆序对数 + MergeSort】

    Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < ...

  5. windows下绑定线程(进程)到指定的CPU核心

    一个程序指定到单独一个CPU上运行会比不指定CPU运行时快.这中间主要有两个原因:1)CPU切换时损耗的性能.2)Intel的自动降频技术和windows的机制冲突:windows有一个功能是平衡负载 ...

  6. Android应用盈利广告平台的嵌入方法详解

    一.如何学习Android  android开发(这里不提platform和底层驱动)你需要对Java有个良好的基础,一般我们用Eclipse作为开发工具.对于过多的具体知识详细介绍我这里不展开,我只 ...

  7. WPF XAML之bing使用StringFormat(转)

    释义 BindingBase.StringFormat 属性 获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式.        命名空间: System.Windows ...

  8. BZOJ2739 最远点(分治 + 决策单调性)

    2739: 最远点 Time Limit: 20 Sec Memory Limit: 256 MB Description 给你一个N个点的凸多边形,求离每一个点最远的点. Input 本题有多组数据 ...

  9. 通过jqueryui实现邮件提示

    //js代码$(function () { var availableTags = ["@qq.com", "@gmail.com", "@126.c ...

  10. mvc上传,下载,浏览文件功能(用uploadify插件)

    类 public class UpLoadFileController : Controller { // // GET: /UpLoadFile/ public ActionResult Index ...