hdu 2114 Calculate S(n)
Calculate S(n)
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15863 Accepted Submission(s): 5576
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
解题思路:
不需要从1算到10^10,必然超时,其实输出S(10000)就会发现它等于0,所以这是循环的,只需要看后四位
代码:
#include<stdio.h>
int s[10004]={0};
int f(int a)
{
a%=10000;
int b=a*a%10000;
a=b*a%10000;
return a;
}
int main()
{
int n,i,q,p,ans;
for(i=1; i<=10000; i++)
s[i]=(s[i-1]+f(i))%10000;
while(scanf("%d",&n)!=EOF)
{
p=n%10000;
ans=s[p];
printf("%04d\n",ans);
}
return 0;
}
评论