# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
474577 | egod1537 | 팔찌 (kriii4_V) | C++14 | 1094 ms | 16076 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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[2000001];
ll pow(ll x, ll cnt) {
if (cnt == 0) return 1;
ll mid = pow(x, cnt >> 1)%mod;
return (((mid * mid)%mod) * (cnt % 2 ? x : 1)) % mod;
}
ll revmod(ll x) { return pow(x, mod - 2) % mod; }
vector<pi> getfactor(ll num) {
vector<pi> ans;
for (ll i = 2; i * i <= num; i++) {
pi p = pi(i, 0);
while (num % i == 0) {
p.cnt++;
num /= i;
}
if (p.cnt > 0) ans.push_back(p);
}
if (num > 1) ans.push_back(pi(num, 1));
return ans;
}
ll eulerp(vector<pi>& factor, vector<vector<ll>>& psum) {
//return 1;
ll ans = 1;
for (int i = 0; i < factor.size(); i++) {
pi& p = factor[i];
if (p.cnt == 0) continue;
ans = (ans * ((psum[i][p.cnt-1] * (p.var - 1)) % mod)) % mod;
}
return ans;
}
ll dfs(int num, ll k, ll now, vector<pi>& arr, vector<pi>& dq, vector<vector<ll>>& psum) {
if (num == arr.size()) return eulerp(dq, psum)*pwk[now];
ll ans = 0;
for (int i = 0; i <= arr[num].cnt; i++) {
dq[num].cnt = arr[num].cnt - i;
ans = (ans + dfs(num+1, k, now*psum[num][i], arr, dq, psum))%mod;
}
return ans;
}
ll r2;
ll func(ll n, ll k) {
//if (n % 10000 == 0) cout << n << "\n";
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;
}
vector<pi> dq(fact.begin(), fact.end());
ans = dfs(0, k, 1, fact, dq, 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);
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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |