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;
}
cf1139D. Steps to One(dp)的更多相关文章
- 题解-CF1139D Steps to One
题面 CF1139D Steps to One 一个数列,每次随机选一个 \([1,m]\) 之间的数加在数列末尾,数列中所有数的 \(\gcd=1\) 时停止,求期望长度 \(\bmod 10^9+ ...
- CF1139D Steps to One(DP,莫比乌斯反演,质因数分解)
stm这是div2的D题……我要对不住我这个紫名了…… 题目链接:CF原网 洛谷 题目大意:有个一开始为空的序列.每次操作会往序列最后加一个 $1$ 到 $m$ 的随机整数.当整个序列的 $\gcd ...
- CF1139D Steps to One 题解【莫比乌斯反演】【枚举】【DP】
反演套 DP 的好题(不用反演貌似也能做 Description Vivek initially has an empty array \(a\) and some integer constant ...
- 【期望dp 质因数分解】cf1139D. Steps to One
有一种组合方向的考虑有没有dalao肯高抬啊? 题目大意 有一个初始为空的数组$a$,按照以下的流程进行操作: 在$1\cdots m$中等概率选出一个数$x$并添加到$a$的末尾 如果$a$中所有元 ...
- CF1139D Steps to One (莫比乌斯反演 期望dp)
\[ f[1] = 0 \] \[ f[i] = 1 + \frac{1}{m} \sum_{j = 1} ^ n f[gcd(i, j)] \ \ \ \ \ \ (i != 1) \] 然后发现后 ...
- sdut2623--The number of steps(概率dp第一弹,求期望)
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...
- 13年山东省赛 The number of steps(概率dp水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud The number of steps Time Limit: 1 Sec Me ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- Codeforces 1139D Steps to One dp
Steps to One 啊, 我要死了, 这种垃圾题居然没写出来, 最后十分钟才发现错在哪. 不知道为什么我以为 对于一个数x , 除了它的因子和它的倍数都是和它互质的, 我脑子是抽了吗? 随便瞎d ...
随机推荐
- Web前端JQuery面试题(一)
Web前端JQuery面试题(一) 一:选择器 基本选择器 什么是#id,element,.class,*,selector1, selector2, selectorN? 答: 根据给定的id匹配一 ...
- 站点的rel="alternate"属性
概述 今天看决战平安京官网源码,突然看到了rel的alternate属性,百度了一下,记录下来,供以后开发时参考,相信对其他人也有用. PC端rel 在pc版网页上,添加指向对应移动版网址的特殊链接r ...
- OAuth2简易实战(三)-JWT
1. OAuth2简易实战(三)-JWT 1.1. 与OAuth2授权码模式差别 授权服务器代码修改 @Configuration @EnableAuthorizationServer public ...
- 【Spark调优】大表join大表,少数key导致数据倾斜解决方案
[使用场景] 两个RDD进行join的时候,如果数据量都比较大,那么此时可以sample看下两个RDD中的key分布情况.如果出现数据倾斜,是因为其中某一个RDD中的少数几个key的数据量过大,而另一 ...
- mac中:不能完成此操作,因为找不到一个或多个需要的项目。(错误代码 -43)
今天使用mac删除某文件时,遇到此错误: 不能完成此操作,因为找不到一个或多个需要的项目.(错误代码 -43) 于是采用命令行删除可以正确删除:在要删除的文件夹坐在目录下执行 rm -rf tes ...
- linux mint 安装 opencv2.4
Download opencv https://github.com/opencv/opencv/tree/2.4 安装必要的依赖 sudo apt-get install build-essenti ...
- Gradle实现自动打包,签名,自定义apk文件名
Gradle实现自动打包,签名,自定义apk文件名 什么是签名,签名有什么用 Android APP都需要我们用一个证书对应用进行数字签名,不然的话是无法安装到Android手机上的,平时我们调试运行 ...
- salesforce零基础学习(九十一)Facet
说Facet以前,我们先说一下浏览器加载解析以及渲染的过程.浏览器获取一个HTML的文件时,会按照自上向下的顺序进行解析,并在加载过程中进行渲染.对html解析成DOM树,对CSS 解析成CSS Ru ...
- MongoDB从入门到优化
目录 一.MongoDB 简介 二.MongoDB 的储存引擎 三.mongodb 配置参数 四.MongoDB 单节点搭建 五.MongoDB 连接 六.MongoDB 常用命令 七.MongoDB ...
- kafka配置项host.name advertised.host.name
遇到的问题: 在本机或者其他机器telnet IP 9092,通,使用域名也通,telnet 127.0.0.1 9092不通 host.name:按配置文件说明,是Kafka绑定的interface ...