2443: Confused 逆序数推排列数

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)表示逆序数为mn元排列的个数,则

| 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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值