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

备注:

1n,m10100000

欧拉降幂公式:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<math.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const ll p=1e9+7;
const ll x=p-1;
 
char a[100086],b[100086];
int c[100086];
 
ll power(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b%2)
            ans=ans*a%p;
        a=a*a%p;
        b=b/2;
    }
    return ans%p;
}
int main()
{
    scanf("%s%s",a,b);
    int len=strlen(a);
    for(int i=0;i<len;i++)
        c[i]=a[i]-'0';
    ll res=0;
    for(int i=0;i<len;i++)
    {
        res=(res*10+c[i])%x;
    }
    printf("%lld",power(2,res+x));
    return 0;
}
//欧拉降幂公式:在%m时,指数k = k%oula(m)+oula(m)

原文地址:https://www.cnblogs.com/shoulinniao/p/10352426.html