from collections import defaultdict
import math
res = {}
def to_prime(n):
global res
if n in res:
return res[n]
res = defaultdict(int)
for i in range(2, n+1):
while n % i == 0:
res[i] += 1
n //= i
return res
CACHE = {}
def get_number_length(number):
return int(math.log10(number)) + 1
def get_nth_number(number, n):
return (number // 10**n) % 10
GLOBAL_COUNTER = 0
GLOBAL_COUNTER2 = 0
def calculate_posibilities_with_limit(primes, limit, can_be_zero=True):
global GLOBAL_COUNTER
GLOBAL_COUNTER += 1
if sum(primes.values()) > get_number_length(limit):
return 0
if sum(primes.values()) == 0 and get_number_length(limit) == 1:
if limit == 0 and can_be_zero:
return 1
if limit == 0 and not can_be_zero:
return 0
return 1
if get_number_length(limit) == 1:
return 1 if limit >= [p for p, v in primes.items() if v == 1][0] else 0
# if sum(primes.values()) == 0 and limit == 0:
# return 1
limit_str = str(limit)
# print(primes, limit)
posibilities = 0
if can_be_zero:
posibilities = calculate_posibilities(primes, get_number_length(limit)-1, False)
if int(limit_str[0]) == 1:
if limit_str[1] != '0':
posibilities += calculate_posibilities_with_limit(primes, int(limit_str[1:] or '0'), False)
else:
posibilities += calculate_posibilities(primes, get_number_length(limit)-1, True)
for p, counter in primes.items():
if counter == 0:
continue
if counter > 0 and p < int(limit_str[0]):
primes_copy = primes.copy()
primes_copy[p] -= 1
posibilities += calculate_posibilities(primes_copy, get_number_length(limit)-1, True)
elif p > int(limit_str[0]):
continue
elif p == int(limit_str[0]) and counter > 0:
primes_copy = primes.copy()
primes_copy[p] -= 1
if limit_str[1] != '0':
posibilities += calculate_posibilities_with_limit(primes_copy, int(limit_str[1:] or '0'), False)
# print(primes, posibilities, limit)
return posibilities
BINOMIAL_CACHE = {}
def binomial(n, k):
if (n, k) in BINOMIAL_CACHE:
return BINOMIAL_CACHE[(n, k)]
if k == 0 or k == n:
return 1
if k == 1:
return n
res = binomial(n-1, k-1) + binomial(n-1, k)
BINOMIAL_CACHE[(n, k)] = res
return res
def calculate_posibilities(primes, max_numbers, exact=False):
if (*sorted(primes.values()), max_numbers, exact) in CACHE:
return CACHE[(*sorted(primes.values()), max_numbers, exact)]
global GLOBAL_COUNTER2
GLOBAL_COUNTER2 += 1
posibilities = 0
start = max_numbers if exact else sum(primes.values())
if not exact and sum(primes.values()) == 0:
return max_numbers
if start < sum(primes.values()):
return 0
for i in range(start, max_numbers+1):
used = 0
tmp = 1
for j in range(2, 10):
tmp *= binomial(i - used, primes.get(j, 0))
used += primes.get(j, 0)
posibilities += tmp
# print(time.perf_counter() - t_start)
CACHE[(*sorted(primes.values()), max_numbers, exact)] = posibilities
return posibilities
results = [0] * 10
p2 = 0
limit = 1e18
while True:
p3 = 0
total = 2**p2
if total > limit:
break
res[total] = {2: p2}
while True:
p5 = 0
total = 2**p2 * 3**p3
if total > limit:
break
res[total] = {2: p2, 3: p3}
while True:
p7 = 0
total = 2**p2 * 3**p3 * 5**p5
if total > limit:
break
res[total] = {2: p2, 3: p3, 5: p5}
while True:
total = 2**p2 * 3**p3 * 5**p5 * 7**p7
if total > limit:
break
res[total] = {2: p2, 3: p3, 5: p5, 7: p7}
p7 += 1
p5 += 1
p3 += 1
p2 += 1
values_by_numbers = defaultdict(list)
for r in res:
tmp = str(r)
values_by_numbers[(tmp.count('0'), tmp.count('2'), tmp.count('3'),
tmp.count('4'), tmp.count('5'), tmp.count('6'),
tmp.count('7'), tmp.count('8'), tmp.count('9'),
)].append(r)
import time
def find_posibilites(number, max_value, test=False):
primes = to_prime(number)
new_numbers = []
posibilities = 0
counter = 0
primes_counter = [0]*10
# times = [time.perf_counter()]
for i8 in range(0, primes.get(2, 0)//3 + 1):
i2__ = primes.get(2, 0) - i8 * 3
primes_counter[8] = i8
for i4 in range(0, i2__//2 + 1):
primes_counter[4] = i4
i2_ = i2__ - i4 * 2
for i6 in range(0, min(i2_, primes.get(3, 0))+1):
primes_counter[6] = i6
i3_ = primes.get(3, 0) - i6
# i2 = i2_ - i6
primes_counter[2] = i2_ - i6
for i9 in range(0, i3_//2 + 1):
# i3 = i3_ - i9 * 2
primes_counter[3] = i3_ - i9 * 2
primes_counter[9] = i9
primes_counter[7] = primes.get(7, 0)
primes_counter[5] = primes.get(5, 0)
counter +=1
posibilities += calculate_posibilities_with_limit(
{v: primes_counter[v] for v in range(2, 10) if primes_counter[v] > 0}, max_value)
tmp = values_by_numbers[tuple(primes_counter[1:])]
tmp = [x for x in tmp if x <= max_value and x != number]
new_numbers.extend(tmp)
# times.append(time.perf_counter())
return new_numbers, posibilities
def run(max_number, test):
all_times = []
import time
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# numbers = [2]
full_res = [0]*10
counter = 0
for number in numbers:
result = 0
queue = [number]
while queue:
counter += 1
next_number = queue.pop()
# print('--', next_number)
t_start = time.perf_counter()
new_numbers, posibilities = find_posibilites(next_number, max_number, test)
all_times.append(time.perf_counter() - t_start)
# print(new_numbers, posibilities)
queue.extend(new_numbers)
result += posibilities
counter += 1
# print(result, end=' ')
full_res[number] = result
# print(number, counter)
# print(counter)
# print(GLOBAL_COUNTER)
# print(GLOBAL_COUNTER2)
full_res[0] = max_number - sum(full_res)
print(' '.join([str(x) for x in full_res]), '')
# print(sum(all_times)/len(all_times))
# print(np.std(all_times))
# print(np.max(all_times))
# print(np.min(all_times))
# print()
# run(101, False)
# run(101, True)
t = int(input())
tests = input().split()
for test in tests:
run(int(test), False)
# for i in range(t):
# print(len(res))
# run(100, False)
# run(1_000_000_000_000_000_000, False)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | from collections import defaultdict import math res = {} def to_prime(n): global res if n in res: return res[n] res = defaultdict(int) for i in range(2, n+1): while n % i == 0: res[i] += 1 n //= i return res CACHE = {} def get_number_length(number): return int(math.log10(number)) + 1 def get_nth_number(number, n): return (number // 10**n) % 10 GLOBAL_COUNTER = 0 GLOBAL_COUNTER2 = 0 def calculate_posibilities_with_limit(primes, limit, can_be_zero=True): global GLOBAL_COUNTER GLOBAL_COUNTER += 1 if sum(primes.values()) > get_number_length(limit): return 0 if sum(primes.values()) == 0 and get_number_length(limit) == 1: if limit == 0 and can_be_zero: return 1 if limit == 0 and not can_be_zero: return 0 return 1 if get_number_length(limit) == 1: return 1 if limit >= [p for p, v in primes.items() if v == 1][0] else 0 # if sum(primes.values()) == 0 and limit == 0: # return 1 limit_str = str(limit) # print(primes, limit) posibilities = 0 if can_be_zero: posibilities = calculate_posibilities(primes, get_number_length(limit)-1, False) if int(limit_str[0]) == 1: if limit_str[1] != '0': posibilities += calculate_posibilities_with_limit(primes, int(limit_str[1:] or '0'), False) else: posibilities += calculate_posibilities(primes, get_number_length(limit)-1, True) for p, counter in primes.items(): if counter == 0: continue if counter > 0 and p < int(limit_str[0]): primes_copy = primes.copy() primes_copy[p] -= 1 posibilities += calculate_posibilities(primes_copy, get_number_length(limit)-1, True) elif p > int(limit_str[0]): continue elif p == int(limit_str[0]) and counter > 0: primes_copy = primes.copy() primes_copy[p] -= 1 if limit_str[1] != '0': posibilities += calculate_posibilities_with_limit(primes_copy, int(limit_str[1:] or '0'), False) # print(primes, posibilities, limit) return posibilities BINOMIAL_CACHE = {} def binomial(n, k): if (n, k) in BINOMIAL_CACHE: return BINOMIAL_CACHE[(n, k)] if k == 0 or k == n: return 1 if k == 1: return n res = binomial(n-1, k-1) + binomial(n-1, k) BINOMIAL_CACHE[(n, k)] = res return res def calculate_posibilities(primes, max_numbers, exact=False): if (*sorted(primes.values()), max_numbers, exact) in CACHE: return CACHE[(*sorted(primes.values()), max_numbers, exact)] global GLOBAL_COUNTER2 GLOBAL_COUNTER2 += 1 posibilities = 0 start = max_numbers if exact else sum(primes.values()) if not exact and sum(primes.values()) == 0: return max_numbers if start < sum(primes.values()): return 0 for i in range(start, max_numbers+1): used = 0 tmp = 1 for j in range(2, 10): tmp *= binomial(i - used, primes.get(j, 0)) used += primes.get(j, 0) posibilities += tmp # print(time.perf_counter() - t_start) CACHE[(*sorted(primes.values()), max_numbers, exact)] = posibilities return posibilities results = [0] * 10 p2 = 0 limit = 1e18 while True: p3 = 0 total = 2**p2 if total > limit: break res[total] = {2: p2} while True: p5 = 0 total = 2**p2 * 3**p3 if total > limit: break res[total] = {2: p2, 3: p3} while True: p7 = 0 total = 2**p2 * 3**p3 * 5**p5 if total > limit: break res[total] = {2: p2, 3: p3, 5: p5} while True: total = 2**p2 * 3**p3 * 5**p5 * 7**p7 if total > limit: break res[total] = {2: p2, 3: p3, 5: p5, 7: p7} p7 += 1 p5 += 1 p3 += 1 p2 += 1 values_by_numbers = defaultdict(list) for r in res: tmp = str(r) values_by_numbers[(tmp.count('0'), tmp.count('2'), tmp.count('3'), tmp.count('4'), tmp.count('5'), tmp.count('6'), tmp.count('7'), tmp.count('8'), tmp.count('9'), )].append(r) import time def find_posibilites(number, max_value, test=False): primes = to_prime(number) new_numbers = [] posibilities = 0 counter = 0 primes_counter = [0]*10 # times = [time.perf_counter()] for i8 in range(0, primes.get(2, 0)//3 + 1): i2__ = primes.get(2, 0) - i8 * 3 primes_counter[8] = i8 for i4 in range(0, i2__//2 + 1): primes_counter[4] = i4 i2_ = i2__ - i4 * 2 for i6 in range(0, min(i2_, primes.get(3, 0))+1): primes_counter[6] = i6 i3_ = primes.get(3, 0) - i6 # i2 = i2_ - i6 primes_counter[2] = i2_ - i6 for i9 in range(0, i3_//2 + 1): # i3 = i3_ - i9 * 2 primes_counter[3] = i3_ - i9 * 2 primes_counter[9] = i9 primes_counter[7] = primes.get(7, 0) primes_counter[5] = primes.get(5, 0) counter +=1 posibilities += calculate_posibilities_with_limit( {v: primes_counter[v] for v in range(2, 10) if primes_counter[v] > 0}, max_value) tmp = values_by_numbers[tuple(primes_counter[1:])] tmp = [x for x in tmp if x <= max_value and x != number] new_numbers.extend(tmp) # times.append(time.perf_counter()) return new_numbers, posibilities def run(max_number, test): all_times = [] import time numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] # numbers = [2] full_res = [0]*10 counter = 0 for number in numbers: result = 0 queue = [number] while queue: counter += 1 next_number = queue.pop() # print('--', next_number) t_start = time.perf_counter() new_numbers, posibilities = find_posibilites(next_number, max_number, test) all_times.append(time.perf_counter() - t_start) # print(new_numbers, posibilities) queue.extend(new_numbers) result += posibilities counter += 1 # print(result, end=' ') full_res[number] = result # print(number, counter) # print(counter) # print(GLOBAL_COUNTER) # print(GLOBAL_COUNTER2) full_res[0] = max_number - sum(full_res) print(' '.join([str(x) for x in full_res]), '') # print(sum(all_times)/len(all_times)) # print(np.std(all_times)) # print(np.max(all_times)) # print(np.min(all_times)) # print() # run(101, False) # run(101, True) t = int(input()) tests = input().split() for test in tests: run(int(test), False) # for i in range(t): # print(len(res)) # run(100, False) # run(1_000_000_000_000_000_000, False) |
English