*题目地址
目的是求S=1!+2!+3!+⋯+n!,n小于等于50
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdio>
#include <queue>
#include <map>
#define endl '\n'
#define Buff std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define inf 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a));
#define memc(a, b) memcpy(a, b, sizeof(b));
#define fi(a, n, b) fill(a, a + n, b);
#define ll long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1000000007;
int n, m;
vector<int> add(vector<int> A, vector<int> B)
{
vector<int> C;
int t = 0;
reverse(A.begin(), A.end()); //倒过来进行运算
reverse(B.begin(), B.end());
for (int i = 0; i < A.size() || i < B.size(); i++)
{
if (i < A.size())
t += A[i];
if (i < B.size())
t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t)
C.push_back(1);
reverse(C.begin(), C.end());
return C;
}
vector<int> mul(vector<int> A, int b)
{
int t = 0;
vector<int> C;
for (int i = A.size() - 1; i >= 0; i--)
{
t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (t)
{
C.push_back(t % 10);
t /= 10;
}
reverse(C.begin(), C.end());//因为前面是倒着算的,所以反过来
return C;
}
int main()
{
cin >> n;
vector<int> C, D;
C.push_back(1);
D.push_back(1);
for (int i = 2; i <= n; i++) //遍历一边
{
C.clear();
C.push_back(1);
for (int j = 2; j <= i; j++)
{
C = mul(C, j);
}
D = add(C, D);
}
// for (auto it : D)
// cout << it;
for (vector<int>::iterator it = D.begin(); it != D.end(); it++)
cout << *it;
return 0;
}