题意

题目链接

从\([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)的更多相关文章

  1. 题解-CF1139D Steps to One

    题面 CF1139D Steps to One 一个数列,每次随机选一个 \([1,m]\) 之间的数加在数列末尾,数列中所有数的 \(\gcd=1\) 时停止,求期望长度 \(\bmod 10^9+ ...

  2. CF1139D Steps to One(DP,莫比乌斯反演,质因数分解)

    stm这是div2的D题……我要对不住我这个紫名了…… 题目链接:CF原网  洛谷 题目大意:有个一开始为空的序列.每次操作会往序列最后加一个 $1$ 到 $m$ 的随机整数.当整个序列的 $\gcd ...

  3. CF1139D Steps to One 题解【莫比乌斯反演】【枚举】【DP】

    反演套 DP 的好题(不用反演貌似也能做 Description Vivek initially has an empty array \(a\) and some integer constant ...

  4. 【期望dp 质因数分解】cf1139D. Steps to One

    有一种组合方向的考虑有没有dalao肯高抬啊? 题目大意 有一个初始为空的数组$a$,按照以下的流程进行操作: 在$1\cdots m$中等概率选出一个数$x$并添加到$a$的末尾 如果$a$中所有元 ...

  5. CF1139D Steps to One (莫比乌斯反演 期望dp)

    \[ f[1] = 0 \] \[ f[i] = 1 + \frac{1}{m} \sum_{j = 1} ^ n f[gcd(i, j)] \ \ \ \ \ \ (i != 1) \] 然后发现后 ...

  6. sdut2623--The number of steps(概率dp第一弹,求期望)

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...

  7. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  8. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  9. Codeforces 1139D Steps to One dp

    Steps to One 啊, 我要死了, 这种垃圾题居然没写出来, 最后十分钟才发现错在哪. 不知道为什么我以为 对于一个数x , 除了它的因子和它的倍数都是和它互质的, 我脑子是抽了吗? 随便瞎d ...

随机推荐

  1. Mysql主从配置实战

    实战mysql主从配置 准备两个docker容器,分别在3306和3307开启两个mysql为主从数据库 可执行以下命令 docker run -p 3306:3306 --name mysql330 ...

  2. Oracle merge合并更新函数

    本博客介绍一下Oracle merge合并函数,业务场景:新增数据的时候要先查询数据库是否已经有改数据,有数据就更新数据,没数据才新增数据,这是很常见的业务场景,如果是用Oracle数据库的话,其实直 ...

  3. Python - 安装并配置Anaconda环境

    1- 简介 官网:https://www.anaconda.com/ Anaconda是一个用于科学计算的Python发行版,适用于数据分析的Python工具,也可以用在大数据和人工智能领域. 支持 ...

  4. odoo开发笔记 -- 模型字段定义中设置默认值

    例如: company_id = fields.Many2one('res.company', string='Company', default=lambda self: self.env['res ...

  5. EL表达式jsp页面double小数点后保留两位

    EL表达式jsp页面double小数点后保留两位,四舍五入 <fmt:formatNumber type="number" value="${member.logi ...

  6. 线程池ThreadPool及Task调度死锁分析

    近1年,偶尔发生应用系统启动时某些操作超时的问题,特别在使用4核心Surface以后.笔记本和台式机比较少遇到,服务器则基本上没有遇到过. 这些年,我写的应用都有一个习惯,就是启动时异步做很多准备工作 ...

  7. leetcode — unique-paths-ii

    /** * Source : https://oj.leetcode.com/problems/unique-paths-ii/ * * * Follow up for "Unique Pa ...

  8. JSON数据从MongoDB迁移到MaxCompute最佳实践

    数据及账号准备 首先您需要将数据上传至您的MongoDB数据库.本例中使用阿里云的云数据库 MongoDB 版,网络类型为VPC(需申请公网地址,否则无法与DataWorks默认资源组互通),测试数据 ...

  9. 使用3D Slicer进行颅骨去除

    关于3D Slicer的下载.安装及模块安装在上一篇博客中以及介绍过,以下将专注于使用3D Slicer进行颅骨去除 准备 此次,我们需要安装SwissSkullStripper模块,安装后需要重启软 ...

  10. 定时备份 MySQL 并上传到七牛

    多数应用场景下,我们需要对重要数据进行备份.并放置到一个安全的地方,以备不时之需. 常见的 MySQL 数据备份方式有,直接打包复制对应的数据库或表文件(物理备份).mysqldump 全量逻辑备份. ...