hdu 2049 不容易系列之(4)——考新郎 (错排公式,注意精度)
hdu 2049 题目链接 题意:n个妹子和n个汉子对应。。然后让每个汉子取选一个妹子,不能重复,问恰好有m个汉子选错妹子的可能的方案数。
思路:从n个中选n-m个,然后做剩余m个错排即可。
答案就是c[n][n-m]*d[m] c[]为组合数公式,d为错排公式。 然而wa到死。。。
因为我用了double….有毒。。。
double表示整数也是会丢失精度的!!!
double表示整数也是会丢失精度的!!!
double表示整数也是会丢失精度的!!!
double表示整数也是会丢失精度的!!!
double表示整数也是会丢失精度的!!!
我自杀去了,世界再见(x
1/* ***********************************************
2Author :111qqz
3Created Time :2016年07月27日 星期三 16时39分21秒
4File Name :code/hdu/2049.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=22;
34LL d[N];
35LL c[N][N];
36int n,m;
37
38void cal_c()
39{
40 c[1][0] = 1;
41 c[1][1] = 1;
42 c[2][0] = 1;
43 c[2][1] = 2;
44 c[2][2] = 1;
45 for ( int i = 1 ; i < N; i++) c[i][0] = 1;
46 for ( int i = 3 ; i < N ; i++)
47 for ( int j = 1 ; j <= i ;j++)
48 c[i][j] = c[i-1][j]+c[i-1][j-1];
49}
50int main()
51{
52 #ifndef ONLINE_JUDGE
53 freopen("code/in.txt","r",stdin);
54 #endif
55
56 cal_c();
57 d[1] = 0 ;
58 d[2] = 1;
59 for ( int i = 3 ; i < N ; i++) d[i] = (i*1LL-1)*(d[i-1]+d[i-2]);
60 int T;
61 cin>>T;
62 while (T--)
63 {
64 scanf("%d%d",&n,&m);
65 LL ans = d[m];
66 ans*=c[n][m];
67 printf("%lld\n",ans);
68 }
69
70
71 #ifndef ONLINE_JUDGE
72 fclose(stdin);
73 #endif
74 return 0;
75}
