*[hackerrank]Sam and sub-strings
https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings
DP。注意到以N[i]结尾的每个字符串的子数字和有规律:
5312
5 | 3 53 | 1 31 531 | 2 12 312 5312
sd[2] = 1 + 31 + 531 = 563
sd[3] = 2 + 12 + 312 + 5312
sd[3] = 2 + 10 + 2 + 310 + 2 + 5310 + 2
sd[3] = 4 * 2 + 10 * (1 + 31 + 531 )
sd[3] = (3 + 1) * *N[3]* + 10 * *sd[2]*
sd[i+1] = (i + 2) * N[i] + 10 * sd[i]
sd[0] = N[0]
#include <iostream> #include <vector> using namespace std; int main() { int MOD = 1000000007; string s; cin >> s; int len = s.size(); long long cur_sum = 0; long long total_sum = 0; for (int i = 0; i < len; i++) { cur_sum = (cur_sum * 10 + (s[i] - '0') * (i + 1)) % MOD; total_sum = (total_sum + cur_sum) % MOD; } cout << total_sum << endl; return 0; }