📜  [L,R]范围内所有i的i * countDigits(i)^ countDigits(i)之和

📅  最后修改于: 2021-04-27 22:55:04             🧑  作者: Mango

给定两个表示范围[L,R]的数字L,R ,任务是找到所有i∈[L,R]的总和i * countDigits(i) countDigits(i)其中countDigits(i)是计数i中的位数。
例子:

方法:想法是根据数字位数将范围分为几部分。也就是说,每个段都包含具有相同位数的数字:

[1 - 9], [10 - 99], [100 - 999], [1000 - 9999], [10000 - 99999] ...

从上面的片段中可以明显看出,每个片段的[L,R]都具有相同的长度。因此,该细分的要求总和为:

countDigits(L)countDigits(L) * (L + R) * (R - L + 1) / 2

证明:

  • [L,R] = [10,14] ,其中LR的长度相同,即2。
  • 因此,段[L,R]的总和为:
10 * 22 + 11 * 22 + 12 * 22 + 13 * 22 + 14 * 22
  • 在采取2 2共同:
22 * (10 + 11 + 12 + 13 + 14) 
=> totalDigitstotalDigits * (Sum of AP)
  • AP的总和=(项数/ 2)*(第一项+最后一项),即(R–L + 1)*(L + R)/ 2
  • 因此,所需的总和为:
countDigits(L)countDigits(L) * (L + R) * (R - L + 1) / 2

下面是上述方法的实现:

C++
// C++ program to find the required sum
 
#include 
using namespace std;
#define MOD 1000000007
 
// Function to return the required sum
int rangeSum(int l, int r)
{
    int a = 1, b = 9, res = 0;
 
    // Iterating for all the number
    // of digits from 1 to 10
    for (int i = 1; i <= 10; i++) {
        int L = max(l, a);
        int R = min(r, b);
 
        // If the range is valid
        if (L <= R) {
 
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += pow(i, i) * (sum % MOD);
            res %= MOD;
        }
 
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
 
// Driver code
int main()
{
    int l = 98, r = 102;
    cout << rangeSum(l, r);
    return 0;
}


Java
// Java program to find the required sum
import java.util.*;
 
class GFG{
static final int MOD = 1000000007;
  
// Function to return the required sum
static int rangeSum(int l, int r)
{
    int a = 1, b = 9, res = 0;
  
    // Iterating for all the number
    // of digits from 1 to 10
    for (int i = 1; i <= 10; i++) {
        int L = Math.max(l, a);
        int R = Math.min(r, b);
  
        // If the range is valid
        if (L <= R) {
  
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += Math.pow(i, i) * (sum % MOD);
            res %= MOD;
        }
  
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
  
// Driver code
public static void main(String[] args)
{
    int l = 98, r = 102;
    System.out.print(rangeSum(l, r));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python 3 program to find the required sum
  
MOD = 1000000007
  
# Function to return the required sum
def rangeSum(l, r):
 
    a = 1
    b = 9
    res = 0
  
    # Iterating for all the number
    # of digits from 1 to 10
    for i in range(1, 11):
        L = max(l, a)
        R = min(r, b)
  
        # If the range is valid
        if (L <= R):
  
            # Sum of AP
            sum = (L + R) * (R - L + 1) // 2
            res += pow(i, i) * (sum % MOD)
            res %= MOD
  
        # Computing the next minimum and maximum
        # numbers by for the (i+1)-th digit
        a = a * 10
        b = b * 10 + 9
    return res
  
# Driver code
if __name__ == "__main__":
     
    l = 98
    r = 102
    print(rangeSum(l, r))
     
# This code is contributed by chitranayal


C#
// C# program to find the required sum
using System;
 
class GFG{
static readonly int MOD = 1000000007;
   
// Function to return the required sum
static int rangeSum(int l, int r)
{
    int a = 1, b = 9, res = 0;
   
    // Iterating for all the number
    // of digits from 1 to 10
    for (int i = 1; i <= 10; i++) {
        int L = Math.Max(l, a);
        int R = Math.Min(r, b);
   
        // If the range is valid
        if (L <= R) {
   
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += (int)Math.Pow(i, i) * (sum % MOD);
            res %= MOD;
        }
   
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
   
// Driver code
public static void Main(String[] args)
{
    int l = 98, r = 102;
    Console.Write(rangeSum(l, r));
}
}
  
// This code is contributed by Rajput-Ji


Javascript


输出:
8969