UVa 11401 - Triangle Countin

本文通过组合数学和计数原理,探讨了利用给定棍子长度构成不同三角形的数量,具体分析了正向求解方法,揭示了构成三角形数量的规律,并给出了求解公式。

题目:给你n根长度分别为1,2,..,n的棍子,问能组成多少个不同的三角形。

分析:组合数学,计数原理。本题可以正向求解也可以反向求补集,这里采用正向求解。

            1.首先写出前几组数据,找规律:{ 里面的括号是子情况 }

            (4,3,(2))

            (5,4,(3,2))

            (6,5,(4,3,2))(6,4,(3))

            (7,6,(5,4,3,2))(7,5,(4,3))

            (8,7,(6,5,4,3,2))(8,6,(5,4,3))(8,5,(4))

             对于上述的数据采用记号[a,b,c,...] 记录对应每种的子情况数,则转化如下:

             [1]

             [2]

             [3,1]

             [4,2]

             [5,3,1]

            观察发现,每组中对应的子情况数依次递增1,每当最后的一组变为3时,后面就出现新的组;

            这是因为n的奇偶性不同产生的影响,当最长的边为l时,对应存在的解应该如下:

            (l,l-1,(2))(l,l-2,(3,2)),... ,(l,l-k,(k,..,2))

            无论l的奇偶性,k均取值l/2(这里是整除),因此解的个数与奇偶性相关的;

           2.然后观察计算

           解的个数为:n-3 + n-5 + .. + r;{ n为奇数r为2,n为偶数r为1 }

           分就两种情况求通向公式有:

           f(n)=(n^2 + 4n +4)/ 4 { n为偶数 };f(n)= (n^2 + 4n +3)/ 4 { n为奇数 };

           因为写成程序时是整除运算,所以这里都是用偶数的通项公式没有影响;

           因此有:f(n)= (n^2 + 4n +4)/ 4,为最长边为n时的解的个数,求和输出即可。

说明:注意使用long long类型。

#include <iostream>
#include <cstdlib>

using namespace std; 

long long F[1000001];
long long S[1000001];

int main()
{
	long long temp;
	for (int i = 4 ; i < 1000001 ; ++ i) {
		F[i] = (1LL*i*i-i*4LL+4LL)/4LL;
		S[i] = 0LL+F[i]+S[i-1];
	}
	int n;
	while (cin >> n && n >= 3)
		cout << S[n] << endl;
	
	return 0;
}


There were different types of fish in your aquarium. But they did not go along well with each other. So there had been Fish-War-1 among them. It was a complete mess. Lot of fishes died, many of them hid in some mountain, some were eaten by other fishes and so on. So you decided to compartmentalize your aquarium. You divided your aquarium into R x C grids, that is R rows and C columns. Then you inserted walls into each cell. The walls are slanted, that is it goes from north-east corner to south-west corner or north-west corner to south-east corner. They look like “/” or “\” respectively. Many years passed since the war. Now the fishes want to unite again. They want to bring down the walls. They measured the strength of each of the walls. What is the minimum amount of strength they need to spend to unite all the compartments? For example, in the following 2 x 2 grid, they can spend 7 + 9 + 12 = 28 unit strength to unite the 4 compartments. And this is the minimum. First line of the input contains number of test case T (<= 20). Hence follows T test cases. First line of the test case describes number of row R and number of columns C (1 <= R, C <= 100). Next R lines describe the walls. Each of these lines contains C characters and the characters are either “/” or “\”. Next R lines contain C positive integers, each describes the strength of the wall at the corresponding cell. The strength of a wall would be at most 10,000. For each test case output the case number and the minimum amount of strength to unite all the compartments in the aquarium. 输入样例 2 2 2 \/ \\ 7 10 12 9 1 3 /\\ 3 4 5 输出样例 Case 1: 28 Case 2: 12
最新发布
12-23
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值