【问题标题】:Finding the number of solutions and the solutions in a given interval of a Linear Diophantine Equation查找线性丢番图方程的给定区间内的解数和解数
【发布时间】:2023-03-14 20:15:02
【问题描述】:

我最近研究了线性丢番图方程,并找到了一种使用扩展欧几里得方法的可能解决方案,但是如果给定一个允许的 'x' 和 ' 范围怎么办y' 并要求计算解决方案的数量找到解决方案。我已经在here 看过这个,但无法更清楚地理解它。任何其他方法或用更简单的语言解释上述方法都是值得赞赏的。谢谢。

【问题讨论】:

  • 一旦你有了参数解决方案,然后强加解决方案在给定的区间内会给你一个参数值的范围。计算该范围,并让参数采用该范围内的整数值。最后,计算对应于每个参数值的解。如果只是计数,则只计算参数允许范围内的整数值个数。
  • 在你的链接中,参数是k
  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议遵循帮助文档中的发布指南。 On topichow to ask 和 ...the perfect question 在此处申请。 StackOverflow 不是设计、编码、研究或教程资源。如果您有具体的困惑点,请解释一下,我们会解决的。但是,要求我们以其他方式解释整个方法超出了这里的范围。

标签: algorithm


【解决方案1】:

我在 cp-algorithms 中阅读了 this。代码对我来说不清楚,但我已经编写了一个代码,它可以正常工作并且更容易理解。实际上,我已经通过在 codeforces 中解决 this problem 来检查我的代码的正确性。这是我的代码的python版本:

from math import ceil, gcd, floor


def GCD(a, b):
    if b == 0:
        x = 1
        y = 0
        return(x, y)
    A, B = GCD(b, a % b)
    x = B
    y = A-B*(a//b)
    return(x, y)


def find_ans(a, b, c, minx, maxx, miny, maxy):
    if c % gcd(abs(a), abs(b)) != 0:
        return 0
    sb = b//abs(b)  # sign of a
    sa = a//abs(a)  # sign of b
    x, y = GCD(abs(a), abs(b))  # a solution to the equation
    x *= sa  # adjusting the sign of x
    y *= sb  # adjusting the sign of x
    g = gcd(abs(a), abs(b))
    x *= c//g
    y *= c//g
    # lk1 (left_k) = lower bound for k due to [minx , maxx]
    lk1 = (minx-x)*g/b  # x+k*b/g >= minx --> k >= (minx-x)*g/b (if b>0)
    # rk1 (right_k) = upper bound for k due to [minx , maxx]
    rk1 = (maxx-x)*g/b  # x+k*b/g <= maxx --> k <= (maxx-x)*g/b (if b>0)
    # till this line of code, we have assumed that b>0 and we have : (minx-x)*g/b <= k <= (maxx-x)*g/b
    # if b<0, then : (minx-x)*g/b >= k >= (maxx-x)*g/b . Thus the lower bound and the upper bound will change
    if sb == -1:
        lk1, rk1 = rk1, lk1
    # for example if lk1= 1.5 , we have k>=2 ( the reason is that k must be integer), so we have to use ceil for lower bound
    lk1 = ceil(lk1)
    # for example if lk1= 10.5 , we have k<=10 , so we have to use floor for upper bound
    rk1 = floor(rk1)

    # we do the same thing for a
    rk2 = (y-miny)*g/a  # y-k*a/g >= miny --> k <= (y-miny)*g/a (if a>0)
    lk2 = (y-maxy)*g/a  # y-k*a/g <= maxy --> k >= lk2 = (y-maxy)*g/a (if a>0)
    if sa == -1:
        lk2, rk2 = rk2, lk2
    lk2 = ceil(lk2)
    rk2 = floor(rk2)
    #### finding the interval ####
    lans = max(lk1, lk2)  # lower bound of interval
    rans = min(rk1, rk2)  # upper bound of interval
    # it occurs when we have ( lk1 ------ rk1   lk2 ------ rk2 ) or (lk2 ------- rk2   lk1 -------- rk1).[lk1,rk1] and [lk2,rk2] don't have intersection
    if rans < lans:
        print(0)
    else:
        print(rans-lans+1)

【讨论】:

    【解决方案2】:

    我已经在 J​​ava 中实现并测试了线性丢番图方程。该实现被注释掉以更好地理解。它涵盖以下几点

    • 寻找一种解决方案
    • 寻找多种解决方案
    • 在一个范围内寻找解决方案

    https://github.com/love1024/Algorithms-Library-In-Java/blob/main/src/Math/LinearDiophantine.java

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多