给定Q查询以2D数组arr [] []的形式,其中每行包含两个数字L和R ,它们表示范围[L,R] ,任务是查找位于给定范围[L,R]。
例子:
Input: Q = 2, arr = [ [1, 13], [121, 211] ]
Output:
45
153
Explanation:
From 1 to 13, 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the Armstrong number. Therefore the sum is 45.
From 121 to 211 there is only one Armstrong number i.e., 153. So the sum is 153.
Input: Q = 4, arr = [ [ 10, 10 ], [ 258, 785 ], [45, 245], [ 1, 1000]]
Output:
0
1148
153
1346
方法:
这个想法是使用一个前缀和数组。直到特定索引被预先计算并存储在数组pref []中之前,所有Armstrong Number的总和才能使每个查询都能在O(1)时间内得到回答。
- 初始化前缀数组pref [] 。
- 从1迭代到N,并检查数字是否为Armstrong:
- 如果该数字为Armstrong,则pref []的当前索引将存储到目前为止找到的由(1 + pref []的上一个索引中的数字)计算得出的Armstrong Numbers的计数。
- 别的PREF的当前索引[]是相同的PREF先前索引处的值[]。
- 对于Q查询,可以计算范围[L,R]的所有Armstrong数之和:
sum = pref[R] - pref[L - 1]
下面是上述方法的实现
C++
// C++ program to find the sum
// of all armstrong numbers
// in the given range
#include
using namespace std;
// pref[] array to precompute
// the sum of all armstrong
// number
int pref[100001] = {0};
// Function that return number
// num if num is armstrong
// else return 0
static int checkArmstrong(int x) {
int n = to_string(x).size();
int sum1 = 0;
int temp = x;
while (temp > 0) {
int digit = temp % 10;
sum1 += pow(digit, n);
temp /= 10;
}
if (sum1 == x)
return x;
return 0;
}
// Function to precompute the
// sum of all armstrong numbers
// upto 100000
void preCompute() {
for (int i = 1; i < 100001; i++) {
// checkarmstrong ()
// return the number i
// if i is armstrong
// else return 0
pref[i] = pref[i - 1] + checkArmstrong(i);
}
}
// Function to print the sum
// for each query
void printSum(int L, int R) {
cout<<(pref[R] - pref[L - 1])<
Java
// Java program to find the sum
// of all armstrong numbers
// in the given range
class GFG {
// pref[] array to precompute
// the sum of all armstrong
// number
static int[] pref = new int[100001];
// Function that return number
// num if num is armstrong
// else return 0
static int checkArmstrong(int x) {
int n = String.valueOf(x).length();
int sum1 = 0;
int temp = x;
while (temp > 0) {
int digit = temp % 10;
sum1 += Math.pow(digit, n);
temp /= 10;
}
if (sum1 == x)
return x;
return 0;
}
// Function to precompute the
// sum of all armstrong numbers
// upto 100000
static void preCompute() {
for (int i = 1; i < 100001; i++) {
// checkarmstrong ()
// return the number i
// if i is armstrong
// else return 0
pref[i] = pref[i - 1] + checkArmstrong(i);
}
}
// Function to print the sum
// for each query
static void printSum(int L, int R) {
System.out.println(pref[R] - pref[L - 1]);
}
// Function to prsum of all
// armstrong numbers between
// [L, R]
static void printSumarmstrong(int[][] arr, int Q) {
// Function that pre computes
// the sum of all armstrong
// numbers
preCompute();
// Iterate over all Queries
// to print the sum
for (int i = 0; i < Q; i++) {
printSum(arr[i][0], arr[i][1]);
}
}
// Driver code
public static void main(String[] args) {
// Queries
int Q = 2;
int[][] arr = { { 1, 13 }, { 121, 211 } };
// Function that print the
// the sum of all armstrong
// number in Range [L, R]
printSumarmstrong(arr, Q);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the sum
# of all armstrong numbers
# in the given range
# pref[] array to precompute
# the sum of all armstrong
# number
pref =[0]*100001
# Function that return number
# num if num is armstrong
# else return 0
def checkArmstrong(x):
n = len(str(x))
sum1 = 0
temp = x
while temp > 0:
digit = temp % 10
sum1 += digit **n
temp //= 10
if sum1 == x:
return x
return 0
# Function to precompute the
# sum of all armstrong numbers
# upto 100000
def preCompute():
for i in range(1, 100001):
# checkarmstrong ()
# return the number i
# if i is armstrong
# else return 0
pref[i] = pref[i - 1]+ checkArmstrong(i)
# Function to print the sum
# for each query
def printSum(L, R):
print(pref[R] - pref[L - 1])
# Function to prsum of all
# armstrong numbers between
# [L, R]
def printSumarmstrong (arr, Q):
# Function that pre computes
# the sum of all armstrong
# numbers
preCompute()
# Iterate over all Queries
# to print the sum
for i in range(Q):
printSum(arr[i][0], arr[i][1])
# Driver code
# Queries
Q = 2
arr = [[1, 13 ], [ 121, 211 ]]
# Function that print the
# the sum of all armstrong
# number in Range [L, R]
printSumarmstrong (arr, Q)
C#
// C# program to find the sum
// of all armstrong numbers
// in the given range
using System;
class GFG
{
// pref[] array to precompute
// the sum of all armstrong
// number
static int[] pref = new int[100001];
// Function that return number
// num if num is armstrong
// else return 0
static int checkArmstrong(int x) {
int n = x.ToString().Length;
int sum1 = 0;
int temp = x;
while (temp > 0) {
int digit = temp % 10;
sum1 += (int)Math.Pow(digit, n);
temp /= 10;
}
if (sum1 == x)
return x;
return 0;
}
// Function to precompute the
// sum of all armstrong numbers
// upto 100000
static void preCompute()
{
for (int i = 1; i < 100001; i++)
{
// checkarmstrong ()
// return the number i
// if i is armstrong
// else return 0
pref[i] = pref[i - 1] + checkArmstrong(i);
}
}
// Function to print the sum
// for each query
static void printSum(int L, int R) {
Console.WriteLine(pref[R] - pref[L - 1]);
}
// Function to prsum of all
// armstrong numbers between
// [L, R]
static void printSumarmstrong(int[,] arr, int Q) {
// Function that pre computes
// the sum of all armstrong
// numbers
preCompute();
// Iterate over all Queries
// to print the sum
for (int i = 0; i < Q; i++) {
printSum(arr[i, 0], arr[i, 1]);
}
}
// Driver code
public static void Main(string[] args)
{
// Queries
int Q = 2;
int[,] arr = { { 1, 13 }, { 121, 211 } };
// Function that print the
// the sum of all armstrong
// number in Range [L, R]
printSumarmstrong(arr, Q);
}
}
// This code is contributed by AnkitRai01
输出:
45
153
时间复杂度: O(N),其中N是查询中的最大元素。