Calculate the formula
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7441 Accepted Submission(s): 2284
Problem Description
You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2.
Input
In each case, there is an odd positive integer n.
Output
Print the sum. Make sure the sum will not exceed 2^31-1
Sample Input
3
Sample Output
10
Author
wangye
Source
HDU 2007-11 Programming Contest_WarmUp
题目大意:给你一个奇数N,求1~N中奇数的平方和。
思路:直接暴力超时了,所以用公式来做 S = N*(N+1)*(N+2)/6,因为结果不超int型,
但是中间过程会超一些,所以用__int64来做就可以了。注意cin、cout会超时,用scanf
和printf就可以了。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
__int64 N,sum;
while(~scanf("%I64d",&N))
{
sum = 0;
sum = N*(N+1)*(N+2)/6;
printf("%I64d\n",sum);
}
return 0;
}