链接:https://ac.nowcoder.com/acm/contest/330/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
精通程序设计的 Applese 叕写了一个游戏。
在这个游戏中,有一个 n 行 m 列的方阵。现在它要为这个方阵涂上黑白两种颜色。规定左右相邻两格的颜色不能相同。请你帮它统计一下有多少种涂色的方法。由于答案很大,你需要将答案对 109+7109+7 取模。
输入描述:
仅一行两个正整数 n, m,表示方阵的大小。
输出描述:
输出一个正整数,表示方案数对 109+7109+7 取模。
示例1
输入
复制
1 1
输出
复制
2
示例2
输入
复制
2 2
输出
复制
4
备注:
1≤n,m≤10^1000001≤n,m≤10^100000
思路:费马小定律(降幂)+快速幂
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#define in(x) scanf("%lld",&x)
#define out(x) printf("%lld",x)
using namespace std;
typedef long long int ll;
const int mod=1e9+7;
ll c;
ll binarym(ll a,ll b)
{
ll sum=1;
while(b){
if(b&1) sum=sum*a%mod;
a=a*a%mod;
b=b>>1;
}
return sum;
}
int main()
{
ll temp=0;
int size;
string n,m;
cin>>n>>m;
size=m.size();
for(int i=0;i<size;i++){
temp=(temp*10+m[i]-'0')%(mod-1);
}
c=binarym(2,temp);
out(c);
}