https://www.luogu.com.cn/problem/U420446

数学作业上的题。

但是还是比较佩服 Really 在老师讲课的时候直接说自己有更简单的方法,然后冲上讲台给大家讲dp。(当时是一名代课老师讲课,丛姐出差去了,要是丛姐在,lmy估计就没机会讲了吧)

这道题的话考虑到一个人的球只能由另两个人转移过来,就非常简单的dp,然后没了。矩阵优化可以做到 O(logn)

upd on 5.1: 还可以简单容斥,设 fn 为传球n轮后传到甲手中的方案数,可以得到 fn=2n1fn1,进而整理后可得通项 fn=2n2(1)n13。常数小一点的 $\log

code
#include<bits/stdc++.h>
#define int long long
int n;
const int mod=1e9+7;
struct Matrix{
	int a[4][4];
	Matrix(){memset(a,0,sizeof(a));}
	Matrix operator*(const Matrix &b)const{
		Matrix ans;
		for(int i=1;i<=3;++i)
			for(int j=1;j<=3;++j)
				for(int k=1;k<=3;++k)
					ans.a[i][j]=(ans.a[i][j]+a[i][k]*b.a[k][j])%mod;
		return ans;
	}
	Matrix operator^(int b)const{
        Matrix ans,c=*this;
        ans.a[1][1]=ans.a[2][2]=ans.a[3][3]=1;     
		while(b){
			if(b&1)ans=ans*c; 
            c=c*c;
            b>>=1;
		}
        return ans;
	}		
}st,tr;
void solve(){
	std::cin>>n;
	std::cout<<((tr^n)*st).a[1][1]<<'\n';
}
signed main(){
	std::cin.tie(nullptr)->sync_with_stdio(false);
	int T;std::cin>>T;
	st.a[1][1]=1;
	tr.a[1][2]=tr.a[1][3]=tr.a[2][1]=tr.a[2][3]=tr.a[3][1]=tr.a[3][2]=1;
	while(T--)solve();
	return 0;
}
posted @   Flandreqwq  阅读(78)  评论(6编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示