码迷,mamicode.com
首页 > 其他好文 > 详细

51nod1020 逆序排列

时间:2017-12-22 22:56:50      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:stdio.h   fine   sum   hid   ring   scanf   ons   分享   printf   

t<=10000个问,每次问n<=1000的全排列中逆序数对为k<=10000个的有多少,mod 1e9+7。

直接dp,$f(i,j)$--i的全排列中逆序数对为j的有多少,$f(i,j)=\sum_{k=max(0,j-i+1)}^{j} f(i-1,k)$,这东西记个前缀和即可n^2。

然后就没了

技术分享图片
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<stdlib.h>
 5 //#include<queue>
 6 //#include<math.h>
 7 //#include<time.h>
 8 //#include<iostream>
 9 using namespace std;
10 
11 int t,n,K;
12 #define maxn 1011
13 #define maxm 20011
14 
15 const int mod=1e9+7;
16 int f[maxn][maxm];
17 int main()
18 {
19     n=1000,K=20000;
20     f[1][0]=1;
21     for (int i=2;i<=n;i++)
22     {
23         f[i][0]=1;
24         for (int j=1,to=min(i*(i-1)/2,K);j<=to;j++)
25             f[i][j]=((f[i][j-1]+f[i-1][j])%mod-(j>=i?f[i-1][j-i]:0)+mod)%mod;
26     }
27     scanf("%d",&t);
28     while (t--) {scanf("%d%d",&n,&K); printf("%d\n",f[n][K]);}
29     return 0;
30 }
View Code

 

51nod1020 逆序排列

标签:stdio.h   fine   sum   hid   ring   scanf   ons   分享   printf   

原文地址:http://www.cnblogs.com/Blue233333/p/8087767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!