zoukankan      html  css  js  c++  java
  • hud 3579 解同余方程的应用

    Hello Kiki

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


    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
     该题是典型的同余方程X≡Ai(mod Mi)求解,题目要求输出最小的整数解,当同余方程组的解为0时,应该输出Mi的最小公倍数lcm
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <stack>
     5 #include <queue>
     6 #include <map>
     7 #include <algorithm>
     8 #include <vector>
     9 
    10 using namespace std;
    11 
    12 const int maxn = 1000005;
    13 
    14 typedef long long LL;
    15 
    16 LL gcd( LL a,LL b)
    17 {
    18     if(b == 0) return a;
    19    else  return gcd(b,a%b);
    20 }
    21 
    22 LL ex_gcd(LL a,LL b,LL &x,LL &y)
    23 {
    24    if(b == 0){
    25         x = 1;
    26         y = 0;
    27         return a;
    28    }
    29    LL r = ex_gcd(b,a%b,x,y);
    30    LL t = x;
    31       x = y;
    32       y = t - a/b*y;
    33       return r;
    34 }
    35 LL aa[20],r[20];
    36 
    37 int main()
    38 {
    39     LL i,ans,a,b,c,d,k,x,y,n,m;
    40     LL T;
    41     cin>>T;
    42     int cas = 0;
    43     while(T--){
    44         cin>>m;
    45         cas++;
    46         bool flag = 1;
    47         LL lcm = 1;
    48         for(i=1;i<=m;i++){
    49             cin>>aa[i];
    50             lcm = lcm/gcd(lcm,aa[i])*aa[i];
    51         }
    52         for( i=1;i<=m;i++){
    53             cin>>r[i];
    54         }
    55         printf("Case %d: ",cas);
    56         for( i=2;i<=m;i++){
    57             a = aa[1];
    58             b = aa[i];
    59             c = r[i] - r[1];
    60             d = ex_gcd(a,b,x,y);
    61             if(c%d!=0){
    62                 flag = 0;
    63                 break;
    64             }
    65             LL t = b/d;
    66                  x = (x*(c/d)%t+t)%t;
    67                  r[1] = aa[1]*x + r[1];
    68                  aa[1] = aa[1]*(aa[i]/d);
    69         }
    70         if(!flag){
    71             puts("-1");
    72             continue;
    73         }
    74 
    75         if(r[1]){
    76            printf("%lld
    ",r[1]);
    77            continue;
    78         }
    79 
    80         printf("%lld
    ",lcm);
    81     }
    82 
    83     return 0;
    84 }
    View Code

    这道题跟hdu1573差不多,代码改改就能过

  • 相关阅读:
    Keil MDK中的Code, RO-data , RW-data, ZI-data分别代表什么意思?(转)
    nrf开发笔记一开发软件
    ARM CORTEX-M3的时钟
    stm32之Cortex系统定时器(SysTick)
    micrium ucprobe使用笔记
    C语言结构体初始化的四种方法(转载)
    setsockopt的作用
    Java之RandomAccessFile小结
    疯狂JAVA讲义---第十五章:输入输出(上)流的处理和文件
    java压缩解压zip文件,中文乱码还需要ant.jar包
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4934245.html
Copyright © 2011-2022 走看看