NYOJ 题目982 Triangle Counting(数学,递推)

27 篇文章 0 订阅

Triangle Counting

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
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.
输入
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.
输出
For each test case, print the number of distinct triangles you can make.
样例输入
5
8
0
样例输出
3
22
来源
UVA
上传者

TC_李远航

思路详解:http://blog.csdn.net/lyhvoyage/article/details/21692527

ac代码

#include<stdio.h>
#include<string.h>
long long ans[1000005];
void fun()
{
	long long i;
	ans[1]=ans[2]=ans[3]=0;
	for(i=4;i<=1000000;i++)
		ans[i]=ans[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2;
}
int main()
{
	int n;
	fun();
	while(scanf("%d",&n)!=EOF)
	{
		if(n<1)break;
		printf("%lld\n",ans[n]);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值