69 lines
967 B
C++
69 lines
967 B
C++
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 1e5 + 10;
|
|
const int BASE = 10000; // 基数为10000
|
|
|
|
int a[N], al;
|
|
|
|
/*
|
|
3 3 6
|
|
100
|
|
|
|
答案:
|
|
6
|
|
9
|
|
648
|
|
*/
|
|
|
|
// 高精度乘以低精度
|
|
void mul(int a[], int &al, int b) {
|
|
int t = 0;
|
|
for (int i = 0; i < al; i++) {
|
|
t += a[i] * b;
|
|
a[i] = t % BASE;
|
|
t /= BASE;
|
|
}
|
|
|
|
if (t)
|
|
a[al++] = t;
|
|
|
|
while (al > 1 && a[al - 1] == 0)
|
|
al--;
|
|
}
|
|
|
|
// 计算压位数组所表示数字的各位之和
|
|
int digitSum() {
|
|
int sum = 0;
|
|
for (int i = 0; i < al; i++) {
|
|
int x = a[i];
|
|
while (x) {
|
|
sum += x % 10;
|
|
x /= 10;
|
|
}
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
int solve(int n) {
|
|
// 初始化为1
|
|
al = 1;
|
|
a[0] = 1;
|
|
|
|
// 计算阶乘
|
|
for (int i = 2; i <= n; i++)
|
|
mul(a, al, i);
|
|
|
|
return digitSum();
|
|
}
|
|
|
|
int main() {
|
|
int T;
|
|
cin >> T;
|
|
while (T--) {
|
|
int n;
|
|
cin >> n;
|
|
cout << solve(n) << endl;
|
|
}
|
|
return 0;
|
|
} |