zoukankan      html  css  js  c++  java
  • 中国剩余定理 hdu 3579

    HDU 3579 Hello Kiki

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


    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
     1 /*看了题解,才知道数据中有只有一组数据,并且整除的数据
     2 if(n==1&&a[1]==0)
     3 {
     4             printf("Case %d: %d
    ",opt,m[1]);
     5             continue;        
     6 }只有一组数据,并且还整除,中国剩余定理是解决不了的,要特判。
     7 */
     8 #include<iostream> 
     9 using namespace std;
    10 #include<cstdio>
    11 #define inf (1<<31)-1
    12 #define N 10
    13 void exgcd(int a,int b,int &x,int &y,int &gcd)
    14 {
    15     if(b==0)
    16     {
    17         x=1;y=0;
    18         gcd=a;
    19         return ;
    20     }
    21     exgcd(b,a%b,x,y,gcd);
    22     int t=x;
    23     x=y;
    24     y=t-(a/b)*y;
    25 }
    26 int main()
    27 {
    28     int T;
    29     scanf("%d",&T);
    30     int opt=0;
    31     while(T--)
    32     {
    33         ++opt;
    34         int n,m[N]={0},a[N]={0};
    35         int m1,m2,a1,a2,x,y,gcd;
    36         scanf("%d",&n);
    37         for(int i=1;i<=n;++i)
    38           scanf("%d",&m[i]);
    39         for(int i=1;i<=n;++i)
    40           scanf("%d",&a[i]);
    41         m1=m[1];a1=a[1];
    42         if(n==1&&a[1]==0)
    43         {
    44             printf("Case %d: %d
    ",opt,m[1]);
    45             continue;
    46         }
    47         bool flag=false;
    48         for(int i=2;i<=n;++i)
    49         {
    50             a2=a[i];m2=m[i];
    51             exgcd(m1,m2,x,y,gcd);
    52             if((a2-a1)%gcd)
    53             {
    54                 flag=true;
    55                 break;
    56             }
    57             int t=m2/gcd;
    58             x=(x*(a2-a1))/gcd;
    59             x=(x%t+t)%t;
    60             a1=m1*x+a1;
    61             m1=(m1*m2)/gcd;
    62             a1=(a1%m1+m1)%m1;
    63         }
    64         if(flag)
    65           printf("Case %d: -1
    ",opt);
    66         else printf("Case %d: %d
    ",opt,a1);
    67     }
    68     return 0;
    69 }
     
  • 相关阅读:
    .NET逻辑分层架构总结
    ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件
    c#实现浏览器端大文件分块上传
    fckeditor如何能实现直接粘贴把图片上传到服务器中?
    web编辑器直接粘贴图片实现
    富文本编辑器直接粘贴图片实现
    百度ueditor编辑器直接粘贴图片实现
    百度编辑器直接粘贴图片实现
    fckeditor直接粘贴图片实现
    wangEditor直接粘贴图片实现
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5514206.html
Copyright © 2011-2022 走看看