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 ...
随机推荐
- Git使用详细教程(2):配置用户名和邮箱
首先,说下最常用的设置用户名和邮箱的命令 git config --global user.name 'xxx' //设置用户名 git config --global user.email 'xxx ...
- 第35节:Java面向对象中的多线程
Java面向对象中的多线程 多线程 在Java面向对象中的多线程中,要理解多线程的知识点,首先要掌握什么是进程,什么是线程?为什么有多线程呢?多线程存在的意义有什么什么呢?线程的创建方式又有哪些?以及 ...
- 用XMLHttpRequest制作一个简易ajax
概述 jquery退出历史舞台之后,我们怎么来发送ajax请求呢?可以用相关的库,也可以自己制作一个简易的ajax. 需要说明的是,我们使用的是XMLHttpRequest 2,它几乎兼容所有主流浏览 ...
- c++多继承多态
C++多继承多态的实现 如果一个类中存在虚函数,在声明类的对象时,编译器就会给该对象生成一个虚函数指针,该虚函数指针指向该类对应的虚函数表. 多态的实现是因为使用了一种动态绑定的机制,在编译期间不确定 ...
- Jade —— 源于 Node.js 的 HTML 模板引擎
2013-12-11 发布 Jade —— 源于 Node.js 的 HTML 模板引擎 开源项目介绍 web 模板引擎 node.js jade 207.8k 次阅读 · 读完需要 69 分钟 ...
- mysql 开发进阶篇系列 50 表的数据导入(load data infile,mysqlimport )
一.概述 上篇讲到的表的数据导出(select .. into outfile 或者mysqldump),这篇继续讲表的数据导入,导入也同样有二个方法,分别是load data infile... 和 ...
- CentOS7.0小随笔——指令基本操作(Part.B)
一.文件与目录基本操作指令 touch命令 在Linux中,touch指令可以建立一个空文件 但如果创建的文件本身存在(指在同一目录下),则会修改文件最后的访问时间,并不会更改文件内的内容. 例:# ...
- 使用IntelliJ IDEA新建Java Web后端resfulAPI模板
初始化项目 打开IntelliJ IDEA,我的版本是Version 2018.1.4.点击Create New Project.在左侧的列表中选择Maven.然后在右侧勾选Create from a ...
- 01 Windows安装Tensorflow
1.安装Python. 点击此处下载Python3.5.2.安装Python时一定要选择安装pip. 2.配置Python环境变量. 将%安装路径%\Scripts添加到Path下面. 3.修改Pip ...
- php-fpm无法使用系统环境变量的解决方法
为了防止任意环境变量到达php-fpm进程,默认默认php-fpm是会清空系统环境变量的, 解决办法 修改php-fpm配置的clear_env = no (默认是yes)