zoukankan      html  css  js  c++  java
  • HDU 3579 Hello Kiki 中国剩余定理

    Hello Kiki

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1678    Accepted Submission(s): 587

    Problem Description
    One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again... Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note. One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
     
    Input
    The first line is T indicating the number of test cases. Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line. All numbers in the input and output are integers. 1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
     
    Output
    For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
     
    Sample Input
    2 2 14 57 5 56 5 19 54 40 24 80 11 2 36 20 76
     
    Sample Output
    Case 1: 341 Case 2: 5996
     
    Author
    digiter (Special Thanks echo)
     
    Source
     
    这一道题要考虑0的情况,要讨论了。0 <= Ai < Mi
     
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 using namespace std;
     5 
     6 int m[10];
     7 int A[10];
     8 
     9 int Ex_gcd(int a,int b,int &x,int &y)
    10 {
    11     if(b==0)
    12     {
    13         x=1;
    14         y=0;
    15         return a;
    16     }
    17     int g=Ex_gcd(b,a%b,x,y);
    18     int hxl=x-(a/b)*y;
    19     x=y;
    20     y=hxl;
    21     return g;
    22 }
    23 
    24 int gcd(int a,int b)
    25 {
    26     if(b==0)
    27     return a;
    28     return gcd(b,a%b);
    29 }
    30 
    31 void make_ini(int n,int time)
    32 {
    33     int x,y,i,m1,r1,m2,r2,d,c,t;
    34     bool flag=false;
    35     m1=m[1];r1=A[1];
    36     for(i=2;i<=n;i++)
    37     {
    38         m2=m[i];
    39         r2=A[i];
    40         if(flag==true)
    41         continue;
    42         d=Ex_gcd(m1,m2,x,y);
    43         c=r2-r1;
    44         if(c%d)
    45         {
    46             flag=true;
    47             continue;
    48         }
    49         x=c/d*x;//x
    50         t=m2/d;//b
    51         x=(x%t+t)%t;//min x
    52         r1=m1*x+r1;
    53         m1=m1*m2/d;
    54     }
    55     if(flag==true)
    56     {
    57         printf("Case %d: -1
    ",time);
    58         return;
    59     }
    60     if(r1==0 && n==1)
    61     {
    62         r1=m[1];
    63     }
    64     else if(r1==0 && n>1)
    65     {
    66         r1=m[1];
    67         r2=1;
    68         for(i=2;i<=n;i++)
    69         r1=gcd(r1,m[i]);
    70 
    71         for(i=1;i<=n;i++)
    72         r2=r2*m[i];
    73 
    74         r1=r2/r1;
    75     }
    76     printf("Case %d: %d
    ",time,r1);
    77 }
    78 
    79 int main()
    80 {
    81     int T,n,i,t;
    82     while(scanf("%d",&T)>0)
    83     {
    84         for(t=1;t<=T;t++)
    85         {
    86             scanf("%d",&n);
    87             for(i=1;i<=n;i++)
    88             scanf("%d",&m[i]);
    89 
    90             for(i=1;i<=n;i++)
    91             scanf("%d",&A[i]);
    92             make_ini(n,t);
    93         }
    94     }
    95     return 0;
    96 }
     
  • 相关阅读:
    How to Change Error Message Colors in Windows 10 PowerShell Console
    How to expand Azure VM OS Disk
    Ubuntu terminal colors
    Docker proxy
    如何对mRemoteNG在进行Linux终端访问时自定义配色
    100阶乘末尾有多少个零
    几道c/c++练习题
    M-BM-
    从FGUI中取一张图片并返回一个Sprite
    为什么手游选择lua热更新方案。
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3260944.html
Copyright © 2011-2022 走看看