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. tensorboard 拒绝连接无法打开相应页面

    启动tensorboard时没有报错,但打开页面却拒绝连接. 解决方法:tensorboard --logdir=TEC4FN --host=127.0.0.1 在命令最后添加 --host=127. ...

  2. day03 MySQL数据库之主键与外键

    day03 MySQL数据库之主键与外键 昨日内容回顾 针对库的基本SQL语句 # 增 create database meng; # 查 show databases; shwo create da ...

  3. css通配样式初始化(多款,供君自选)

    腾讯官网 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0; ...

  4. 超过三张表禁止join

    一. 问题提出 <阿里巴巴JAVA开发手册>里面写超过三张表禁止join,这是为什么? 二.问题分析 对这个结论,你是否有怀疑呢?也不知道是哪位先哲说的不要人云亦云,今天我设计sql,来验 ...

  5. proxysql+MHA+半同步复制

    先配置成主从同步 先在各节点安装服务 [root@inotify ~]# yum install mariadb-server -y 编辑主节点的配置文件,并启动 [root@centos7 ~]# ...

  6. C#获取Windows10屏幕的缩放比例

    现在1920x1080以上分辨率的高分屏电脑渐渐普及了.我们会在Windows的显示设置里看到缩放比例的设置.在Windows桌面客户端的开发中,有时会想要精确计算窗口的面积或位置.然而在默认情况下, ...

  7. C++内存管理:简易内存池的实现

    什么是内存池? 在上一篇 C++内存管理:new / delete 和 cookie中谈到,频繁的调用 malloc 会影响运行效率以及产生额外的 cookie, 而内存池的思想是预先申请一大块内存, ...

  8. Java如何生成随机数 - Random、ThreadLocalRandom、SecureRandom

    Java7 的Random伪随机数和线程安全的ThreadLocalRandom 一.Random伪随机数: Random 类专门用于生成一个伪随机数,它有两个构造器: 一个构造器使用默认的种子(以当 ...

  9. 【dva】如何监听异步请求是否完成(页面loading)

    方案1.你可以在model里面操作 在model里面的state里面声明一个变量state,默认是false,effect函数执行开始就将其改为true,然后等call()然后结束后又将其改为fals ...

  10. 微前端框架 qiankun 技术分析

    我们在single-spa 技术分析 基本实现了一个微前端框架需要具备的各种功能,但是又实现的不够彻底,遗留了很多问题需要解决.虽然官方提供了很多样例和最佳实践,但是总显得过于单薄,总给人一种&quo ...