您的位置:首页 > 其它

UVA 11401 - Triangle CountingTriangle Counting 数学

2015-12-28 19:53 459 查看
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 = 1000000+100;
const int M = 1000001;
const int inf = 0x3f3f3f3f;
const int MOD = 1000000007;

ll dp
,c
[3];
ll n,tmp,tmpp;
void init() {
c[1][1] = 1;c[1][2] = 0;
for(int i=2;i<N;i++) {
c[i][1] = i;
c[i][2] = c[i-1][2] + c[i-1][1];
}
dp[3] = 0;
for(int i=4;i<N;i++) {
tmp = i%2?i/2+1:i/2;
tmpp = (tmp-1)*(tmp-2)/2;
dp[i] = dp[i-1] + c[i - tmp][2] + tmpp;
}
}
int main() {
init();
while(~scanf("%lld",&n)!=EOF) {
if(n<3) break;
printf("%lld\n",dp
);
}
return 0;
}


代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: