在 N 的表示中计数包含一个设置位作为最高有效位的基数
给定一个正整数N ,任务是计算不同基数的数量,在这些基数中,当表示N时,发现N的最高有效位是一个设置位。
例子:
Input: N = 6
Output: 4
Explanation: The number 6 can be represented in 5 different bases, i.e. base 2, base 3, base 4, base 5, base 6.
- (6)10 in base 2: (110)2
- (6)10 in base 3: (20)3
- (6)10 in base 4: (12)4
- (6)10 in base 5: (11)5
- (6)10 in base 6: (10)6
The base representation for (6)10 in base 2, base 4, base 5, base 6 starts with 1. Hence, the required count of bases is 4.
Input: N = 5
Output: 4
方法:给定问题可以通过为每个可能的碱基找到给定数N的 MSB 并计算那些MSB为1的碱基来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如count为0 ,以存储所需的结果。
- 使用变量B遍历范围[2, N]并执行以下步骤:
- 存储base B的最高功率 需要在变量P中表示数字N 。这可以通过找到(以B为底的log N )的值来轻松实现,即log B (N)被截断为最接近的整数。
- 要查找log B (N)的值,请使用 log 属性: log B (N) = log(N)/log(B)
- 存储N的最高位 通过将N除以(B) P 。如果它等于1 ,则将计数值增加 1。
- 完成上述步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count bases
// having MSB of N as a set bit
int countOfBase(int N)
{
// Store the required count
int count = 0;
// Iterate over the range [2, N]
for (int i = 2; i <= N; ++i) {
int highestPower
= (int)(log(N) / log(i));
// Store the MSB of N
int firstDigit = N / (int)pow(
i, highestPower);
// If MSB is 1, then increment
// the count by 1
if (firstDigit == 1) {
++count;
}
}
// Return the count
return count;
}
// Driver Code
int main()
{
int N = 6;
cout << countOfBase(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
public static int countOfBase(int N)
{
// Store the required count
int count = 0;
// Iterate over the range [2, N]
for (int i = 2; i <= N; ++i) {
int highestPower
= (int)(Math.log(N) /Math.log(i));
// Store the MSB of N
int firstDigit = N / (int)Math.pow(
i, highestPower);
// If MSB is 1, then increment
// the count by 1
if (firstDigit == 1) {
++count;
}
}
// Return the count
return count;
}
// DRIVER CODE
public static void main (String[] args)
{
int N = 6;
System.out.println(countOfBase(N));
}
}
// This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count bases
// having MSB of N as a set bit
static int countOfBase(int N)
{
// Store the required count
int count = 0;
// Iterate over the range [2, N]
for(int i = 2; i <= N; ++i)
{
int highestPower = (int)(Math.Log(N) /
Math.Log(i));
// Store the MSB of N
int firstDigit = N / (int)Math.Pow(
i, highestPower);
// If MSB is 1, then increment
// the count by 1
if (firstDigit == 1)
{
++count;
}
}
// Return the count
return count;
}
// Driver Code
public static void Main()
{
int N = 6;
Console.Write(countOfBase(N));
}
}
// This code is contributed by ipg2016107
Javascript
Python3
# Python 3 program for the above approach
import math
# Function to count bases
# having MSB of N as a set bit
def countOfBase(N) :
# Store the required count
count = 0
# Iterate over the range [2, N]
for i in range(2, N+1):
highestPower = int(math.log(N) / math.log(i))
# Store the MSB of N
firstDigit = int(N / int(math.pow(i, highestPower)))
# If MSB is 1, then increment
# the count by 1
if (firstDigit == 1) :
count += 1
# Return the count
return count
# Driver Code
N = 6
print(countOfBase(N))
输出:
4
时间复杂度: O(N)
辅助空间: O(1)