• hdu 3579 Hello Kiki (中国剩余定理)


    Hello Kiki

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


    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
     
    Recommend
    zhouzeyong   |   We have carefully selected several similar problems for you:  3573 3574 3575 3576 3577 
     

    模板题,注意ans=0的情况,题目要求的是最小的正整数。

     1 //0MS    244K    1571 B    C++    
     2 #include<stdio.h>
     3 __int64 gcd(__int64 a,__int64 b)
     4 {
     5     return b?gcd(b,a%b):a;
     6 }
     7 __int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
     8 {
     9     if(b==0){
    10         x=1;y=0;
    11         return a;
    12     }
    13     __int64 d=extend_euclid(b,a%b,x,y);
    14     __int64 t=x;
    15     x=y;
    16     y=t-a/b*y;
    17     return d;
    18 }
    19 __int64 inv(__int64 a,__int64 n)
    20 {
    21     __int64 x,y;
    22     __int64 t=extend_euclid(a,n,x,y);
    23     if(t!=1) return -1;
    24     return (x%n+n)%n;
    25 }
    26 bool merge(__int64 a1,__int64 n1,__int64 a2,__int64 n2,__int64 &a3,__int64 &n3)
    27 {
    28     __int64 d=gcd(n1,n2);
    29     __int64 c=a2-a1;
    30     if(c%d) return false;
    31     c=(c%n2+n2)%n2;
    32     c/=d;
    33     n1/=d;
    34     n2/=d;
    35     c*=inv(n1,n2);
    36     c%=n2;
    37     c*=n1*d;
    38     c+=a1;
    39     n3=n1*n2*d;
    40     a3=(c%n3+n3)%n3;
    41     return true;
    42 }
    43 __int64 china_reminder2(int len,__int64 *a,__int64 *n,__int64 &lcm)
    44 {
    45     __int64 a1=a[0],n1=n[0];
    46     __int64 a2,n2;
    47     for(int i=1;i<len;i++){
    48          __int64 aa,nn;
    49          a2=a[i],n2=n[i];
    50          if(!merge(a1,n1,a2,n2,aa,nn)) return -1;
    51          a1=aa;
    52          n1=nn;
    53     }
    54     lcm=n1;
    55     return (a1%n1+n1)%n1;
    56 }
    57 int main(void)
    58 {
    59     int t,n,k=1;
    60     __int64 a[10],b[10],lcm;
    61     scanf("%d",&t);
    62     while(t--)
    63     {
    64         scanf("%d",&n);
    65         for(int i=0;i<n;i++)
    66             scanf("%I64d",&a[i]);
    67         for(int i=0;i<n;i++)
    68             scanf("%I64d",&b[i]);
    69         printf("Case %d: ",k++);
    70         __int64 ans=china_reminder2(n,b,a,lcm);
    71         if(ans==0) ans+=lcm;
    72         printf("%I64d
    ",ans);
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    unity tilemap 缝隙解决
    JAVA使用geoip2库解析IP地址信息
    JAVA使用Ip2region获取IP定位信息
    打印二叉树边界
    git pull 和 git fetch的区别?
    格式化配置
    vscode的settings.json配置(个人习惯)
    sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, "MySQL server has gone away
    大数据利器分享clickhouse
    k8s应用发版报错
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3785685.html
Copyright © 2020-2023  润新知