跳到正文

题解

dyb's分拆uneasy

yangqingjing约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年3月28日
篇幅
约 3 分钟

版本

  1. v1当前

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; long long combination(int a, int b) { if (b < 0 || b > a) return 0; if (b == 0 || b == a) return 1; b = min(b, a - b); long long result = 1; for (int i = 1; i <= b; ++i) { result = result * (a - b + i) / i; } return result; } long long calculateG(int n, int k, vector<vector<long long>>& memo) { if (n < k) return 0; if (k == 1 || n == k) return 1; if (memo[n][k] != -1) return memo[n][k]; long long res = calculateG(n-1, k-1, memo) + calculateG(n-k, k, memo); memo[n][k] = res; return res; } void parseInput(const string& s, char& type, int& n, int& k) { type = s[0]; int left = s.find(’(’); int comma = s.find(’,’); int right = s.find(’)’); n = stoi(s.substr(left+1, comma-left-1)); k = stoi(s.substr(comma+1, right-comma-1)); } int main() { string input; getline(cin, input); char type; int n, k; parseInput(input, type, n, k); long long result = 0; if (type == ‘H’) { result = combination(n-1, k-1); } else if (type == ‘G’) { vector<vector<long long>> memo(n+1, vector<long long>(k+1, -1)); result = calculateG(n, k, memo); } cout << result << endl; return 0; }

原文