[HDU] [2049] Not Easy Series (4) - Testing the Groom

Problem Description

Loading...

Problem Analysis

This problem also got me stuck for quite a while
At first I thought it was just simple permutations and combinations
But now it looks like it’s actually a Derangement problem

What’s different from an ordinary Derangement is that here the grooms are distinct from one another
So which ones are matched correctly (and which ones are matched incorrectly) also differs
Therefore, this must also be taken into account when counting the possibilities

An array is used here to store the Derangement data
Another array stores the factorial data
This saves time

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main() {
	long long a[21]= {0,0,1};
	long long b[21]= {1};
	for(int i=3; i<21; i++) {
		a[i]=(i-1)*(a[i-1]+a[i-2]);
	}
	for(int i=1; i<21; i++) {
		b[i]=i*b[i-1];
	}
	int T;
	scanf("%d",&T);
	while(T--) {
		int n,m;
		scanf("%d %d",&n,&m);
		printf("%lld\n",b[n]/b[n-m]/b[m]*a[m]);
	}
	return 0;
}

Problem link: [HDU][2049] Not Easy Series (4) — Testing the Groom

Loading...