模拟与高精度 P1009 阶乘之和
题目
https://www.luogu.com.cn/problem/P1009
代码
#include<iostream> #include<string> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> using namespace std; #define max 2010 vector<int> mul(vector<int> a, int b) { vector<int>p; int t = 0; for (int i = 0; i < a.size(); i++) { p.push_back((t + a[i] * b) % 10); t = (t + a[i] * b) / 10; } while (t != 0) { p.push_back(t % 10); t /= 10; } return p; } vector<int>add(vector<int>a, vector<int>b) { vector<int>p,tmpa,tmpb; tmpa = a; tmpb = b; int aa = tmpa.size(), bb = tmpb.size(); if (aa > bb) for (int i = 0; i < aa - bb;i++)tmpb.push_back(0); if (aa < bb) for (int i = 0; i < bb-aa; i++)tmpa.push_back(0); int t = 0; for (int i = 0; i < tmpa.size(); i++) { p.push_back((t + tmpa[i] + tmpb[i]) % 10); t = (t + tmpa[i] + tmpb[i]) / 10; } if (t != 0) p.push_back(t); return p; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int t; cin >> t; vector<int>p,answer; p.push_back(1); answer.push_back(1); for (int i = 2; i <= t; i++) { p = mul(p, i); answer = add(answer, p); } bool ok = false; for (int i = answer.size() - 1; i >= 0; i--) { if (answer[i] != 0)ok = true; if (ok)cout << answer[i]; } if (!ok)cout << 0; }