problem
code
from leetcode
class Solution {
public int numFactoredBinaryTrees(int[] A) {
int MOD = 1_000_000_007;
int N = A.length;
Arrays.sort(A);
long[] dp = new long[N];
Arrays.fill(dp, 1);
Map<Integer, Integer> index = new HashMap();
for (int i = 0; i < N; ++i)
index.put(A[i], i);
for (int i = 0; i < N; ++i)
for (int j = 0; j < i; ++j) {
if (A[i] % A[j] == 0) {
int right = A[i] / A[j];
if (index.containsKey(right)) {
dp[i] = (dp[i] + dp[j] * dp[index.get(right)]) % MOD;
}
}
}
long ans = 0;
for (long x: dp) ans += x;
return (int) (ans % MOD);
}
}
youtube solution