This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
typedef pair<ll, ll> pi;
#define var first
#define cnt second
ll pwk[1000001], phi[1000001], low[1000001], inv[1000001];
ll pow(ll x, ll p) {
ll ret = 1, piv = x % mod;
while (p) {
if (p & 1) ret *= piv;
piv *= piv;
ret %= mod;
piv %= mod;
p >>= 1;
}
return ret;
}
ll revmod(ll x) { return pow(x, mod - 2) % mod; }
ll r2;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
ll n, k; cin >> n >> k;
phi[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = i; j <= n; j += i) {
if (!low[j]) low[j] = i;
}
phi[i] = i;
for (int j = i; j != 1; ) {
int p = low[j];
while (j % p == 0) {
j /= p;
}
phi[i] = (1ll * phi[i] * (p - 1)) / p;
}
}
pwk[0] = 1;
for (int i = 1; i <= 1000000; i++) {
pwk[i] = (pwk[i - 1] * k) % mod;
inv[i] = revmod(i);
}
r2 = revmod(2);
ll ans = 2;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
ll diff = (((pwk[i] * phi[j / i]) % mod) * inv[j])%mod;
ans = (ans + diff) % mod;
}
}
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) ans = (ans + (1 * (k + 1) * pwk[i / 2] % mod) * ((mod + 1) / 2) % mod)%mod;
else ans = (ans + pwk[i / 2 + 1])%mod;
}
ans *= (mod + 1) / 2;
ans %= mod;
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |