跳到正文

题解

AT_cpsco2019_s1_d 题解

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

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

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

这样对于第 xx 天,方案数 fxf_x 就等于 14×fx−1×8+34×fx−1×4\frac{1}{4}\times f_{x-1}\times 8+\frac{3}{4}\times f_{x-1}\times 4,化简后就得到 5×fx−15\times f_{x-1}。

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

C++
#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;
}
原文