# [前缀和优化dp][HAOI 2009] 逆序对数列

4 1

3

## 测试数据范围

30%的数据 n<=12
100%的数据 n<=1000，k<=1000

## 题解

$$L=max\left(0,j-i+1\right),R=min\left(\frac{\left(i-1\right)\left(i-2\right)}{2},j\right)$$
$$dp\left[i\right]\left[j\right]=\sum_{k=L}^{R}dp\left[i-1\right]\left[k\right]$$
$$Sum\left[i\right]\left[j\right]=\sum_{j} d p\left[i\right]\left[j\right]$$
$$dp\left[i\right]\left[j\right]=Sum\left[i-1\right]\left[R\right]-Sum\left[i-1\right]\left[L-1\right]$$

## Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

const int MOD=10000;
int dp[1005][1005],Sum[1005][2];
int N,M;

int main(){
scanf("%d%d",&N,&M);
int sta=1;
for(register int i=1;i<=N;++i){
dp[i][0]=1;
Sum[0][sta]=1;
for(register int j=1;j<=min(M,i*(i-1)/2);++j){
int L=max(0,j-i+1);
int R=min((i-1)*(i-2)/2,j);
if(L==0) dp[i][j]=Sum[R][sta^1]%MOD;
else dp[i][j]=((Sum[R][sta^1]-Sum[L-1][sta^1])%MOD+MOD)%MOD;
Sum[j][sta]=(Sum[j-1][sta]+dp[i][j])%MOD;
}
sta^=1;
}
printf("%d\n",dp[N][M]%MOD);
return 0;
}

posted @ 2020-02-20 10:56  AE酱  阅读(140)  评论(0编辑  收藏  举报