Codeforces-1097D:Makoto and a Blackboard(期望+DP)

探讨了在给定正整数和操作次数的情况下,通过随机选择数的因子并替换原数,最终得到黑板上数字期望值的计算方法。采用概率论和组合数学原理,分解目标数字为素因子乘积,独立计算每个素因子幂的期望贡献。

D. Makoto and a Blackboard
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output

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\frac PQQP where P and Q are coprime integers and Q≠0(mod109+7)Q\neq0(mod10^9+7)Q̸=0(mod109+7). Print the value of P⋅Q−1P⋅Q^{−1}PQ1 modulo 109+710^9+7109+7.

Input
The only line of the input contains two integers n and k (1≤n≤1015,1≤k≤104)(1≤n≤10^{15}, 1≤k≤10^4)(1n1015,1k104).

Output
Print a single integer — the expected value of the number on the blackboard after k steps as P⋅Q−1(mod109+7)P⋅Q^{−1}(mod10^9+7)PQ1(mod109+7) for P, Q defined above.

Examples
input
6 1
output
3
input
6 2
output
875000008
input
60 5
output
237178099
Note
In the first example, after one step, the number written on the blackboard is 1, 2, 3 or 6 — each occurring with equal probability. Hence, the answer is 1+2+3+64=3\frac {1+2+3+6}{4}=341+2+3+6=3.

In the second example, the answer is equal to 1⋅916+2⋅316+3⋅316+6⋅116=158.1⋅\frac{9}{16}+2⋅\frac{3}{16}+3⋅\frac{3}{16}+6⋅\frac{1}{16}=\frac{15}{8}.1169+2163+3163+6161=815.

思路:n分解为素因子相乘形式为n=a1p1a2p2.....akpkn={a_1}^{p_1}{a_2}^{p_2}.....{a_k}^{p_k}n=a1p1a2p2.....akpk。经过k次变换后就相当于是pip_ipi发生了变化。
可以发现素因子之间并没有相互影响,可以单独求出pip_ipi的概率,然后求出aia_iai对于结果的期望贡献,然后累乘。
对于aka_kakd[i][j]d[i][j]d[i][j]表示经过iii次变换后aka_kak的幂pk=jp_k=jpk=j的概率,那么aka_kak的期望贡献为∑i=0md[m][i]∗aki\sum_{i=0}^m d[m][i]*{a_k}^{i}i=0md[m][i]aki

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+10;
const int MOD=1e9+7;
typedef long long ll;
ll d[MAX][65],inv[MAX];
ll n,m;
ll solve(ll x,ll y)
{
    ll sum=0,now=1;
    memset(d,0,sizeof d);
    d[0][y]=1;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<=y;j++)
        {
            if(d[i][j])
            {
                for(ll k=0;k<=j;k++)d[i+1][k]=(d[i+1][k]+d[i][j]*inv[j+1])%MOD;
            }
        }
    }
    for(ll i=0;i<=y;i++)
    {
        sum=(sum+now*d[m][i])%MOD;
        now=now*x%MOD;
    }
    return sum;
}
int main()
{
    inv[1]=1;
    for(int i=2;i<=2000;i++)inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
    ll ans=1;
    cin>>n>>m;
    for(ll i=2;i*i<=n;i++)
    {
        if(n%i)continue;
        int cnt=0;
        while(n%i==0)n/=i,cnt++;
        ans=ans*solve(i,cnt)%MOD;
    }
    if(n>1)ans=ans*solve(n,1)%MOD;
    cout<<ans<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值