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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(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 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 havent 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
Output for Sample Input
Case 1: 69
Case 2: 113

题目大意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底部,后来往上跳,每次跳的距离是pi,最后不能跳到树上面所以最后会有个到顶端的距离ri,求L的最小值;

思路:这是一题典型的中国剩余定理题。

关于中国剩余定理:http://www.cnblogs.com/zhengguiping--9876/p/5319813.html

关于扩展欧几里得定理:http://www.cnblogs.com/zhengguiping--9876/p/5308276.html

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <map>
#include <string>
#include <vector>
#include<iostream>
using namespace std;
#define N 10006
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
LL p[N],r[N];
LL n,sum;
void ex_gcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x = 1;
        y = 0;
        return ;
    }
    ex_gcd(b, a%b, x, y);
    LL t = x;
    x = y;
    y = (t - a/b * y);
    if(a*b<0)
    {
        x = -x;
        y = -y;
    }
}
LL china()
{
    LL ans = 0;
    for(int i = 0; i < n; i++)
    {
        LL x, y;
        ex_gcd(sum / p[i], -p[i], x, y);
        LL N0 = y * p[i] + 1;
        ans = (ans + N0 * r[i] + sum) %sum;
    }
    return (ans + sum) % sum;
}
int main()
{
    int T,con=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld", &n);
        sum = 1;
        for(int i=0; i<n; i++)
        {
            scanf("%lld %lld", &p[i], &r[i]);

            sum *= p[i];
        }

        LL x = china();

        printf("Case %d: %lld\\n",con++,x);
    }
    return 0;
}

 

以上是关于(light oj 1319) Monkey Tradition 中国剩余定理(CRT)的主要内容,如果未能解决你的问题,请参考以下文章

Light oj 1140 How Many Zeroes?

概率 light oj 1104

概率 light oj 1248

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

light oj 1058

(light OJ 1005) Rooks dp