HAWK16 - Editorial

Problem Link:

Contest
Practice

Author: Nikhil Raghav
Tester: Nikhil Raghav
Editorialist: Nikhil Raghav

Difficulty

Cakewalk

Problem

Given a pattern, observe it and find the sum of first N terms of the series.

Explanation

You were given a pattern, which goes like this: 1, 6, 12, 18, 24 …
This is moreover an AP when it starts from the second element.
You just need to find out the sum of AP.

We can write the series as : 1 + 6*(1 + 2 + 3 + .... + Z) … (Equation 1)
Z will be (total number of concentric hexagons - 1) which is N/6.
Sum of the first Z elements can be written as : (Z * (Z + 1) )/2 … (Equation 2)
Now, just replace the value from Equation 2 in Equation 1,
1 + 6*( ( Z * ( Z + 1 ) ) / 2)
where Z = N/6

Solution

Author's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
long long n;
while(t–)
{
scanf("%lld",&n);
if(n==1){printf(“1\n”);continue;}
long long z = n/6, ans=1+ 6*((z*(z+1))/2);
printf("%lld\n",ans);
}
}