zoukankan      html  css  js  c++  java
  • HDU 2049 不容易系列之(4)——考新郎( 错排 )


    链接:传送门
    思路:错排水题,从N个人中选出M个人进行错排,即 C(n,m)*d[m]
    补充:组合数C(n,m)能用double计算吗?第二部分有解释


    Part 1.

    1. 分别求出来组合数的分子和分母然后相除

    /*************************************************************************
        > File Name: hdu2049.cpp
        > Author:    WArobot 
        > Blog:      http://www.cnblogs.com/WArobot/ 
        > Created Time: 2017年04月27日 星期四 22时50分49秒
     ************************************************************************/
    
    #include<bits/stdc++.h>
    using namespace std;
    
    #define ull unsigned long long
    
    int c,n,m;
    ull  d[23];
    
    void init(){
    	d[1] = 0;	d[2] = 1;
    	for(int i=3;i<23;i++)	d[i] = (i-1)*(d[i-1] + d[i-2]);
    }
    int main(){
    	init();
    	scanf("%d",&c);
    	while(c--){
    		scanf("%d%d",&n,&m);
    		ull ck1 = 1 , ck2 = 1;
    		for(int i=1;i<=m;i++)	ck1 *= (n-m+i) , ck2 *= i;
    
    		printf("%lld
    ",(ck1/ck2)*d[m]);
    	}
    	return 0;
    }
    

    Part 2.

    1. double也是可以计算C(n,m),但是需要注意在最后强制类型转换的时候需要手动 +0.5 来手动进行四舍五入
    2. 可以直接输出%.25lf 来查看ck1的值
      例如20 11的时候 ck1 = 167959.9999999999708961695432663
      但是C(20,11) = 167960 所以有些值需要手动进位

    /*************************************************************************
        > File Name: hdu2049.cpp
        > Author:    WArobot 
        > Blog:      http://www.cnblogs.com/WArobot/ 
        > Created Time: 2017年04月27日 星期四 22时50分77秒
     ************************************************************************/
    #include<bits/stdc++.h>
    using namespace std;
    
    
    #define ll long long
    
    int c,n,m;
    ll  d[21];
    
    void init(){
        d[1] = 0;   d[2] = 1;
        for(int i=3;i<=20;i++)   d[i] = (i-1)*(d[i-1] + d[i-2]);
    }
    int main(){
        init();
        scanf("%d",&c);
        while(c--){
    	scanf("%d%d",&n,&m);
    	double ck1 = 1;
    	for(int i=1;i<=m;i++)   ck1 *= (n-m+i)*1.0/i;
    	ck1 += 0.5;
    	ll ans = (ll)ck1;
    	printf("%lldn",ans*d[m]);
        }
        return 0;
    }
    

    <font color = bule ,size = "4">好吧,我还因为过于无聊设置了小彩蛋,你找到这个随笔那个地方写错了吗?

  • 相关阅读:
    Luogu2751 [USACO Training4.2]工序安排Job Processing
    BZOJ4653: [Noi2016]区间
    BZOJ1537: [POI2005]Aut- The Bus
    CF1041F Ray in the tube
    POJ1186 方程的解数
    Luogu2578 [ZJOI2005]九数码游戏
    BZOJ2216: [Poi2011]Lightning Conductor
    CF865D Buy Low Sell High
    BZOJ1577: [Usaco2009 Feb]庙会捷运Fair Shuttle
    几类区间覆盖
  • 原文地址:https://www.cnblogs.com/WArobot/p/6777822.html
Copyright © 2011-2022 走看看