链接:
823. 带因子的二叉树
题意:
用给的数字建二叉树,要求父节点是子节点的乘积
解:
乐了
1500ms+30MB //注释版120ms+18MB
实际代码:文章来源:https://www.toymoban.com/news/detail-681522.html
#include<bits/stdc++.h>
using namespace std;
static constexpr int Mod=1E9+7;
int numFactoredBinaryTrees(vector<int>& arr)
{
map<int,int>mp;
for(auto ar:arr) mp[ar]=1;
//set<int>st(arr.begin(),arr.end());
//int lg=st.size();
long long ret=0;
map<int,long long>dp;
//for(auto s1:st)
for(auto mp1:mp)
{
dp[mp1.first]=1;
for(auto mp2:mp)
{
if(mp2.first==mp1.first) break;
if(mp1.first%mp2.first==0 && mp[mp1.first/mp2.first]>0)
{
//cout<<s2<<"&"<<s1/s2<<endl;
dp[mp1.first]+=dp[mp2.first]*dp[mp1.first/mp2.first];
dp[mp1.first]%=Mod;
}
}
//cout<<s1<<" "<<dp[s1]<<endl;
ret+=dp[mp1.first];
ret%=Mod;
}
return ret;
}
/*
int numFactoredBinaryTrees(vector<int>& arr)
{
map<int,int>mp;
for(auto ar:arr) mp[ar]=1;
set<int>st(arr.begin(),arr.end());
int lg=st.size();
long long ret=0;
map<int,long long>dp;
for(auto s1:st)
{
dp[s1]=1;
for(auto s2:st)
{
if(s2==s1) break;
if(s1%s2==0 && mp[s1/s2]>0)
{
//cout<<s2<<"&"<<s1/s2<<endl;
if(s2==s1/s2) dp[s1]+=dp[s2]*dp[s2];
else dp[s1]+=dp[s2]*dp[s1/s2];
dp[s1]%=Mod;
}
}
//cout<<s1<<" "<<dp[s1]<<endl;
ret+=dp[s1];
ret%=Mod;
}
return ret;
}*/
int main()
{
vector<int>in;int temp;
while(cin>>temp)in.push_back(temp);
int ans=numFactoredBinaryTrees(in);
cout<<ans<<endl;
return ans;
}
限制:文章来源地址https://www.toymoban.com/news/detail-681522.html
1 <= arr.length <= 1000
2 <= arr[i] <= 109
-
arr
中的所有值 互不相同
到了这里,关于2023-08-29力扣每日一题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!