LightOJ - 1319 Monkey Tradition (中国剩余定理 模板)

LightOJ - 1319
Time Limit: 2000MSMemory Limit: 32768KB64bit 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 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

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

首先讲一下中国剩余定理:(大神总结的,很详细)

( 假设n % 3 =2, n % 5 =3, n % 7 =2; )

那么:要使得n % 3 = 2,那么( 5 * 7 * 2 )*2  % 3 = 2;( 因为5 * 7 * 2 % 3 = 1 )

同理: 要使得n % 5 = 3,那么( 3 * 7 * 1 )*3  % 5 = 3;( 因为3 * 7 * 1 % 5 = 1 )

同理:要使得n % 7 = 2,那么( 3 * 5 * 1 )* 2  % 7 = 2;( 因为3 * 5 * 1 % 7 = 1 )

那么现在将( 5 * 7 * 2 )* 2和( 3 * 7 * 1 )* 3和( 3 * 5 * 1 )* 2相加会怎么样呢?我们知道

( 5 * 7 * 2 )* 2可以被5和7整除,但是%3等于2

( 3 * 7 * 1 )* 3可以被3和7整除,但是%5等于3

( 3 * 5 * 1 )* 2可以被3和5整除,但是%7等于2

那么即使相加后,%3, 5, 7的情况也还是一样的!

那么就得到一个我们暂时需要的数( 5 * 7 * 2 )* 2 +( 3 * 7 * 1 )* 3 +( 3 * 5 * 1 )* 2 = 233

但不是最小的!所有我们还要 233 % ( 3 * 5 * 7 ) == 23  得解!

 

//题意:先输入一个n,接下来的n行,每行输入一个p[i],b[i]。

有n个猴子爬同一个杆子,猴子每次先上跳p[i]米,到最后剩余b[i]米。问这个杆子的长度是多少。

思路;

//一个裸的中国剩余定理题,不会的先搜索一下看一下什么是中国剩余定理,知道思想后,这个题就简单了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<algorithm>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 1010
using namespace std;
ll p[15];
ll r[15];
int n;
void extgcd(ll a,ll b,ll &x,ll &y)//扩展GCD 
{
	if(!b)
	{
		x=1;
		y=0;
	}
	else
	{
		extgcd(b,a%b,y,x);
		y-=x*(a/b);
	}
}
ll chinese_remainder()//中国剩余定理 
{
	ll M=1;
	for(int i=0;i<n;i++)
		M*=p[i];
	ll ans=0;
	ll m;
	ll x,y;
	for(int i=0;i<n;i++)
	{
		m=M/p[i];
		extgcd(m,p[i],x,y);
		ans=(ans+r[i]*m*x)%M;
	}
	return (ans+M)%M;
}
int main()
{
	int t,T=1;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%lld%lld",&p[i],&r[i]);
		printf("Case %d: %lld\n",T++,chinese_remainder());
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值