欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

cf1139D. Steps to One(dp)

程序员文章站 2022-10-06 11:03:05
题意 "题目链接" 从$[1, M]$中随机选数,问使得所有数gcd=1的期望步数 Sol 一个很显然的思路是设$f[i]$表示当前数为$i$,期望的操作轮数,转移的时候直接枚举gcd $f[i] = 1 + \frac{ \sum_{j=1}^N f[gcd(i, j)]}{N}$ 然后移一下项就 ......

题意

题目链接

\([1, m]\)中随机选数,问使得所有数gcd=1的期望步数

sol

一个很显然的思路是设\(f[i]\)表示当前数为\(i\),期望的操作轮数,转移的时候直接枚举gcd

\(f[i] = 1 + \frac{ \sum_{j=1}^n f[gcd(i, j)]}{n}\)

然后移一下项就可以算出\(f[i]\)了。

发现gcd相同的有很多,可以预处理一下。

复杂度\(o(跑的过)\)

还有一种反演做法表示推不出来qwq

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
template <typename a, typename b> inline ll fp(a a, b p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename a, typename b> inline a gcd(a x, b y) {return !y ? x : gcd(y, x % y);}
int inv(int x) {
    return fp(x, mod - 2);
}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, f[maxn], invn;
vector<int> d[maxn], cnt[maxn];
void sieve() {
    for(int i = 1; i <= n; i++) 
        for(int k = i; k <= n; k += i) d[k].push_back(i);
    for(int i = 1; i <= n; i++) {
        cnt[i].resize(d[i].size() + 1);
        for(int j = d[i].size() - 1; ~j; j--) {
            cnt[i][j] = n / d[i][j];
            for(int k = j + 1; k < d[i].size(); k++)
                if(!(d[i][k] % d[i][j])) cnt[i][j] -= cnt[i][k];
        }
        //for(int j = 0; j < d[i].size(); j++)
        //  printf("%d %d %d\n", i, d[i][j], cnt[i][j]);
    }
            
}
signed main() {
    n = read(); invn = inv(n);
    sieve();
    int ans = 0;
    for(int i = 2; i <= n; i++) {
        int lf = n, tmp = 0;
        /*
        for(int j = 1, t = 1; j <= n; j++) {
            if((t = gcd(i, j)) == i) lf--;
            else add2(tmp, f[t]);
        }
        */
        for(int j = 0; j < d[i].size(); j++) {
            if(d[i][j] == i) lf -= cnt[i][j];
            else add2(tmp, mul(cnt[i][j], f[d[i][j]]));
        }
        f[i] = add(n, tmp); 
        mul2(f[i], inv(lf));
    }
    for(int i = 1; i <= n; i++) add2(ans, f[i] + 1);
    cout << mul(ans, invn);
    return 0;
}