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代码:

这里是是打表的思路

 1 #include<stdio.h>
 2 #include<string.h>
 3
 4 typedef long long LL;
 5
 6 const LL MAXN = 10001;
 7
 8 int main(){
 9     int T, A, B;
10     LL ans, a[MAXN];
11
12     memset(a, 0, sizeof(a));
13     for(LL i = 1; i < MAXN; ++i){
14         a[i] = a[i - 1] + i * i * i;
15     }
16
17     scanf("%d", &T);
18     for(int cs = 1; cs <= T; ++cs){
19         scanf("%d%d", &A, &B);
20
21         printf("Case #%d: %I64d\n", cs, a[B] - a[A - 1]);
22     }
23     return 0;
24 }

利用公式:

 1 #include<stdio.h>
 2 #include<string.h>
 3
 4 typedef long long LL;
 5
 6 LL f(LL n){
 7     return n * (n + 1) * n * (n + 1) / 4;
 8 }
 9
10 int main(){
11     int T, A, B;
12     scanf("%d", &T);
13     for(int cs = 1; cs <= T; ++cs){
14         scanf("%d%d", &A, &B);
15         printf("Case #%d: %I64d\n", cs, f(B) - f(A - 1));
16     }
17     return 0;
18 }
时间: 2024-11-05 12:25:33

HDU 5053 the Sum of Cube(简单数论)的相关文章

HDU 5053 the Sum of Cube(数学求立方和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 Problem Description A range is given, the begin and the end are both integers. You should sum the cube of all the integers in the range. Input The first line of the input is T(1 <= T <= 1000), whic

HDU - 5053 the Sum of Cube

Problem Description A range is given, the begin and the end are both integers. You should sum the cube of all the integers in the range. Input The first line of the input is T(1 <= T <= 1000), which stands for the number of test cases you need to so

hdu 5053 the Sum of Cube(上海网络赛)

the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 405    Accepted Submission(s): 224 Problem Description A range is given, the begin and the end are both integers. You should sum

2014上海网络赛 HDU 5053 the Sum of Cube

水 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<math.h> 4 #include<iostream> 5 #define LL long long 6 using namespace std; 7 8 int main() 9 { 10 int t; 11 int a,b; 12 int cas; 13 LL sum; 14 while(~scanf("%d",&

hdu 5053 the Sum of Cube---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 140    Accepted Submission(s): 80 Problem Description A range is given, the b

杭电 5053 the Sum of Cube(求区间内的立方和)打表法

Description A range is given, the begin and the end are both integers. You should sum the cube of all the integers in the range. Input The first line of the input is T(1 <= T <= 1000), which stands for the number of test cases you need to solve. Eac

hdu 1220 Cube 简单数论

Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1516    Accepted Submission(s): 1206 Problem Description Cowl is good at solving math problems. One day a friend asked him such a question:

hdu 2058 The sum problem(简单因式分解,,)

Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. Input Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 100000000

HDU - 2058 The sum problem(简单数学题)

题意:求出所有的情况,等差上去可以达到m值. 原来想着暴力搜索,但是题中的数据太大,所以时间超限. 百度了一下,发现可以套公式. 等差求和公式: Sn=(a1+aN)*n/2     =(a1+a1+d(n-1))*n/2     =a1*n+d(n-1)*n/2; 因为此处公差d=1,所以Sn=a1*n+(n-1)*n/2,当从第一项开始算起时(因本题首项为1,即a1=1时),Sn=M时的项的个数n最多; a1=1,现在又可化简为Sn=n+(n-1)*n/2=(n+1)n/2; 由题意得M=S