cf1097D. Makoto and a Blackboard(期望dp)

自為風月馬前卒發表於2019-01-05

題意

題目連結

Sol

首先考慮當(n = p^x),其中(p)是質數,顯然它的因子只有(1, p, p^2, dots p^x)(最多logn個)

那麼可以直接dp, 設(f[i][j])表示經過了(i)輪,當前數是(p^j)的概率,轉移的時候列舉這一輪的(p^j)轉移一下

然後我們可以把每個質因子分開算,最後乘起來就好了

至於這樣為什麼是對的。(開始瞎扯),考慮最後的每個約數,它一定是一堆質因子的乘積,而每個質因子的概率我們是知道的,所以這樣乘起來算是沒問題的。

其實本質上還是因為約數和/個數都是積性函式

時間複雜度:(O(sqrt{n} + k log^2 n))

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long 
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << `
`;}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int N, K, top, inv[MAXN];
Pair st[MAXN];
int f[10001][1001];
int fp(int a, int p) {
    int base = 1;
    while(p) {
        if(p & 1) base = mul(base, a);
        a = mul(a, a); p >>= 1;
    }
    return base;
}
int solve(int a, int b) {//a^b
    memset(f, 0, sizeof(f));
    f[0][b] = 1;
    for(int i = 1; i <= K; i++) 
        for(int j = 0; j <= b; j++) 
            for(int k = j; k <= b; k++) 
                add2(f[i][j], mul(f[i - 1][k], inv[k + 1]));
    int res = 0;
    for(int i = 0; i <= b; i++) add2(res, mul(f[K][i], fp(a, i)));
    return res;
}
signed main() {
    for(int i = 1; i <= 666; i++) inv[i] = fp(i, mod - 2);
    cin >> N >> K;
    for(int i = 2; i * i <= N; i++) {
        if(!(N % i)) {
            st[++top] = MP(i, 0);
            while(!(N % i)) st[top].se++, N /= i;
        }
    }
    if(N) st[++top] = MP(N, 1);
    int ans = 1;
    for(int i = 1; i <= top; i++)
        ans = mul(ans, solve(st[i].fi, st[i].se));
    cout << ans;
    return 0;
}
/*
2
(()))
(
*/

相關文章