UVa12024 - Hats(错排问题)

Background

John Hatman, the honest cloakroom attendant of the RoyalTheatre of London, would like to know the solution to the followingproblem.

TheProblem

Whenthe show finishes, all spectators in the theatre are in a hurry to see the Final of the UEFAChampionship. So, they run to the cloakroom to take their hats back.

Some of them take a wrong hat. But, how likely is thateveryone take a wrong hat?

TheInput

The first lineof the input contains an integer, t,indicating the number of test cases. For each test case, one lineappears, that contains anumber n, 2<=n<=12,representing the number of people and hats.

TheOutput

For each test case, the output should contain asingle line with the number representing the number of favourable cases(i.e., the number of cases where all people take a wrong hat),followed by a bar, "/", and followed by a number representing thetotal number of possible cases.

SampleInput

3
2
3
4

SampleOutput

1/2
2/6
9/24
题意 :求错排个数与排列的个数之比

思路:错排递推关系式为f(n) = (n-1)(f(n - 2) + f(n - 1)),排列的递推式为d(n) = n * d(n -1)

#include <cstdio>

using namespace std;

const int MAXN = 13;

typedef long long LL;

LL f[MAXN], fact[MAXN];
int n;

void init()
{
    f[0] = 0; f[1] = 0; f[2] = 1;
    fact[1] = 1; fact[2] = 2;
    
    for (int i = 3; i < MAXN; i++) {
        f[i] = (i - 1) * (f[i - 2] + f[i - 1]);
        fact[i] = i * fact[i - 1];
    }
}

void input()
{
    scanf("%d", &n);
}

void solve()
{
    printf("%d/%d\n", f[n], fact[n]);
}

int main(int argc, char **argv) 
{
#ifndef ONLINE_JUDGE
    freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
    
    init();
    
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        input();
        solve();
    }
    
    return 0;
}




你可能感兴趣的:(UVa12024 - Hats(错排问题))