HDU-3579 Hello Kiki 线性同余方程的运用

1 篇文章 0 订阅

题目:

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

题意理解:

这题的意思就是小女孩数硬币,数了N次,每次分成 M_{i} 组,会余下 A_{i} 个硬币,稍微转换一下,假设硬币数量为X

                                                                 \left\{\begin{matrix} \right.X\equiv A_{0}\left ( mod\mathbf{M_{0}} \right ) \\ \right.X\equiv A_{1}\left ( mod\mathbf{M_{1}} \right ) \\... \\\right.X\equiv A_{i}\left ( mod\mathbf{M_{i}} \right ) \end{matrix}

推到这个公式后,再进行转换,对于复杂问题简单化,先分析前两个

                                                                \left\{\begin{matrix} X = A_{0} + M_{0}*k_{0} \\ X = A_{1} + M_{1}*k_{1} \end{matrix}\right.

再变形

                                                 A_{0} + M_{0}*k_{0} = A_{1} + M_{1}*k_{1}

推到这里应该很明了了,那么我们再推一步

                                                         M_{1}*k_{1} - M_{0}*k_{0} = A_{0} -A_{1}

那么,解线性同余方程即可,解出 d = exgcd\left ( M_{0} \right, M_{1} )  存在解的条件是 d\mid \left ( A_{0} \right - A_{1} ) 

这样我们就可以通过取模求出一个在小于 M_{0} / d 的 k_{1} 的正整数解,继而推出一个小于 lcm\left ( M_{0} \right M_{1} )  的唯一的 X^{'} 。

这样我们就将前两个转换成了

                                                              X\equiv X^{'}\left ( mod \right\mathit{lcm(M_{0} , M_{1})} )

这样就可以递推解下去,最终得到一个小于 lcm\left ( M_{0} \right,M_{1},...M_{i} ) 的唯一X解。但我们一定要注意取模的时候,X可能为0,这时的X其实是 lcm\left ( M_{0} \right,M_{1},...M_{i} ) 。那么这题就完美的解出来了。

 

 总结:

关于线性同余方程,这是最后一个练习的题目,想了想还是要写一篇博客留个记号,其实做很多题只是为了让自己强化知识点,对于解线性同余方程,运用的是扩展欧几里得,字面意思就能体会出来实在欧几里得方法上进行扩展,有两种写法一种递归写法,一种非递归的,对于递归的主要是要理解

                                                                 \left\{\begin{matrix} gcd(a, b) = ax_{1} + by_{1} \\ gcd(b, a\%b) = bx_{2} + (a\%b )y_{2} \end{matrix}\right.

推导一下其实就是

                                                                  \\ax_{1} + by_{1} = bx_{2} + [a - (a/b)*b]y_{2} \\ax_{1} + by_{1} = ay_{2} + b[x_{2} - (a/b)y_{2}]

就可以得出递推式

                                                                               \\ x_{1} = y_{2} \\ y_{1} = x_{2} - (a/b)y_{2}

对于非递归的,其实就是需要推出

                                                                     gcd(a, b) = a s_{n} + bt_{n}

                                                                              \\s_{n} = s_{n-2} -q_{n-1}s_{n-1} \\t_{n} = t_{n-2} -q_{n-1}t_{n-1}

辅助推导公式是

                                                                                 \\r_{0} = a \\r_{1} = b \\r_{2} = r_{0} - q_{1}r_{1} \\... \\r_{n} = r_{n-2} - q_{n-1}r_{n-1}

假设 r_{j} = as_{j} + bt{j} 然后把带入 r_{k} = r_{k-2} - r_{k-1}q_{k-1} 即可推出 s_{n} 与 t_{n}的递推式 t_{n} 的递推式。

代码:

#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
const int MAXN = 10;
LL A[MAXN], M[MAXN];

// 递归写法
/*LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x = 1, y = 0;
        return a;
    }
    else
    {
        LL r = exgcd(b, a%b, x, y);
        LL t = y;
        y = x - (a/b)*y;
        x = t;
        return r;
    }
}*/

// 非递归写法
LL exgcd(LL a, LL b, LL &x, LL &y)
{
    LL x0 = 1, y0 = 0;
    LL x1 = 0, y1 = 1;
    LL r = a%b, q = a/b;
    x = 0, y = 1;
    while(r)
    {
        x1 = x0 - q*x1, y1 = y0 - q*y1;
        x0 = x, y0 = y;
        x = x1, y = y1;
        a = b, b = r;
        r = a%b, q = a/b;
    }
    return b;
}

LL solve(int N)
{
    LL a = A[0], m = M[0];
    bool flag = true;
    for(int i = 1; i < N; i++)
    {
        LL R = a - A[i], x, y;
        LL d = exgcd(M[i], m, x, y);
        if(R % d != 0)
        {
            flag = false;
            break;
        }
        LL t = m/d;
        x = x*(R/d);
        x = (x%t + t)%t;
        a = A[i] + M[i]*x;
        m = M[i]*(m/d);
        a = (a%m + m)%m;
    }
    if(!flag)
        return -1;
    else
        if(a == 0)  return m;
        else    return a;
}

int main()
{
    int N, T;
    scanf("%d", &T);
    for(int k = 1; k <= T; k++)
    {
        scanf("%d", &N);
        for(int i = 0; i < N; i++)
            scanf("%I64d", &M[i]);
        for(int i = 0; i < N; i++)
            scanf("%I64d", &A[i]);
        printf("Case %d: %I64d\n", k, solve(N));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值