📜  对一个数字的所有因子的奇数数字总和的查询

📅  最后修改于: 2021-04-26 05:15:27             🧑  作者: Mango

给定Q查询。每个查询包含一个正整数n 。任务是输出n的所有除数中包含的奇数位数的总和。

例子 :

这个想法是预先计算所有数字的奇数数字之和。此外,我们可以使用前一个数字的奇数位数字之和来计算当前数字的奇数位数字之和。
例如,要计算“ 123”的奇数位数之和,我们可以使用“ 12”和“ 3”的奇数位数之和。因此,奇数之和“ 123” =奇数之和“ 12” +如果最后一个数字是奇数(即3),则将其相加。
现在,要找到因子的奇数位数总和,我们可以使用Eratosthenes筛网的跳跃现象。因此,对于所有可能的因素,请将其贡献增加至其倍数。
例如,对于1作为因子,将1(因为1仅具有1个奇数位)加到其所有倍数上。
对于2作为因子,将0乘以2的所有倍数,即2、4、8…
对于3作为因子,将1加到3的所有倍数上,即3、6、9…..

以下是此方法的实现:

C++
// CPP Program to answer queries on sum 
// of sum of odd number digits of all 
// the factors of a number
#include 
using namespace std;
#define N 1000005
  
// finding sum of odd digit number in each integer.
void sumOddDigit(int digitSum[])
{
    // for each number
    for (int i = 1; i < N; i++) {
  
        // using previous number sum, finding
        // the current number num of odd digit
        // also, adding last digit if it is odd.
        digitSum[i] = digitSum[i / 10] + (i & 1) * (i % 10);
    }
}
  
// finding sum of sum of odd digit of all
// the factors of a number.
void sumFactor(int digitSum[], int factorDigitSum[])
{
    // for each possible factor
    for (int i = 1; i < N; i++) {
        for (int j = i; j < N; j += i) {
  
            // adding the contribution.
            factorDigitSum[j] += digitSum[i];
        }
    }
}
  
// Wrapper function
void wrapper(int q, int n[])
{
    int digitSum[N];
    int factorDigitSum[N];
  
    sumOddDigit(digitSum);
    sumFactor(digitSum, factorDigitSum);
  
    for (int i = 0; i < q; i++) 
        cout << factorDigitSum[n[i]] << " ";
}
  
// Driven Program
int main()
{
    int q = 2;
    int n[] = { 10, 36 };
  
    wrapper(q, n);
    return 0;
}


Java
// Java Program to answer queries 
// on sum of sum of odd number  
// digits of all the factors of 
// a number
class GFG 
{
    static int N = 1000005;
      
    // finding sum of odd digit 
    // number in each integer.
    static void sumOddDigit(int digitSum[])
    {
          
        // for each number
        for (int i = 1; i < N; i++)
        {
      
            // using previous number sum,
            // finding the current number 
            // num of odd digit also, 
            // adding last digit if it
            // is odd.
            digitSum[i] = digitSum[i / 10] + 
                         (i & 1) * (i % 10);
        }
    }
      
    // finding sum of sum of odd digit 
    // of all the factors of a number.
    static void sumFactor(int digitSum[], 
                    int factorDigitSum[])
    {
          
        // for each possible factor
        for (int i = 1; i < N; i++) 
        {
            for (int j = i; j < N; j += i)
            {
                // adding the contribution.
                factorDigitSum[j] += digitSum[i];
            }
        }
    }
      
    // Wrapper function
    static void wrapper(int q, int n[])
    {
        int digitSum[] = new int[N];
        int factorDigitSum[] = new int[N];
      
        sumOddDigit(digitSum);
        sumFactor(digitSum, factorDigitSum);
      
        for (int i = 0; i < q; i++) 
            System.out.print(factorDigitSum[n[i]]
                                          + " ");
    }
      
    // Driver Code
    public static void main(String args[]) 
    {
        int q = 2;
        int n[] = new int[]{10, 36};
      
        wrapper(q, n);
          
    }
}
  
// This code is contributed by Sam007


Python3
# Python Program to answer queries 
# on sum of sum of odd number
# digits of all the factors 
# of a number
N = 100
digitSum = [0] * N
factorDigitSum = [0] * N
  
# finding sum of odd digit 
# number in each integer.
def sumOddDigit() :
    global N,digitSum,factorDigitSum
      
    # for each number
    for i in range(1, N) : 
          
        # using previous number
        # sum, finding the current 
        # number num of odd digit
        # also, adding last digit 
        # if it is odd.
        digitSum[i] = (digitSum[int(i / 10)]
                    + int(i & 1) * (i % 10))
  
# finding sum of sum of 
# odd digit of all the
# factors of a number.
def sumFactor() :
    global N,digitSum,factorDigitSum 
    j = 0
      
    # for each possible factor
    for i in range(1, N) : 
        j = i
        while (j < N) :
              
            # adding the contribution.
            factorDigitSum[j] = (factorDigitSum[j]
                                   + digitSum[i])
            j = j + i
  
# Wrapper def
def wrapper(q, n) :
  
    global N,digitSum,factorDigitSum
      
    for i in range(0, N) :     
        digitSum[i] = 0
        factorDigitSum[i] = 0
      
    sumOddDigit()
    sumFactor()
  
    for i in range(0, q) : 
        print ("{} ".
        format(factorDigitSum[n[i]]), end = "")
  
# Driver Code
q = 2
n = [ 10, 36 ]
wrapper(q, n)
  
# This code is contributed by 
# Manish Shaw(manishshaw1)


C#
// C# Program to answer queries on sum 
// of sum of odd number digits of all 
// the factors of a number
using System;
  
class GFG {
      
    static int N = 1000005;
      
    // finding sum of odd digit number in
    // each integer.
    static void sumOddDigit(int []digitSum)
    {
          
        // for each number
        for (int i = 1; i < N; i++) {
      
            // using previous number sum,
            // finding the current number 
            // num of odd digit also, 
            // adding last digit if it
            // is odd.
            digitSum[i] = digitSum[i / 10] 
                     + (i & 1) * (i % 10);
        }
    }
      
    // finding sum of sum of odd digit 
    // of all the factors of a number.
    static void sumFactor(int []digitSum, 
                     int []factorDigitSum)
    {
          
        // for each possible factor
        for (int i = 1; i < N; i++) {
            for (int j = i; j < N; j += i)
            {
                // adding the contribution.
                factorDigitSum[j] += digitSum[i];
            }
        }
    }
      
    // Wrapper function
    static void wrapper(int q, int []n)
    {
        int []digitSum = new int[N];
        int []factorDigitSum = new int[N];
      
        sumOddDigit(digitSum);
        sumFactor(digitSum, factorDigitSum);
      
        for (int i = 0; i < q; i++) 
            Console.Write(factorDigitSum[n[i]]
                                       + " ");
    }
          
    // Driver code
    public static void Main()
    {
        int q = 2;
        int []n = new int[]{ 10, 36 };
      
        wrapper(q, n);
    }
}
  
// This code is contributed by Sam007.


PHP


输出 :
7 18