Codeforces 913F Strongly Connected Tournament dp(看题解)

Strongly Connected Tournament

好菜啊, 根本不会写啊。。。

这种一下分成很多块的dp题, 可以考虑最后一部分, 前面的继续往下分的时候再考虑。

题解讲的很清楚啦。

https://codeforces.com/blog/entry/56992

感觉就是每一部分都不难, 但是真的很难想到呀。

还有就是以后遇到自己转移到自己的优先考虑解方程, 不要想当然。。。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 2000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-10;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

inline int power(int a, int b) {
    int ans = 1;
    while(b) {
        if(b & 1) ans = 1LL * ans * a % mod;
        a = 1LL * a * a % mod; b >>= 1;
    }
    return ans;
}

inline int inv(int x) {
    return power(x, mod - 2);
}

int n, a, b, p, q;

int dp[N], cp[N][N], scc[N];

int main() {

    scanf("%d%d%d", &n, &a, &b);
    p = 1LL * a * inv(b) % mod;
    q = (1 - p + mod) % mod;

    for(int i = 0; i <= n; i++) {
        cp[i][0] = 1;
        for(int j = 1; j <= i; j++) {
            cp[i][j] = 1LL * cp[i - 1][j] * power(q, j) % mod + 1LL * cp[i - 1][j - 1] * power(p, i - j) % mod;
            if(cp[i][j] >= mod) cp[i][j] -= mod;
        }
    }
    for(int i = 0; i <= n; i++) {
        scc[i] = 1;
        for(int j = 1; j < i; j++) {
            sub(scc[i], 1LL * scc[j] * cp[i][j] % mod);
        }
    }

    dp[1] = 0;

    for(int i = 2; i <= n; i++) {
        for(int j = 1; j < i; j++) {
            int P = 1LL * scc[j] * cp[i][j] % mod;
            int C = (1LL * j * (j - 1) / 2 % mod + 1LL * (i - j) * j % mod + dp[j] + dp[i - j]) % mod;
            add(dp[i], 1LL * P * C % mod);
        }
        add(dp[i], 1LL * scc[i] * i * (i - 1) / 2 % mod);
        dp[i] = 1LL * dp[i] * inv(1 - scc[i] + mod) % mod;
    }

    printf("%d\n", dp[n]);

    return 0;
}

/*

ans[i] = x + scc[i] *(i * (i - 1 / 2 + ans[i]))
ans[i] = x + (scc[i] * i * (i - 1) / 2) + scc[i] * ans[i]
ans[i] = (x + (scc[i] * i * (i - 1) / 2)) / (1 - scc[i])


*/

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/11123208.html