1 条题解
-
0
利用杨辉三角
递推模式
#include <bits/stdc++.h> using namespace std; const long long MOD = 998'244'353; long long n, k, C[1005][1005]; int main() { cin >> n >> k; for (int i = 0; i <= n; i++) for (int j = 0; j <= i; j++) if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; cout << C[n][k] << "\n"; return 0; }
递归模式
#include <bits/stdc++.h> using namespace std; int C(int x, int y); int n, m; const int mod = 998244353; int a[1005][1005]; //定义在全局自动初始化为全0了 int C(int x, int y) { if (a[x][y] != 0) return a[x][y]; if (y == 0 || y == x) return a[x][y] = 1; return a[x][y] = (C(x - 1, y) + C(x - 1, y - 1)) % mod; } int main() { cin >> n >> m; cout << C(n, m); return 0; }
除法用逆元处理
#include <bits/stdc++.h> using namespace std; const long long MOD = 998'244'353; long long inv[1005]; long long n, k, ans; int main() { inv[1] = 1; for (int i = 2; i <= 1000; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD; cin >> n >> k; ans = 1; for (int i = n; i >= n - k + 1; i--) ans = (ans * i) % MOD; for (int i = 1; i <= k; i++) ans = (ans * inv[i]) % MOD; cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 1114
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 62
- 已通过
- 29
- 上传者