zoukankan      html  css  js  c++  java
  • 1319

    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


    Problem Setter: Tanvir Hassan
    Special Thanks: Jane Alam Jan
    思路:中国剩余定理;扩展欧基里德。模板题。
     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<queue>
     6 #include<vector>
     7 using namespace std;
     8 typedef long long LL;
     9 typedef unsigned long long ll;
    10 pair<LL ,LL>P(LL n,LL m);
    11 typedef struct pp
    12 {
    13     long long x;
    14     long long y;
    15 } ss;
    16 ss aa[100];
    17 int main(void)
    18 {
    19     LL i,j,k,p,q;int ca;
    20     int ba;int s;
    21     ll sum;
    22     scanf("%d",&ca);
    23     for(ba=1; ba<=ca; ba++)
    24     {
    25         sum=1;
    26         scanf("%d",&s);
    27         for(i=0; i<s; i++)
    28         {
    29             scanf("%lld %lld",&aa[i].x,&aa[i].y);
    30             sum*=aa[i].x;
    31         }
    32         LL ans=0;
    33         for(i=0; i<s; i++)
    34         {
    35             LL mn=sum/aa[i].x;
    36             LL x;
    37             LL y;
    38            pair<LL,LL>xx;
    39            xx=P(mn,aa[i].x);
    40            x=xx.first;y=xx.second;
    41             LL z=(aa[i].x+x%aa[i].x)%aa[i].x;
    42             ans=((ans+((aa[i].y*mn)%sum)*z)%sum)%sum;
    43         }
    44         ans=(ans+sum)%sum;
    45         printf("Case %d:",ba);
    46         printf(" %lld
    ",ans);
    47     }return 0;
    48 }
    49 
    50 pair<LL ,LL>P(LL n,LL m)
    51 {
    52       if(m==0)
    53       {
    54         pair<LL,LL> c=make_pair(1,0);
    55          return c;
    56       }
    57       else
    58       {
    59          pair<LL,LL>x=P(m,n%m);
    60           LL k=x.second;
    61           LL kk=x.first;
    62           x.first=k;
    63           x.second=kk-(n/m)*k;
    64          return x;
    65       }
    66 }
    油!油!you@
  • 相关阅读:
    shell 逻辑操作符
    shell 整数
    shell 字符串
    常用文件测试操作符
    系统级脚本 rpcbind
    shell 特殊字符
    redhat7.5 升级OpenSSH_7.8p1
    kubernetes 项目
    salt 安装kubernetes集群3节点
    web应用
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5321090.html
Copyright © 2011-2022 走看看