Problem Description
Problem Analysis
A combinatorics problem
The idea here is that more than half are answered correctly
so fewer than half are completely wrong
so it is also a derangement problem
each time choose which people are correct, and derange the rest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| #include<stdio.h>
long long CMI[30][30];
void GetCMI() {
for(int i=0; i<=25; i++)
CMI[i][0]=1;
for(int i=1; i<=25; i++) {
for(int j=1; j<=i; j++) {
CMI[i][j]=(CMI[i-1][j-1]+CMI[i-1][j]);
}
}
}
long long jie(int m) {
long long sum=1;
while(m)
sum*=m--;
return sum;
}
int main() {
long long a[15]= {0,0,1};
for(int i=3; i<15; i++)
a[i]=(i-1)*(a[i-1]+a[i-2]);
GetCMI();
int n;
while(scanf("%d",&n),n) {
int t=n/2;
long long sum=1;
for(int i=2; i<=t; i++) {
sum+=CMI[n][n-i]*a[i];
}
printf("%lld\n",sum);
}
return 0;
}
|
Problem link: HDU [2068] RPG’s Derangement