File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-02/BEESA_MANISH Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments