Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import sys
- sys.setrecursionlimit(1500)
- dp = {}
- def getDivisorCount(n):
- rootN = int(math.sqrt(n))
- divisors = []
- for num in range(1, rootN + 1):
- if(n % num == 0):
- a = int(n / num)
- # check for perfect square
- if a * a == n:
- divisors.append(a)
- else:
- divisors.append(num)
- divisors.append(a)
- # print(divisors)
- return len(divisors)
- def validPermutation(n):
- if(n == 1):
- return 1
- if(n == 2):
- return 1
- if(n == 3):
- return 3
- ans = 0
- if n - 1 in dp:
- ans += dp[n - 1]
- else:
- dp[n - 1] = (n - 1) * validPermutation(n - 1)
- ans += dp[n - 1]
- if n - 2 in dp:
- ans += dp[n - 2]
- else:
- dp[n - 2] = (n - 2) * validPermutation(n - 2)
- ans += dp[n - 2]
- dp[n] = ans
- return ans
- t = int(input())
- for _ in range(t):
- n = int(input())
- divisorCount = getDivisorCount(n)
- print(validPermutation(divisorCount))
Add Comment
Please, Sign In to add comment