Light oj 1319 - Monkey Tradition【CRT】

通过给定的猴子爬竹游戏中每只猴子的跳跃能力和未覆盖长度,利用中国剩余定理求解最小竹子高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



题意:

给出 n 对 p 和 r ,使得n %p= r ,求满足题意的最小值


题解:

中国剩余定理,详细解释请点这里

个人是不会的,虽然知道是这个知识点,但不会写,比赛确实是卡住了.....

后来见到的大神的详细代码,想膜拜大神来点这里


说实话,这道题不会做!

数学确实伤不起,感觉需要学的东西太多了,数学没学好,真伤心啊

第一次做这样的题,算是整理出一个模板吧,希望以后能真正理解这个解法的本质!

/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
typedef long long ll;
void extgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
	if(!b)
	{
		d=a;x=1;y=0;
		return;
	}
	extgcd(b,a%b,d,y,x);
	y-=x*(a/b);
}
ll crt(ll n,ll p[],ll r[]) 
{
	ll s=1,ans=0;
	for(int i=0;i<n;++i)
	{
		s=s*p[i];
	}
	for(int i=0;i<n;++i)
	{
		ll m=s/p[i],d,x,y;
		extgcd(p[i],m,d,x,y);
		ans=(ans+y*m*r[i])%s;
	}
	return (ans%s+s)%s;
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int k=0;k<t;++k)
	{
		ll n,p[15],r[15];
		scanf("%lld",&n);
		for(int i=0;i<n;++i)
		{
			scanf("%lld%lld",&p[i],&r[i]);
		}
		printf("Case %d: %lld\n",k+1,crt(n,p,r));
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值