牛客训练四:Applese 涂颜色(费马小定理+快速幂)

题目链接:传送门

思路:

考虑每一列有2种颜色,总共有n行,每一行的第一个格确定颜色,由于左右颜色不相同,后面的行就确定了。

所以总共有2^n中结果。

由于n太大,所以要用到费马小定理a^n%mod=a^(n%(mod-1))%mod,所以先求出a的指数,然后用快速幂求解就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long LL;
const LL MOD = 1e9+7;
const int maxn = 100100;
char str1[maxn],str2[maxn];
int main(void)
{
    int i,j;
    LL ans=0,n,res=1,tp=2;
    scanf("%s%s",str1,str2);
    n=strlen(str1);
    for(i=0;i<n;i++) ans=(ans*10+str1[i]-'0')%(MOD-1);
    n=ans;
    while(n){
        if(n&1) res=res*tp%MOD;
        tp=tp*tp%MOD;
        n>>=1;
    }
    printf("%lld\n",res);
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/2018zxy/p/10335704.html
今日推荐