CodeForces - 1097D:Makoto and a Blackboard (积性)
Makoto has a big blackboard with a positive integer n written on it. He will perform the following action exactly k
times:
Suppose the number currently written on the blackboard is v
. He will randomly pick one of the divisors of v (possibly 1 and v) and replace v with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses 58
as his generator seed, each divisor is guaranteed to be chosen with equal probability.
He now wonders what is the expected value of the number written on the blackboard after k
steps.
It can be shown that this value can be represented as PQ
where P and Q are coprime integers and Q≢0(mod109+7). Print the value of P⋅Q−1 modulo 109+7
.
Input
The only line of the input contains two integers n
and k (1≤n≤1015, 1≤k≤104
).
Output
Print a single integer — the expected value of the number on the blackboard after k
steps as P⋅Q−1(mod109+7) for P, Q
defined above.
Examples
6 1
3
6 2
875000008
60 5
237178099
题意:开始给定一个数N,然后让你K轮如下操作:等概率的变为当前数的一个因子,求期望。
思路:发现有点像孟德尔定律的数据,其实就是在暗示我们:豌豆的几种颜色可以单独算,然后作乘。
事实上就是这样的,对于每个素数因子,我们假设他在N中的幂为p(a^p,本题当a=2时,p最大为50),那么我们可以算出最后幂为1到p的概率。 而不同素因子之间不会相互影响。
所以我们预处理出开始幂为p,K轮后幂为q的次数mp[p][q];然后就可以对于每个素数得到其概率,然后累乘即可。
(注意不要爆ll
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=;
const int Mod=1e9+;
ll ans,a[maxn],f[maxn],rev[maxn],tot,mp[][],M[][],sum; int K;
int qpow(ll a,int x){
int res=; while(x){
if(x&) res=1LL*res*a%Mod;
a=1LL*a*a%Mod; x>>=;
} return res;
}
ll get(int p)
{
f[p]++; ll tmp=qpow(qpow(f[p],K),Mod-),res=;
rep(i,,f[p]) {
res=(res+1LL*mp[f[p]][i]*tmp%Mod)%Mod;
tmp=tmp*a[p]%Mod;
}
return res;
}
void solve(int p)
{
ll tmp=; mp[p][p]=;
rep(i,,K){
rep(j,,p) M[p][j]=1LL*mp[p][j]*p%Mod,mp[p][j]=;
rep(j,,p) {
rep(k,,j) mp[p][k]=(mp[p][k]+1LL*M[p][j]*rev[j]%Mod)%Mod;
}
}
}
int main()
{
ll N,tN; scanf("%lld%d",&N,&K); tN=N;
rev[]=; rep(i,,) rev[i]=qpow(i,Mod-);
rep(i,,) solve(i); ans=1LL;
for(ll i=;i*i<=tN;i++){
if(tN%i==){
a[++tot]=i; while(tN%i==) tN/=i,f[tot]++;
sum=sum*(f[tot]+);
}
}
if(tN>) a[++tot]=tN,f[tot]=;
rep(i,,tot) ans=1LL*ans*get(i)%Mod;
printf("%lld\n",ans);
return ;
}
CodeForces - 1097D:Makoto and a Blackboard (积性)的更多相关文章
- CF1097D Makoto and a Blackboard 积性函数、概率期望、DP
传送门 比赛秒写完ABC结果不会D--最后C还fst了qwq 首先可以想到一个约数个数\(^2\)乘上\(K\)的暴力DP,但是显然会被卡 在\(10^{15}\)范围内因数最多的数是\(978217 ...
- Codeforces 1097D. Makoto and a Blackboard
传送门 首先考虑如果 $n$ 只有一个质因数的情况,即 $n=p^t$ 那么显然可以 $dp$ ,设 $f[i][j]$ 表示第 $i$ 步,当前剩下 $p^j$ 的概率 那么转移很简单: $f[i] ...
- Codeforces E. Bash Plays with Functions(积性函数DP)
链接 codeforces 题解 结论:\(f_0(n)=2^{n的质因子个数}\)= 根据性质可知\(f_0()\)是一个积性函数 对于\(f_{r+1}()\)化一下式子 对于 \[f_{r+1} ...
- CF 1097D Makoto and a Blackboard
算是记一下昨天晚上都想了些什么 官方题解 点我 简单题意 给定两个正整数$n$和$k$,定义一步操作为把当前的数字$n$等概率地变成$n$的任何一个约数,求$k$步操作后的期望数字,模$1e9 + ...
- Makoto and a Blackboard CodeForces - 1097D (积性函数dp)
大意: 初始一个数字$n$, 每次操作随机变为$n$的一个因子, 求$k$次操作后的期望值. 设$n$经过$k$次操作后期望为$f_k(n)$. 就有$f_0(n)=n$, $f_k(n)=\frac ...
- D. Makoto and a Blackboard(积性函数+DP)
题目链接:http://codeforces.com/contest/1097/problem/D 题目大意:给你n和k,每一次可以选取n的因子代替n,然后问你k次操作之后,每个因子的期望. 具体思路 ...
- Bash Plays with Functions CodeForces - 757E (积性函数dp)
大意: 定义函数$f_r(n)$, $f_0(n)$为pq=n且gcd(p,q)=1的有序对(p,q)个数. $r \ge 1$时, $f_r(n)=\sum\limits_{uv=n}\frac{f ...
- CF 1097D - Hello 2019 D题: Makoto and a Blackboard
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门 Portal 原题目描述在最下面. 给一个数n ...
- CF1097D Makoto and a Blackboard
题目地址:CF1097D Makoto and a Blackboard 首先考虑 \(n=p^c\) ( \(p\) 为质数)的情况,显然DP: 令 \(f_{i,j}\) 为第 \(i\) 次替换 ...
- codeforces757E. Bash Plays with Functions(狄利克雷卷积 积性函数)
http://codeforces.com/contest/757/problem/E 题意 Sol 非常骚的一道题 首先把给的式子化一下,设$u = d$,那么$v = n / d$ $$f_r(n ...
随机推荐
- 快速幂模n运算
模运算里的求幂运算,比如 5^596 mod 1234, 当然,直接使用暴力循环也未尝不可,在书上看到一个快速模幂算法 大概思路是,a^b mod n ,先将b转换成二进制,然后从最高位开始(最高位一 ...
- Lua和C++交互 学习记录之一:C++嵌入脚本
主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3 参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...
- iconv编码转换
环境:cocos2dx 3.10 1.vs环境下编译windows版本,需要增加头文件和链接库①cocos2d-x-3.10\external\win32-specific\icon\include② ...
- [ORA-28001: the password has expired]的处理
http://irikintwtr.com/wordpress/?p=420 alter profile default limit password_life_time unlimited; alt ...
- 转发一篇分析LinQ是什么?
LINQ(发音:Link)是语言级集成查询(Language INtegrated Query) ?LINQ是一种用来进行数据访问的编程模型,使得.NET语言可以直接支持数据查询 ?LINQ的目标是降 ...
- 『PyTorch』第十一弹_torch.optim优化器
一.简化前馈网络LeNet import torch as t class LeNet(t.nn.Module): def __init__(self): super(LeNet, self).__i ...
- 『cs231n』线性分类器损失函数
代码部分 SVM损失函数 & SoftMax损失函数: 注意一下softmax损失的用法: SVM损失函数: import numpy as np def L_i(x, y, W): ''' ...
- bzoj1834: [ZJOI2010]network 网络扩容 费用流
bzoj1834 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容 ...
- .net 外部CSS文件不起作用总结
外部css文件样式全部不起作用 asp.net 页面引用路径的问题 缺少必须属性<link rel="stylesheet" type="text/css" ...
- LTrim、RTrim 和 Trim 函数
返回不带前导空格 (LTrim).后续空格 (RTrim) 或前导与后续空格 (Trim) 的字符串副本. LTrim(string) RTrim(string) Trim(string) strin ...