HDU 5053 the Sum of Cube(简单数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5053
题目大意:
求出A^3+(A+1)^3+(A+2)^3+...+B^3和是多少
解题思路:
设f(n)=1~n的立方和,则A^3+(A+1)^3+(A+2)^3+...+B^3=f(B) - f(A - 1)
题目给的数的范围是1~10000,1~10000立方和不会超过__int64(long long)的范围。由于是10000个数立方和。
所以可以选择打表(不知道立方和的公式,可以选择打表)。
立方和公式:1^3+2^3+3^3+4^3+...+n^3 = (n*(n+1)/2)^2
AC代码:
这里是是打表的思路
#include<stdio.h>
#include<string.h> typedef long long LL; const LL MAXN = ; int main(){
int T, A, B;
LL ans, a[MAXN]; memset(a, , sizeof(a));
for(LL i = ; i < MAXN; ++i){
a[i] = a[i - ] + i * i * i;
} scanf("%d", &T);
for(int cs = ; cs <= T; ++cs){
scanf("%d%d", &A, &B); printf("Case #%d: %I64d\n", cs, a[B] - a[A - ]);
}
return ;
}
利用公式:
#include<stdio.h>
#include<string.h> typedef long long LL; LL f(LL n){
return n * (n + ) * n * (n + ) / ;
} int main(){
int T, A, B;
scanf("%d", &T);
for(int cs = ; cs <= T; ++cs){
scanf("%d%d", &A, &B);
printf("Case #%d: %I64d\n", cs, f(B) - f(A - ));
}
return ;
}
HDU 5053 the Sum of Cube(简单数论)的更多相关文章
- hdu 5053 the Sum of Cube(上海网络赛)
the Sum of Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5053 the Sum of Cube
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 解题报告:用来陶冶情操的题,求a到b的三次方的和. #include<stdio.h> ...
- 2014上海网络赛 HDU 5053 the Sum of Cube
水 #include <stdio.h> #include <stdlib.h> #include<math.h> #include<iostream> ...
- 杭电 5053 the Sum of Cube(求区间内的立方和)打表法
Description A range is given, the begin and the end are both integers. You should sum the cube of al ...
- HDU 1024 Max Sum Plus Plus 简单DP
这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...
- hdu 2058 The sum problem(简单因式分解,,)
Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...
- Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】
Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...
- 七夕节 (HDU - 1215) 【简单数论】【找因数】
七夕节 (HDU - 1215) [简单数论][找因数] 标签: 入门讲座题解 数论 题目描述 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们 ...
- (step7.2.1)hdu 1395(2^x mod n = 1——简单数论)
题目大意:输入一个整数n,输出使2^x mod n = 1成立的最小值K 解题思路:简单数论 1)n可能不能为偶数.因为偶数可不可能模上偶数以后==1. 2)n肯定不可能为1 .因为任何数模上1 == ...
随机推荐
- win10休眠选项在哪里设置?如何设置?
本人以前安装的Win7也是碰到这个问题 http://www.jb51.net/os/win10/373383.html 查询.打开休眠命令 1.右键开始菜单,选择命令提示符(管理员) 或 win+R ...
- NoSQL-Redis【2】-HDEL给我的一个惊喜
用HDEL删除Hash最后一个域的时候,Redis奇迹般的帮我清除了Key,真是考虑周到啊! 127.0.0.1:6673[2]> HMSET USER:Zhangshan Name 'Zhan ...
- 如何用inno setup打包activex
需要解决三个问题,运行环境检测与安装,按顺序执行安装,activex注册. 运行环境检测与安装 最开始的方法,百度之后,根据网上的搜索的结果,使用了RegQueryDWordValue(HKLM, ' ...
- Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken
在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...
- struts2自定义MVC框架
自定义MVC:(首先了解Model1和Model2的概念) Model1与Model2: Model1:就是一种纯jsp开发技术,将业务逻辑代码和视图渲染代码杂糅在一起. Model2:Model2是 ...
- Vue.js组件之联动下拉框
Html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- Javascript实现页面加载完成后自动刷新一遍清除缓存文件
我们有些时候在加载页面时,会出现缓存文件对当前文件的表现效果有干扰,如有些缓存的样式文件会是页面效果发生改变,这时我们希望页面在加载时能自动刷新一遍清楚缓存文件. 但是由于跳转页面肯定会用到BOM部分 ...
- Javascript学习之无缝滚动
无缝滚动Javascript: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- C 格式输出
1 一般格式 printf(格式控制,输出表列) 例如:printf("i=%d,ch=%c\n",i,ch); 说明: (1) “格式控制”是用双撇号括起 ...
- 总结-Intellij Idea (快捷键 配置修改)
忽略大小写 输入sensitive,选择Code Completion,右边第一个下拉框,选择noneEditor 鼠标悬浮show quick docEditor Editor Tabs : Mar ...