cf1139D. Steps to One(dp)

题意

题目链接

\([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;
}
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2019-03-22 15:24  自为风月马前卒  阅读(555)  评论(0编辑  收藏  举报

Contact with me