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

📅  最后修改于: 2021-05-05 01:48:27             🧑  作者: Mango

给定范围[l,r] ,任务是从给定范围中找到数字的所有偶数因子之和。
例子:

方法:我们可以修改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