UVa 11401 - Triangle Countin

本文探讨了如何通过组合数学和计数原理解决一个特定的数学问题:在给定长度范围内的木棍中,有多少种方式可以组成合法的三角形。通过分析数据规律,我们得到了一个通用的求解公式,并提供了简化后的AC代码实现。

【题意】给了n根木棍,木棍的长度是1-n,问有多少种方式可以组成合法的三角形,n的上限可以到1000000。

【解题思路】当然,这题是数学题,当然要想办法找规律了。我自己推了很久,都没发现,自己太弱啦,只能看网上的解法了。

【他人的思路】

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

            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类型。


【按照公式简单获取AC代码】

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll unsigned long long
const int maxn = 1000002;
ll a[maxn],sum[maxn];
int main()
{
    sum[3]=0;
    for(int i=4; i<=maxn; i++)
    {
        a[i] = (ll)(i*i-i*4+4)/4;
        sum[i]=sum[i-1]+a[i];
    }
    int n;
    while(~scanf("%d",&n)&&n>=3)
    {
        printf("%llu\n",sum[n]);
    }
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值