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 ...
随机推荐
- IntelliJ IDEA 使用前常用设置
0.设置位置 以下设置基于IntelliJ IDEA 2018.3.2 版本. IDEA 的设置一般都在 File 下的 Settings... 里进行设置的. 1.设置字体字号行间距 2.设置背景图 ...
- HTTP 协议服务器相关概念
1.HTTP/1.1规范允许一台HTTP服务器搭建多个Web站点,即物理层面只有一台服务器,使用虚拟主机功能,可就假想有多台服务器. 在相同IP地址下,由于虚拟主机可以寄存多个不同主机名和域名的网站, ...
- 嘿!我用python帮我干这些事
python 无疑是当下火上天的语言,但是我们又不拿来工作,那么能拿来干啥呢?我是这么干的. 1. 平时工作开发用不上,就当个计算器吧! python # 加减乘除 >>> (3 + ...
- 玩转Python图片处理 (OpenCV-Python )
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效——由一系列 C 函数和少量 C++ 类 ...
- SVN密码找回 完美方案
问题背景 SVN(Subversion)版本管理工具.本文以Windows操作系统下使用SVN的场景. 长时间不使用SVN,可能会出现忘记了SVN密码的尴尬局面.那么,该如何找回SV密码呢? 处理思路 ...
- vue 关于vue.set的学习笔记
vue新手小白,在看vue文档的时候 发现vue关于 数组,对象值改变的与 ng有那么点不同. 官方表示 由于 JavaScript 的限制,Vue 不能检测以下变动的数组: 当你利用索引直接设置一个 ...
- rest-framework之认证组件
认证组件 认证简介 作用:校验是否登录 首先定义一个类,集成BaseAuthentication,写一个方法:authenticate,在方法内部,实证过程,认证通过,返回None或者两个对象(use ...
- IdentityServer4(8)- 使用密码认证方式控制API访问(资源所有者密码授权模式)
一.前言 本文已经更新到 .NET Core 2.2 OAuth 2.0 资源所有者密码模式允许客户端向令牌服务发送用户名和密码,并获取代表该用户的访问令牌. 除了通过无法浏览器进行交互的应用程序之外 ...
- Plugin with id 'com.novoda.bintray-release' not found.的解决方案
import Module的时候,有时候会提示Plugin with id 'com.novoda.bintray-release' not found. 点击Open File,定位到该Module ...
- 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装爬虫框架Scrapy(离线方式和在线方式)(图文详解)
不多说,直接上干货! 参考博客 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装OpenCV(离线方式和在线方式)(图文详解) 第一步:首先,提示升级下pip 第二步 ...