题意

题目链接

从\([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. MQ 简单的使用

    需要创建两个控制台应用 引用用下面的包 (1)生产者 static void Main(string[] args) { ConnectionFactory factory = new Connect ...

  2. mysql原生sql盘点

    select*from (select*from test1 union all select*from test2 ) //两个查询的数据行数需要对应一致,且名字as 一致. 1.如果直接用如下sq ...

  3. sql server 备份与恢复系列二 事务日志概述

    1.1  日志文件与数据文件一致性 在上一章备份与恢复里了解到事务日志的重要性,这篇重点来了解事务日志. 事务日志记录了数据库所有的改变,能恢复该数据库到改变之前的任意状态.在sql server实例 ...

  4. [Jenkins]JDK版本过高导致的java.io.IOException: Remote call on xxxx failed

    ------------------------------------------------------ 如需转载,请注明出处. 文章链接:https://www.cnblogs.com/dzbl ...

  5. 使用数组制作简易的用户管理系统【java】

    思路: 一.分析用户管理功能模块 - User类型属性值设定 private String username; // 用户id(唯一字段) private String nickname; // 昵称 ...

  6. Spring Boot 系列(九)数据层-集成Spring-data-jpa

    实际开发中,不可避免地会对数据进行反复的增删改查操作,然而这部分工作是十分繁琐枯燥的.那么,随即而生的ORM框架就能很好的解决这个问题. 我们常用的ORM框架有:Hibernate.Mybatis.J ...

  7. maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

    项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...

  8. 手把手在Ubuntu上面安装Docker

    一.环境准备 1.Ubuntu64位系统(目前docker仅支持64位系统) 2.官方支持的Ubuntu版本(1)Ubuntu Trusty 14.04(LTS)(2)Ubuntu Precise 1 ...

  9. 三大主流软件负载均衡器对比(LVS VS Nginx VS Haproxy)

    LVS:1.抗负载能力强.抗负载能力强.性能高,能达到F5硬件的60%:对内存和cpu资源消耗比较低2.工作在网络4层,通过vrrp协议转发(仅作分发之用),具体的流量由linux内核处理,因此没有流 ...

  10. CSS定位概述

    CSS中有三种基本的定位机制:普通流,浮动和绝对定位.   1.相对定位:relative 如果对一个元素进行相对定位,它将出现在它所在的位置上,然后可以通过设置垂直或者水平位置,让这个元素“相对于” ...