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 + ...
随机推荐
- Debian10+OpenMediaVault(OMV)安装
前言:测试打造NAS平台,以下是步骤. 安装Debian10 注:请下载amd64,不要下载i836平台,因为OMV外挂插件不支持I836所以不建议用i836,如只使用官方插件可以无视 安装前-安装, ...
- margin与padding的区别是什么?
margin与padding的区别是什么? 目录 1.背景介绍 2.知识剖析 3.常见问题 4.解决方案 5.编码实战 6.扩展思考 7.参考文献 8.更多讨论 1.背景介绍 什么是margin 什么 ...
- CentOS 系统free命令
CentOS 6 下free命令 各参数含义:total:总物理内存used:已使用内存free:完全未被使用的内存shared:应用程序共享内存buffers:缓存,主要用于目录方面,inode值等 ...
- R 保存图片——tiff
tiff(filename = "c:\\aaa.tiff", res = 800, pointsize = 2) plot(1:100) dev.off() tiff(file= ...
- warp(图像仿射变换)
仿射变换是一种二维坐标(x,y)到二维坐标(u,v)的线性变换. 对应的齐次坐标矩阵表示形式为: 仿射变换特点: 直线经仿射变换后依然为直线: ’直线之间的相对位置关系保持不变,平行线经仿射变换后依然 ...
- Vue中的MVVM框架
ViewModel:数据双向绑定 场景: 针对具有复杂交互逻辑的前段应用 提供基础的架构抽象 通过Ajax数据持久化,保证前端用户体验 什么是vue.js? 是一个轻量级的mvvm框架 数据驱动+组 ...
- Centos7 yum安装OpenLDAP(普通用户可以更改密码)
环境 系统版本:centos7.4 openldap版本2.4 安装和配置 安装并启动服务 安装: yum install openldap openldap-servers openldap-cli ...
- angular项目开发
第 1 步:安装 Angular CLI 你可以使用 Angular CLI 来创建项目.生成应用和库代码,以及执行各种持续开发任务,比如测试.打包和部署. 全局安装 Angular CLI. 要使用 ...
- os.environ.get()的用法
os.environ.get()是python中os模块获取环境变量的一个方法 import os JS_ADDRESS = os.environ.get("PALM_JS_ADDRESS& ...
- 建立起BI的支撑团队
Bobby Luo 罗如意(18907295660@189.cn) 2011年7月 http://weibo.com/cquptvlry 电子商务中的BI应用初探 系统架构 对整个数据仓库的架构进行规 ...