Tanky WooRSS

HDU/HDOJ 2139 Calculate the formula

19 Jan 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2139

分析:

数学题。

已知:1^2+2^2+3^2+……+n^2 =n(n+1)(2n+1)/6 —① 那么1^2+2^2+3^2+……+n^2+……+(2n+1)^2 =(2n+1)(n+1)(4n+3)/3 —② 又有2^2+4^2+6^2+……+(2n)^2 =4[1^2+2^2+3^2+……+n^2]=4*①=2n(n+1)(2n+1)/3 —③   设所求为S     比较②和③可知 S=②-③=(2n+1)(n+1)(4n+3)/3-2n(n+1)(2n+1)/3 =(2n+1)(n+1)(2n+3)/3 —④ 因为S是2n+1项的和 把它一般化 则奇数项平方和一般公式Sn=n(n+1)(n+2)/6

#include
using namespace std;
int main()
{
    __int64 sum,n;
    while(scanf("%I64d", &n;)!=EOF)
    {
        if(n%2)
        {
            sum = n*(n+1)*(n+2)/6;
            printf("%I64d\n", sum);
        }
    }
    return 0;
}