题目大意
问你有多少个由(n)个数组成的,逆序对个数为(k)的排列。
(n,kleq 1000)
题解
我们考虑从小到大插入这(n)个数。
设当前插入了(i)个数,插入下一个数可以形成(0,1,ldots,i-1)个逆序对。
[f_{i,j}=sum_{k=j-i+1}^jf_{i-1,k}
]
用前缀和优化即可。
时间复杂度:(O(nk))
UPD:这个问题可以做到(O(nlog n))(FFT)或(O(nsqrt n))(五边形数定理)。((nk)同阶)
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void sort(int &a,int &b)
{
if(a>b)
swap(a,b);
}
void open(const char *s)
{
#ifndef ONLINE_JUDGE
char str[100];
sprintf(str,"%s.in",s);
freopen(str,"r",stdin);
sprintf(str,"%s.out",s);
freopen(str,"w",stdout);
#endif
}
int f[1010][1010];
int s[1010][1010];
int p=10000;
int main()
{
// open("bzoj2143");
int n,k;
scanf("%d%d",&n,&k);
f[0][0]=1;
int i,j;
for(i=0;i<=k;i++)
s[0][i]=1;
for(i=1;i<=n;i++)
for(j=0;j<=k;j++)
{
f[i][j]=s[i-1][j];
if(j-i+1>=1)
f[i][j]=(f[i][j]-s[i-1][j-i])%p;
s[i][j]=f[i][j];
if(j>=1)
s[i][j]=(s[i][j]+s[i][j-1])%p;
}
int ans=f[n][k];
ans=(ans+p)%p;
printf("%d
",ans);
return 0;
}