1701. Cannonball Pyramids
Description
If you visit historical battlegrounds from periods when cannon were used, you may see cannonballs (metal spheres) stacked in various ways. In this problem we're interested in the number of cannonballs in such pyramids with triangular bases -- that is, with cannonballs arranged in an equilateral triangle on the base, then similar equilateral triangles of cannonballs stacked on top of that, until a single cannonball is placed on the top. This three-sided pyramid is, in fact, one of the Platonic solids, a tetrahedron.
For example, suppose we begin with a base containing 6 cannonballs arranged in an equilateral triangle as shown in the figure on the left; this equilateral triangle has 3 cannonballs on each edge. The next layer will contain 3 cannonballs, 2 on each edge -- as shown in the middle. The final layer always contains just one cannonball. The total number of cannonballs in this pyramid is thus 6 + 3 + 1 = 10. In general, each layer of such a pyramid will have one fewer cannonballs on each edge of the triangle than the next lower layer.
The number of cannonballs in a layer is just the sum of the integers from 1 to N, where N is the number of cannonballs on one side of the layer.
Given the number of cannonballs on each side of the base, compute the total number of cannonballs in the entire tetrahedral pyramid.
Input
The first line of the input contains a single integer N giving the number of cases posed, for a maximum of 100 cases. Following that are exactly N lines, each with a single integer giving the number of cannonballs on each side of the base for a tetrahedron of cannonballs, a number less than 1000.
Output
For each input case, display the case number (1, 2, ...), a colon and a blank, the number of cannonballs on each side of the base, one blank, and finally the total number of cannonballs in the tetrahedron.
Sample Input
5 3 5 27 999 1
Sample Output
1: 3 10 2: 5 35 3: 27 3654 4: 999 166666500 5: 1 1
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int k=1;k<=t;k++)
{
int n,t=0;
cin>>n;
long sum=0;
for(int i=1;i<=n;i++)
{
t+=i;
sum+=t;
}
cout<<k<<": "<<n<<" "<<sum<<endl;
}
return 0;
}