**HackerRank City**

### Problem Statement :

HackerRank-city is an acyclic connected graph (or tree). Its not an ordinary place, the construction of the whole tree takes place in N steps. The process is described below: It initially has 1 node. At each step, you must create 3 duplicates of the current tree, and create 2 new nodes to connect all 4 copies in the following H shape: At each ith step, the tree becomes 4 times bigger plus 2 new nodes, as well as 5 new edges connecting everything together. The length of the new edges being added at step i is denoted by input Ai. Calculate the sum of distances between each pair of nodes; as these answers may run large, print your answer modulo 1000000007. Input Format The first line contains an integer, N (the number of steps). The second line contains N space-separated integers describing A0,A1,...,An-2,An-1 . Constraints 1 <= N <= 10^6 1 <= Ai <= 9 Subtask For 50% score 1 <= N <=10 Output Format Print the sum of distances between each pair of nodes modulo 1000000007.

### Solution :

` ````
Solution in C :
In C++ :
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Ford(i,a,b) for(int i=a;i>=b;i--)
#define fi first
#define se second
#define sr(x) (int)x.size()
#define BUG(x) {cout << #x << " = " << x << endl;}
#define PR(x,a,b) {cout << #x << " = "; For(_,a,b) cout << x[_] << ' '; cout << endl;}
#define Bit(s,i) (((s)&(1<<(i)))>0)
#define Two(x) (1<<(x))
const int MOD = 1000000007;
const int nmax = 1000100;
const double E = 1e-8;
const double PI = acos(-1);
typedef long long LL;
typedef pair<int,int> pii;
int N,m,stest;
LL s[nmax], n[nmax];
LL a, f[nmax];
LL l[nmax];
int main() {
//freopen("input.txt","r",stdin);
s[0] = 0;
n[0] = 1;
l[0] = 0;
f[0] = 0;
cin >> N;
For(i,1,N) {
cin >> a;
n[i] = (4 * n[i-1] + 2) % MOD;
l[i] = ( 2 * l[i-1] + 3 * a ) % MOD;
f[i] = (
f[i-1] +
l[i-1] + a +
(f[i-1] + ( 2 * a + l[i-1] ) * n[i-1]) % MOD +
l[i-1] + 2 * a +
( f[i-1] + ( 3 * a + l[i-1] ) * n[i-1] ) * 2 % MOD
) % MOD;
s[i] = (
4 * s[i-1] +
( f[i-1] * n[i-1] * 2 % MOD + n[i-1] * n[i-1] * 2 % MOD * a % MOD) * 2 +
( f[i-1] * n[i-1] * 2 % MOD + n[i-1] * n[i-1] * 3 % MOD * a % MOD) * 4 +
( f[i-1] + a * n[i-1] ) * 4 +
( f[i-1] + a * 2 * n[i-1] ) * 4 +
a
) % MOD;
}
cout << s[N] << endl;
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static final long mod = (int) 1e9+7;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
long b = 1, c = 0, d = 0, e = 0;
for (int i = 1; i <= n; i++) {
c = (a[i] +
8 * d +
12 * a[i] * b +
12 * ((b * d) % mod) +
16 * a[i] * ((b * b) % mod) +
(c * 4) % mod) % mod;
d = (4 * d +
8 * a[i] * b +
3 * a[i] +
((3 * b + 2) * e) % mod) % mod;
b = (4 * b + 2) % mod;
e = (3 * a[i] + 2 * e) % mod;
}
System.out.println(c);
}
}
In C :
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const long long int P=1000000007;
int main() {
int n;
scanf("%d",&n);
long long x=0,y=1,z=0,ans=0;
int a;
for (int i=0;i<n;i++) {
scanf("%d",&a);
ans=ans*4+(y*12+8)%P*x%P+(y*12+8)%P*y%P*a+(y*2+1)%P*(y*2+1)%P*a;
x= x*4+(z+a*2)%P*y%P+(z+a*3)%P*y%P*2+z*2+a*3;
y= y*4+2;
z= z*2+a*3;
ans%=P;
x%=P;
y%=P;
z%=P;
}
printf("%lld",ans);
return 0;
}
In Python3 :
N=int(input())
A=[int(x) for x in input().split()]
S=29*A[0]
L=11*A[0]
LL=3*A[0]
M=1000000007
n=6
for i in range(1,N):
na=n*A[i]
na2=na+na
L2=(L+L)%M
na2L=(na2+L2)%M
S=(4*(S+na+na2L+n*(na2L+na))+2*n*na2L+A[i])%M
L=(L2+2*(3*na+n*LL)+na2L+n*LL+3*A[i]+2*LL)%M
LL= (LL+LL+3*A[i])%M
n = (4 * n + 2)%M
print(S)
```

## View More Similar Problems

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →