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
/* ***********************************************
Author :111qqz
Created Time :2016年07月27日 星期三 16时39分21秒
File Name :code/hdu/2049.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=22;
7LL d[N];
8LL c[N][N];
9int n,m;
1void cal_c()
2{
3 c[1][0] = 1;
4 c[1][1] = 1;
5 c[2][0] = 1;
6 c[2][1] = 2;
7 c[2][2] = 1;
8 for ( int i = 1 ; i < N; i++) c[i][0] = 1;
9 for ( int i = 3 ; i < N ; i++)
10 for ( int j = 1 ; j <= i ;j++)
11 c[i][j] = c[i-1][j]+c[i-1][j-1];
12}
13int main()
14{
15 #ifndef ONLINE_JUDGE
16 freopen("code/in.txt","r",stdin);
17 #endif
1 cal_c();
2 d[1] = 0 ;
3 d[2] = 1;
4 for ( int i = 3 ; i < N ; i++) d[i] = (i*1LL-1)*(d[i-1]+d[i-2]);
5 int T;
6 cin>>T;
7 while (T--)
8 {
9 scanf("%d%d",&n,&m);
10 LL ans = d[m];
11 ans*=c[n][m];
12 printf("%lld\n",ans);
13 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}