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 |
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的更多相关文章
- LightOJ 1319 Monkey Tradition(中国剩余定理)
题目链接:https://vjudge.net/contest/28079#problem/U 题目大意:给你n(n<12)行,每行有pi,ri,求一个数ans满足ans%pi=ri(i从1~n ...
- LightOJ 1319 - Monkey Tradition CRT除数互质版
本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...
- Monkey Tradition(中国剩余定理)
Monkey Tradition Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submi ...
- (light oj 1319) Monkey Tradition 中国剩余定理(CRT)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...
- 【初学python】使用python调用monkey测试
目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...
- Monkey Patch/Monkey Testing/Duck Typing/Duck Test
Monkey Patch Monkey Testing Duck Typing Duck Test
- monkey命令选项参考
基本参数: --help 打印帮助消息 -v 可以在命令行中出现多次,每次一个-V选项都会增加monkey向命令行打印输出的详细级别.默认的级别0只会打印启动信息. ...
- monkey之monkey日志分析
一.初步分析方法:Monkey测试出现错误后,一般的差错步骤为以下几步:1.找到是monkey里面的哪个地方出错2.查看Monkey里面出错前的一些事件动作,并手动执行该动作3.若以上步骤还不能找出, ...
- monkey之monkey命令详解
四大类-- 常用选项.事件选项.约束选项.调试选项 1.常用选项 --help:打印帮助信息 -v:指定打印信息的详细级别,一个-v增加一个级别 ,默认级别为 0 .用于指定反馈信息级别(信息级别就是 ...
随机推荐
- 漏洞分析:CVE-2017-17215
漏洞分析:CVE-2017-17215 华为HG532路由器的命令注入漏洞,存在于UPnP模块中. 漏洞分析 什么是UPnP? 搭建好环境(使用IoT-vulhub的docker环境),启动环境,查看 ...
- 双向循环链表模板类(C++)
双向链表又称为双链表,使用双向链表的目的是为了解决在链表中访问直接前驱和后继的问题.其设置前驱后继指针的目的,就是为了节省其时间开销,也就是用空间换时间. 在双向链表的每个节点中应有两个链接指针作为它 ...
- JmxTest
package mbeanTest; import java.util.Set; import javax.management.Attribute; import javax.management. ...
- spring 事务处理中,同一个类中:A方法(无事务)调B方法(有事务),事务不生效问题
public class MyEntry implements IBaseService{ public String A(String jsonStr) throws Exception{ User ...
- OSGI 生命周期
1 生命周期管理 对于非模块化应用,生命周期将应用作为一个整体来操作: 而对于模块化应用,则可以以细粒度的方式来管理应用的某一个独立部分. OSGi生命周期管理 OSGi生命周期层有两种不同的作用: ...
- CentOs 7 yum 安装Nginx
打开官网下载文档:http://nginx.org/en/download.html 2进入操作系统 centOs 7,建立文件夹 nginx ,进入nginx ,拷贝 上图1编辑命令:/etc/yu ...
- C#获取Windows10屏幕的缩放比例
现在1920x1080以上分辨率的高分屏电脑渐渐普及了.我们会在Windows的显示设置里看到缩放比例的设置.在Windows桌面客户端的开发中,有时会想要精确计算窗口的面积或位置.然而在默认情况下, ...
- opencv学习(四)——轨迹栏作为调色板
轨迹栏作为调色板 在这里,我们将创建一个简单的应用程序,以显示指定的颜色.有一个显示颜色的窗口,以及三个用于指定B.G.R颜色的轨迹栏.滑动轨迹栏,并相应地更改窗口颜色.默认情况下,初始颜色将设置为黑 ...
- Android: Client-Server communication
Refer to: http://osamashabrez.com/simple-client-server-communication-in-android/ I was working of an ...
- 去除爬虫采集到的\xa0、\u3000等字符
\xa0表示不间断空白符,爬虫中遇到它的概率不可谓不小,而经常和它一同出现的还有\u3000.\u2800.\t等Unicode字符串.单从对\xa0.\t.\u3000等含空白字符的处理来说,有以下 ...