-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48Self_powers.cpp
More file actions
44 lines (38 loc) · 887 Bytes
/
Copy path48Self_powers.cpp
File metadata and controls
44 lines (38 loc) · 887 Bytes
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
/*
Key Observation
We only need the LAST 10 DIGITS of the result.
Instead of computing huge numbers like i^i (which can have millions
of digits), we compute everything modulo 10^10.
Why?
Because:
last k digits of a number = number % (10^k)
So for last 10 digits:
MOD = 10^10
Core Idea
For each i from 1 to n:
value = (i^i) % MOD
Add it to answer:
ans = (ans + value) % MOD
*/
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 10000000000LL;
long long modpow(long long a, long long b) {
long long res = 1;
a %= MOD;
while (b) {
if (b & 1) res = (__int128)res * a % MOD;
a = (__int128)a * a % MOD;
b >>= 1;
}
return res;
}
int main() {
long long n;
cin >> n;
long long ans = 0;
for (long long i = 1; i <= n; i++) {
ans = (ans + modpow(i, i)) % MOD;
}
cout << ans << endl;
}