由于三个数N,K和B,任务是检查如果N只含有K作为在基B位。
例子:
Input: N = 13, B = 3, K = 1
Output: Yes
Explanation:
13 base 3 is 111 which contain all one’s(K).
Input: N = 5, B = 2, K = 1
Output: No
Explanation:
5 base 2 is 101 which doesn’t contains all one’s (K).
简单方法:一种简单的解决方案是通过一个检查,以给定的数目N转换成基极B和一个,如果所有其数字是K或不。
时间复杂度:O(d),其中d是在数N的位数
辅助空间: O(1)
有效途径:在问题的关键发现是,任何数量的在碱B中的所有位为K可以表示为:
这些项采用几何级数的形式,第一项为 K,公比为 B。
GP系列总和:
因此,所有数字为K的基数B中的数字是:
因此,只要检查这个总和等于N与否。如果相等,然后打印“是”,否则打印“否”。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the number of digits
int findNumberOfDigits(int n, int base)
{
// Calculate log using base change
// property and then take its floor
// and then add 1
int dig = (floor(log(n) / log(base)) + 1);
// Return the output
return (dig);
}
// Function that returns true if n contains
// all one's in base b
int isAllKs(int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
// Calculate the sum
int sum = k * (1 - pow(b, len)) /
(1 - b);
if(sum == n)
{
return(sum);
}
}
// Driver code
int main()
{
// Given number N
int N = 13;
// Given base B
int B = 3;
// Given digit K
int K = 1;
// Function call
if (isAllKs(N, B, K))
{
cout << "Yes";
}
else
{
cout << "No";
}
}
// This code is contributed by vikas_g
C
// C implementation of the approach
#include
#include
// Function to print the number of digits
int findNumberOfDigits(int n, int base)
{
// Calculate log using base change
// property and then take its floor
// and then add 1
int dig = (floor(log(n) / log(base)) + 1);
// Return the output
return (dig);
}
// Function that returns true if n contains
// all one's in base b
int isAllKs(int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
// Calculate the sum
int sum = k * (1 - pow(b, len)) /
(1 - b);
if(sum == n)
{
return(sum);
}
}
// Driver code
int main(void)
{
// Given number N
int N = 13;
// Given base B
int B = 3;
// Given digit K
int K = 1;
// Function call
if (isAllKs(N, B, K))
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
// This code is contributed by vikas_g
Java
// Java implementation of above approach
import java.util.*;
class GFG{
// Function to print the number of digits
static int findNumberOfDigits(int n, int base)
{
// Calculate log using base change
// property and then take its floor
// and then add 1
int dig = ((int)Math.floor(Math.log(n) /
Math.log(base)) + 1);
// Return the output
return dig;
}
// Function that returns true if n contains
// all one's in base b
static boolean isAllKs(int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
// Calculate the sum
int sum = k * (1 - (int)Math.pow(b, len)) /
(1 - b);
return sum == n;
}
// Driver code
public static void main(String[] args)
{
// Given number N
int N = 13;
// Given base B
int B = 3;
// Given digit K
int K = 1;
// Function call
if (isAllKs(N, B, K))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
import math
# Function to print the number of digits
def findNumberOfDigits(n, base):
# Calculate log using base change
# property and then take its floor
# and then add 1
dig = (math.floor(math.log(n) /
math.log(base)) + 1)
# Return the output
return dig
# Function that returns true if n contains
# all one's in base b
def isAllKs(n, b, k):
len = findNumberOfDigits(n, b)
# Calculate the sum
sum = k * (1 - pow(b, len)) / (1 - b)
return sum == N
# Driver code
# Given number N
N = 13
# Given base B
B = 3
# Given digit K
K = 1
# Function call
if (isAllKs(N, B, K)):
print("Yes")
else:
print("No")
C#
// C# implementation of above approach
using System;
class GFG{
// Function to print the number of digits
static int findNumberOfDigits(int n, int bas)
{
// Calculate log using base change
// property and then take its floor
// and then add 1
int dig = ((int)Math.Floor(Math.Log(n) /
Math.Log(bas)) + 1);
// Return the output
return dig;
}
// Function that returns true if n contains
// all one's in base b
static bool isAllKs(int n, int b, int k)
{
int len = findNumberOfDigits(n, b);
// Calculate the sum
int sum = k * (1 - (int)Math.Pow(b, len)) /
(1 - b);
return sum == n;
}
// Driver code
public static void Main()
{
// Given number N
int N = 13;
// Given base B
int B = 3;
// Given digit K
int K = 1;
// Function call
if (isAllKs(N, B, K))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by vikas_g
Javascript
输出:
Yes
时间复杂度:O(日志(d)),其中d是在数N的位数
辅助空间: O(1)