cf1097D. Makoto and a Blackboard(期望dp)
题意
Sol
首先考虑当\(n = p^x\),其中\(p\)是质数,显然它的因子只有\(1, p, p^2, \dots p^x\)(最多logn个)
那么可以直接dp, 设\(f[i][j]\)表示经过了\(i\)轮,当前数是\(p^j\)的概率,转移的时候枚举这一轮的\(p^j\)转移一下
然后我们可以把每个质因子分开算,最后乘起来就好了
至于这样为什么是对的。(开始瞎扯),考虑最后的每个约数,它一定是一堆质因子的乘积,而每个质因子的概率我们是知道的,所以这样乘起来算是没问题的。
其实本质上还是因为约数和/个数都是积性函数
时间复杂度:\(O(\sqrt{n} + k log^2 n)\)
#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 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;}
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, K, top, inv[MAXN];
Pair st[MAXN];
int f[10001][1001];
int fp(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a);
a = mul(a, a); p >>= 1;
}
return base;
}
int solve(int a, int b) {//a^b
memset(f, 0, sizeof(f));
f[0][b] = 1;
for(int i = 1; i <= K; i++)
for(int j = 0; j <= b; j++)
for(int k = j; k <= b; k++)
add2(f[i][j], mul(f[i - 1][k], inv[k + 1]));
int res = 0;
for(int i = 0; i <= b; i++) add2(res, mul(f[K][i], fp(a, i)));
return res;
}
signed main() {
for(int i = 1; i <= 666; i++) inv[i] = fp(i, mod - 2);
cin >> N >> K;
for(int i = 2; i * i <= N; i++) {
if(!(N % i)) {
st[++top] = MP(i, 0);
while(!(N % i)) st[top].se++, N /= i;
}
}
if(N) st[++top] = MP(N, 1);
int ans = 1;
for(int i = 1; i <= top; i++)
ans = mul(ans, solve(st[i].fi, st[i].se));
cout << ans;
return 0;
}
/*
2
(()))
(
*/
cf1097D. Makoto and a Blackboard(期望dp)的更多相关文章
- CF1097D Makoto and a Blackboard(期望)
link 题目大意:给您一个数 n, 每次从n的所有约数(包含1.n)中等概率选出一个约数替换n,重复操作k次,求最后结果期望值%1e9+7. 题解:考虑暴力,我们设f(n,k)代表答案,则有f(n, ...
- CF1097D Makoto and a Blackboard
题目地址:CF1097D Makoto and a Blackboard 首先考虑 \(n=p^c\) ( \(p\) 为质数)的情况,显然DP: 令 \(f_{i,j}\) 为第 \(i\) 次替换 ...
- CF1097D Makoto and a Blackboard 积性函数、概率期望、DP
传送门 比赛秒写完ABC结果不会D--最后C还fst了qwq 首先可以想到一个约数个数\(^2\)乘上\(K\)的暴力DP,但是显然会被卡 在\(10^{15}\)范围内因数最多的数是\(978217 ...
- CF1097D Makoto and a Blackboard(期望)
[Luogu-CF1097D] 给定 \(n,k\)一共会进行 \(k\) 次操作 , 每次操作会把 \(n\) 等概率的变成 \(n\) 的某个约数 求操作 \(k\) 次后 \(n\) 的期望是多 ...
- CF1097D Makoto and a Blackboard 质因数分解 DP
Hello 2019 D 题意: 给定一个n,每次随机把n换成它的因数,问经过k次操作,最终的结果的期望. 思路: 一个数可以表示为质数的幂次的积.所以对于这个数,我们可以分别讨论他的质因子的情况. ...
- codeforces#1097 D. Makoto and a Blackboard(dp+期望)
题意:现在有一个数写在黑板上,它以等概率转化为它的一个约数,可以是1,问经过k次转化后这个数的期望值 题解:如果这个数是一个素数的n次方,那么显然可以用动态规划来求这个数的答案,否则的话,就对每个素因 ...
- codeforces1097D Makoto and a Blackboard 数学+期望dp
题目传送门 题目大意: 给出一个n和k,每次操作可以把n等概率的变成自己的某一个因数,(6可以变成1,2,3,6,并且概率相等),问经过k次操作后,期望是多少? 思路:数学和期望dp 好题好题!! ...
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
- [NOIP2016]换教室 D1 T3 Floyed+期望DP
[NOIP2016]换教室 D1 T3 Description 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 ...
随机推荐
- position:absolute;宽度自适应
http://blog.csdn.net/isaisai/article/details/45640515 设置left:0,right:0
- Jira API传字符串的换行问题 (文本编辑器使用)
因为jira的文本编辑器自动进行了2次转义,如果从API过来的文本换行需要手动处理.吐血. net里: desc = desc.Replace("\n", "\\n&qu ...
- Storm系列三: Storm消息可靠性保障
Storm系列三: Storm消息可靠性保障 在上一篇 Storm系列二: Storm拓扑设计 中我们已经设计了一个稍微复杂一点的拓扑. 而本篇就是在上一篇的基础上再做出一定的调整. 在这里先大概提一 ...
- PHP查找与搜索数组元素
in_array()函数 in_array()函数在一个数组汇总搜索一个特定值,如果找到这个值返回true,否则返回false.其形式如下: boolean in_array(mixed needle ...
- C# 基元类型
C#编程中,初始化一个整数有两种方式: (1).较繁琐的方法,代码如下: Int32 a = new Int32(); (2).极简的方法,代码如下: ; 对比两种方法,分析如下: 第一种:过于繁琐, ...
- Android Studio SVN的使用
一 SVN的配置 这篇文章使用的Android studio版本为1.4 RC3. 我选择的是TortoiseSVN,版本为1.8,不要选择1.9版本(目前的最新版),因为如果你安装的是1.9版本当你 ...
- IE10以下优雅降级(作为范例)
扒下来一段 优雅降级的代码. <!--[if lt IE 10]> <style> .ie-tip{margin-top: 100px;font-size: 16px;text ...
- System.Windows.Forms.Timer的简单用法
Timer就是用来计时操作,如:你想在多少秒之后执行某个动作 Timer showTextBoxTimer = new Timer(); //新建一个Timer对象 showTextBoxTimer. ...
- java NIO系列教程2
7.FileChannel Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开Fi ...
- 【.Net】含Unicode的字符串截断 VB.NET C#
Function AnsiLeftB(ByVal strArg As String, ByVal arg1 As Integer) As String Dim unicodeEncoding As E ...