NYOJp次方求和

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=420


快速幂的模板题;


代码:

#include <cstdio>

#define mod 10003

int pow_mod(int a,int p,int m)

{
    if(p == 0)
        return 1;
    int x = pow_mod(a,p / 2,m);
    long long ans = (long long)x * x % m;
    if(p & 1)
        ans = ans * a % m;
    return (int)ans;
}

int main()

{
    int n,p;
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            int ans = 0;
            scanf("%d%d",&n,&p);
            for(int i = 1;i <= n;++i)
                ans = (ans + pow_mod(i,p,mod)) % mod;
            printf("%d\n",ans);
        }
    }
    return 0;
}

你可能感兴趣的:(ACM,快速幂)