牛客小白月赛1Iあなたの蛙が帰っています 【卡特兰数】

mac2022-06-30  22

题目链接

https://www.nowcoder.com/acm/contest/85/I

思路 A1 不能第一个出栈 所有的出栈顺序 为第N个卡特兰数 如果A1第一个出栈 那么所有的出栈顺序就是 第(N - 1)个卡特兰数 所以 所有可用的出战顺序 就是 H(N) - H (N - 1) 注意取模

AC代码

#include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cmath> #include <climits> #include <ctime> #include <iostream> #include <algorithm> #include <deque> #include <vector> #include <queue> #include <string> #include <map> #include <stack> #include <set> #include <numeric> #include <sstream> #include <iomanip> #include <limits> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; const double PI = 3.14159265358979323846264338327; const double E = 2.718281828459; const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int maxn = 1e5+ 5; const ll MOD = 998244353; ll ans[maxn] = {1, 1}; ll powerMod(ll x, ll n) { ll res = 1; while (n > 0){ if (n & 1) res = (res * x) % MOD; x = (x * x) % MOD; n >>= 1; } return res; } int main() { for (ll i = 2; i <= maxn; i++) ans[i] = ans[i - 1] * (4 * i - 2) % MOD * powerMod(i + 1, MOD - 2) % MOD; int t; int count = 1; cin >> t; while (t--) { int n; scanf("%d", &n); printf("Case #%d: %lld\n", count++, (ans[n] - ans[n - 1] + 2 * MOD) % MOD); } }

转载于:https://www.cnblogs.com/Dup4/p/9433239.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)