hdu 5225 Tom and permutation

Tom and permutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 425    Accepted Submission(s): 199


Problem Description
Tom has learned how to calculate the number of inversions in a permutation of n distinct objects by coding, his teacher gives him a problem:
Give you a permutation of n distinct integer from 1 to n, there are many permutations of 1-n is smaller than the given permutation on dictionary order, and for each of them, you can calculate the number of inversions by coding. You need to find out sum total of them.
Tom doesn't know how to do, he wants you to help him.
Because the number may be very large, output the answer to the problem modulo 109+7 .
 

Input
Multi test cases(about 20). For each case, the first line contains a positive integer n, the second line contains n integers, it's a permutation of 1-n.
n100
 

Output
For each case, print one line, the answer to the problem modulo 109+7 .
 

Sample Input
  
  
3 2 1 3 5 2 1 4 3 5
 

Sample Output
  
  
1 75
Hint
The input may be very big, we might as well optimize input.
 

Source
题目分析:
数位dp,首先利用排列组合的知识很容易知道,设dp[i]表示i位任意排列得到的逆序数的个数
那么dp[i] = sigma ( (j-1)*A(i-1,i-1) + dp[i-1] ) , j < i;
也就是当前首位分别是各个数字导致的逆序数,和后面随机排列导致的内部出现的逆序数
然后就是对于每个序列,
对于第i位,若当前位置为比num[i]小的数,那么后面的数可以随机排列,如果当前数为i,那么继续后推,记录前面已经确定的数字对于后面造成的逆序数,后面不管如何排列,前面的数导致的逆序数的个数无变化,那么利用数位dp的思想就很简单了,注意大整数做乘法不要溢出,利用同余模定理进行取模
 
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define MAX 107
#define MOD (1000000007LL)

using namespace std;

typedef long long LL;

int n;
LL dp[MAX];
LL f[MAX];
bool used[MAX];
int num[MAX];
LL sum[MAX];

void init ( )
{
    f[1] = 1;
    for ( LL i = 2 ; i < MAX ; i++ )
        f[i] = f[i-1]*i%MOD;
    dp[1] = 0;
    dp[2] = 1;
    for ( int i = 3 ; i < MAX ; i++ )
        for ( int j = 1 ; j <= i ; j++ )
            dp[i]  = ((dp[i] + f[i-1]*(LL)(j-1)%MOD)%MOD+dp[i-1])%MOD;
}

int main ( )
{
    init ( );
    while ( ~scanf ( "%d" , &n ) )
    {
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%d" , &num[i] );
        memset ( used , 0 , sizeof ( used ) );
        LL ans = 0;
        sum[0] = 0;
        for ( int i = 1 ; i < n ; i++ )
        {
            int temp = 0;
            for ( int j = i+1 ; j <= n ; j++ )
                if ( num[i] > num[j] ) temp++;
            sum[i] = sum[i-1] + temp;
        }
        for ( int i = 1 ; i <= n ; i++ )
        {
            for ( int j = 1 ; j < num[i] ; j++ )
                if ( !used[j] )
                {
                    int temp = 0;
                    for ( int k = 1 ; k < j ; k++ )
                        if ( !used[k] ) temp++;
                    ans += (dp[n-i] + (temp+sum[i-1])*f[n-i]%MOD)%MOD;
                    ans %= MOD;
                }
            used[num[i]] = 1;
        }
        printf ( "%lld\n" , ans );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值