📜  找出f(n)/ f(r)* f(nr)的值

📅  最后修改于: 2021-06-26 22:45:27             🧑  作者: Mango

给定两个正整数n和r,其中n> r> 1。任务是找到f(n)/(f(r)* f(nr))的值。 F(n)定义如下:

例子:

Input: n = 5, r = 3
Output: 1/200000

Input: n = 3, r = 2
Output: 1/27

解决此问题的一种简单方法是分别计算f(n),f(r)和f(nr),然后根据给定的公式计算结果,但是这会花费大量时间。
解决此问题的更好方法是在r和nr之间找到更大的值,然后使用给定的特性f(n)= f(n-1)* n -n = f(n-1)/ n n函数,从分子和分母中消除f(r)和f(nr)中的较大者。之后,使用简单的循环和幂函数计算其余的值。
算法:

下面是上述方法的实现:

C++
// CPP to find the value of f(n)/f(r)*f(n-r)
#include 
using namespace std;
 
// Function to find value of given F(n)
int calcFunction(int n, int r)
{
    int finalDenominator = 1;
    int mx = max(r, n - r);
 
    // iterate over n
    for (int i = mx + 1; i <= n; i++) {
 
        // calculate result
        int denominator = (int)pow(i, i);
        int numerator = (int)pow(i - mx, i - mx);
        finalDenominator = (finalDenominator
                            * denominator)
                           / numerator;
    }
 
    // return the result
    return finalDenominator;
}
 
// Driver code
int main()
{
    int n = 6, r = 2;
    cout << "1/" << calcFunction(n, r) << endl;
 
    return 0;
}


Java
// Java program to find the value of f(n)/f(r)*f(n-r) 
 
class GFG {
// Function to find value of given F(n)
 
    static int calcFunction(int n, int r) {
        int finalDenominator = 1;
        int mx = Math.max(r, n - r);
 
        // iterate over n
        for (int i = mx + 1; i <= n; i++) {
 
            // calculate result
            int denominator = (int) Math.pow(i, i);
            int numerator = (int) Math.pow(i - mx, i - mx);
            finalDenominator = (finalDenominator
                    * denominator)
                    / numerator;
        }
 
        // return the result
        return finalDenominator;
    }
 
// Driver code
    public static void main(String[] args) {
        int n = 6, r = 2;
        System.out.println("1/" + calcFunction(n, r));
 
    }
}
// This code is contributed by RAJPUT-JI


Python 3
# Python3 to find the value of f(n)/f(r)*f(n-r)
 
# Function to find value of given F(n)
def calcFunction(n, r):
 
    finalDenominator = 1
    mx = max(r, n - r)
 
    # iterate over n
    for i in range(mx + 1, n + 1):
 
        # calculate result
        denominator = pow(i, i)
        numerator = pow(i - mx, i - mx)
        finalDenominator = (finalDenominator *
                            denominator) // numerator
 
    # return the result
    return finalDenominator
 
# Driver code
if __name__ == "__main__":
     
    n = 6
    r = 2
    print("1/", end = "")
    print(calcFunction(n, r))
 
# This code is contributed by ita_c


C#
// C# program to find the value of f(n)/f(r)*f(n-r)
using System;
 
public class GFG {
// Function to find value of given F(n)
 
    static int calcFunction(int n, int r) {
        int finalDenominator = 1;
        int mx = Math.Max(r, n - r);
 
        // iterate over n
        for (int i = mx + 1; i <= n; i++) {
 
            // calculate result
            int denominator = (int) Math.Pow(i, i);
            int numerator = (int) Math.Pow(i - mx, i - mx);
            finalDenominator = (finalDenominator
                    * denominator)
                    / numerator;
        }
 
        // return the result
        return finalDenominator;
    }
 
// Driver code
   public static void Main() {
        int n = 6, r = 2;
        Console.WriteLine("1/" + calcFunction(n, r));
 
    }
}
// This code is contributed by RAJPUT-JI


PHP


Javascript


输出:
1/36450000