Concise Python solution which beats 95%
The idea is very simple. We can define subproblem dp[i] as the number of different trees rooted A[i]. \nHere is the recurision formula: dp[i] = sum(dp[x] * dp[y
The idea is very simple. We can define subproblem dp[i] as the number of different trees rooted A[i]. \nHere is the recurision formula: dp[i] = sum(dp[x] * dp[y] | x * y = i). To reduce the complexity we can only compute where x <= y, and plus x > y on the time x < y. \n``` python\n    def numFactoredBinaryTrees(self, A):\n        A.sort()\n        N = len(A)\n        dp = {A[0]: 1}\n        for i in range(1, N):\n            dp[A[i]] = 1\n            for j in range(i):\n                div, mod = divmod(A[i], A[j])\n                if div < A[j]: break\n                if mod != 0: continue\n                if div in dp: \n                    mul = dp[div] * dp[A[j]]\n                    dp[A[i]] += mul if div == A[j] else 2 * mul\n                    \n        return sum(dp.values()) % (10 ** 9 + 7)\n```
[]