[HDU] [2048] God, the Lord, and Heaven

Problem Description

Loading...

Problem Analysis

Derangement problem
that is,
a[i]=(i-1)*(a[i-1]+a[i-2])

This involves factorials
and I didn’t want to change the data type either
so, from the pattern, I added one conditional check

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

Problem link: HDU 2048: God, God and the Lord

Loading...