牛客训练四: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

 

转载于:https://www.cnblogs.com/2018zxy/p/10335704.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值