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
题意:问你从1......n中任意选择三个数,能形成三角形的方案数
题解:我是递推 假设已经找出 从前i个数找到的方案数, 那么dp[i] = dp[i-1] + i与前面的数形成的方案
根据排列组合计算可以得到
//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
#include<vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = +;
const int M = ;
const int inf = 0x3f3f3f3f;
const int MOD = ; ll dp[N],c[N][];
ll n,tmp,tmpp;
void init() {
c[][] = ;c[][] = ;
for(int i=;i<N;i++) {
c[i][] = i;
c[i][] = c[i-][] + c[i-][];
}
dp[] = ;
for(int i=;i<N;i++) {
tmp = i%?i/+:i/;
tmpp = (tmp-)*(tmp-)/;
dp[i] = dp[i-] + c[i - tmp][] + tmpp;
}
}
int main() {
init();
while(~scanf("%lld",&n)!=EOF) {
if(n<) break;
printf("%lld\n",dp[n]);
}
return ;
}
代码