-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path048-self-powers.js
More file actions
41 lines (34 loc) · 790 Bytes
/
048-self-powers.js
File metadata and controls
41 lines (34 loc) · 790 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
/**
* Self Powers
* Time Complexity: O(N log N)
* Space Complexity: O(1)
*/
function modPow(base, exp, mod) {
let result = 1n;
let b = BigInt(base) % mod;
let e = BigInt(exp);
while (e > 0n) {
if (e & 1n) result = (result * b) % mod;
b = (b * b) % mod;
e >>= 1n;
}
return result;
};
function processData(input) {
const N = Number(input.trim());
const MOD = 10n ** 10n;
let sum = 0n;
for (let i = 1; i <= N; i++) {
sum = (sum + modPow(i, i, MOD)) % MOD;
}
console.log(sum.toString());
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});