Solução do Problema Christmas Party do CSES
Resolução problema Christmas Party do CSES.
Solução do Problema Christmas Party do CSES
Problem
There are \(n\) children at a Christmas party, and each of them has brought a gift. The idea is that everybody will get a gift brought by someone else. In how many ways can the gifts be distributed?
Input
The only input line has an integer \(n\): the number of children.
Output
Print the number of ways modulo 10^9+7.
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
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define int long long
void solve()
{
int n;
cin >> n;
int vec[n + 1];
vec[1] = 0;
vec[2] = 1;
for(int i = 3; i <= n; i++) // derangement formula -> Conta as permutações em que os elementos não aparecem nas posições originais
vec[i] = (((vec[i-1] + vec[i-2]) % MOD) * (i -1)) % MOD;
cout << vec[n] << endl;
}
signed main()
{
int t;
t = 1;
while(t--)
{
solve();
}
}
This post is licensed under CC BY 4.0 by the author.