首页 > 代码库 > HDU2114 Calculate S(n)【数学】

HDU2114 Calculate S(n)【数学】

Calculate S(n)

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8423    Accepted Submission(s): 3060

Problem Description
Calculate S(n).

S(n)=13+23 +33 +......+n3 .
 
Input
Each line will contain one integer N(1 < n < 1000000000). Process to end of file.
 
Output
For each case, output the last four dights of S(N) in one line.
 
Sample Input
1
2
 
Sample Output
0001
0009
 
Author
天邪
 
Source

HDU 2007-10 Programming Contest_WarmUp


题目大意:计算1~N的立方和

思路:1~N的立方和公式为S(N) = 1^3 + 2^3 + 3^3 + … + N^3 = N^2*(N+1)^2/4。

然后题目要求后四位,取余即可。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    __int64 N,ans;
    while(cin >> N)
    {
        N %= 10000;
        ans = (N*(N+1)/2)%10000 * (N*(N+1)/2)%10000;
        printf("%04I64d\n",ans);
    }

    return 0;
}


HDU2114 Calculate S(n)【数学】