FallingFactorialPower
SRM 405 · 2008-06-14 · by Petr
Problem Statement
A number n taken to the falling factorial power k is defined as n*(n-1)*...*(n-k+1). We will denote it by n^^k. For example, 7^^3=7*6*5=210. By definition, n^^1=n.
We will now continue this definition to the non-positive values of k using the following fact: (n-k)*(n^^k)=n^^(k+1), or, in other words, n^^k=(n^^(k+1))/(n-k). It is directly derived from the above definition.
By using it, we find:
- n^^0=n^^1/(n-0)=1,
- n^^(-1)=n^^0/(n+1)=1/(n+1),
- n^^(-2)=1/(n+1)/(n+2),
- and, in general, n^^(-k)=1/(n+1)/(n+2)/.../(n+k).
Given a positive
Notes
- Your return must have relative or absolute error less than 1E-9.
Constraints
- n will be between 1 and 10, inclusive.
- k will be between -5 and 5, inclusive.
7 3 Returns: 210.0
7^^3=7*6*5=210.
10 1 Returns: 10.0
5 0 Returns: 1.0
3 -1 Returns: 0.25
2 -3 Returns: 0.016666666666666666
Submissions are judged against all 115 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class FallingFactorialPower with a public method double compute(int n, int k) · 115 test cases · 2 s / 256 MB per case