Problem Description
source:https://uva.onlinejudge.org/external/114/11401.html
You are given n rods of length 1, 2, . . . , n. You have to pick any 3 of them and build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different length.
Input
The input for each case will have only a single positive integer n (3 ≤ n ≤ 1000000). The end of input will be indicated by a case with n < 3. This case should not be processed.
Output
For each test case, print the number of distinct triangles you can make.
Sample Input
5
8
0
Sample Output
3
22
source:https://uva.onlinejudge.org/external/114/11401.html
You are given n rods of length 1, 2, . . . , n. You have to pick any 3 of them and build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different length.
Input
The input for each case will have only a single positive integer n (3 ≤ n ≤ 1000000). The end of input will be indicated by a case with n < 3. This case should not be processed.
Output
For each test case, print the number of distinct triangles you can make.
Sample Input
5
8
0
Sample Output
3
22
Solution:
#include<stdio.h>
#include<math.h>
#define N 1000005
unsigned long long int output[N];
void calculate();
int main()
{
unsigned long long int n, i;
calculate();
while(scanf("%llu",&n)==1)
{
if(n<3)
break;
printf("%llu\n",output[n]);
}
return 0;
}
void calculate()
{
unsigned long long int i,n;
output[3]=0;
output[4]=1;
for(i=5;i<N;i++)
{
n=i/2-1;
output[i]=output[i-1]+2*(n*(n+1)/2)-(!((i&1))*n);
}
}
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience