快速幂取模

    快速幂取模

原理为(a*b)%c=(a%c*b%c)%c。
用于求幂之后取模。
例题:
Description

求a的b次方后四位。

Input

输入的第一行是T(不超过1000)。T表示测试部分的个数,每一部分都要求单独计算并按照要求输出结果。

接下来是每个测试部分。第一行给出a, b,0

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        long long m=1,x=a%10000;
        while(b)
        {
            if(b%2==1)
                m=m*x%10000;//*
              x=x*x%10000;//*
            b/=2;

        }
        printf("%04lld\n",m);
    }
}

posted @ 2016-11-26 17:48  _大美  阅读(123)  评论(0编辑  收藏  举报