📜  范围 [l, r] 中所有偶数因子的总和

📅  最后修改于: 2021-09-17 16:07:41             🧑  作者: Mango

给定一个范围[l, r] ,任务是找到给定范围内数字的所有偶数因子的总和。
例子:

方法:我们可以修改 Sieve Of Eratosthenes 来存储一个数字的所有偶数因子的总和在它的相应索引处。然后我们将创建一个前缀数组来存储该索引的总和。现在每个查询都可以使用 prefix[r] – prefix[l – 1] 在 O(1) 中回答。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
 
const int MAX = 100000;
 
ll prefix[MAX];
 
// Function to calculate the prefix sum
// of all the even factors
void sieve_modified()
{
    for (int i = 2; i < MAX; i += 2) {
 
        // Add i to all the multiples of i
        for (int j = i; j < MAX; j += i)
            prefix[j] += i;
    }
 
    // Update the prefix sum
    for (int i = 1; i < MAX; i++)
        prefix[i] += prefix[i - 1];
}
 
// Function to return the sum of
// all the even factors of the
// numbers in the given range
ll sumEvenFactors(int L, int R)
{
    return (prefix[R] - prefix[L - 1]);
}
 
// Driver code
int main()
{
    sieve_modified();
    int l = 6, r = 10;
    cout << sumEvenFactors(l, r);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    static final int MAX = 100000;
    static long prefix[] = new long[MAX];
 
    // Function to calculate the prefix sum
    // of all the even factors
    static void sieve_modified()
    {
        for (int i = 2; i < MAX; i += 2) {
 
            // Add i to all the multiples of i
            for (int j = i; j < MAX; j += i)
                prefix[j] += i;
        }
 
        // Update the prefix sum
        for (int i = 1; i < MAX; i++)
            prefix[i] += prefix[i - 1];
    }
 
    // Function to return the sum of
    // all the even factors of the
    // numbers in the given range
    static long sumEvenFactors(int L, int R)
    {
        return (prefix[R] - prefix[L - 1]);
    }
 
    // Driver code
    public static void main(String args[])
    {
        sieve_modified();
        int l = 6, r = 10;
        System.out.print(sumEvenFactors(l, r));
    }
}


Python3
# Python3 implementation of the approach.
 
# Function to calculate the prefix sum
# of all the even factors
def sieve_modified():
 
    for i in range(2, MAX, 2):
 
        # Add i to all the multiples of i
        for j in range(i, MAX, i):
            prefix[j] += i
 
    # Update the prefix sum
    for i in range(1, MAX):
        prefix[i] += prefix[i - 1]
 
# Function to return the sum of
# all the even factors of the
# numbers in the given range
def sumEvenFactors(L, R):
 
    return (prefix[R] - prefix[L - 1])
 
# Driver code
if __name__ == "__main__":
     
    MAX = 100000
    prefix = [0] * MAX
    sieve_modified()
    l, r = 6, 10
    print(sumEvenFactors(l, r))
 
# This code is contributed by Rituraj Jain


C#
using System;
// C# implementation of the approach
using System;
 
class GFG
{
 
    public const int MAX = 100000;
    public static long[] prefix = new long[MAX];
 
    // Function to calculate the prefix sum
    // of all the even factors
    public static void sieve_modified()
    {
        for (int i = 2; i < MAX; i += 2)
        {
 
            // Add i to all the multiples of i
            for (int j = i; j < MAX; j += i)
            {
                prefix[j] += i;
            }
        }
 
        // Update the prefix sum
        for (int i = 1; i < MAX; i++)
        {
            prefix[i] += prefix[i - 1];
        }
    }
 
    // Function to return the sum of
    // all the even factors of the
    // numbers in the given range
    public static long sumEvenFactors(int L, int R)
    {
        return (prefix[R] - prefix[L - 1]);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        sieve_modified();
        int l = 6, r = 10;
        Console.Write(sumEvenFactors(l, r));
    }
}
 
// This code is contributed by Shrikant13


PHP


Javascript


输出:
34

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程