全部评论
我只知道暴力解(没写完哭辽),递归,用一个hash表存已经算出来的值对应的三叉树数目即可...😖
我觉得动态规划吧
蹲一波
一楼白熊的小号...2和3的结果都是对的..没写完TT 求人品嘤嘤嘤.. //include "bits/stdc++.h" //include<unordered_map> using namespace std; long treeNum(int n, unordered_map<int, long>& mymap) { if(n == 0 || n == 1)return 1; long re = 0; for(int i = 0; i <= n - 1; i++){ if(mymap.find(i) == mymap.end()) { mymap[i] = treeNum(i, mymap); } for(int j = 0; j<= n - 1 - i; j++) { int k = n - 1 - i - j; if(mymap.find(j) == mymap.end()) { mymap[j] = treeNum(i, mymap); } if(mymap.find(k) == mymap.end()) { mymap[k] = treeNum(k, mymap); } re += mymap[i] * mymap[j] * mymap[k]; } } mymap[n] = re; return re; } int main(){ int n; cin >> n; unordered_map<int, long>mymap; mymap[0] = 1; mymap[1] = 1; cout << treeNum(n, mymap) % (int)(pow(10, 9) + 7) << '\n&(392)#39;; return 0; }
相关推荐