zoukankan      html  css  js  c++  java
  • UVA 11401

    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
    ",dp[n]);
        }
        return 0;
    }
    代码
  • 相关阅读:
    杭电1075
    杭电1016深度搜索问题
    杭电1015
    stringstream
    向量的点乘和叉乘
    杭电1010
    FCKEditor2.6.3 配置
    JQuery实现全选 与 批量删除
    JQuery实现下拉框的选择 与当CheckBox为服务器控件时如何获取值的操作,实现全选与删除
    JS 对GridView的一些操作
  • 原文地址:https://www.cnblogs.com/zxhl/p/5083604.html
Copyright © 2011-2022 走看看