D Makoto and a Blackboard
2 seconds
256 megabytes
standard input
standard output
Makoto has a big blackboard with a positive integer nn written on it. He will perform the following action exactly kk times:
Suppose the number currently written on the blackboard is vv. He will randomly pick one of the divisors of vv (possibly 11 and vv) and replace vv with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses 5858 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 kk steps.
It can be shown that this value can be represented as PQPQ where PP and QQ are coprime integers and Q≢0(mod109+7)Q≢0(mod109+7). Print the value of P⋅Q−1P⋅Q−1 modulo 109+7109+7.
The only line of the input contains two integers nn and kk (1≤n≤10151≤n≤1015, 1≤k≤1041≤k≤104).
Print a single integer — the expected value of the number on the blackboard after kk steps as P⋅Q−1(mod109+7)P⋅Q−1(mod109+7) for PP, QQ defined above.
6 1
3
6 2
875000008
60 5
237178099
In the first example, after one step, the number written on the blackboard is 11, 22, 33 or 66 — each occurring with equal probability. Hence, the answer is 1+2+3+64=31+2+3+64=3.
In the second example, the answer is equal to 1⋅916+2⋅316+3⋅316+6⋅116=1581⋅916+2⋅316+3⋅316+6⋅116=158.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=;
const long long mod=;
int k,pri[MAXN];
bool exist[MAXN];
long long n,inv[],f[][];
long long pOw(long long a,long long m)
{
long long pro;
for(pro=1LL;m;m>>=,a=a*a%mod)
if(m&)
pro=pro*a%mod;
return pro;
}
void pre_calc()
{
memset(exist,true,sizeof(exist));
pri[]=;
for(int i=;i<MAXN;i++)
{
if(exist[i]) pri[++pri[]]=i;
for(int j=;j<=pri[]&&(long long)i*pri[j]<MAXN;j++)
{
exist[i*pri[j]]=false;
if(i%pri[j]==)
break;
}
}
inv[]=;
for(int i=;i<;i++)
inv[i]=pOw(i,mod-);
return;
}
long long calc(long long p,int num)
{
f[][]=1LL;
for(int i=;i<=num;i++)
f[][i]=f[][i-]*p%mod;
for(int t=;t<=k;t++)
{
f[t][]=f[t-][];
for(int i=;i<=num;i++)
f[t][i]=(f[t][i-]+f[t-][i])%mod;
for(int i=;i<=num;i++)
f[t][i]=f[t][i]*inv[i+]%mod;
}
return f[k][num];
}
int main()
{
int num;
pre_calc();
cin>>n>>k;
long long ans=1LL;
for(int i=;i<=pri[]&&(long long)pri[i]*pri[i]<=n;i++) if(n%pri[i]==)
{
for(num=;n%pri[i]==;n/=pri[i],num++);
ans=ans*calc(pri[i],num)%mod;
}
if(n!=) ans=ans*calc(n,)%mod;
cout<<ans;
fclose(stdin);
fclose(stdout);
return ;
}
学习一下 Dinic+当前弧优化
网络流的dinic算法详解以及当前弧优化备注:点开
/*
最大流 Dinic */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<queue>
#include<cstring>
#define min(x,y) ((x<y)?(x):(y))
#define rev(i)(i&1?(i+1):(i-1))
using namespace std;
typedef long long ll;
int dis[]; //分层图,距源点距离
int cur[]; //当前弧优化
int n,m,ans,st,ed;
struct node{
int x,y,len,nxt;
node(){}
node(int nx,int ny,int nlen,int nnxt){
x=nx;y=ny;len=nlen;nxt=nnxt;
}
} E[];
int head[],cnt;
int bfs(){
for (int i=;i<=n;i++) dis[i]=-;
queue<int> Q;
dis[st]=;Q.push(st);
while (!Q.empty()){
int j=Q.front();
Q.pop();
for (int i=head[j];i;i=E[i].nxt)
if (dis[E[i].y]<&&E[i].len>){
dis[E[i].y]=dis[j]+;
Q.push(E[i].y);
}
}
if (dis[ed]>) return ;
else return ;
}
int find(int x,int low){
int res=;
if (x==ed) return low;
for (int i=cur[x];i;i=E[i].nxt){
cur[x]=i;
if (E[i].len>&&dis[E[i].y]==dis[x]+&&(res=find(E[i].y,min(low,E[i].len))))
{
E[i].len-=res;
E[i^].len+=res;
return res;
}
}
return ;
}
inline void link(int x,int y,int z){
E[++cnt]=node(x,y,z,head[x]);
head[x]=cnt;
E[++cnt]=node(y,x,,head[y]);
head[y]=cnt;
}
int main(){
scanf("%d%d%d%d",&n,&m,&st,&ed);
cnt=;
for (int i=;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
link(a,b,c);
}
ans=;int tans=;
while(bfs()){
for (int i=;i<=n;i++) cur[i]=head[i];
while (tans=find(st,2e6)) ans+=tans;
} printf("%d\n",ans);
return ;
}
网络流
https://www.cnblogs.com/SYCstudio/p/7260613.html
经典的最大流题POJ1273
D Makoto and a Blackboard的更多相关文章
- CF1097D Makoto and a Blackboard
题目地址:CF1097D Makoto and a Blackboard 首先考虑 \(n=p^c\) ( \(p\) 为质数)的情况,显然DP: 令 \(f_{i,j}\) 为第 \(i\) 次替换 ...
- CodeForces - 1097D:Makoto and a Blackboard (积性)
Makoto has a big blackboard with a positive integer n written on it. He will perform the following a ...
- Codeforces1097D. Makoto and a Blackboard(数论+dp+概率期望)
题目链接:传送门 题目大意: 给出一个整数n写在黑板上,每次操作会将黑板上的数(初始值为n)等概率随机替换成它的因子. 问k次操作之后,留在黑板上的数的期望. 要求结果对109+7取模,若结果不是整数 ...
- codeforces#1097 D. Makoto and a Blackboard(dp+期望)
题意:现在有一个数写在黑板上,它以等概率转化为它的一个约数,可以是1,问经过k次转化后这个数的期望值 题解:如果这个数是一个素数的n次方,那么显然可以用动态规划来求这个数的答案,否则的话,就对每个素因 ...
- CF1097D Makoto and a Blackboard 积性函数、概率期望、DP
传送门 比赛秒写完ABC结果不会D--最后C还fst了qwq 首先可以想到一个约数个数\(^2\)乘上\(K\)的暴力DP,但是显然会被卡 在\(10^{15}\)范围内因数最多的数是\(978217 ...
- cf1097D. Makoto and a Blackboard(期望dp)
题意 题目链接 Sol 首先考虑当\(n = p^x\),其中\(p\)是质数,显然它的因子只有\(1, p, p^2, \dots p^x\)(最多logn个) 那么可以直接dp, 设\(f[i][ ...
- 【DP】【CF1097D】 Makoto and a Blackboard
更好的阅读体验 Description 给定一个数 \(n\),对它进行 \(k\) 次操作,每次将当前的数改为自己的因数,包括 \(1\) 和自己.写出变成所有因数的概率是相等的.求 \(k\) 次 ...
- D. Makoto and a Blackboard(积性函数+DP)
题目链接:http://codeforces.com/contest/1097/problem/D 题目大意:给你n和k,每一次可以选取n的因子代替n,然后问你k次操作之后,每个因子的期望. 具体思路 ...
- CF 1097D Makoto and a Blackboard
算是记一下昨天晚上都想了些什么 官方题解 点我 简单题意 给定两个正整数$n$和$k$,定义一步操作为把当前的数字$n$等概率地变成$n$的任何一个约数,求$k$步操作后的期望数字,模$1e9 + ...
随机推荐
- 047:创建和映射ORM模型
创建ORM模型: ORM 模型一般都是放在 app 的 models.py 文件中.每个 app 都可以拥有自己的模型.并且如果这个模型想要映射到数据库中,那么这个 app 必须要放在 setting ...
- 运行biggan demo
http://www.zhuanzhi.ai/document/8705953a704e1bf8e051c161d1587d88
- BZOJ 5129: [Lydsy1712月赛]树上传送 点分树+Dijkstra
Description http://www.lydsy.com/JudgeOnline/upload/201712/prob12.pdf Input Output 暑假集训的时候点分树做的比较少,所 ...
- 博弈论 x
——关于博弈论 四道例题带你走进博弈论~ (考虑必败态,必胜态) Ps:要理解这种思想,首先要明白什么叫必败态.说简单点,必败态就是“在对方使用最优策略时,无论做出什么决策都会导致失败的局面”.其他的 ...
- H. The Game of Life
题目链接:http://exam.upc.edu.cn/problem.php?id=5206 题意:邻居为八个方向.若一个活人有2或3个邻居,遗传一代,否则死亡:若一个死人有3个邻居,则下一代复活. ...
- 使用struts2的内置标签,采用submit()提交表单时,浏览器报404
如图 url是没有问题的,结果我将提交方式改为get时,发现有2个参数的name值是一样的,如下图, 解决方法:将name的值修改就OK了.
- 纯css实现Material Design中的水滴动画按钮
前言 大家平时应该经常见到这种特效,很炫酷不是吗 这是谷歌Material Design中最常见的特效了,市面上也有很多现成的js库,用来模拟这一特效.但是往往要引入一大堆js和css,其实在已有的项 ...
- 国内npm镜像使用方法
npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便.下面整理出了一部分国内优秀的npm镜像资源,国内用户可以选 ...
- spark 学习网站和资料
spark 官网首页 https://spark.apache.org/ spark 官网文档 spark scala API 文档 https://spark.apache.org/docs/lat ...
- Linux驱动开发5——同步机制
上一章讲到了并发,指的是多个进程同时存取临界区资源的处理机制.这一章讲的同步机制,讲的是多个进程之间协同工作的处理机制,如临界区数据还没有准备好,A进程负责准备数据,B进程等待A进程完成之后读取数据. ...