hdu2114: Calculate S(n)

hdu2114: http://acm.hdu.edu.cn/showproblem.php?pid=2114
题意:求x=1^3+2^3+3^3+……+n^3结果的后四位。 解法:原式x=((n*(n+1))/2)^2,求后四位,即求x%10000,由公式(a*b)%m=((a%m)*(b%m))%m,(a/2)%m=(a%m)/2可求。 code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
int n;
int main()
{
    int s;
    while(scanf("%d",&n)!=EOF)
    {
        s=(n%10000)*((n+1)%10000);
        s=(s%10000)/2;
        s=(s*s)%10000;
        printf("%04d\n",s);  //表示输出后四位,不够补0
    }
}

posted on 2012-07-25 21:21  acmer-jun  阅读(150)  评论(0编辑  收藏  举报

导航