from collections import defaultdict
def factor_sieve(n):
"""
返回一个list,表示小于等于n的所有数的最小质因子
"""
sieve = [0] * (n + 1)
for i in range(2, n + 1):
if sieve[i] == 0: # i是质数
for j in range(i * i, n + 1, i):
if sieve[j] == 0:
sieve[j] = i
return sieve
def get_factor_counts(sieve, x):
"""
返回x的所有质因子的出现次数的和,如20=2*2*5,返回4+1=5
"""
counts = defaultdict(int)
while x > 1:
prime = sieve[x]
if prime == 0:
prime = x
counts[prime] += 1
x //= prime
return sum(counts.values())
n = int(input())
a = list(map(int, input().split()))
sieve = factor_sieve(max(a))
# 预处理每个数在各个质因子下的出现次数,放到二维数组counts中
counts = [[0] * len(sieve) for _ in range(n)]
for i in range(n):
for j in range(len(sieve)):
if a[i] % (j+1) == 0:
counts[i][j] = get_factor_counts(sieve, a[i] // (j+1))
# 计算每个左端点和右端点的区间公因数及其总和,找到最大值
best_l, best_r, best_value = -1, -1, 0
for l in range(n):
prod_counts = [0] * len(sieve)
for r in range(l, n):
for j in range(len(sieve)):
prod_counts[j] += counts[r][j]
# 计算当前区间的公因数及其总和
factor = 1
sum_of_divisors = 0
for j in range(len(sieve)):
if prod_counts[j] == r - l + 1:
factor *= (j+1)
sum_of_divisors += prod_counts[j] * (j+1)
value = factor + sum_of_divisors
# 更新最大值
if value > best_value:
best_l, best_r, best_value = l, r, value
print(best_l+1, best_r+1)
print(best_value)
|