线性丢番图方程 - 求解的个数以及给定区间内的解

问题描述 投票:0回答:2

我正在从cp算法学习线性丢番图方程。总的来说,我理解了这个理论。但我在实施中遇到了问题。

请提供一个测试用例来帮助我,其中

shift_solution(x, y, a, b, (minx - x) / b);
shift_solution(x, y, a, b, sign_b);
都被执行。我尝试过一些方程式,但在每种情况下,执行
shift_solution(x, y, a, b, (minx - x) / b);
后,
x becomes equal to minx
。基本上,我需要一个可以通过执行这两行的测试用例。

shift_solution(x, y, a, b, (minx - x) / b);
    if (x < minx)
        shift_solution(x, y, a, b, sign_b);
    if (x > maxx)
        return 0;
    int lx1 = x;

这是来自ref的示例代码:

void shift_solution(int & x, int & y, int a, int b, int cnt) {
    x += cnt * b;
    y -= cnt * a;
}

int find_all_solutions(int a, int b, int c, int minx, int maxx, int miny, int maxy) {
    int x, y, g;
    if (!find_any_solution(a, b, c, x, y, g))
        return 0;
    a /= g;
    b /= g;

    int sign_a = a > 0 ? +1 : -1;
    int sign_b = b > 0 ? +1 : -1;

    shift_solution(x, y, a, b, (minx - x) / b);
    if (x < minx)
        shift_solution(x, y, a, b, sign_b);
    if (x > maxx)
        return 0;
    int lx1 = x;

    shift_solution(x, y, a, b, (maxx - x) / b);
    if (x > maxx)
        shift_solution(x, y, a, b, -sign_b);
    int rx1 = x;

    shift_solution(x, y, a, b, -(miny - y) / a);
    if (y < miny)
        shift_solution(x, y, a, b, -sign_a);
    if (y > maxy)
        return 0;
    int lx2 = x;

    shift_solution(x, y, a, b, -(maxy - y) / a);
    if (y > maxy)
        shift_solution(x, y, a, b, sign_a);
    int rx2 = x;

    if (lx2 > rx2)
        swap(lx2, rx2);
    int lx = max(lx1, lx2);
    int rx = min(rx1, rx2);

    if (lx > rx)
        return 0;
    return (rx - lx) / abs(b) + 1;
}
c++ algorithm equation number-theory
2个回答
0
投票

如果 x < minix then first function call iterates it to closest possible x to mini and x less then mini so that we need to add one more b here.


0
投票

尝试以下值:

a = 1
b = 1
c = 3
minx = 0
maxx = 4
miny = 0
maxy = 4

它会返回给你4个解决方案(正确答案)。

© www.soinside.com 2019 - 2024. All rights reserved.