📌  相关文章
📜  可以使用给定的一组数字生成的不同 N 位奇数整数的计数

📅  最后修改于: 2022-05-13 01:56:05.599000             🧑  作者: Mango

可以使用给定的一组数字生成的不同 N 位奇数整数的计数

给定一个大小为N的数组arr[]表示从09的数字,任务是计算可以使用数组中给定数字形成的不同奇数N位整数的数量。

例子:

方法:给定的问题可以通过以下观察来解决:

  • 对于奇数,它的个位(即第 1)应该有一个奇数,即 {1, 3, 5, 7, 9}
  • 由于整数应该有N位,因此最高有效位(N 位的数字)不能等于0
  • 除了第 1和第 N的数字之外的所有数字都可以有任何其他数字。
  • X位数的排列方式总数是X! / ( freq[0]! * freq[1]! *…* freq[9]! ) ,其中freq[i]表示给定X位中第i位的频率。

为了解决这个问题,请跟踪变量odd中奇数位数和变量zero中等于 0 的位数。所以根据上面的观察,如果i代表第N位, j代表第1位,迭代ij的所有可能值,对于每个有效的(i, j) ,计算排列方式的数量剩余的(N-2) 个数字。

下面是上述方法的实现:

C++14
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to find the count of distinct
// odd integers with N digits using the
// given digits in the array arr[]
int countOddIntegers(int arr[], int N)
{
    // Stores the factorial of a number
    int Fact[N] = {};
 
    // Calculate the factorial of all
    // numbers from 1 to N
    Fact[0] = 1;
    for (int i = 1; i < N; i++) {
        Fact[i] = i * Fact[i - 1];
    }
 
    // Stores the frequency of each digit
    int freq[10] = {};
    for (int i = 0; i < N; i++) {
        freq[arr[i]]++;
    }
 
    // Stores the final answer
    int ans = 0;
 
    // Loop to iterate over all values of
    // Nth digit i and 1st digit j
    for (int i = 1; i <= 9; i += 2) {
 
        // If digit i does not exist in
        // the given array move to next i
        if (!freq[i])
            continue;
 
        // Fixing i as Nth digit
        freq[i]--;
 
        for (int j = 1; j <= 9; j++) {
 
            // Stores the answer of a specific
            // value of i and j
            int cur_ans = 0;
 
            // If digit j does not exist
            // move to the next j
            if (freq[j] == 0) {
                continue;
            }
 
            // Fixing j as 1st digit
            freq[j]--;
 
            // Calculate number of ways to
            // arrange remaining N-2 digits
            cur_ans = Fact[N - 2];
            for (int k = 0; k <= 9; k++) {
                cur_ans = cur_ans / Fact[freq[k]];
            }
            ans += cur_ans;
 
            // Including j back into
            // the set of digits
            freq[j]++;
        }
        // Including i back into the
        // set of the digits
        freq[i]++;
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 3, 4, 1, 2, 3 };
    int N = sizeof(A) / sizeof(int);
 
    // Function Call
    cout << countOddIntegers(A, N);
 
    return 0;
}


Java
// Java program of the above approach
import java.util.*;
 
class GFG{
 
// Function to find the count of distinct
// odd integers with N digits using the
// given digits in the array arr[]
static int countOddIntegers(int arr[], int N)
{
     
    // Stores the factorial of a number
    int Fact[] = new int[N];
 
    // Calculate the factorial of all
    // numbers from 1 to N
    Fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        Fact[i] = i * Fact[i - 1];
    }
 
    // Stores the frequency of each digit
    int freq[] = new int[10];
    for(int i = 0; i < N; i++)
    {
        freq[arr[i]]++;
    }
 
    // Stores the final answer
    int ans = 0;
 
    // Loop to iterate over all values of
    // Nth digit i and 1st digit j
    for(int i = 1; i <= 9; i += 2)
    {
         
        // If digit i does not exist in
        // the given array move to next i
        if (freq[i] == 0)
            continue;
 
        // Fixing i as Nth digit
        freq[i]--;
 
        for(int j = 1; j <= 9; j++)
        {
             
            // Stores the answer of a specific
            // value of i and j
            int cur_ans = 0;
 
            // If digit j does not exist
            // move to the next j
            if (freq[j] == 0)
            {
                continue;
            }
 
            // Fixing j as 1st digit
            freq[j]--;
 
            // Calculate number of ways to
            // arrange remaining N-2 digits
            cur_ans = Fact[N - 2];
            for(int k = 0; k <= 9; k++)
            {
                cur_ans = cur_ans / Fact[freq[k]];
            }
            ans += cur_ans;
 
            // Including j back into
            // the set of digits
            freq[j]++;
        }
         
        // Including i back into the
        // set of the digits
        freq[i]++;
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 2, 3, 4, 1, 2, 3 };
    int N = A.length;
 
    // Function Call
    System.out.print(countOddIntegers(A, N));
}
}
 
// This code is contributed by code_hunt


Python3
# Python Program for the above approach
from array import *
from math import *
 
# Function to find the count of distinct
# odd integers with N digits using the
# given digits in the array arr[]
def countOddIntegers(arr, N):
   
    # Stores the factorial of a number
    # Calculate the factorial of all
    # numbers from 1 to N
    Fact = [0] * N
    Fact[0] = 1
    for i in range(1,N):
        Fact[i] = i * Fact[i - 1]
     
    # Stores the frequency of each digit
    freq= [0] * 10
    for i in range(len(freq)):
        freq[i] = 0;
    for i in range(N):
        freq[arr[i]] = freq[arr[i]] + 1;
     
 
    # Stores the final answer
    ans = 0
 
    # Loop to iterate over all values of
    # Nth digit i and 1st digit j
    for i in range(1, 10, 2) :
 
        # If digit i does not exist in
        # the given array move to next i
        if (freq[i] == 0):
            continue
 
        # Fixing i as Nth digit
        freq[i] = freq[i] - 1;
 
        for j in range(1, 10, 1) :
 
            # Stores the answer of a specific
            # value of i and j
            cur_ans = 0
 
            # If digit j does not exist
            # move to the next j
            if (freq[j] == 0) :
                continue
             
            # Fixing j as 1st digit
            freq[j]=freq[j]-1;
 
            # Calculate number of ways to
            # arrange remaining N-2 digits
            cur_ans = Fact[N - 2]
            for k in range(10):
                cur_ans = cur_ans / Fact[freq[k]]
             
            ans += cur_ans
 
            # Including j back into
            # the set of digits
            freq[j] = freq[j] + 1;
         
        # Including i back into the
        # set of the digits
        freq[i] = freq[i] + 1;
 
    # Return Answer
    return ceil(ans)
 
# Driver Code
if __name__ ==  "__main__":
    A = [ 2, 3, 4, 1, 2, 3 ]
    N = len(A)
 
    # Function Call
    print(countOddIntegers(A, N))
 
    # This code is contributed by anudeep23042002.


C#
// C# program of the above approach
using System;
 
class GFG{
 
// Function to find the count of distinct
// odd integers with N digits using the
// given digits in the array arr[]
static int countOddIntegers(int []arr, int N)
{
     
    // Stores the factorial of a number
    int []Fact = new int[N];
 
    // Calculate the factorial of all
    // numbers from 1 to N
    Fact[0] = 1;
    for(int i = 1; i < N; i++)
    {
        Fact[i] = i * Fact[i - 1];
    }
 
    // Stores the frequency of each digit
    int []freq = new int[10];
    for(int i = 0; i < N; i++)
    {
        freq[arr[i]]++;
    }
 
    // Stores the final answer
    int ans = 0;
 
    // Loop to iterate over all values of
    // Nth digit i and 1st digit j
    for(int i = 1; i <= 9; i += 2)
    {
         
        // If digit i does not exist in
        // the given array move to next i
        if (freq[i] == 0)
            continue;
 
        // Fixing i as Nth digit
        freq[i]--;
 
        for(int j = 1; j <= 9; j++)
        {
             
            // Stores the answer of a specific
            // value of i and j
            int cur_ans = 0;
 
            // If digit j does not exist
            // move to the next j
            if (freq[j] == 0)
            {
                continue;
            }
 
            // Fixing j as 1st digit
            freq[j]--;
 
            // Calculate number of ways to
            // arrange remaining N-2 digits
            cur_ans = Fact[N - 2];
            for(int k = 0; k <= 9; k++)
            {
                cur_ans = cur_ans / Fact[freq[k]];
            }
            ans += cur_ans;
 
            // Including j back into
            // the set of digits
            freq[j]++;
        }
         
        // Including i back into the
        // set of the digits
        freq[i]++;
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 2, 3, 4, 1, 2, 3 };
    int N = A.Length;
 
    // Function Call
    Console.Write(countOddIntegers(A, N));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
90

时间复杂度: O(N * 50)
辅助空间: O(1)