Codeforces 1139D Steps to One dp

2021年09月16日 阅读数：3

Steps to Onec++

``````#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;
const double eps = 1e-6;
const double PI = acos(-1);

int Power(int a, int b) {
int ans = 1;
while(b) {
if(b & 1) ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod; b >>= 1;
}
return ans;
}
void add(int &a, int b) {
a += b; if(a >= mod) a -= mod;
}

int n, ans, dp[N];
int inv[N];

vector<int> fac[N];
vector<int> cnt[N];

int dfs(int x) {
if(x == 1) return 1;
if(~dp[x]) return dp[x];
dp[x] = 0;
int ret = n / x;
int res = SZ(fac[x]);
add(dp[x], (1ll * inv[n - ret] * n + mod) % mod);
for(int i = 0; i < SZ(fac[x]) - 1; i++) {
add(dp[x], 1ll * inv[n - ret] * cnt[x][i] % mod * dfs(fac[x][i]) % mod);
}
return dp[x];
}

int main() {
memset(dp, -1, sizeof(dp));
inv[1] = 1;
for(int i = 2; i < N; i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for(int i = 1; i < N; i++)
for(int j = i; j < N; j += i)
fac[j].push_back(i);
scanf("%d", &n);
for(int i = 2; i <= n; i++) {
cnt[i].resize(SZ(fac[i]));
for(int j = SZ(fac[i]) - 1; j >= 0; j--) {
cnt[i][j] = n / fac[i][j];
for(int k = j + 1; k < SZ(fac[i]); k++)
if(fac[i][k] % fac[i][j] == 0)
cnt[i][j] -= cnt[i][k];
}
}
for(int i = 1; i <= n; i++)
add(ans, 1ll * inv[n] * dfs(i) % mod);
printf("%d\n", ans);
return 0;
}

/*
*/``````