Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 12024 - Hats
Source : UVA Online Judge
Category : Combinatorics
Algorithm : Derangement
Verdict : Accepted
- #include <bits/stdc++.h>
-
- using namespace std;
-
- #define ll long long
-
- static const int maxn = 21;
-
- ll nCr[maxn][maxn];
- ll fact[maxn];
-
- void factorial()
- {
- fact[0] = 1;
- for (int i = 1; i <= 20; i++) fact[i] = i * fact[i-1];
- }
-
- void combination()
- {
- nCr[0][0] = 1;
- for (int i = 1; i <= 20; i++)
- {
- nCr[i][i] = 1;
- nCr[i][0] = 1;
- for (int j = 1; j < i; j++)
- {
- nCr[i][j] = nCr[i-1][j] + nCr[i-1][j-1];
- }
- }
- }
-
- int main()
- {
- factorial();
- combination();
-
- int tc;
- scanf("%d", &tc);
- for (int tcase = 1; tcase <= tc; tcase++)
- {
- int n;
- scanf("%d", &n);
- ll derangement = fact[n];
- for (int i = 1; i <= n; i++)
- {
- if (i & 1) derangement -= nCr[n][i] * fact[n-i];
- else derangement += nCr[n][i] * fact[n-i];
- }
- printf("%lld/%lld\n", derangement, fact[n]);
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.