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 + ...
随机推荐
- 链表中倒数第k个节点(python)
题目描述 输入一个链表,输出该链表中倒数第k个结点. 无力吐槽牛客网... class Solution: def FindKthToTail(self, head, k): # write code ...
- apiCloud通过ajax获取数据
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- sh_09_格式化输出
sh_09_格式化输出 # 定义字符串变量 name,输出 我的名字叫 小明,请多多关照! name = "大小明" print("我的名字叫 %s,请多多关照!&quo ...
- IntelliJ IDEA 开发工具的一些设置
IntelliJ IDEA 开发工具的一些设置 参考资料 IntelliJ IDEA 的学习,离不开网络上技术热爱者们的分享,在此向他们表示感谢. 成吨提高开发效率:https://github.co ...
- 文档流&文字&CSS常用命令
文档流 文档流就是文档内元素流动方向 流动方向 内联元素从左往右流,宽度不够,之字形,且元素会被截断 块元素从上往下流动,一排一排 注意事项 内联元素中有英文单词,流动时宽度不够,英文单词会整体迁移, ...
- 纯css实现手机通讯录
我们经常在手机上看到通讯录列表,这类布局一般有两个显著的效果 首字母吸顶 快速定位 下面我们来实现一下 页面结构 这里页面结构很简单,就是两个列表 <div class="con&qu ...
- leetcode-mid-array-49 Group Anagrams
mycode 95.35% 思路:构建字典 class Solution(object): def groupAnagrams(self, strs): """ :ty ...
- 如何将阿里云上的RDS 备份的mysql数据还原到windows环境中
一.本地mysql数据库创建与备份库一致的数据库名,如testdb: 二.本地创建与备份库一致的数据库表,记得设置ALTER TABLE tableName1 ROW_FORMAT = compact ...
- 杂项-职位-DBA:DBA
ylbtech-杂项-职位-DBA:DBA 数据库管理员(Database Administrator,简称DBA),是从事管理和维护数据库管理系统(DBMS)的相关工作人员的统称,属于运维工程师的 ...
- Delphi XE2 之 FireMonkey 入门(8) - TImage
TImage 主要成员: { 属性 } Bitmap : TBitmap; //图像 BitmapMargins : TBounds; ...