nyoj 420 p次方求和

来源:互联网 发布:在线真心话大冒险软件 编辑:程序博客网 时间:2024/06/05 02:20

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

快速幂!

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int iMod = 10003;LL Quic_Mod(int a, int k){    if(k == 1)        return a;    LL tmp = 1, num = a;    while(k)    {        if(k&1)        {            tmp = (tmp%iMod * num)%iMod;            k--;        }        k >>= 1;        num = (num * num) % iMod;    }    return tmp;}int main(){    int T, n, p, i;    LL res;    scanf("%d", &T);    while(T--)    {        scanf("%d %d", &n, &p);        if(p == 0)        {            printf("%d\n", n);            continue;        }        res = 0;        for(i = 1; i <= n; ++i)            res = (res%iMod + (Quic_Mod(i, p)%iMod))%iMod;        printf("%lld\n", res);    }    return 0;}


0 0
原创粉丝点击