随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

1∼n 的排列 A 中,满足前 m 个数中,刚好有 K 个数使得 A[ i ]=i AA 的个数。

 

错位排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
#define int long long
 int c[1002][1002], F[1002] ;
 void init(){
    int i,j;
    c[1][1]=1;
    for(i=0;i<=1000;i++)  c[i][0]=1;
    for(i=2;i<=1000;i++)
     for(j=1;j<=i;j++)
        c[i][j]=c[i-1][j-1]+c[i-1][j],c[i][j]%=mod;
         
    F[0]=1;for(i=1;i<=1000;i++) F[i]=F[i-1]*i,F[i]%=mod;
 }
 int E(int n,int m){
    int t= 0;
    for(int i=0;i<=m;i++){
        if(i%2==0)
            t=(t+c[m][i]*F[n-i]%mod),t%=mod;
        else
            t=(t-c[m][i]*F[n-i]%mod+mod)%mod;
    }
    return t;
 }
 void sov(int cas){
    int n,m,K;
    cin>>n>>m>>K;
    cout<<"Case "<<cas<<": "<<
    c[m][K]*E(n-K,m-K)%mod<<endl;
 }
 signed main(){
    init();
    int tes;cin>>tes;
    int cas=0;
    while(tes--) sov(++cas);
 }

 

posted on   towboat  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示