From 2ed700228a253fb4e97c880698d48a2a641d6df0 Mon Sep 17 00:00:00 2001 From: chunnusingh090 <73756136+chunnusingh090@users.noreply.github.com> Date: Sat, 31 Oct 2020 23:54:55 +0530 Subject: [PATCH 1/2] Create catalan numbers --- Programs/C,C++/catalan numbers | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Programs/C,C++/catalan numbers diff --git a/Programs/C,C++/catalan numbers b/Programs/C,C++/catalan numbers new file mode 100644 index 0000000..e639395 --- /dev/null +++ b/Programs/C,C++/catalan numbers @@ -0,0 +1,41 @@ +#include +using namespace std; +#define int long long + +// n'th Catalan number Cn = choose(2 * n, n) / (n + 1) +// https://www.geeksforgeeks.org/applications-of-catalan-numbers/ + +const int N = 105; +const int mod = 1e9 + 7; + +int dp[N]; + +int catalan(int n) { + int &mem = dp[n]; + if (mem != -1) + return mem; + + if (n <= 1) + return mem = 1; + + int res = 0; + for (int i = 0; i < n; i++) + res = (res + catalan(i) * catalan(n - 1 - i)) % mod; + return mem = res; +} + +signed main() { + ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + memset(dp, -1, sizeof(dp)); + + int T = 1; + cin >> T; + while (T--) { + int n; + cin >> n; + cout << catalan(n) << '\n'; + } + + return 0; +} From c223fb2ccf1d4532926b1c4d58dac45a983eb03c Mon Sep 17 00:00:00 2001 From: chunnusingh090 <73756136+chunnusingh090@users.noreply.github.com> Date: Sun, 1 Nov 2020 16:12:12 +0530 Subject: [PATCH 2/2] Rename catalan numbers to catalan numbers.cpp --- Programs/C,C++/{catalan numbers => catalan numbers.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Programs/C,C++/{catalan numbers => catalan numbers.cpp} (100%) diff --git a/Programs/C,C++/catalan numbers b/Programs/C,C++/catalan numbers.cpp similarity index 100% rename from Programs/C,C++/catalan numbers rename to Programs/C,C++/catalan numbers.cpp