UVA11806容斥原理,位运算排列,递推组合数

本文介绍了一种利用容斥原理解决组合数问题的方法,并通过递推公式计算所有可能的组合数,最后采用位运算来处理四个集合的选择问题。

可能是本人太渣了,当时看到这个题的做法感觉太巧妙了,起初自己推公式推了半天没有推出来,

然后就开始看书,书上用容斥原理完美解决了这个问题,同时使用递推公式推出了所有组合数,

然后使用位运算讨论4个集合的选择问题,代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#define LL long long
using namespace std;
const int mod=1e6+7;
const int MAXN=500+10;
int t,m,n,k;
int C[MAXN][MAXN];
int main()
{
    memset(C,0,sizeof(C));
    C[0][0]=1;
    for(int i=0;i<=500;i++)
    {
        C[i][0]=C[i][i]=1;
        for(int j=1;j<i;j++)
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    cin>>t;
    for(int kase=1;kase<=t;kase++)
    {
        cin>>m>>n>>k;
        int sum=0;
        for(int S=0;S<16;S++)
        {
            int b=0,r=n,c=m;
            if(S&1) {r--;b++;}
            if(S&2) {r--;b++;}
            if(S&4) {c--;b++;}
            if(S&8) {c--;b++;}
            if(b&1) sum=(sum+mod-C[r*c][k])%mod;
            else sum=(sum+C[r*c][k])%mod;
        }
        printf("Case %d: %d\n",kase,sum);
    }
    return 0;
}


12000 K-Transformed Permutations Consider a sequence of n integers < 1 2 3 4 ... n >. Since all the values are distinct, we know that there are n factorial permutations. A permutation is called K-transformed if the absolute difference between the original position and the new position of every element is at most K. Given n and K, you have to find out the total number of K-transformed permutations. Example: n = 4, K =2 1 2 3 4 Valid Annotation (position) P1 1234 Yes Theoriginalsequence. All the elements are in their original position P2 1243 Yes 3and4arereordered, but each is shifted by 1 position only. P3 1324 Yes P4 1342 Yes 2isshiftedby2positions. 2≤K, so it’s a valid one. P5 1423 Yes P6 1432 Yes P7 2134 Yes P8 2143 Yes P9 2314 Yes P10 2 3 4 1 No 1isshiftedby3positions. 3>K and so this is an invalid permutation P11 2 4 1 3 Yes P12 2 4 3 1 No P13 3 1 2 4 Yes P14 3 1 4 2 Yes P15 3 2 1 4 Yes P16 3 2 4 1 No P17 3 4 1 2 Yes P18 3 4 2 1 No P19 4 1 2 3 No 4isshiftedby3positions. 3>K and so this is also invalid P20 4 1 3 2 No P21 4 2 1 3 No P22 4 2 3 1 No P23 4 3 1 2 No P24 4 3 2 1 No Here,both4and1are breaking the property. So, for the above case, there are 14 2-transformed permutations. Input The first line of input is an integer T (T < 20) that indicates the number of test cases. Each case consists of a line containing two integers n and K. (1 ≤ n ≤ 109) and (0 ≤ K ≤ 3). Output For each case, output the case number first followed by the required result. Since the result could be huge, output result modulo 73405. UniversidaddeValladolidOJ:12000–K-TransformedPermutations 2/2 SampleInput 3 4 2 100 0 101 SampleOutput Case1:14 Case2:1 Case3:89 UVA12000 可以給我C++法分析嗎
最新发布
08-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值