UVa 11481 Arrange the Number(容斥原理)

来源:互联网 发布:网络安全设备巡检报告 编辑:程序博客网 时间:2024/05/20 11:36

从M中选定K个不变的数是C[M][K]种方案,对于每种方案容斥,在剩下的N-K个数中不能有不变的,那么就要减掉有一个不变的,加上有两个不变的,减去3个不变的。。。以此类推。


代码:

////  main.cpp//  11481 Arrange the Number////  Created by Baoli1100 on 15/2/28.//  Copyright (c) 2015年 Baoli1100. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>using namespace std;#include <algorithm>#define LL long longconst LL mod=1000000007;LL C[1005][1005];LL k[1005];int N,M,K;int main(){    C[0][0]=1;    for(int i=1;i<=1000;i++){        C[i][0]=1;C[i][i]=1;        for(int j=1;j<i;j++){            C[i][j]=C[i-1][j-1]+C[i-1][j];            C[i][j]%=mod;        }    }    k[0]=1;    for(int i=1;i<=1000;i++){        k[i]=k[i-1]*i;        k[i]%=mod;    }    int T;    cin>>T;    for(int kase=1;kase<=T;kase++){        scanf("%d%d%d",&N,&M,&K);        LL res=0;        for(int i=0;i<=M-K;i++){            if(i&1){                res-=(C[M-K][i]*k[N-K-i])%mod;                while(res<0) res+=mod;            }            else {                res+=C[M-K][i]*k[N-K-i];                res%=mod;            }        }        res*=C[M][K];        res%=mod;        printf("Case %d: %lld\n",kase,res);    }    return 0;}


0 0