Problem:
Score : 900 points

Problem Statement

For a positive integer n, let us define f(n) as the number of digits in base 10.

You are given an integer S.
Count the number of the pairs of positive integers (l, r) (l \leq r) such that f(l) + f(l + 1) + ... + f(r) = S, and find the count modulo 10^9 + 7.

Constraints

1 \leq S \leq 10^8

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.

Sample Input 1

1

Sample Output 1

9

There are nine pairs (l, r) that satisfies the condition: (1, 1), (2, 2), ..., (9, 9).

Sample Input 2

2

Sample Output 2

98

There are 98 pairs (l, r) that satisfies the condition, such as (1, 2) and (33, 33).

Sample Input 3

123

Sample Output 3

460191684

Sample Input 4

36018

Sample Output 4

966522825

Sample Input 5

1000

Sample Output 5

184984484

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
using namespace std;
int S;
int Partial[15], Power[15];
int ans;
const int MOD = 1000000007;
inline void Add(int& x, int y)
{
    x += y;
    if(x >= MOD) x -= MOD;
}
void precalcPartial()
{
    Partial[1] = 9;
    Power[0] = 1;
    for(int i = 1; i <= 8; i++)
        Power[i] = Power[i - 1] * 10;
    int power = 10;
    for(int i = 2; i <= 8; i++)
    {
        power *= 10;
        Partial[i] = Partial[i - 1] + (power - power / 10) * i;
    }
}

void Solve(int a, int b)
{
    int sum = 0;
    sum += Partial[b - 1] - Partial[a];
    sum = S - sum;
    if(sum < 0)
        return;
    int point = Power[a - 1], point2 = (sum - a * (Power[a] - Power[a - 1])) / b;
    for(; point < Power[a]; point++)
    {
        point2 = (sum - a * (Power[a] - point)) / b;
        if(point2 >= 1 && point2 <= Power[b] - Power[b - 1] && (sum - a * (Power[a] - point)) % b == 0)
            Add(ans, 1);
    }
}

void Solve2(int k)
{
    int sum = S - k - k - 1;
    if(sum < 0)
        return;
    int moda = (k + 1) - (sum) % (k + 1);
    if(moda == k + 1)
        moda = 0;
    int lim = sum / k;
    if(lim < moda)
        return;
    Add(ans, (lim - moda) / (k + 1) + 1);
}

void getAns()
{
    int pw = 1;
    for(int i = 1; i <= 7; i++)
    {
        if(S % i == 0)
        {
            int number = S / i;
            int next = pw * 10;
            next = next - pw;
            if(next >= number - 1)
            {
                next -= (number - 1);
                Add(ans, next);
            }
        }
        for(int j = i + 1; j <= 8; j++)
            Solve(i, j);
        pw = pw * 10;
    }

    for(int i = 8; i <= S; i++)
    {
        Solve2(i);
        if(S % i == 0)
        {
            int number = S / i;
            int next = (1LL * pw * 10) % MOD;
            next = next - pw;
            if(next < 0)
                next += MOD;
            next -= (number - 1);
            if(next < 0)
                next += MOD;
            Add(ans, next);
        }
        pw = (1LL * pw * 10) % MOD;
    }
    cout << ans << "\n";
}
int main()
{
    cin >> S;
    precalcPartial();
    getAns();
    return 0;
}