HDU - 1573 - X问题(中国剩余定理不满足互质情况下的求解)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1573

题解:

推导过程太长写纸上了,夹在ACM/ICPC那本书里面。
转一个题解:
http://972169909-qq-com.iteye.com/blog/1266328
作者 (KIDx)

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define LL __int64
#define M 10
int N;
LL exgcd(LL a,LL b, LL& x, LL& y)
{
    if(b == 0)
    {
        x = 1, y= 0;
        return a;
    }
    LL re = exgcd(b, a%b, x , y);
    LL tmp = x;
    x = y;
    y = tmp - a/b*y;
    return re;
}

LL CRT2 (LL b[], LL n[], int num)
{
    int i;
    bool flag = false;
    LL n1 = n[0], n2, b1 = b[0], b2, bb, d, t, k, x, y;
    for (i = 1; i < num; i++)
    {
        n2 = n[i], b2 = b[i];
        bb = b2 - b1;
        d = exgcd(n1, n2, x, y);
        if (bb % d)     //模线性解k1时发现无解
        {
            flag = true;
            break;
        }
        k = bb / d * x;
        t = n2 / d;
        if (t < 0) t = -t;
        k = (k % t + t) % t;
        b1 = b1 + n1*k;
        n1 = n1 / d * n2;
    }
    if (flag)
        return 0;
    if (b1 == 0)
        b1 = n1;
    if (b1 > N)
        return 0;
    return (N-b1)/n1+1;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值