首页 > ACM题库 > HDU-杭电 > hdu 2139 Calculate the formula[解题报告]C++
2013
12-29

hdu 2139 Calculate the formula[解题报告]C++

Calculate the formula

问题描述 :

You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2.

输入:

In each case, there is an odd positive integer n.

输出:

In each case, there is an odd positive integer n.

样例输入:

3

样例输出:

10

2011-12-16 07:00:16

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2139

题意:算平方和的奇数项目。

mark:1wa,没用long long。公式是(4*p*p*p+12*p*p+11*p+3)/3。其中2p+1 == n。

代码:

# include <stdio.h>


int main ()
{
    long long n, p ;
    while (~scanf ("%I64d", &n))
    {
        p = (n-1) / 2 ;
        printf ("%I64d\n", (4*p*p*p+12*p*p+11*p+3)/3) ;
    }
    return 0 ;
}

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/06/2314956.html