任务描述
给定正整数a,b,c,求不定方程ax+by=c
关于未知数x和y的所有非负整数解组数并输出。部分用例有时间限制。
输入格式
输入为一行,包含三个正整数a、b、c,两个整数之间用单个空格隔开,本题保证每个数均不大于10000.
输出格式
第一行以二维列表的形式输出所有的非负整数解
第二行输出一个整数,即不定方程的非负整数解组数
示例
输入: 2 3 18
输出:[[0, 6], [3, 4], [6, 2], [9, 0]]
4
平均代码量 11 行
def solve_equation(a, b, c):
solutions = []
count = 0
for x in range(c // a + 1):
remainder = c - a * x
if remainder % b == 0:
y = remainder // b
solutions.append([x, y])
count += 1
return solutions, count
# 获取输入
input_str = input()
a, b, c = map(int, input_str.split())
# 求解不定方程
result, count = solve_equation(a, b, c)
# 输出结果
print(result)
print(count)