快速幂模板

快速幂求n^n:

int f1(int a,int b)
{
    int t=1;
    while(b)
    {
        if(b % 2 != 0)
        {
            t*=a;
            b--;            
        }
        a*=a;
        b/=2;
    }
    return t;
}

快速幂求n^n后y位:

int f2(int a,int b)
{
    int t=1;
    while(b)
    {
        if(b % 2 != 0)
        {
            t=(t*a)%x;    //x控制要求的位数 
            b--;
        }
        a=(a*a)%x;
        b/=2;
    }
    return t;
}

posted @ 2018-06-07 21:11  MCQ  阅读(81)  评论(0编辑  收藏  举报