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. javaWeb - 3 — JSP (技术已淘汰)— 更新完毕

    1.jsp 在servlet中说过java中前端到后台有两条路线嘛 后台 <------ ajax.json <--------- 前端 后台 <------ jsp( EL.JST ...

  2. 15. Linux提取RPM包文件(cpio命令)详解

    在讲解如何从 RPM 包中提取文件之前,先来系统学习一下 cpio 命令.cpio 命令用于从归档包中存入和读取文件,换句话说,cpio 命令可以从归档包中提取文件(或目录),也可以将文件(或目录)复 ...

  3. Output of C++ Program | Set 12

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  4. mybatis-扩展

    分页插件 使用pageHelper参考官方https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse. ...

  5. ubuntu基础

    下载地址: http://cdimage.ubuntu.com/releases/ #:配置多网卡静态IP地址和路由 root@ubuntu:~# vim /etc/netplan/01-netcfg ...

  6. SpringSecurity Oauth2.0

    1.用户认证分析 上面流程图描述了用户要操作的各个微服务,用户查看个人信息需要访问客户微服务,下单需要访问订单微服务,秒杀抢购商品需要访问秒杀微服务.每个服务都需要认证用户的身份,身份认证成功后,需要 ...

  7. ssm+mysql+jsp打造在线考试系统WeKnow-学生端

    一.登陆模块 前台提交账号和密码传到后台处理控制层 1.1 首先是控制器 @RequestMapping(value="/studentLogin", method=Request ...

  8. 『与善仁』Appium基础 — 23、操作滑动的方式

    目录 1.swipe滑动 2.scroll滑动 3.drag拖拽事件 4.滑动方法小结 5.拓展:多次滑动 6.综合练习 在Appium中提供了三种滑动的方式,swipe滑动.scroll滑动.dra ...

  9. Apache Hudi 与 Hive 集成手册

    1. Hudi表对应的Hive外部表介绍 Hudi源表对应一份HDFS数据,可以通过Spark,Flink 组件或者Hudi客户端将Hudi表的数据映射为Hive外部表,基于该外部表, Hive可以方 ...

  10. java基础---局部变量和全局变量

    1.成员变量的概念: 成员变量就是属于类的变量,在类中,方法体外定义的变量 1)成员变量又分为两种: 类变量(又称静态变量) 实例变量(又称非静态变量) 类变量(静态变量)   :是被static所修 ...