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

25 篇文章 0 订阅

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

Input
The only line of the input contains two integers n and k ( 1 ≤ n ≤ 1 0 15 , 1 ≤ k ≤ 1 0 4 ) (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 ( m o d 1 0 9 + 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 + 6 4 = 3 \frac {1+2+3+6}{4}=3 41+2+3+6=3.

In the second example, the answer is equal to 1 ⋅ 9 16 + 2 ⋅ 3 16 + 3 ⋅ 3 16 + 6 ⋅ 1 16 = 15 8 . 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 = a 1 p 1 a 2 p 2 . . . . . a k p k n={a_1}^{p_1}{a_2}^{p_2}.....{a_k}^{p_k} n=a1p1a2p2.....akpk。经过k次变换后就相当于是 p i p_i pi发生了变化。
可以发现素因子之间并没有相互影响,可以单独求出 p i p_i pi的概率,然后求出 a i a_i ai对于结果的期望贡献,然后累乘。
对于 a k a_k ak d [ i ] [ j ] d[i][j] d[i][j]表示经过 i i i次变换后 a k a_k ak的幂 p k = j p_k=j pk=j的概率,那么 a k a_k ak的期望贡献为 ∑ i = 0 m d [ m ] [ i ] ∗ a k i \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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值