Uva:11401-Triangle Counting
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
解题心得:
- 刚开始看到题的时候以为是以前做过的一个dfs的三角形拼接,但是这个题问的是用从1到n长度的木棍来拼接,然后比较容易就可以看出是一个递推的问题,每增添一根木棍答案就会增加一部分,然后就是怎么递推了。
- 这个递推的思想其实就是先算出所有能够选择的方案,然后减去重复的方案。从第一条边开始看有多少种方案,会发现是0+1+2+…..(x-2) = (x-1)*(x-2)/2。但是里面包含了不符合要求的,不符合要求的(两边之和不大于第三边)数量(n-1)/2,另外还有重复的,每个符合条件的情况算了两次(从前往后依次,从后往前一次),还要除2。
- 还可以按照组合数学来算,但是这样也挺麻烦的,好像还可以寻找循环节。
#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的更多相关文章
- uva 11401 Triangle Counting
// uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...
- 【递推】【组合计数】UVA - 11401 - Triangle Counting
http://blog.csdn.net/highacm/article/details/8629173 题目大意:计算从1,2,3,...,n中选出3个不同的整数,使得以它们为边长可以构成三角形的个 ...
- UVa 11401 Triangle Counting (计数DP)
题意:给定一个数 n,从1-n这些数中任意挑出3个数,能组成三角形的数目. 析:dp[i] 表示从1-i 个中任意挑出3个数,能组成三角形的数目. 代码如下: #pragma comment(link ...
- uva11401:Triangle Counting 递推 数学
uva11401:Triangle Counting 题目读不清楚的下场就是多做两个小时...从1-n中任选3个不重复数字(不重复啊!!坑爹啊!)问能组成三角形的有多少个, 显然1~n能组成的三角形集 ...
- [Usaco2010 OPen]Triangle Counting 数三角形
[Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 394 Solved: 1 ...
- bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形 计算机和
[Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 526 Solved: 2 ...
- acdream.A Very Easy Triangle Counting Game(数学推导)
A - A Very Easy Triangle Counting Game Time Limit:1000MS Memory Limit:64000KB 64bit IO Forma ...
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥
1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 272 Sol ...
- 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. ...
随机推荐
- Spring整合Struts2 XML版
1.jar包 <!--spring配置--> <dependency> <groupId>org.springframework</groupId> & ...
- tomcat的备份脚本
reference:Crontab的20个例子 先科普一下date的使用方法,在sh脚本中经常会使用得到 date -d<字符串>:显示字符串所指的日期与时间.字符串前后必须加上双引号: ...
- 【Python】python2 html safe string
import cgi s = '<>&' s += u'哈哈' print type(s) print s r = cgi.escape(s) print type(r) prin ...
- bootstrapValidator 重置表单
最近在公司做业务系统后台,用的js框架还是jquery ui框架 是以bootstrap为基础的beyondadmin-v1.4.s3这套,用起来还挺不错,所以公司所有的后台ui都使用这套.这套ui ...
- Vue日历
Vue生成日历,根据返回值将日期标红 HTML: <h1>CSS 日历</h1> <div id="calendar"> <div cla ...
- /usr/local/sbin/arpspoof
/usr/local/sbin/arpspoof arpspoof -t 攻击者ip地址 网关ip地址 稍等系,被攻击者机器的arp的缓存就已经变了.
- 在MVC中使用NHibernate学习记录
NHibernate简介: NHibernate是一个面向.net环境的对象/关系数据库映射工具,对象/关系数据库映射(object/relational mapping,ORM)是一种技术,可以将对 ...
- 常用工具使用(sublimeText)
1.sublime Text (插件的安装,删除,更新) 1.1 使用 ctrl+`快捷键(Esc下面的波浪线按钮) 或者 菜单项View > Show Console 来调出命令界面,下面代 ...
- 【Orange Pi Lite2】 ——2《在使用之前的配置》(未完)
[Orange Pi Lite2] --2<在使用之前的配置> 本文只在博客园发布 在开始前你需要准备的材料与软件 filezilla/或者不 声明 : 本教程适合0基础新手,本章将会介绍 ...
- IOS 长按+轻扫(手势识别)
@interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...