当前位置: 代码迷 >> 综合 >> UVa 11401 Triangle Countin(找规律)
  详细解决方案

UVa 11401 Triangle Countin(找规律)

热度:78   发布时间:2023-11-17 14:14:30.0

Triangle Counting

Input: Standard Input

Output: Standard Output

 

You are given n rods of length 1, 2…, n. You have to pick any 3 of them & 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 (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

 Output for Sample Input

4

22


题解:

暴力打了个表找规律

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
ll p[1000005];
int main()
{ll i,j,k,ans,n,d1=1,d2=1;p[3]=0;p[4]=1;for(i=5;i<=1000000;i++){if(d1==d2)d2++;elsed1++;p[i]=p[i-1]+d1*d2;}while(scanf("%lld",&n)!=EOF&&n>=3){printf("%lld\n",p[n]);}return 0;
}