cf1097D. Makoto and a Blackboard(期望dp)

题意

题目链接

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 << '\n';}
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
(()))
(
*/

猜你喜欢

转载自www.cnblogs.com/zwfymqz/p/10224565.html