Cerinta completa

Samantha and Sam are playing a numbers game. Given a number as a string, no leading zeros, determine the sum of all integer values of substrings of the string.

Given an integer as a string, sum all of its substrings cast as integers. As the number may become large, return the value modulo .

Example

Here is a string that has integer substrings: , , and . Their sum is , and .

Function Description

Complete the substrings function in the editor below.

substrings has the following parameter(s):

  • string n: the string representation of an integer

Returns

  • int: the sum of the integer values of all substrings in , modulo

Input Format

A single line containing an integer as a string, without leading zeros.

Constraints

Sample Input 0

16

Sample Output 0

23

Explanation 0

The substrings of 16 are 16, 1 and 6 which sum to 23.

Sample Input 1

123

Sample Output 1

164

Explanation 1

The substrings of 123 are 1, 2, 3, 12, 23, 123 which sum to 164.


Limbajul de programare folosit: cpp14

Cod:

//sam-and-substrings.cpp
//Sam and sub-strings
//Weekly Challenges - Week 3
//Author: derekhh

#include<iostream>
#include<string>
using namespace std;

const int MOD = 1000000007;
string num;
int save[200001];

void init()
{
	save[1] = 1;
	for (int i = 2; i <= 200000; i++)
		save[i] = ((long long)save[i - 1] * 10 + 1) % MOD;
}

int main()
{
	init();
	string num;
	cin >> num;
	int n = (int)num.size();
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int digit = num[i - 1] - '0';
		ans = ((long long)digit * i * save[n - i + 1] + ans) % MOD;
	}
	cout << ans << endl;
	return 0;
}

Scor obtinut: 1.0

Submission ID: 464604710

Link challenge: https://www.hackerrank.com/challenges/sam-and-substrings/problem

Sam and substrings