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 :



title-img


                            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

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →