Calculate the formula

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
 
用普通的做法会超时,只能用公式算n*(4*n*n-1)/3(其中n为第几个数),还有题目明明说好结果在int范围内的,但是必须用__int64才能过,郁闷。。。
 
 1 #include <stdio.h>
 2 
 3 int main(){
 4     __int64 number;
 5     __int64 n;
 6     __int64 result;
 7 
 8     while(scanf("%I64d",&number)!=EOF){
 9         n=(number+1)/2;
10 
11         result=n*(4*n*n-1)/3;
12         printf("%I64d\n",result);
13 
14     }
15     return 0;
16 }

 

 
 

 

posted @ 2014-11-10 21:17  zqxLonely  阅读(392)  评论(0编辑  收藏  举报