Given a positive integer N, let us define a subnumber of N as a consecutive number of digits NOT starting with 0. For example if N=1021, it has 7 subnumbers, namely, 1, 10, 102, 1021, 2, 21 and 1 (again). Here is your task: please calculate the sum of all the subnumbers of N. For 1021, the sum is 1+10+102+1021+2+21+1 = 1158. Since the result may be very large, output the answer modulo 1000000007 (109+7) please.
Input Specification:
Each input file contains one test case, which gives the integer N (0<N<10100000) in a line.
Output Specification:
Print in a line the sum of all N's subnumbers (modulo 1000000007).
对于M位的整数,左边包括它自己在内有位的非零数字,对最后结果的贡献为
.
最后的结果就是
.
提前以递推的形式计算10的若干次方有助于降低计算复杂度。代码如下。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const long long N = 1e9 + 7;
int main()
{
string input;
cin >> input;
vector<long long> residual(input.size(), 0);
residual[0] = 1;
for(size_t i = 1; i < residual.size(); i++){
residual[i] = (residual[i - 1] * 10 + 1) % N;
}
long long result = 0;
int g = 0;
for(size_t i = 0; i < input.size(); i++){
long long n = input[i] - '0';
if(n) g += 1;
int k = input.size() - i;
result += (g * n * residual[k - 1]);
result %= N;
}
cout << result;
return 0;
}