zoukankan      html  css  js  c++  java
  • Monkey Tradition(中国剩余定理)

    Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

     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 notL. 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

    题解:

    用扩展GCD求;剩下的就是模版;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    LL p[20], r[20];
    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 temp = x;
        x = y;
        y = temp - a/b * y;
    }
    int main(){
        int T, n, kase = 0;
        scanf("%d", &T);
        while(T--){
            scanf("%d", &n);
            LL MOD = 1;
            for(int i = 0; i < n; i++){
                scanf("%lld%lld", &p[i], &r[i]);
                MOD *= p[i];
            }
            LL x, y;
            LL ans = 0;
            for(int i = 0; i < n; i++){
                ex_gcd(MOD/p[i], p[i], x, y);
                ans = (ans + MOD/p[i]*x*r[i] + MOD) % MOD;
            }
            printf("Case %d: %lld
    ",++kase, (ans + MOD) % MOD);
        }
        return 0;
    }
  • 相关阅读:
    Netty指定分隔的字符
    Netty解决TCP粘包/拆包问题
    TCP粘包/拆包问题
    Netty5-应答服务器
    线程池中的线程的排序问题
    Java NIO 概述
    在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字
    OXM
    Mysql event时间触发器,实现定时修改某些符合某一条件的某一字段
    linux 环境 tomcat 莫名奇妙挂掉
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5515804.html
Copyright © 2011-2022 走看看