Consider a sequence of N integers where each integer between 1 and N appears exactly once. A pair of numbers in the sequence is confused if the number that comes earlier in the sequence is larger than the later number. The confusion of the sequence is the number of confused pairs in it. For example, the confusion of the sequence (1, 4, 3, 2) is 3 because there are 3 confused pairs: (4, 3), (4, 2) and (3, 2). Write a program that calculates the number of sequences of length N whose confusion is exactly C.
Input
The first and only line of input contains two integers, N (1 <= N <= 1000) and C (0 <= C <= 10000).
Output
Output the number of sequences modulo 1 000 000 007.
Sample Input
10 1 4 3 9 13
Sample Output
9 6 17957
动态规划:f(n,m)表示逆序数为m的n元排列的个数,则
| f(n+1,m)=f(n,m)+f(n,m-1)+...+f(n,m-n)(当b<0时,f(a,b)=0)
| 优化 又考虑到如果直接利用上式计算时间复杂度为O(n^3),我们分析上
| 式不难发现f(n+1,m)=f(n,m)+f(n+1,m-1)
| if( m-n-1 >= 0 ) f(n+1, m) -= f(n, m-n-1).
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1001;
const int C=10001;
const int MOD = 1000000007;
int a[N][C];
void confused()
{
a[1][0]=a[2][0]=a[2][1]=1;
for(int i=3;i<N;i++)
{
a[i][0]=1;
int h=i*(i+1)/2;
if(h>C-1) h=C-1;
for(int j=1;j<=h;j++)
{
a[i][j]=(a[i-1][j]+a[i][j-1])%MOD;
if(j>=i)
{
a[i][j]-=a[i-1][j-i];
if(a[i][j]<0) a[i][j]+=MOD;
}
}
}
}
int main()
{
confused();
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
printf("%d/n",a[n][m]);
}
return 0;
}