hdu2114 Calculate S(n)

来源:互联网 发布:淘宝董事长是谁 编辑:程序博客网 时间:2024/05/11 15:05

Calculate S(n)

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


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
12
 

Sample Output
00010009
 

不需要从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;}


0 0