提交时间:2024-08-20 23:31:16

运行 ID: 173674

#include <iostream> #include <sstream> #include <vector> #include <string> #include <algorithm> using namespace std; // 自定义高精度乘法函数 string multiply(string a, int b) { int carry = 0; string result; for (int i = a.size() - 1; i >= 0; i--) { int product = (a[i] - '0') * b + carry; carry = product / 10; stringstream ss; ss << (product % 10); result += ss.str(); } while (carry) { stringstream ss; ss << (carry % 10); result += ss.str(); carry /= 10; } reverse(result.begin(), result.end()); return result; } // 自定义高精度除法函数 string divide(string a, int b) { string result; int idx = 0; int temp = a[idx] - '0'; while (temp < b) temp = temp * 10 + (a[++idx] - '0'); while (a.size() > idx) { result += (temp / b) + '0'; temp = (temp % b) * 10 + a[++idx] - '0'; } if (result.length() == 0) return "0"; return result; } // 自定义高精度计算组合数 string combination(int m, int n) { string result = "1"; for (int i = 1; i <= m; i++) { result = multiply(result, n - i + 1); result = divide(result, i); } return result; } int main() { int M, N; cin >> M >> N; // 路径数为C(M+N-2, M-1) string result = combination(M - 1, M + N - 2); cout << result << endl; return 0; }