zoukankan      html  css  js  c++  java
  • hdu 3579(中国剩余定理+考虑0)

    Hello Kiki

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


    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
     
     
    output the least positive integer X
    注意这句话,当为0的时候输出mod
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    const int N = 10;
    LL extend_gcd(LL a,LL b,LL &x,LL &y)
    {
        if(b==0)
        {
            x=1,y=0;
            return a;
        }
        else
        {
            LL x1,y1;
            LL d = extend_gcd(b,a%b,x1,y1);
            x = y1;
            y = x1-a/b*y1;
            return d;
        }
    }
    LL m[N],a[N];///模数为m,余数为a, X % m = a
    bool solve(LL &m0,LL &a0,LL m,LL a)
    {
        long long y,x;
        LL g = extend_gcd(m0,m,x,y);
        LL t = a-a0>0?a-a0:a0-a;
        if( t%g )return false;
        x *= (a - a0)/g;
        x %= m/g;
        a0 = (x*m0 + a0);
        m0 *= m/g;
        a0 %= m0;
        if( a0 < 0 )a0 += m0;
        return true;
    }
    /**
    * 无解返回false,有解返回true;
    * 解的形式最后为 a0 + m0 * t (0<=a0<m0)
    */
    bool MLES(LL &m0 ,LL &a0,LL n)///解为 X = a0 + m0 * k
    {
        bool flag = true;
        m0 = 1;
        a0 = 0;
        for(int i = 0; i < n; i++)
            if( !solve(m0,a0,m[i],a[i]) )
            {
                flag = false;
                break;
            }
        return flag;
    }
    int main()
    {
        int n;
        int tcase;
        scanf("%d",&tcase);
        int t=1;
        while(tcase--)
        {
            scanf("%d",&n);
            for(int i=0; i<n; i++)
            {
                scanf("%lld",&m[i]);
            }
            for(int i=0; i<n; i++)
            {
                scanf("%lld",&a[i]);
            }
            LL m0,a0;
            bool flag = MLES(m0,a0,n);
            LL x = (a0%m0+m0)%m0;
            printf("Case %d: ",t++);
            if(!flag) printf("-1
    ");
            else
            {
                if(x==0) x+=m0;
                printf("%lld
    ",x);
            }
        }
        return 0;
    }
  • 相关阅读:
    获得spring代理对象的原对象
    自动注入bean,简化持久化
    java Instrument修改字节码实现aop功能
    c# 笔记
    js 字符串与json互转
    js Deferred的使用
    chrome 插件开发 通讯机制
    ajax 采用XMLHttpRequest post、get 发送数据
    [转]c fscanf 按行读取文件_(Testbench用法总结)1. Testbench中文本数据的存储读取操作对比...
    转:VIVADO使用技巧:设置DCI与内部参考电压
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5534577.html
Copyright © 2011-2022 走看看