Triangle Counting

Time limit1000 ms

Description

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

Output

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

Simple Input

5

8

0

Simple Output

3

22


解题心得:

  1. 刚开始看到题的时候以为是以前做过的一个dfs的三角形拼接,但是这个题问的是用从1到n长度的木棍来拼接,然后比较容易就可以看出是一个递推的问题,每增添一根木棍答案就会增加一部分,然后就是怎么递推了。
  2. 这个递推的思想其实就是先算出所有能够选择的方案,然后减去重复的方案。从第一条边开始看有多少种方案,会发现是0+1+2+…..(x-2) = (x-1)*(x-2)/2。但是里面包含了不符合要求的,不符合要求的(两边之和不大于第三边)数量(n-1)/2,另外还有重复的,每个符合条件的情况算了两次(从前往后依次,从后往前一次),还要除2。
  3. 还可以按照组合数学来算,但是这样也挺麻烦的,好像还可以寻找循环节。

#include<stdio.h>
#include<iostream>
#include<queue>
#include<cstring>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 1000010;
typedef long long ll;
ll f[maxn];
void pre_deal()
{
for(long long i=4;i<maxn;i++)
{
ll k = ((ll)(i-1)*(ll)(i-2)/2-(ll)(i-1)/2)/2;
f[i]=f[i-1]+k;
}
}
int main()
{
memset(f,0,sizeof(f));
ll n;
pre_deal();
while(scanf("%lld",&n))
{
if(n<3)
break;
printf("%lld\n",f[n]);
}
return 0;
}

Uva:11401-Triangle Counting的更多相关文章

  1. uva 11401 Triangle Counting

    // uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...

  2. 【递推】【组合计数】UVA - 11401 - Triangle Counting

    http://blog.csdn.net/highacm/article/details/8629173 题目大意:计算从1,2,3,...,n中选出3个不同的整数,使得以它们为边长可以构成三角形的个 ...

  3. UVa 11401 Triangle Counting (计数DP)

    题意:给定一个数 n,从1-n这些数中任意挑出3个数,能组成三角形的数目. 析:dp[i] 表示从1-i 个中任意挑出3个数,能组成三角形的数目. 代码如下: #pragma comment(link ...

  4. uva11401:Triangle Counting 递推 数学

    uva11401:Triangle Counting 题目读不清楚的下场就是多做两个小时...从1-n中任选3个不重复数字(不重复啊!!坑爹啊!)问能组成三角形的有多少个, 显然1~n能组成的三角形集 ...

  5. [Usaco2010 OPen]Triangle Counting 数三角形

    [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 394  Solved: 1 ...

  6. bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形 计算机和

    [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 526  Solved: 2 ...

  7. acdream.A Very Easy Triangle Counting Game(数学推导)

    A - A Very Easy Triangle Counting Game Time Limit:1000MS     Memory Limit:64000KB     64bit IO Forma ...

  8. bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥

    1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 272  Sol ...

  9. 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. ...

随机推荐

  1. 关于安卓visualizer的用法

    看别人显示播放wav文件显示频谱写的代码都是断断续续的,在这里我贴了完整的代码,给有需要的人做参考,显示频谱还没有完成,不知道怎么弄,已经可以得到byte[] fft数据了,参考别人的写法也可以开方取 ...

  2. 从github克隆内容到本地时权限问题

    从github克隆内容到本地时权限问题

  3. Android 5.0以上获取系统运行进程信息

    在Android 5.0以上系统,调用getRunningAppProcesses 方法返回的列表为空,这是因为谷歌考虑到安全原因,已经把这个方法移除掉了, 那以后要获取系统运行的后台进程这个方法用不 ...

  4. Python高效开发实战——Django、Tornado、Flask、Twisted

    今天要推荐的就是这本书,内容涉及四种主流的Python Web开发框架,零基础完成网站搭建.数据库设计.前后端开发,全方位领悟Python原理与应用. 最新最全的框架实战,尽在这本书,可搜索亚马逊.京 ...

  5. uvm_port_base——TLM1事务级建模方法(五)

    文件: src/tlm1/uvm_port_base.svh 类: uvm_port_base uvm_port_component_base派生自uvm_component,因此具有其所有特性.提供 ...

  6. zabbix中监控项报错

    报错信息: zabbix报错(Not all processes could be identified, non-owned process info will not be shown, you ...

  7. IIS7.5配置自动添加www 及 限制通过IP访问web

    IIS7.5配置自动添加www 方法 新建一个站点2(主机名为不带www的站点),将其重定向至带www的URL即可. 注意以下几点 站点2不可与站点1的路径一致,否则会导致站点1也会添加同样的重定向, ...

  8. android RadioGroup设置某一个被选中

    见码滚 mPriorityRadioGroup.clearCheck(); mStatusRadioGroup.clearCheck(); RadioButton r1 = (RadioButton) ...

  9. [转载]AngularJS快速开始

    AngularJS快速开始 Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:hel ...

  10. Aizu 2304 Reverse Roads(无向流)

    把有向图修改成无向图,并保证每条边的流量守恒并满足有向容量(即abs(flow(u,v) - flow(v,u)) <= 1)满足限制. 得到最大流,根据残流输出答案. 因为最后少了'\n'而W ...