Skip to content

Instantly share code, notes, and snippets.

@zwang96-dl
Created March 28, 2021 04:23
Show Gist options
  • Select an option

  • Save zwang96-dl/9e49c63074cb3b25a56670a771c0d8d3 to your computer and use it in GitHub Desktop.

Select an option

Save zwang96-dl/9e49c63074cb3b25a56670a771c0d8d3 to your computer and use it in GitHub Desktop.
from functools import lru_cache
class Solution:
def maxNiceDivisors(self, primeFactors):
res = 1
for num_pieces in range(1, primeFactors // 2 + 1):
if primeFactors % num_pieces == 0:
each_piece_cnt = primeFactors // num_pieces
# 均值定理
res = max(res, each_piece_cnt ** num_pieces)
else:
each_piece_cnt = self._helper(num_pieces, primeFactors)
rest = primeFactors - each_piece_cnt * (num_pieces - 1)
temp = each_piece_cnt ** (num_pieces - 1)
temp *= rest
res = max(res, temp)
return res % (10 ** 9 + 7)
@lru_cache(None)
def _helper(self, num_pieces, total):
each_piece_cnt = total // num_pieces
while each_piece_cnt * num_pieces < total:
each_piece_cnt += 1
return each_piece_cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment