UVA 11401 - Triangle CountingTriangle Counting 数学

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[N],c[N][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[n]);
    }
    return 0;
}
代码

 

posted @ 2015-12-28 19:53  meekyan  阅读(238)  评论(0编辑  收藏  举报