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>
#include <unordered_map>
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[2000001], phi[1000001], low[1000001];
bitset<1000001> vit;
vector<ll> prime;
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; }
vector<pi> getfactor(ll num) {
unordered_map<ll, ll> mp;
while (num > 1)
mp[low[num]]++, num /= low[num];
vector<pi> ans;
for (auto& p : mp) ans.push_back(p);
return ans;
}
ll dfs(int num, ll n, ll k, ll now, vector<pi>& arr, vector<vector<ll>>& psum) {
if (num == arr.size()) return phi[n/now] * pwk[now];
ll ans = 0;
for (int i = 0; i <= arr[num].cnt; i++)
ans = (ans + dfs(num + 1, n, k, now * psum[num][i], arr, psum)) % mod;
return ans;
}
ll r2;
ll func(ll n, ll k) {
ll ans = 0;
vector<pi> fact = getfactor(n);
vector<vector<ll>> psum(fact.size());
for (int i = 0; i < fact.size(); i++) {
psum[i].resize(fact[i].cnt + 1);
psum[i][0] = 1;
for (int j = 1; j <= fact[i].cnt; j++)
psum[i][j] = psum[i][j - 1] * fact[i].var;
}
ans = dfs(0, n, k, 1, fact, psum);
if (n % 2) ans = (ans + (n * pwk[n / 2 + 1]) % mod) % mod;
else {
ll pw = (pwk[n / 2] + pwk[n / 2 + 1]) % mod;
ans = (ans + (((n * pw) % mod) * r2) % mod) % mod;
}
return (ans * revmod(2 * n)) % mod;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
phi[1] = 1;
for (int i = 2; i <= 1000000; i++) {
if (!vit[i])
for (int j = i; j <= 1000000; j += i) {
if (!low[j])low[j] = i;
vit[j] = true;
}
phi[i] = i;
for (int j = i; j != 1;) {
ll p = low[j];
while (j % p == 0) j /= p;
phi[i] = (phi[i] * (p - 1)) / p;
}
}
ll n, k; cin >> n >> k;
pwk[0] = 1;
for (int i = 1; i <= 2000000; i++) pwk[i] = (pwk[i - 1] * k) % mod;
r2 = revmod(2);
ll ans = 1;
for (int i = 1; i <= n; i++) ans = (ans + func(i, k)) % mod;
cout << ans;
return 0;
}
Compilation message (stderr)
V.cpp: In function 'll dfs(int, ll, ll, ll, std::vector<std::pair<long long int, long long int> >&, std::vector<std::vector<long long int> >&)':
V.cpp:41:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if (num == arr.size()) return phi[n/now] * pwk[now];
| ~~~~^~~~~~~~~~~~~
V.cpp: In function 'll func(ll, ll)':
V.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < fact.size(); i++) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |