-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1009.cpp
73 lines (64 loc) · 1.35 KB
/
P1009.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class BigInt {
private:
vector<int> digits;public:
BigInt(int num = 0)
{
while (num > 0)
{
digits.push_back(num % 10);
num /= 10;
}
if (digits.empty()) digits.push_back(0);
}
BigInt& operator+=(const BigInt& other)
{
int carry = 0, sum = 0;
size_t maxLength = max(digits.size(), other.digits.size());
for (size_t i = 0; i < maxLength || carry; ++i)
{
if (i == digits.size()) digits.push_back(0);
sum = digits[i] + (i < other.digits.size() ? other.digits[i] : 0) + carry;
carry = sum / 10;
digits[i] = sum % 10;
}
return *this;
}
BigInt& operator*=(int num)
{
int carry = 0, prod = 0;
for (size_t i = 0; i < digits.size() || carry; ++i)
{
if (i == digits.size()) digits.push_back(0);
prod = digits[i] * num + carry;
carry = prod / 10;
digits[i] = prod % 10;
}
while (digits.size() > 1 && digits.back() == 0) digits.pop_back();
return *this;
}
friend ostream& operator<<(ostream& os, const BigInt& num)
{
for (auto it = num.digits.rbegin(); it != num.digits.rend(); ++it)
{
os << *it;
}
return os;
}
};
int main() {
int n;
cin >> n;
BigInt sum = 0;
BigInt factorial = 1;
for (int i = 1; i <= n; ++i)
{
factorial *= i;
sum += factorial;
}
cout << sum << endl;
return 0;
}