牛客挑战赛46 C
概率就是组合数分之一,现在需要求组合数有多少。
因为是排列,每一次长度加1,就是第i个插入的过程,考虑
\(dp[i][j][k]\)表示第\(i\)个数字插入到\(k\)的位置,\(逆序数是j\)。
分段转移(因为超级逆序数表示\(a_I>a_j+1\)):
当\(i-1<k\)的时候,有\(dp[i][j][k]=\sum_{m=0}^{m=k-1}dp[i-1][j-i+k][m]\)
当\(i>=k\)的时候,有\(dp[i][j][k]=\sum_{m=k}^{m=i-1}dp[i-1][j-i+k+1][m]\)
第一维数组,我们可以滚动掉,求和很明显可以再开一个数组记录,既可以o(1)求和。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#include<unordered_map>
#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
#pragma GCC target("avx","sse2")
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 510;
ll dp[2][N][N], sum[N][N];
const int mod = 998244353;
int n, k;
ll quick_pow(ll a, ll b = mod - 2) {
ll res = 1;
while (b) {
if (b & 1)res = res * a%mod;
a = a * a%mod;
b >>= 1;
}
return res;
}
void add(ll &a, ll b) {
a = (a + b >= mod ? a + b - mod : a + b);
}
int main() {
n = read(), k = read();
upd(i, 1, 500) {
upd(j, 1, 500)dp[0][i][j] = dp[1][i][j] = 1e18;
}
int now = 0;
dp[1][0][1] = 1;
sum[0][1] = 1;
upd(i, 2, n) {
upd(j, 0, k)
{
upd(p, 1, i) {
ll ss = 0;
if (j - i + p>=0)add(ss, (sum[j - i + p][p - 1] - sum[j - i + p][0] + mod) % mod);
if (j - i + p + 1>=0)add(ss, (sum[j - i + p + 1][i - 1] - sum[j - i + p + 1][p - 1] + mod) % mod);
dp[now][j][p] = ss;
}
}
upd(j, 0, k)upd(p, 0, i)sum[j][p] = 0;
upd(j, 0, k) {
upd(p, 1, i) {
if (dp[now][j][p] != 1e18)
{
add(sum[j][p], dp[now][j][p]); add(sum[j][p], sum[j][p - 1]);
}
}
}
now ^= 1;
}
//cout << sum[k][n];
printf("%lld\n", quick_pow(sum[k][n]));
return 0;
}
橙橙橙