UVa 11401 Triangle Counting:组合计数

11401 - Triangle Counting

Time limit: 1.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_problem&problem=2396

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 n (3<=n<=1000000). The end of input will be indicated by a case with n<3. This case should not be processed.

Output

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45374.htm

For each test case, print the number of distinct triangles you can make.

Sample Input    Output for Sample Input

5
8
0
3
22

首先算出当最大边为x的符合题意的个数有多少,再用递推式累加即可。(因为题目要算的是边长不超过n的个数)

完整代码:

/*0.029s*/

#include<cstdio>  

long long f[1000010];  

int main()
{
    f[3] = 0;
    for (long long x = 4; x <= 1000000; ++x)
        f[x] = f[x - 1] + ((x - 1) * (x - 2) / 2 - (x - 1) / 2) / 2;
    int n;
    while (scanf("%d", &n), n >= 3)
        printf("%lld\n", f[n]);
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索length
, have
Counting
triangle、little triangle、bermuda triangle、triangle社、triangle 韩剧,以便于您获取更多的相关知识。

时间: 2025-01-19 17:04:07

UVa 11401 Triangle Counting:组合计数的相关文章

uva 11401 - Triangle Counting

点击打开链接 题意:给定一个n表示有n个1~n的数,现在要从这里面选出3个不同的整数问可以组成三角形的个数 思路: 1 n的上限是10^6,O(n^2)以上的时间复杂度都无法满足要求 2 设最大的变长为x,另外两边的为y和z并且x y 和z是不同的,那么有y+z > x,因此就有x-y < z < x    根据这个不等式我们知道,y = 1时无解,y = 2时有1个解 ........  y = x-1式有x-2个解.总的就是0+1+2.......+x-2 = (x-1)*(x-2)

UVa 1225 Digit Counting:枚举

1225 - Digit Counting Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3666 N<10000,干脆O(NlogN)建表得了. 完整代码: /*0.012s*/ #include<cstdio> int c[10000]

UVa 494 Kindergarten Counting Game:字符串处理

494 - Kindergarten Counting Game Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=435 Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo

UVa 488 Triangle Wave (water ver.)

488 - Triangle Wave Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=429 In this problem you are to generate a triangular wave form according to a specifie

UVa 11538 Chess Queen:组合&amp;amp;分类

11538 - Chess Queen Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_problem&problem=2533 You probably know how the game of chess is played and how chess queen operates.

UVa 11806 Cheerleaders:组合&amp;amp;逆向思维||容斥定理

11806 - Cheerleaders Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2906 In most professional sporting events, cheerleaders play a major role in entertaining the spectat

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

ACM练级

一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上.  下面给个计划你练练: 第一阶段: 练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打 出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)  3.大数(