📌  相关文章
📜  [l,r]范围内数字的所有奇数因子的总和

📅  最后修改于: 2021-04-24 18:49:23             🧑  作者: 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 = 100001;
 
ll prefix[MAX];
 
// Function to calculate the prefix sum
// of all the odd factors
void sieve_modified()
{
    for (int i = 1; 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 odd factors of the
// numbers in the given range
ll sumOddFactors(int L, int R)
{
    return (prefix[R] - prefix[L - 1]);
}
 
// Driver code
int main()
{
    sieve_modified();
    int l = 6, r = 10;
    cout << sumOddFactors(l, r);
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
static int MAX = 100001;
static int prefix[] = new int[MAX];
 
// Function to calculate the prefix sum
// of all the odd factors
static void sieve_modified()
{
    for (int i = 1; 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 odd factors of the
// numbers in the given range
static int sumOddFactors(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.println (sumOddFactors(l, r));
    }
}
 
// This code is contributed by jit_t


Python3
# Python3 implementation of the approach
MAX = 100001;
 
prefix = [0] * MAX;
 
# Function to calculate the prefix sum
# of all the odd factors
def sieve_modified():
 
    for i in range(1, 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 odd factors of the
# numbers in the given range
def sumOddFactors(L, R):
 
    return (prefix[R] - prefix[L - 1]);
 
# Driver code
sieve_modified();
l = 6;
r = 10;
print(sumOddFactors(l, r));
 
# this code is contributed by chandan_jnu


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
public static int MAX = 100001;
public static int[] prefix = new int[MAX];
 
// Function to calculate the prefix sum
// of all the odd factors
public static void sieve_modified()
{
    for (int i = 1; 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 odd factors of the
// numbers in the given range
public static int sumOddFactors(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.WriteLine(sumOddFactors(l, r));
}
}
 
// This code is contributed by Shrikant13


PHP


Javascript


输出:
32