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

86 lines
2.2 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; // 数组最大长度
const int WIDTH = 4; // 压位宽度每4位十进制数压缩成一个数字
const int BASE = 10000; // 压位后的基数因为是4位所以基数是10000
int a[N], al; // a数组存储结果al为结果的长度
int b[N], bl; // b数组存储乘数bl为乘数的长度
// 字符串转压位数组(逆序存储)
// 例如:"12345" -> [2345, 1]
void str2num(string s, int c[], int &cl) {
cl = 0;
for (int i = s.size() - 1; i >= 0; i -= WIDTH) {
int v = 0;
int st = max(i - WIDTH + 1, 0); // 计算每组起始位置最后一组可能不足4位
for (int j = st; j <= i; j++)
v = v * 10 + (s[j] - '0'); // 将字符转换为数字并组合
c[cl++] = v; // 存储压缩后的数字
}
}
// 高精度乘法函数
void mul(int a[], int &al, int b[], int &bl) {
vector<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 = al + bl + 1; // 结果最大长度为两数长度之和+1
// 处理进位
for (int i = 0; i < al; i++) {
c[i + 1] += c[i] / BASE; // 向高位进位
c[i] %= BASE; // 保留在BASE以内
}
// 将结果转移到a数组
for (int i = 0; i < al; i++)
a[i] = c[i];
// 去除前导零
while (al > 1 && a[al - 1] == 0)
al--;
}
int main() {
int n;
cin >> n; // 输入要计算的阶乘数n
// 初始化结果为1
a[0] = 1;
al = 1;
// 计算阶乘从2开始乘到n
for (int i = 2; i <= n; i++) {
// 将当前数字i转换为压位形式
string num = to_string(i);
str2num(num, b, bl);
// 执行高精度乘法
mul(a, al, b, bl);
}
// 输出结果,注意格式
for (int i = al - 1; i >= 0; i--) {
if (i == al - 1)
printf("%d", a[i]); // 最高位不需要补零
else
printf("%04d", a[i]); // 其余位需要补足4位前导零
}
/*
123 0089 0012
a[0] = 123
a[1] = 89
a[2] = 12
123 0089 0012
*/
return 0;
}