Sam and substrings


Problem Statement :


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 10^9 +7.

Example
n = '42'
Here n is a string that has 3 integer substrings: 4, 2, and 42. Their sum is 48, and 48 modulo{10^9 +7)=48.

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 n, modulo 10^9 + 7
Input Format

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

Constraints

1 <= ncastasaninteger <= 2 x 10^5



Solution :



title-img


                            Solution in C :

In C++ :






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

#define MOD 1000000007

int main() {
	char num[200005];
	gets(num);
	int len = strlen(num);
	long long factor = 1, ans = 0;

	for (int i = len-1; i >= 0; --i) {
		long long tmp = (num[i]-'0') * (i+1) * factor % MOD;
		ans += tmp;
		ans %= MOD;
		factor = (factor * 10 + 1) % MOD;
	}
	printf("%d\n", ans);

	return 0;
}








In Java :






import java.util.Scanner;


public class Solution {

	static long mod = 1000000007;
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		char s[] = cin.next().toCharArray();
		long ten[] = new long[s.length + 2];
		
		ten[0] = 1;
		for (int i=1; i<ten.length; i++) {
			ten[i] = ten[i - 1] * 10 + 1;
			ten[i] %= mod;
		}
		
		long ans = 0;
		for (int i=0; i<s.length; i++) {
			ans += ((s[i] - '0') * ten[s.length - i - 1]) % mod * (i + 1) % mod;
			ans %= mod;
		}
		
		System.out.println(ans);
		cin.close();
	}

}








In C :





#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 200001
#define MODULUS 1000000007LL

int main() {
	long long total, multiplier, partialsum;
	char c;
	
	total = partialsum = 0;
	multiplier = 1;
	while (1) {
		c=getchar();
		if (c<'0' || c>'9') break;
		c -= '0';
		partialsum += multiplier * c;
		total = (total * 10 + partialsum) % MODULUS;
		multiplier++;
	}
	
	printf("%lld\n",total);
	return 0;
}








In Python3 :





import operator

MOD = 1000000007

s = input()
n = len(s)
a = []
p10 = 0
for i in range(n, 0, -1):
	p10 = (p10 * 10 + 1) % MOD
	a.append(p10 * i % MOD)

b = [ord(c) - ord('0') for c in s]
print(sum(map(operator.mul, reversed(a), b)) % MOD)
                        








View More Similar Problems

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →