[L, R] 范围内的数字计数,可以表示为两个完美幂的和
给定一个范围[L, R] ,任务是找到范围[L, R]中可以 表示为两个完美幂的和。
例子:
Input: L = 0, R = 1
Output: 2
Explanation:
The valid numbers are:
- 1 as it can be expressed as, 1 = 12 + 02.
- 0 as it can be expressed as, 0 = 02 + 02.
Therefore, the count of such numbers is 2.
Input: L = 5, R = 8
Output: 2
Explanation:
The valid numbers are:
- 5 as it can be expressed as, 5 = 12 + 22.
- 8 as it can be expressed as, 0 = 02 + 23.
Therefore, the count of such numbers is 2.
方法:给定的问题可以通过使用一些数学观察来解决。请按照以下步骤解决问题:
- 从2生成小于R的所有数字的所有可能幂,并将这些数字存储在数组pow[]中。
- 初始化一个布尔数组,例如arr[]大小(R + 1)为0 。
- 生成数组pow[]的所有可能的不同对,如果对的总和最多为 R ,则在数组arr[]中将其标记为1 。
- 现在,找到数组arr[]的前缀和。
- 完成上述步骤后,打印arr[R] – arr[L – 1]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of numbers
// that can be expressed in the form of
// the sum of two perfect powers
int TotalPerfectPowerSum(long long L,
long long R)
{
// Stores all possible powers
vector pows;
// Push 1 and 0 in it
pows.push_back(0);
pows.push_back(1);
// Iterate over all the exponents
for (int p = 2; p < 25; p++) {
// Iterate over all possible numbers
long long int num = 2;
// This loop will run for a
// maximum of sqrt(R) times
while ((long long int)(pow(num, p) + 0.5) <= R) {
// Push this power in
// the array pows[]
pows.push_back(
(long long int)(pow(num, p) + 0.5));
// Increase the number
num++;
}
}
// Stores if i can be expressed as
// the sum of perfect power or not
int ok[R + 1];
memset(ok, 0, sizeof(ok));
// Iterate over all possible pairs
// of the array pows[]
for (int i = 0;
i < pows.size(); i++) {
for (int j = 0;
j < pows.size(); j++) {
if (pows[i] + pows[j] <= R
and pows[i] + pows[j] >= L) {
// The number is valid
ok[pows[i] + pows[j]] = 1;
}
}
}
// Find the prefix sum of the
// array ok[]
for (int i = 0; i <= R; i++) {
ok[i] += ok[i - 1];
}
// Return the count of required number
return ok[R] - ok[L - 1];
}
// Driver Code
signed main()
{
int L = 5, R = 8;
cout << TotalPerfectPowerSum(L, R);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the number of numbers
// that can be expressed in the form of
// the sum of two perfect powers
static int TotalPerfectPowerSum(int L, int R)
{
// Stores all possible powers
ArrayList pows = new ArrayList();
// Push 1 and 0 in it
pows.add(0);
pows.add(1);
// Iterate over all the exponents
for (int p = 2; p < 25; p++) {
// Iterate over all possible numbers
int num = 2;
// This loop will run for a
// maximum of sqrt(R) times
while ((int)(Math.pow(num, p) + 0.5) <= R) {
// Push this power in
// the array pows[]
pows.add((int)(Math.pow(num, p) + 0.5));
// Increase the number
num++;
}
}
// Stores if i can be expressed as
// the sum of perfect power or not
int[] ok = new int[R + 2];
// memset(ok, 0, sizeof(ok));
// Iterate over all possible pairs
// of the array pows[]
for (int i = 0; i < pows.size(); i++) {
for (int j = 0; j < pows.size(); j++) {
if (pows.get(i) + pows.get(j) <= R
&& pows.get(i) + pows.get(j) >= L) {
// The number is valid
ok[pows.get(i) + pows.get(j)] = 1;
}
}
}
// Find the prefix sum of the
// array ok[]
for (int i = 1; i <= R; i++) {
ok[i] += ok[i - 1];
}
// Return the count of required number
return ok[R] - ok[L - 1];
}
// Driver Code
public static void main(String args[])
{
int L = 5, R = 8;
System.out.print(TotalPerfectPowerSum(L, R));
}
}
// This code is contributed by avijitmondal1998.
Python3
# python program for the above approach
# Function to find the number of numbers
# that can be expressed in the form of
# the sum of two perfect powers
def TotalPerfectPowerSum(L, R):
# Stores all possible powers
pows = []
# Push 1 and 0 in it
pows.append(0)
pows.append(1)
# Iterate over all the exponents
for p in range(2, 25):
# Iterate over all possible numbers
num = 2
# This loop will run for a
# maximum of sqrt(R) times
while ((int)(pow(num, p) + 0.5) <= R):
# Push this power in
# the array pows[]
pows.append((int)(pow(num, p) + 0.5))
# Increase the number
num = num + 1
# Stores if i can be expressed as
# the sum of perfect power or not
ok = [0 for _ in range(R + 1)]
# int ok[R + 1];
# memset(ok, 0, sizeof(ok));
# Iterate over all possible pairs
# of the array pows[]
for i in range(0, int(len(pows))):
for j in range(0, len(pows)):
if (pows[i] + pows[j] <= R and pows[i] + pows[j] >= L):
# The number is valid
ok[pows[i] + pows[j]] = 1
# Find the prefix sum of the
# array ok[]
for i in range(0, R+1):
ok[i] += ok[i - 1]
# Return the count of required number
return ok[R] - ok[L - 1]
# Driver Code
if __name__ == "__main__":
L = 5
R = 8
print(TotalPerfectPowerSum(L, R))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the number of numbers
// that can be expressed in the form of
// the sum of two perfect powers
static int TotalPerfectPowerSum(long L, long R)
{
// Stores all possible powers
List pows = new List();
// Push 1 and 0 in it
pows.Add(0);
pows.Add(1);
// Iterate over all the exponents
for (int p = 2; p < 25; p++) {
// Iterate over all possible numbers
long num = 2;
// This loop will run for a
// maximum of sqrt(R) times
while ((long)(Math.Pow(num, p) + 0.5) <= R) {
// Push this power in
// the array pows[]
pows.Add((long)(Math.Pow(num, p) + 0.5));
// Increase the number
num++;
}
}
// Stores if i can be expressed as
// the sum of perfect power or not
int[] ok = new int[R + 2];
// memset(ok, 0, sizeof(ok));
// Iterate over all possible pairs
// of the array pows[]
for (int i = 0; i < pows.Count; i++) {
for (int j = 0; j < pows.Count; j++) {
if (pows[i] + pows[j] <= R
&& pows[i] + pows[j] >= L) {
// The number is valid
ok[pows[i] + pows[j]] = 1;
}
}
}
// Find the prefix sum of the
// array ok[]
for (int i = 1; i <= R; i++) {
ok[i] += ok[i - 1];
}
// Return the count of required number
return ok[R] - ok[L - 1];
}
// Driver Code
public static void Main()
{
int L = 5, R = 8;
Console.WriteLine(TotalPerfectPowerSum(L, R));
}
}
// This code is contributed by ukasp.
Javascript
输出:
2
时间复杂度: O(R*log(R))
辅助空间: O(R)