hdu 3422 Triangle

Problem Description
K likes to play with the balls.That day he piled a triangle (n layers, layer 1 start,the first layer has 1 ball,the secode layer has 2 balls,……,the nth layer has n balls), but he felt uncomfortable after completion, and want to pile a right triangle, because he felt that the number 4 is a lucky one (because of “si ji fa cai”). So he decided to use 4 times of the balls he just used as a right-edge side of the right triangle(the three edges have no common factor).He only to pile the three edges,that is the middle is empty.But there will not have so many balls,so he wants to know the minimum of balls he must use.Now ,he turn to you for help.
 

Input
The layer of the triangle as the promble describes n,1 <= n < 2^16
 

Output
The minimum of balls he must use and the length of the hypotenuse. One case one line
 

Sample Input
  
  
1 2 3
 

Sample Output
  
  
9 5 27 13 53 25 The second case: Let the right_edge promble describles is b ,The layer is 2,so b is 4 * (1 + 2) = 12.wo can know the edges of right triangle is 5 , 12 ,13. So the minimum of balls he must use is (5 + 12 + 13 – 3) = 27
 

Author
Klion@CSU
 

Source
 

Recommend
lcy


题意很简单,给定n,求4*(1+..+n)为直角三角形一直角边长的三角形中,周长最小的三角形的斜边和(周长-3),且三边长为整数且三边长无公因子。 ps:因为题目中说是摆小球,所以周长会吧三个顶点都算了两遍。

首先4*(1+...+n)=2n^2+2n,在前面提到过了,如果一个直角三角形三边长为整数且无公因子,则三个数为本原勾股数组,其通项为(st,(s^2-t^2)/2,(s^2+t^2)/2),所以推导过程如下:

这样代码就一行解决了,话说前几天宣儿还说我做的题一般推半天结果最后随便敲两行代码过的……看来是真的咩……orz

代码象征性的送上,另外终于搞清楚hdu的longlong要用%I64d,不然会错的……

#include <stdio.h>

int main()
{
    long long i,j,n;
    while(scanf("%I64d",&n)!=EOF)
    {
        printf("%I64d %I64d\n",4*n*n+6*n-1,2*n*n+2*n+1);
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值