给定一个正整数N ,任务是计算范围[1, N]中整数的数量,可以表示为a b ,其中a和b是大于1 的整数。
例子:
Input: N = 6
Output: 1
Explanation:
Only such integer from the range [1, 6] is 4 (= 22).
Therefore, the required count is 1.
Input: N = 10
Output: 3
方法:可以通过计算所有可能的元素对(a, b)来解决给定的问题,使得a b至多为 N 。请按照以下步骤解决问题:
- 初始化一个 HashSet 以存储a b 的所有可能值,最多为 N 。
- 遍历范围[2,1√N],并为的每一个值,至多N,插入一个具有B值的所有可能值,其中b位于在范围[1,N]。
- 完成上述步骤后,将 HashSet 的大小打印为整数的结果计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the integers
// up to N that can be represented
// as a ^ b, where a &b > 1
void printNumberOfPairs(int N)
{
// Initialize a HashSet
unordered_set st;
// Iterating over the range
// [2, sqrt(N)]
for(int i = 2; i * i <= N; i++)
{
int x = i;
// Generate all possible
// power of x
while (x <= N)
{
// Multiply x by i
x *= i;
// If the generated number
// lies in the range [1, N]
// then insert it in HashSet
if (x <= N)
{
st.insert(x);
}
}
}
// Print the total count
cout << st.size();
}
// Driver code
int main()
{
int N = 10000;
printNumberOfPairs(N);
return 0;
}
// This code is contributed by Kingash
Java
// Java program for the above approach
import java.util.HashSet;
public class GFG {
// Function to count the integers
// up to N that can be represented
// as a ^ b, where a &b > 1
static void printNumberOfPairs(int N)
{
// Initialize a HashSet
HashSet st
= new HashSet();
// Iterating over the range
// [2, sqrt(N)]
for (int i = 2; i * i <= N; i++) {
int x = i;
// Generate all possible
// power of x
while (x <= N) {
// Multiply x by i
x *= i;
// If the generated number
// lies in the range [1, N]
// then insert it in HashSet
if (x <= N) {
st.add(x);
}
}
}
// Print the total count
System.out.println(st.size());
}
// Driver Code
public static void main(String args[])
{
int N = 10000;
printNumberOfPairs(N);
}
}
Python3
# Python 3 program for the above approach
from math import sqrt
# Function to count the integers
# up to N that can be represented
# as a ^ b, where a &b > 1
def printNumberOfPairs(N):
# Initialize a HashSet
st = set()
# Iterating over the range
# [2, sqrt(N)]
for i in range(2, int(sqrt(N)) + 1, 1):
x = i
# Generate all possible
# power of x
while(x <= N):
# Multiply x by i
x *= i
# If the generated number
# lies in the range [1, N]
# then insert it in HashSet
if (x <= N):
st.add(x)
# Print the total count
print(len(st))
# Driver code
if __name__ == '__main__':
N = 10000
printNumberOfPairs(N)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count the integers
// up to N that can be represented
// as a ^ b, where a &b > 1
static void printNumberOfPairs(int N)
{
// Initialize a HashSet
HashSet st = new HashSet();
// Iterating over the range
// [2, sqrt(N)]
for(int i = 2; i * i <= N; i++)
{
int x = i;
// Generate all possible
// power of x
while (x <= N)
{
// Multiply x by i
x *= i;
// If the generated number
// lies in the range [1, N]
// then insert it in HashSet
if (x <= N)
{
st.Add(x);
}
}
}
// Print the total count
Console.WriteLine(st.Count);
}
// Driver Code
public static void Main(string[] args)
{
int N = 10000;
printNumberOfPairs(N);
}
}
// This code is contributed by ukasp
Javascript
输出:
124
时间复杂度: O(N log N)
辅助空间: O(N)