[BZOJ2431][HAOI2009]逆序对数列(DP)

前缀和优化一下DP

O(n3)->O(n2)

Code

#include <cstdio>
#define N 1010
const int mo=1e4;
int n,m,f[N][N],x;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;f[i++][0]=1);
	for(int i=2;i<=n;++i){
		x=f[i-1][0];
		for(int j=1;j<=m;++j){
			if(j-i>=0) x-=f[i-1][j-i];
			x+=f[i-1][j];
			f[i][j]=x%mo;
		}
	} 
	printf("%d\n",f[n][m]);
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/void-f/p/9123293.html