Light OJ 1319 Monkey Tradition(中国剩余定理)

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 knowN, 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

题意:告诉你猴子爬杆子,每次跳素数长度的距离p[i],最后剩余r[i],问你杆子长度

分析:首先我们先学习下中国剩余定理:中国剩余定理

在模数mi两两互素的情况下,可以用中国剩余定理求n使其满足:

n=b1(mod m1)

n=b2(mod m2)

n=b3(mod m3)

....

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=1e6+100;
void exgcd(LL a,LL b,LL &xx,LL &yy)
{
    if(b==0)
    {
        xx=1;yy=0;
        return;
    }
    exgcd(b,a%b,xx,yy);
    LL t=xx;xx=yy;yy=t-a/b*yy;
    return;

}
LL China(LL m[],LL b[],int k)//m:模数 b:余数
{
    LL n=1,xx,yy;
    LL ans=0;
    for(int i=0;i<k;i++)
        n*=m[i];
    for(int i=0;i<k;i++)
    {
        LL t=n/m[i];
        exgcd(t,m[i],xx,yy);
        ans=(ans+xx*t*b[i])%n;
    }
    return (ans%n+n)%n;
}
LL p[45],r[45];
int main()
{
    int t,n,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        REP(i,n)
            scanf("%lld%lld",&p[i],&r[i]);
        LL ans=China(p,r,n);
        printf("Case %d: %lld\n",cas++,ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 04:50:12

Light OJ 1319 Monkey Tradition(中国剩余定理)的相关文章

(light oj 1319) Monkey Tradition 中国剩余定理(CRT)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 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 h

lightoj 1319 - Monkey Tradition (中国剩余定理)

1319 - Monkey Tradition PDF (English) Statistics Forum 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

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dim

lightoj 1319 - Monkey Tradition

可以用中国剩余定理也可以用线性同余方程组,时间分别是0.028和0.036 在这个问题里逆元一定有解 #include<bits/stdc++.h> using namespace std; long long p[15];//ans==r[i](mod p[i]) long long r[15]; int n; void extgcd(long long a,long long b,long long &d,long long &x,long long &y)//re

Monkey Tradition(中国剩余定理)

Monkey Tradition Time Limit: 2000MS   Memory Limit: 32768KB   64bit 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

中国剩余定理的应用:猪的安家 -&gt;福州大学 OJ

                                                                 Problem 1402 猪的安家 Accept: 984    Submit: 5945Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Andy 和Mary养了很多猪.他们想要给猪安家.但是Andy没有足够的猪圈,很多猪只能够在一个猪圈安家.举个例子,假如有16头猪,Andy建

数论快速入门(同余、扩展欧几里德、中国剩余定理、大素数测定和整数分解、素数三种筛法、欧拉函数以及各种模板)

数学渣渣愉快的玩了一把数论,来总结一下几种常用的算法入门,不过鶸也是刚刚入门, 所以也只是粗略的记录下原理,贴下模板,以及入门题目(感受下模板怎么用的) (PS:文中蓝色字体都可以点进去查看百度原文) 附赠数论入门训练专题:点我打开专题(题目顺序基本正常,用以配套数论入门) 一.同余定理 简单粗暴的说就是:若 a-b == m 那么 a%m == b%m 这个模运算性质一眼看出...直接上入门水题: Reduced ID Numbers 附AC代码(这个也没啥模板....知道就好) #inclu

HDU1370(中国剩余定理)

昨天我仔细一想,发现自己之前的分类(用OJ来划分,毫无意义啊!)太失败了,所以我重新划分了一下大分类,在分到数论的时候,我就想起了中国剩余定理了,于是乎今天就刷了一题中国剩余定理的题目了,话说太久没作数学题,导致我连样例都调了好多次(在算逆元时候老是算错-烦恼!),好在提交时候是1A. 题目的意思就是:人有三个周期,记为p,e,i,周期天数分别为23,28,33,现在给定你三个时间a,b,c,和一个天数d,a,b,c分别表示p,e,i出现的天数,问下一次出现的大于d的天数是今年的第几天,即输出天

学习拓展中国剩余定理小结

前言 话说中国剩余定理好早就会了,但是一直木有接触过拓展的. 只知道它是个什么东东. 最近似乎需要它了,稍微学了学,似乎还挺简单的. 小结一下~ 简介 中国剩余定理我们都懂吧? 而拓展则是把它后面的模数变成一个非质数,(当然,各个方程的模数互质). 然后求出最小的x的解. 做法 似乎拓展之后很难用原来的套路来搞了. 怎么办? 我们发现,我们可以利用一些奇怪的推柿子大法来合并柿子. 考虑合并一下两个柿子: \(x \equiv c1 (mod\ m1)\) \(x \equiv c2 (mod\