Files
python/GESP/Mkx/jiecheng-simple.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

70 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], al;
int b[N], bl;
void mul(int a[], int &al, int b[], int &bl) {
int t = 0;
int c[N] = {0};
for (int i = 0; i < al; i++)
for (int j = 0; j < bl; j++)
c[i + j] += a[i] * b[j];
al += bl;
for (int i = 0; i < al; i++) {
t += c[i];
a[i] = t % 10;
t /= 10;
}
while (al > 0 && a[al - 1] == 0)
al--;
}
void jiecheng(int x) {
a[0] = 1;
al = 1;
for (int i = 2; i <= x; i++) {
int t = i;
bl = 0;
while (t > 0) {
b[bl++] = t % 10;
t /= 10;
}
mul(a, al, b, bl);
}
}
void add(int a[], int &al, int b[], int &bl) {
int t = 0;
al = max(al, bl);
for (int i = 0; i < al; i++) {
t += a[i] + b[i];
a[i] = t % 10;
t /= 10;
}
if (t)
a[al++] = 1;
while (al > 0 && a[al - 1] == 0)
al--;
}
int main() {
int n;
cin >> n; // 读入n
int sum[N] = {0}; // 存储总和
int suml = 0; // 总和的长度
for (int i = 1; i <= n; i++) {
jiecheng(i); // 计算i的阶乘结果存在a数组中
add(sum, suml, a, al); // 将a数组的结果加到sum数组中
}
// 从高位到低位输出结果
for (int i = suml - 1; i >= 0; i--) {
cout << sum[i];
}
cout << endl;
return 0;
}