Road preliminaries of the 2019 meter garlic Game 3 - Alibaba assist SARS campaign Fermat's Little Theorem descending

Topic links: https://nanti.jisuanke.com/t/38352

After the discovery rule is considered ans = 2 ^ (n-1) + 4 ^ (n-1). But note that n is a very large length of digital 1e5. To find a way descending.

Our concept Chafei Ma little theorem: a ^ (p-1)% p = 1. Found that for a prime number modulus, a ^ (p-1) is a circular section (calculated as equal to 1), it can not be ignored.

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int P=1e9+7;
typedef long long LL;
char s[N];

LL power(LL x,LL p) {
    LL ret=1;
    for (;p;p>>=1) {
        if (p&1) ret=(ret*x)%P;
        x=(x*x)%P;
    }
    return ret;
}

int main()
{
    while (scanf("%s",s) && s[0]!='0') {
        LL n=0;
        for (int i=0;i<strlen(s);i++) {
            n=n*10%(P-1)+(s[i]-'0'); n%=(P-1);
        }
        n=((n-1)+(P-1))%(P-1);
        printf("%d\n",(power(2,n)+power(4,n))%P);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/clno1/p/10987740.html