牛客寒假算法基础训练集中营4 E题 Applese 涂颜色

链接: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

题解:每行只有两种情况,一共有{\color{Red} 2^{n}}种,利用费马小定理,{\color{Red} ans=2^{\, n\, mod\, \, 10^{9}+6}mod\, \, 10^{9}+7}

#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\n",b%inf);
        return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42060896/article/details/86705328
今日推荐