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

📅  最后修改于: 2021-04-21 23:38:18             🧑  作者: Mango

给定的范围内[L,R],任务是找到我* countDigits(ⅰ)2对于所有的i的总和∈[L,R],其中countDigits(i)是数字的计数在
也就是说,找到:

例子:

方法:我们将段[L,R]分成数个具有相同位数的段。
[1 – 9],[10 – 99],[100 – 999],[1000 – 9999],[10000 – 99999],[100000 – 999999],[10000000 – 99999999]等。
LR的长度相同时,所需的总和将为countDigits(L) 2 *(L + R)*(R – L + 1)/ 2
证明:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#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;
    for (int i = 1; i <= 10; i++) {
        int L = max(l, a);
        int R = min(r, b);
 
        // If range is valid
        if (L <= R) {
 
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += (i * i) * (sum % MOD);
            res %= MOD;
        }
        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 implementation of the approach
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;
        for (int i = 1; i <= 10; i++) {
            int L = Math.max(l, a);
            int R = Math.min(r, b);
 
            // If range is valid
            if (L <= R) {
 
                // Sum of AP
                int sum = (L + R) * (R - L + 1) / 2;
                res += (i * i) * (sum % MOD);
                res %= MOD;
            }
            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));
    }
}


Python3
# Python3 implementation of the approach
 
MOD = 1000000007;
 
# Function to return the required sum
def rangeSum(l, r) :
 
    a = 1; b = 9; res = 0;
    for i in range(1, 11) :
        L = max(l, a);
        R = min(r, b);
         
        # If range is valid
        if (L <= R) :
             
            # Sum of AP
            sum = (L + R) * (R - L + 1) // 2;
            res += (i * i) * (sum % MOD);
            res %= MOD;
         
        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 Ryuga


C#
// C# implementation of the approach
using System;
class GFG {
 
    const int MOD = 1000000007;
 
    // Function to return the required sum
    static int rangeSum(int l, int r)
    {
 
        int a = 1, b = 9, res = 0;
        for (int i = 1; i <= 10; i++) {
            int L = Math.Max(l, a);
            int R = Math.Min(r, b);
 
            // If range is valid
            if (L <= R) {
 
                // Sum of AP
                int sum = (L + R) * (R - L + 1) / 2;
                res += (i * i) * (sum % MOD);
                res %= MOD;
            }
            a = a * 10;
            b = b * 10 + 9;
        }
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        int l = 98, r = 102;
        Console.WriteLine(rangeSum(l, r));
    }
}


PHP


Javascript


输出:
3515