Let d[i] be the number of ways we can split i*4 golfers into groups of 4 golfers each.
We have d[1] = 1 and d[i] = d[i - 1] * choose(i * 4, 4) / i, where choose(x, y) is the binomial coefficient, calculated using the formula: choose(x, y) = x! / y! / (x-y)!.
When going from d[i-1] to d[i], we can pick any 4 golfers to be in the newly formed group (choose(i * 4, 4)). However, we need to discount repetitions, thus need to divide by the number of groups i.
The final solution is d[N / 4]R.
Perl solution (Marius Gavrilescu)
In languages with builtin big numbers, we can compute d[N / 4] directly. Since d[N / 4]R has more than 200 digits, we will print d[N / 4]^R.1 #!/usr/bin/perl 2 use v5.14; 3 use warnings; 4 use bigint; 5 6 my ($n, $r) = split ' ', <>; 7 my $ans = 1; 8 $ans *= (4 * $_)->bnok(4) / $_ for 2 .. $n / 4; 9 say "$ans^$r";
C solution (Marius Gavrilescu)
We know that choose(K,4)=K!/(K-4)!/4! = K*(K-1)*(K-2)*(K-3)/24.
By expanding the definition of d we get d[N/4] = N*(N-1)*(N-2)*(N-3) / 24 * (N-4)*(N-5)*(N-6)*(N-7) / 24 * ... * 4*3*2*1 / 24 / (N/4)! which is equal to N! / (N/4)! / 24^(N/4).
Thus, the answer is (N*(N-1)*...*(N/4+1)/24^K)^R.
1 #include <stdio.h> 2 3 int main(void) { 4 int n, k; 5 scanf("%d%d", &n, &k); 6 7 printf("(%d", n); 8 for(int i = n - 1 ; i > n / 4 ; i--) 9 printf("*%d", i); 10 printf("/24^%d)^%d\n", n / 4, k); 11 return 0; 12 }
C++ solution (Gabriel Inelus)
We denote the number of ways to split N golfers into groups of 4 each when the order is important by the function F(x) = x choose 4 * F(x-4). To avoid the repetitions we can divide the answer by (x/4)! and then raise it to the power R. It's easy to observe that x choose 4 = x*(x-1)*(x-2)*(x-3)/24, which is less than 231, thus we can give a straight forward recursive implementation of F(N).1 #include <bits/stdc++.h> 2 using namespace std; 3 4 void F(int k){ 5 cout << k*(k-1)*(k-2)*(k-3) / 24; 6 if(k == 4) return; 7 cout << "*"; 8 F(k - 4); 9 } 10 11 int main() 12 { 13 int N,R; 14 cin >> N >> R; 15 cout << "(("; F(N); cout << ")"; 16 for(int i = 2; i <= N/4; ++i) 17 cout << "/" << i; 18 cout << ")^" << R; 19 return 0; 20 }