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
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 →Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →