zoukankan      html  css  js  c++  java
  • Hdu3579 Hello Kiki

    Hello Kiki

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

    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的情况.
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    const ll maxn = 100010;
    ll T,n,a[maxn],m[maxn],cas;
    
    ll gcd(ll a, ll b)
    {
        if (!b)
            return a;
        return gcd(b, a % b);
    }
    
    ll exgcd(ll a, ll b, ll &x, ll &y)
    {
        if (!b)
        {
            x = 1;
            y = 0;
            return a;
        }
        ll temp = exgcd(b, a % b, x, y), t = x;
        x = y;
        y = t - (a / b) * y;
        return temp;
    }
    
    ll niyuan(ll x, ll mod)
    {
        ll px, py, t;
        t = exgcd(x, mod, px, py);
        if (t != 1)
            return -1;
        return (px % mod + mod) % mod;
    }
    
    bool hebing(ll a1, ll n1, ll a2, ll n2, ll &a3, ll &n3)
    {
        ll d = gcd(n1, n2), c = a2 - a1;
        if (c % d != 0)
            return false;
        c = (c % n2 + n2) % n2;
        n1 /= d;
        n2 /= d;
        c /= d;
        c *= niyuan(n1, n2);
        c %= n2; //取模,在哪一个模数下就要模哪个,模数要跟着变化.
        c *= n1 * d;
        c += a1;
        n3 = n1 * n2 * d;
        a3 = (c % n3 + n3) % n3;
        return true;
    }
    
    
    ll solve()
    {
        ll a1 = a[1],m1 = m[1],a2,m2,a3,m3;
        for (ll i = 2; i <= n; i++)
        {
            a2 = a[i],m2 = m[i];
            if (!hebing(a1,m1,a2,m2,a3,m3))
                return -1;
            a1 = a3;
            m1 = m3;
        }
        if (a1 == 0)
        {
            m1 = 1;
            for (int i = 1; i <= n; i++)
                m1 = m1 * m[i] / gcd(m1,m[i]);
            return m1;
        }
        ll t = (a1 % m1 + m1) % m1;
        return t;
    }
    
    int main()
    {
        scanf("%lld",&T);
        while (T--)
        {
            scanf("%lld",&n);
            for (ll i = 1; i <= n; i++)
                scanf("%lld",&m[i]);
            for (ll i = 1; i <= n; i++)
                scanf("%lld",&a[i]);
            printf("Case %lld: %lld
    ",++cas,solve());
        }
    
        return 0;
    }
  • 相关阅读:
    Gradle在大型Java项目上的应用
    2015年,移动开发都有哪些热点?
    为什么寄存器比内存快?
    Gogs:可能是比Gitlab更好的选择
    自定义元素–为你的HTML代码定义新元素
    在DLL编程中,导出函数为什么需要extern "C"
    c调用c++编的dll,c++调用c编写的dll,extern “C”的用法
    C/C++:函数的编译方式与调用约定以及extern “C”的使用
    在VS2015中用C++编写可被其它语言调用的动态库DLL
    C++在VS下创建、调用dll
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8675750.html
Copyright © 2011-2022 走看看