您的位置:首页 > 其它

HDU-#5053 the Sum of Cube

2014-09-27 18:20 369 查看
题目大意:求[a,b]之间每个数的立方和。

解题思路:签到题,直接求解会WA的,所以直接用公式求解,为:1^3+2^3+3^3+……+n^3=(n*(n+1))^2,详见code。

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5053

code:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

#define ll __int64
int t,a,b;

ll cube(ll m){
return (m*(m+1)/2)*(m*(m+1)/2);
}

int main(){
int cases=0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&a,&b);
printf("Case #%d: %I64d\n",++cases,cube(b)-cube(a-1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: