提交时间:2026-01-11 17:51:30
运行 ID: 82345
#include <iostream> #include <cstring> using namespace std; const int MAXLEN = 200; // 50! 位数约65,求和也不会超过200位 struct BigInt { int d[MAXLEN]; int len; BigInt() { memset(d, 0, sizeof(d)); len = 1; } void set(int x) { memset(d, 0, sizeof(d)); len = 0; if (x == 0) { len = 1; return; } while (x > 0) { d[len++] = x % 10; x /= 10; } } void print() { for (int i = len - 1; i >= 0; i--) { cout << d[i]; } } }; // 高精度乘低精度 BigInt multiply(const BigInt &a, int b) { BigInt c; int carry = 0; c.len = 0; // 重置长度 for (int i = 0; i < a.len; i++) { int temp = a.d[i] * b + carry; c.d[c.len++] = temp % 10; carry = temp / 10; } while (carry > 0) { c.d[c.len++] = carry % 10; carry /= 10; } return c; } // 高精度加高精度 BigInt add(const BigInt &a, const BigInt &b) { BigInt c; int carry = 0; int maxlen = max(a.len, b.len); c.len = 0; for (int i = 0; i < maxlen; i++) { int temp = a.d[i] + b.d[i] + carry; c.d[c.len++] = temp % 10; carry = temp / 10; } if (carry > 0) { c.d[c.len++] = carry; } return c; } int main() { int N; cin >> N; BigInt sum; sum.set(0); BigInt fact; fact.set(1); for (int i = 1; i <= N; i++) { if (i > 1) { fact = multiply(fact, i); } sum = add(sum, fact); } sum.print(); cout << endl; return 0; }