一个数字,偶数位可选的数字为[0,2,4,6,8]共有5位, 奇数位可选的数字为[1,3,5,7]共有4位. 当确定长度后,一个数字有的奇数位和偶数位的个数就确定了。例如n = 7,4位奇数,3位偶数,总的个数为(44) *(53)。
class Solution:
def countGoodNumbers(self, n: int) -> int:
even = n //2
odd = math.ceil(n/2)
Mod = 10**9 + 7
parteven = int( pow(4, even , Mod))
partodd = int(pow(5, odd , Mod))
res = (parteven*partodd) % Mod
return res
#pow(4,3) = 64.0
#4**3 = 64