|
| 1 | +// जय हरियाणा ♡ |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | +#ifndef ONLINE_JUDGE |
| 5 | +#include "template.cpp" |
| 6 | +#else |
| 7 | +#define debug(...) |
| 8 | +#define debugArr(...) |
| 9 | +#endif |
| 10 | +#ifdef ONLINE_JUDGE |
| 11 | +#define DISABLE_STACK_SIZE_CHANGE |
| 12 | +#endif |
| 13 | +#ifndef DISABLE_STACK_SIZE_CHANGE |
| 14 | +#include <sys/resource.h> |
| 15 | +#endif |
| 16 | +typedef long long ll; |
| 17 | +const int MOD = 1000000007; |
| 18 | +#define pb push_back |
| 19 | +#define is insert |
| 20 | +#define ff first |
| 21 | +#define ss second |
| 22 | +#define nl "\n" |
| 23 | +#define all(x) (x).begin(), (x).end() |
| 24 | +#define loop(i,a,b) for(ll i=(a);i<(b);i++) |
| 25 | +#define print(x) for(auto it:(x)) cout<<(it)<<" "; cout<<endl; |
| 26 | +ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} |
| 27 | +ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);} |
| 28 | +ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} |
| 29 | +ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} |
| 30 | +ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} |
| 31 | +ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} |
| 32 | +class node { |
| 33 | +public: |
| 34 | + int val; |
| 35 | + node* left; |
| 36 | + node* right; |
| 37 | + node(int val) { |
| 38 | + this->val=val; |
| 39 | + left=nullptr; |
| 40 | + right=nullptr; |
| 41 | + } |
| 42 | +}; |
| 43 | +void printer(node* root) { |
| 44 | + queue<node*>q; |
| 45 | + q.push(root); |
| 46 | + q.push(nullptr); |
| 47 | + while(!q.empty()) { |
| 48 | + node* temp=q.front(); |
| 49 | + q.pop(); |
| 50 | + if(temp==nullptr) { |
| 51 | + cout<<nl; |
| 52 | + if(!q.empty())q.push(nullptr); |
| 53 | + continue; |
| 54 | + } |
| 55 | + cout<<temp->val<<' '; |
| 56 | + if(temp->left!=nullptr)q.push(temp->left); |
| 57 | + if(temp->right!=nullptr)q.push(temp->right); |
| 58 | + } |
| 59 | +} |
| 60 | +int num; |
| 61 | +vector<node*>trees; |
| 62 | +vector<vector<node*>>dp_trees(20); |
| 63 | +void solve(int l,int r,int n) { |
| 64 | + for(auto it:dp_trees[l]) { |
| 65 | + node* root=new node(1); |
| 66 | + root->left=it; |
| 67 | + for(auto i:dp_trees[r]) { |
| 68 | + root->right=i; |
| 69 | + if(n==num)trees.push_back(root); |
| 70 | + dp_trees[n].push_back(root); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +int main() { |
| 76 | + ios_base::sync_with_stdio(false); |
| 77 | + cin.tie(NULL); |
| 78 | +#ifndef DISABLE_STACK_SIZE_CHANGE |
| 79 | + rlimit rlim; |
| 80 | + if (getrlimit(RLIMIT_STACK, &rlim) != 0) { return 1; } |
| 81 | + rlim.rlim_cur = 1024 * 1024 * 1024; |
| 82 | + if (setrlimit(RLIMIT_STACK, &rlim) != 0) { return 2; } |
| 83 | +#endif |
| 84 | + cin>>num; |
| 85 | + vector<int>dp(num+1,0); |
| 86 | + dp[0]=1; |
| 87 | + dp_trees[0].push_back(nullptr); |
| 88 | + loop(i,1,num+1){ |
| 89 | + int ans=0; |
| 90 | + for(int j=0;j<i;j++) { |
| 91 | + int l=j,r=i-j-1; |
| 92 | + solve(l,r,i); |
| 93 | + ans+=dp[l]*dp[r]; |
| 94 | + } |
| 95 | + dp[i]=ans; |
| 96 | + } |
| 97 | + debug(dp); |
| 98 | + for(auto it:trees) { |
| 99 | + printer(it); |
| 100 | + cout<<nl; |
| 101 | + } |
| 102 | +} |
0 commit comments