Calculate S(n)
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10074 Accepted Submission(s): 3646
Problem Description
Calculate S(n).
S(n)=13+23 +33 +......+n3 .
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
Recommend
威士忌
有两种方法可以实现
第一种方法是套公式
前n项和的立方公式为 : s(n)=(n*(n+1)/2)^2;
前n项和的平方公式为:s(n)=n*(n+1)(2*n+1)/6;
前n项和的平方公式为:s(n)=n*(n+1)(2*n+1)/6;
如果不用公式的话可以看看下面的代码 因为是以10000为一个循环 所以只要乘上循环次数就基本可以了
代码如下
#include<stdio.h>
int main()
{
__int64 k,c,s,n,i,a[10001]={0};
for(i=1;i<=10000;i++)
{
a[i]=(a[i-1]+i*i*i)%10000;
}
while(scanf("%I64d",&n)!=EOF)
{
k=n/10000;
c=n%10000;
s=(a[10000]*k+a[c])%10000;
if(s<1000)
printf("0");
if(s<100)
printf("0");
if(s<10)
printf("0");
printf("%I64d\n",s);
}
}