1319 - Monkey Tradition
Time Limit: 2 second(s) Memory Limit: 32 MB

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 not L. 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

Output for Sample Input

2

3

5 4

7 6

11 3

4

2 1

3 2

5 3

7 1

Case 1: 69

Case 2: 113


Problem Setter: Tanvir Hassan
Special Thanks: Jane Alam Jan
思路:中国剩余定理;扩展欧基里德。模板题。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<vector>
7 using namespace std;
8 typedef long long LL;
9 typedef unsigned long long ll;
10 pair<LL ,LL>P(LL n,LL m);
11 typedef struct pp
12 {
13 long long x;
14 long long y;
15 } ss;
16 ss aa[100];
17 int main(void)
18 {
19 LL i,j,k,p,q;int ca;
20 int ba;int s;
21 ll sum;
22 scanf("%d",&ca);
23 for(ba=1; ba<=ca; ba++)
24 {
25 sum=1;
26 scanf("%d",&s);
27 for(i=0; i<s; i++)
28 {
29 scanf("%lld %lld",&aa[i].x,&aa[i].y);
30 sum*=aa[i].x;
31 }
32 LL ans=0;
33 for(i=0; i<s; i++)
34 {
35 LL mn=sum/aa[i].x;
36 LL x;
37 LL y;
38 pair<LL,LL>xx;
39 xx=P(mn,aa[i].x);
40 x=xx.first;y=xx.second;
41 LL z=(aa[i].x+x%aa[i].x)%aa[i].x;
42 ans=((ans+((aa[i].y*mn)%sum)*z)%sum)%sum;
43 }
44 ans=(ans+sum)%sum;
45 printf("Case %d:",ba);
46 printf(" %lld\n",ans);
47 }return 0;
48 }
49
50 pair<LL ,LL>P(LL n,LL m)
51 {
52 if(m==0)
53 {
54 pair<LL,LL> c=make_pair(1,0);
55 return c;
56 }
57 else
58 {
59 pair<LL,LL>x=P(m,n%m);
60 LL k=x.second;
61 LL kk=x.first;
62 x.first=k;
63 x.second=kk-(n/m)*k;
64 return x;
65 }
66 }

1319 - Monkey Tradition的更多相关文章

  1. LightOJ 1319 Monkey Tradition(中国剩余定理)

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

  2. LightOJ 1319 - Monkey Tradition CRT除数互质版

    本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...

  3. Monkey Tradition(中国剩余定理)

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

  4. (light oj 1319) Monkey Tradition 中国剩余定理(CRT)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...

  5. 【初学python】使用python调用monkey测试

    目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...

  6. Monkey Patch/Monkey Testing/Duck Typing/Duck Test

    Monkey Patch Monkey Testing Duck Typing Duck Test

  7. monkey命令选项参考

    基本参数:     --help              打印帮助消息 -v  可以在命令行中出现多次,每次一个-V选项都会增加monkey向命令行打印输出的详细级别.默认的级别0只会打印启动信息. ...

  8. monkey之monkey日志分析

    一.初步分析方法:Monkey测试出现错误后,一般的差错步骤为以下几步:1.找到是monkey里面的哪个地方出错2.查看Monkey里面出错前的一些事件动作,并手动执行该动作3.若以上步骤还不能找出, ...

  9. monkey之monkey命令详解

    四大类-- 常用选项.事件选项.约束选项.调试选项 1.常用选项 --help:打印帮助信息 -v:指定打印信息的详细级别,一个-v增加一个级别 ,默认级别为 0 .用于指定反馈信息级别(信息级别就是 ...

随机推荐

  1. 01 eclipse搭建maven的web工程(3.1)

    eclipse搭建maven的web工程(3.1) 一.下载并在eclipse安装JDK环境[查看] 二.下载并在eclipse安装maven环境[查看] 三.新建maven-webapp工程: 1. ...

  2. R2CNN模型——用于文本目标检测的模型

    引言 R2CNN全称Rotational Region CNN,是一个针对斜框文本检测的CNN模型,原型是Faster R-CNN,paper中的模型主要针对文本检测,调整后也可用于航拍图像的检测中去 ...

  3. 在windows 10家庭版上安装docker的步骤

    本人之前写Redis书和Spring Cloud Alibaba书时,发现一些分布式组件更适合安装在linux环境,而在搭建Redis等集群时,更需要linux环境. 本人日常练习代码和写书所用的机器 ...

  4. day30线程(Threads)

    day30线程(Threads) 1.开启线程 一.什么是线程: 1.进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程. 2.主进程中的线程称为主线程,其他开启的线程称 ...

  5. @RequestBody配合@Valid 校验入参参数

    自定义一个Controller import com.example.demo.pojo.User; import org.springframework.web.bind.annotation.Po ...

  6. 一、手把手教你docker搭建fastDFS文件上传下载服务器

    在搭建fastDFS文件上传下载服务器之前,你需要准备的有一个可连接的linux服务器,并且该linux服务器上已经安装了docker,若还有没安装docker的,先百度自行安装docker. 1.执 ...

  7. 【Java多线程】ExecutorService和ThreadPoolExecutor

    ExecutorService Java.util.concurrent.ExecutorService接口代表一种异步执行机制,它能够在后台执行任务.因此ExecutorService与thread ...

  8. Android CameraX 打开摄像头预览

    目标很简单,用CameraX打开摄像头预览,实时显示在界面上.看看CameraX有没有Google说的那么好用.先按最简单的来,把预览显示出来. 引入依赖 模块gradle的一些配置,使用的Andro ...

  9. CURD系统怎么做出技术含量惊艳面试官

    在<CURD系统怎么做出技术含量--怎样引导面试>有朋友开玩笑说都用上了领域驱动了,就不叫CURD系统了吧.这里我解释一下,怕大家对DDD领域驱动设计有什么误解. DDD是为解决软件复杂性 ...

  10. Python模块和函数

    目录 一.基础 二.特殊函数 一.基础 #导入模块 import xxx #调用 xxx.dd() from xxx import xx  as dd #导入某个函数,as给函数加别名,调用xx() ...