1. 程式人生 > >cf 1139D - Steps to One

cf 1139D - Steps to One

http turn pow type ORC class amp space ons

題目鏈接:https://codeforces.com/contest/1139/problem/D

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 100;
ll mu[maxn];
int vis[maxn];
ll mua[maxn];

ll pow_mod(ll a, ll b) {
    ll ret = 1;
    while (b) {
        if (b & 1
) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } ll inv(ll x) { return pow_mod(x, mod - 2); } int main() { ll m; cin >> m; mu[1] = 1; for (int i = 1; i <= m; i++) mu[i] = 1; for (int i = 2; i <= m; i++) {
if (!vis[i]) { mu[i] = -1; for (int j = i + i; j <= m; j += i) { vis[j] = 1; if ((j / i) % i == 0) mu[j] = 0; else mu[j] *= -1; } } } ll ans = 1; for (int i = 2; i <= m; i++) {
if (mu[i] != 0) { ans = ans - mu[i] * (m / i) * inv(m - m / i); } } cout << (ans % mod + mod) % mod << endl; return 0; }

cf 1139D - Steps to One