Summing Pieces

Problem Statement :

Consider an array, A, of length n. We can split A into contiguous segments called pieces and store them as another array, B. For example, if A = [1,2,3], we have the following arrays of pieces:

 B = [(1),(2),(3)] contains three 1-element pieces.
 B = [(1,2),(3)] contains two pieces, one having 2 elements and the other having 1 element.
 B = [(1),(2,3)] contains two pieces, one having 1 element and the other having 2 elements.
 B = [(1,2,3)] contains one 3-element piece.
We consider the value of a piece in some array B to be (sum of all numbers in the piece) * (length of piece), and we consider the total value of some array B to be the sum of the values for all pieces in that B. For example, the total value of B = [1,2,4),(5,1),(2)] is (1+2+4)*3+(5+1)*2+(2)*1 = 35.

Given A, find the total values for all possible B's, sum them together, and print this sum modulo (10^9 +  7) on a new line.

Input Format

The first line contains a single integer, n, denoting the size of array A.
The second line contains n space-separated integers describing the respective values in A (i.e.,a0,a1,...,an-1).

1 <= n <= 10^6
1 <= ai <= 10^9

Output Format

Print a single integer denoting the sum of the total values for all piece arrays (B's) of A, modulo (10^9 + 7).

Solution :


                            Solution in C :

In C++ :

//It’s never too late to become what you might have been....
 #include <iostream>
using namespace std;
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define S second
#define F first
#define boost1 ios::sync_with_stdio(false);
#define boost2 cin.tie(0);
#define mem(a,val) memset(a,val,sizeof a)
#define endl "\n"
#define maxn 1000001

ll power[maxn],sub[maxn],pre[maxn],suff[maxn],a[maxn];

ll ways(ll x)
	return 1;
	return power[x-1];
int main()
	ll i,j,n,q,x,y,sum=0,ans=0;

	return 0;

In Java :

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(;
        int n = in.nextInt();
        long sum = 0;
        long[] powers2 = new long[n+1];
        powers2[0] = 1;
        for(int i=1; i<=n; i++)
            powers2[i] = (powers2[i-1] << 1) % 1000000007;
        for(int i=1; i<=n; i++){
            long left = ((powers2[i] - 1) * powers2[n-i]) % 1000000007;
            long right = ((powers2[1+n-i]-1) * powers2[i-1]) % 1000000007;
            long v = left + right - powers2[n-1];
            sum = (sum + (v * in.nextLong())) % 1000000007;

In C :

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

typedef long long int LL;

#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

int arr[1000005];
int dp[1000005];
int m = 1e9 + 7;
int mod=1e9+7;

int mult(int a,int b)
	LL tmp = (LL)a*(LL)b ;
	tmp = tmp%m;
	return (int)tmp;

int add(int a, int b)
	LL tmp = (LL)a + (LL)b;
	tmp = tmp%m;
	return (int)tmp;

int max(int a, int b)
	if(a>b) return a; return b;

long long po(int x, int y)
	int pro=1;
		else if(y&1 != 0)
			pro = mult(pro, x);
		x = mult(x,x);
	return pro;

int main()
	int n; din(n);
	for(int i=0;i<n;i++) 
	dp[0] = po(2,n) - 1;
	dp[n-1] = po(2,n) - 1;
	int len1 = n-2, len2 = 0;
	for(int i=1;i<=n/2;i++)
		dp[i] = add(dp[i-1], (po(2, len1--)-po(2,len2++))%m ); // mod
		dp[n-i-1] = dp[i];
	int ans = 0;
	for(int i=0;i<n;i++)
		//printf("%d ", dp[i]);
		ans = add(ans, mult(arr[i], dp[i]));

In Python3 :

import math

n = int(input())
A = list(map(int,input().split()))
T = [0]*n
MOD = 10**9 +7

def pow_mod(x, y):
    number = 1
    while y:
        if y & 1:
            number = number * x % MOD
        y >>= 1
        x = x * x % MOD
    return number

mem = pow_mod(2,n) + pow_mod(2,n-1)
ans = 0
k = 0
for i in range(1,math.ceil(n/2)+1):
    temp = mem - pow_mod(2,n-i) - pow_mod(2,i-1) 
    T[i-1] = temp
    T[n-i] = temp

# print(T)

for a in A:
    # print(k)
    ans = (ans + T[k]*a)%MOD

View More Similar Problems

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →