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 }
时间: 2024-10-13 16:00:03

Calculate the formula的相关文章

HDU2139 Calculate the formula【水题】

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+--+

HDOJ(HDU) 2139 Calculate the formula(水题,又一个用JavaAC不了的题目)

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 看到这个时间,我懵逼了... 果然,Java就是打表,都不能AC,因为Java的输入是流,需要的时间比C真的长好多.... Problem Description You just need to calculate the sum of the formula: 1^2+3^2+5^2+--+ n ^2. Input In each

J - Calculate the formula

You just need to calculate the sum of the formula: 1^2+3^2+5^2+--+ n ^2. InputIn each case, there is an odd positive integer n.OutputPrint the sum. Make sure the sum will not exceed 2^31-1Sample Input 3 Sample Output 10代码如下: (不过还是建议大家看看下面的错误代码和思路)#in

HDU 2139 Calculate the formula

http://acm.hdu.edu.cn/showproblem.php?pid=2139 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 excee

【转】朱兆祺教你如何攻破C语言学习、笔试与机试的难点(连载)

原文网址:http://bbs.elecfans.com/jishu_354666_1_1.html 再过1个月又是一年应届毕业生应聘的高峰期了,为了方便应届毕业生应聘,笔者将大学四年C语言知识及去年本人C语言笔试难点进行梳理,希望能对今年应届毕业生的应聘有所帮助. 2013年10月18日更新-->    攻破C语言这个帖子更新到这里,我不仅仅是为了补充大学学生遗漏的知识,我更重要的是希望通过我的经验,你们实际项目中的C语言写得漂亮,写出属于你的风格.“朱兆祺STM32手记”(http://bb

先贴一段上学时候写的代码占个位

C#计算器 using System; using System.Collections.Generic; using System.Linq; namespace System { /// <summary> /// 计算器类 /// </summary> public class Calculator { string strFormula; /// <summary> /// 用一个字符串表达式初始化实例. /// </summary> /// <

HDU 4342History repeat itself 数学

C - History repeat itself Time Limit:1000MS     Memory Limit:32768KB Description Tom took the Discrete Mathematics course in the 2011,but his bad attendance angered Professor Lee who is in charge of the course. Therefore, Professor Lee decided to let

2012 #5 History repeat itself

History repeat itself Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4342 Description Tom took the Discrete Mathematics course in the 2011,but his bad attendance angered Professor Lee who is in

HDU 4342——History repeat itself——————【数学规律】

History repeat itself Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 434264-bit integer IO format: %I64d      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-S