本文共 1736 字,大约阅读时间需要 5 分钟。
为了解决这个问题,我们需要找到满足条件的整数对 (i, j) 的数量,使得 N = i * j + i + j,其中 0 < i ≤ j。
我们可以将问题转化为数学公式:N = i * j + i + j可以改写为:N + 1 = (i + 1) * (j + 1)
这意味着我们需要找到 N + 1 的所有因数对 (a, b),其中 a ≤ b 且 a 和 b 都是至少 2 的整数。因此,问题转化为计算 N + 1 的因数数目,并统计其中满足条件的因数对数目。
具体步骤如下:
import mathdef factorize(M): factors = {} while M % 2 == 0: factors[2] = factors.get(2, 0) + 1 M = M // 2 i = 3 while i * i <= M: while M % i == 0: factors[i] = factors.get(i, 0) + 1 M = M // i i += 2 if M > 1: factors[M] = 1 return factorsdef generate_factors(factors): factor_list = list(factors.items()) factors = [1] for (p, exp) in factor_list: temp = [] for d in factors: current = d for e in range(exp + 1): temp.append(current) current *= p factors = list(set(temp)) return sorted(factors)def count_valid_factors(factors, M): sqrt_m = math.isqrt(M) count = 0 for f in factors: if f >= 2 and f <= sqrt_m: count += 1 return countdef main(): import sys input = sys.stdin.read().split() T = int(input[0]) for i in range(1, T + 1): N = int(input[i]) M = N + 1 if M == 1: print("00") continue factors = factorize(M) factors_list = generate_factors(factors) count = count_valid_factors(factors_list, M) print(f"{count:02d}")if __name__ == "__main__": main() 该方法通过质因数分解高效地计算因数数目,确保在大数据范围内也能快速解决问题。
转载地址:http://hznfk.baihongyu.com/