本帖最后由 tommyyu 于 2023-1-30 18:40 编辑
可以使用预处理出质数使效率提升def prime():
global primes
def is_prime(n):
if n<2: return False
return all(n%i for i in range(2, int(n**0.5+1)))
primes = [i for i in range(int(1e9**0.5+2)) if is_prime(i)]
def count(yinshu, exp_2):
# print(yinshu, exp_2)
if exp_2 == 0: return yinshu // 2 + yinshu % 2
if exp_2 == 1: return 0
s = (yinshu // 2) * (exp_2-1)
s += (yinshu % 2) * (exp_2 // 2)
return s
def count_yinshu(n):
#print(n, end = ' ')
i = 1
yinshu = dict()
try:
while n != 1:
if n < primes[i] and n != 1: return 2
while n % primes[i] == 0:
# print(n)
yinshu[primes[i]] = yinshu.get(primes[i], 0) + 1
n = n // primes[i]
i += 1
except:
yinshu[n] = 1
s = 1
for i in yinshu:
s *= (yinshu[i]+1)
return s
def count_2_exp(n):
exp_2 = 0
while not n % 2:
n //= 2
exp_2 += 1
return exp_2, n
def count_n(n):
exp_2, new_n = count_2_exp(n)
return count(count_yinshu(new_n), exp_2)
input()
n = list(map(int, input().split()))
#import time
#start = time.time()
prime()
for i in n:
print(count_n(i), end = ' ')
#end = time.time()
#print('\n', end - start, sep = '')
|