823. Binary Trees With Factors
题目描述
Given an array of unique integers, each integer is strictly greater than 1.
We make a binary tree using these integers and each number may be used for any number of times.
Each non-leaf node’s value should be equal to the product of the values of it’s children.
How many binary trees can we make? Return the answer modulo 10 ** 9 + 7.
Example 1:
|
|
Example 2:
|
|
Note:
- 1 <= A.length <= 1000.
- 2 <= A[i] <= 10 ^ 9.
题目大意
给定一数组,求最多能生成多少棵,满足左右子节点相乘等于根节点的树,结果最大为 10 ** 9 + 7
解题思路
动态规划
对A
进行排序。
如果m * n == a
,并且m * m < a, a % m == 0
则a
能生成的树为m * n
如果m != n
则再乘以 2。
代码
|
|