HDU 2049 不容易系列之(4)——考新郎( 错排 )
生活随笔
收集整理的這篇文章主要介紹了
HDU 2049 不容易系列之(4)——考新郎( 错排 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:傳送門
思路:錯排水題,從N個人中選出M個人進行錯排,即 C(n,m)*d[m]
補充:組合數C(n,m)能用double計算嗎?第二部分有解釋
Part 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 longint 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\n",(ck1/ck2)*d[m]);}return 0; }
Part 2.
例如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 longint 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">好吧,我還因為過于無聊設置了小彩蛋,你找到這個隨筆那個地方寫錯了嗎?
轉載于:https://www.cnblogs.com/WArobot/p/6777822.html
總結
以上是生活随笔為你收集整理的HDU 2049 不容易系列之(4)——考新郎( 错排 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四个好看的table表格样式
- 下一篇: R语言软件安装