链接:https://ac.nowcoder.com/acm/contest/330/E
来源:牛客网
题目描述
精通程序设计的 Applese 叕写了一个游戏。
在这个游戏中,有一个 n 行 m 列的方阵。现在它要为这个方阵涂上黑白两种颜色。规定左右相邻两格的颜色不能相同。请你帮它统计一下有多少种涂色的方法。由于答案很大,你需要将答案对 109+7109+7 取模。
输入描述:
仅一行两个正整数 n, m,表示方阵的大小。
输出描述:
输出一个正整数,表示方案数对 109+7109+7 取模。
示例1
输入
1 1
输出
2
示例2
输入
2 2
输出
4
备注:
1≤n,m≤10^100000
题解:每行只有两种情况,一共有
种,利用费马小定理,
#include<iostream>
#include<string.h>
#include<algorithm>
#define ll long long
using namespace std;
const ll inf=1e9+7;
char n[100011],m[100011];
int main()
{
scanf("%s%s",n,m);
ll p=0;
for(int i=0;n[i];i++)
p=(p*10+n[i]-'0')%(inf-1);
ll a=2,b=1;
while(p){
if(p&1)
b=a*b%inf;
p>>=1;
a=a*a%inf;
}
printf("%lld
",b%inf);
return 0;
}