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

版权声明:http://blog.csdn.net/Mitsuha_。 https://blog.csdn.net/Mitsuha_/article/details/85848144

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

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) 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 .

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}.

思路: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} 。经过k次变换后就相当于是 p i p_i 发生了变化。
可以发现素因子之间并没有相互影响,可以单独求出 p i p_i 的概率,然后求出 a i a_i 对于结果的期望贡献,然后累乘。
对于 a k a_k d [ i ] [ j ] d[i][j] 表示经过 i i 次变换后 a k a_k 的幂 p k = j p_k=j 的概率,那么 a k a_k 的期望贡献为 i = 0 m d [ m ] [ i ] a k i \sum_{i=0}^m d[m][i]*{a_k}^{i}

#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;
}

猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/85848144