AT_cpsco2019_s1_d 题解

· · 题解

先考虑第一天的情况:早餐只有两种选择,因为不能连续选同样的点心,所以午餐也只有两种选择,同理,晚餐也只有两种选择,那么第一天就有 2\times 2\times 2=8 种选择。

其中两种选法对第二天的早餐没有影响,一天中 \frac{1}{4} 的选择对第二天没有影响,第二天有 8 种选法,其余 \frac{3}{4} 的选择对第二天有影响,第二天有 4 种选法。

这样对于第 x 天,方案数 f_x 就等于 \frac{1}{4}\times f_{x-1}\times 8+\frac{3}{4}\times f_{x-1}\times 4,化简后就得到 5\times f_{x-1}。

求次幂类问题就可以使用快速幂优化时间。

#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int n;
int f(int x){
    int s=8,a=5;
    while(x){
        if(x&1)s=1ll*s*a%mod;
        a=1ll*a*a%mod;
        x>>=1;
    }
    return s;
}
signed main(){
    cin>>n;
    cout<<f(n-1);
    return 0;
}