Inverse
The inverse of permutation (p1,p2,…,pn) is the number of pairs (i,j) which i<j and pi>pj .
Count the number of permutaion of {1,2,…,n} , whose inverse is k .
Input
Two integers n , k .
(1≤n≤100,0≤k≤n(n−1)2)
Ouptut
An integer denotes the numbers of permutaions modulo (109+7) .
Sample input
2 1
Sample output
1
基础动态规划。关键是想清楚n个排列数与n-1个排列数的递推关系。
计dp[x][y]: 表示x个数所组成的逆序个数为y的个数。
那么状态转移方程为:dp[x][y+k]+=dp[x-1][k], 0<=k<=x-1.
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <stack>
#include <deque>
using namespace std;
typedef long long LL;
#define eps 10e-9
#define inf 0x3f3f3f3f
const int maxn = 100000+100;
int dp[110][10000];
int main(){
dp[1][0]=1; dp[1][1]=0;
for(int i=2;i<=100;i++){
for(int j=0;j<=(i-1)*(i-2)/2;j++){
for(int k=0;k<=i-1;k++)
dp[i][j+k]=(dp[i][j+k]+dp[i-1][j])%(1000000000+7);
}
}
int n,k;
while(scanf("%d %d",&n,&k)!=EOF){
printf("%d\n",dp[n][k]);
}
return 0;
}