Skip to content

Commit 29dbfdf

Browse files
committed
added day2 q3 solution
1 parent c2c242a commit 29dbfdf

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
TC - O(N log N)
3+
SC - O(N)
4+
5+
*/
6+
7+
8+
#include <iostream>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
long long MOD = 1000000007;
14+
const int MAXN = 1000005;
15+
int phi[MAXN];
16+
long long f[MAXN];
17+
18+
void sieve(int n) {
19+
for (int i = 0; i <= n; i++) phi[i] = i;
20+
for (int i = 2; i <= n; i++) {
21+
if (phi[i] == i) {
22+
for (int j = i; j <= n; j += i)
23+
phi[j] -= phi[j] / i;
24+
}
25+
}
26+
for (int d = 1; d <= n; d++) {
27+
for (int j = d; j <= n; j += d) {
28+
f[j] = (f[j] + 1LL * d * phi[j / d]) % MOD;
29+
}
30+
}
31+
}
32+
33+
int main() {
34+
int N;
35+
cin >> N;
36+
sieve(N);
37+
38+
long long total_sum = 0;
39+
for (int d = 1; d <= N; d++) {
40+
long long current_f_sum = 0;
41+
for (int k = d; k <= N; k += d) {
42+
current_f_sum = (current_f_sum + f[k]) % MOD;
43+
}
44+
long long term = (1LL * phi[d] * ((current_f_sum * current_f_sum) % MOD)) % MOD;
45+
total_sum = (total_sum + term) % MOD;
46+
}
47+
48+
cout << total_sum << endl;
49+
return 0;
50+
}

0 commit comments

Comments
 (0)