📌  相关文章
📜  N的基数B表示形式中尾随零的数目!

📅  最后修改于: 2021-06-26 21:26:21             🧑  作者: Mango

给定两个正整数B和N。任务是找出N!的b进制(基数B)表示形式中尾随零的数目! (N的阶乘)

例子:

Input: N = 5, B = 2
Output: 3
5! = 120 which is represented as 1111000 in base 2. 

Input: N = 6, B = 9
Output: 1

一个幼稚的解决方案是找到给定数字的阶乘并将其转换为给定的基数B。然后,计算尾随零的数量,但这将是一项昂贵的操作。同样,要找到大数阶乘并将其存储为整数也不容易。

高效的方法:假设基数是10,即十进制,那么我们将必须计算10的最高幂除以N!使用Legendre的公式。因此,将数字B转换为基数B时表示为10。假设基数B = 13,则基数13中的13将表示为10,即13 10 = 10 13 。因此,问题减少到找到N!中B的最高幂。 (k在n中的最大幂!)

下面是上述方法的实现。

C++
// CPP program to find the number of trailing
// zeroes in base B representation of N!
#include 
using namespace std;
  
// To find the power of a prime p in
// factorial N
int findPowerOfP(int N, int p)
{
    int count = 0;
    int r = p;
    while (r <= N) {
  
        // calculating floor(n/r)
        // and adding to the count
        count += (N / r);
  
        // increasing the power of p
        // from 1 to 2 to 3 and so on
        r = r * p;
    }
    return count;
}
  
// returns all the prime factors of k
vector > primeFactorsofB(int B)
{
    // vector to store all the prime factors
    // along with their number of occurrence
    // in factorization of B
    vector > ans;
  
    for (int i = 2; B != 1; i++) {
        if (B % i == 0) {
            int count = 0;
            while (B % i == 0) {
                B = B / i;
                count++;
            }
  
            ans.push_back(make_pair(i, count));
        }
    }
    return ans;
}
  
// Returns largest power of B that
// divides N!
int largestPowerOfB(int N, int B)
{
    vector > vec;
    vec = primeFactorsofB(B);
    int ans = INT_MAX;
    for (int i = 0; i < vec.size(); i++)
  
        // calculating minimum power of all
        // the prime factors of B
        ans = min(ans, findPowerOfP(N,
                                    vec[i].first)
                           / vec[i].second);
  
    return ans;
}
  
// Driver code
int main()
{
    cout << largestPowerOfB(5, 2) << endl;
    cout << largestPowerOfB(6, 9) << endl;
    return 0;
}


Java
// Java program to find the number of trailing
// zeroes in base B representation of N!
import java.util.*;
class GFG 
{
static class pair
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// To find the power of a prime p in
// factorial N
static int findPowerOfP(int N, int p)
{
    int count = 0;
    int r = p;
    while (r <= N)
    {
  
        // calculating floor(n/r)
        // and adding to the count
        count += (N / r);
  
        // increasing the power of p
        // from 1 to 2 to 3 and so on
        r = r * p;
    }
    return count;
}
  
// returns all the prime factors of k
static Vector primeFactorsofB(int B)
{
    // vector to store all the prime factors
    // along with their number of occurrence
    // in factorization of B
    Vector ans = new Vector();
  
    for (int i = 2; B != 1; i++)
    {
        if (B % i == 0) 
        {
            int count = 0;
            while (B % i == 0)
            {
                B = B / i;
                count++;
            }
  
            ans.add(new pair(i, count));
        }
    }
    return ans;
}
  
// Returns largest power of B that
// divides N!
static int largestPowerOfB(int N, int B)
{
    Vector vec = new Vector();
    vec = primeFactorsofB(B);
    int ans = Integer.MAX_VALUE;
    for (int i = 0; i < vec.size(); i++)
  
        // calculating minimum power of all
        // the prime factors of B
        ans = Math.min(ans, findPowerOfP(
                       N, vec.get(i).first) / 
                          vec.get(i).second);
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    System.out.println(largestPowerOfB(5, 2));
    System.out.println(largestPowerOfB(6, 9));
}
}
  
// This code is contributed by Princi Singh


Python3
# Python 3 program to find the number of 
# trailing zeroes in base B representation of N!
import sys
  
# To find the power of a prime 
# p in factorial N
def findPowerOfP(N, p):
    count = 0
    r = p
    while (r <= N):
          
        # calculating floor(n/r)
        # and adding to the count
        count += int(N / r)
  
        # increasing the power of p
        # from 1 to 2 to 3 and so on
        r = r * p 
      
    return count
  
# returns all the prime factors of k
def primeFactorsofB(B):
      
    # vector to store all the prime factors
    # along with their number of occurrence
    # in factorization of B'
    ans = []
    i = 2
  
    while(B!= 1):
        if (B % i == 0):
            count = 0
            while (B % i == 0):
                  
                B = int(B / i)
                count += 1
  
            ans.append((i, count))
  
        i += 1
      
    return ans
  
# Returns largest power of B that
# divides N!
def largestPowerOfB(N, B):
    vec = []
    vec = primeFactorsofB(B)
    ans = sys.maxsize
      
    # calculating minimum power of all
    # the prime factors of B
    ans = min(ans, int(findPowerOfP(N, vec[0][0]) / 
                                       vec[0][1]))
  
    return ans
  
# Driver code
if __name__ == '__main__':
    print(largestPowerOfB(5, 2))
    print(largestPowerOfB(6, 9))
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find the number of trailing
// zeroes in base B representation of N!
using System;
using System.Collections.Generic; 
  
class GFG 
{
public class pair
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// To find the power of a prime p in
// factorial N
static int findPowerOfP(int N, int p)
{
    int count = 0;
    int r = p;
    while (r <= N)
    {
  
        // calculating floor(n/r)
        // and adding to the count
        count += (N / r);
  
        // increasing the power of p
        // from 1 to 2 to 3 and so on
        r = r * p;
    }
    return count;
}
  
// returns all the prime factors of k
static List primeFactorsofB(int B)
{
    // vector to store all the prime factors
    // along with their number of occurrence
    // in factorization of B
    List ans = new List();
  
    for (int i = 2; B != 1; i++)
    {
        if (B % i == 0) 
        {
            int count = 0;
            while (B % i == 0)
            {
                B = B / i;
                count++;
            }
  
            ans.Add(new pair(i, count));
        }
    }
    return ans;
}
  
// Returns largest power of B that
// divides N!
static int largestPowerOfB(int N, int B)
{
    List vec = new List();
    vec = primeFactorsofB(B);
    int ans = int.MaxValue;
    for (int i = 0; i < vec.Count; i++)
  
        // calculating minimum power of all
        // the prime factors of B
        ans = Math.Min(ans, findPowerOfP(
                       N, vec[i].first) / 
                          vec[i].second);
  
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    Console.WriteLine(largestPowerOfB(5, 2));
    Console.WriteLine(largestPowerOfB(6, 9));
}
}
  
// This code is contributed by 29AjayKumar


输出:
3
1