codeforces 17 D Notepad 快速幂?

题意:求((b-1)*bn-1)%c  b和n都很大很大

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
char b[1000005],n[1000005];
int main()
{
    int i,j,k,m,len1,len2,c;
    LL tp,ans,x;
    while(scanf(" %s %s %d",b,n,&c)!=EOF)
    {
        len1=strlen(b);
        x=0;
        for(i=0; i<len1; i++)
        {
            x=x*10+b[i]-'0';
            x%=c;
        }
        ans=x-1;
        if(ans<0) ans+=c;
        len2=strlen(n);
        for(i=len2-1; i>=0; i--)
        {
            if(n[i]>'0')
            {
                n[i]--;
                break;
            }
            n[i]='9';
        }
        for(i=len2-1; i>=0; i--)
        {
            tp=x;
            for(j=1; j<=9; j++)
            {
                if(j==n[i]-'0') ans*=x;
                x*=tp;
                x%=c;
            }
            ans%=c;
        }
        if(ans==0) ans=c;
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zuferj115/p/5333951.html