矩阵快速幂
就是说a[1]=a,a[2]=b--------a[n]=2*a[n-2]+a[n-1]+n^4
注意这个a[n-1]=a[n-1]
这个第二行没有想到
代码:这个就是个板子,换一个矩阵和N就行
#include<iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long long LL; const int N = 7; const LL mod = 2147493647; LL n, m; struct mac { LL c[N][N]; void reset() { memset(c, 0, sizeof(c)); for (int i = 0; i <= 6; i++) c[i][i] = 1; } }; mac multi(mac a, mac b) { mac ans; for (int i = 0; i <= 6; i++) for (int j = 0; j <= 6; j++) { ans.c[i][j] = 0; for (int k = 0; k <= 6; k++) { ans.c[i][j] += (a.c[i][k] * b.c[k][j]) % mod; ans.c[i][j] %= mod; } } return ans; } mac Pow(mac a, LL b) { mac ans; ans.reset(); while (b) { if (b & 1) ans = multi(ans, a); a = multi(a, a); b >>= 1; } return ans; } int main() { int t; LL n,kk,ll; mac a={ 1,2,1,0,0,0,0, 1,0,0,0,0,0,0, 0,0,1,4,6,4,1, 0,0,0,1,3,3,1, 0,0,0,0,1,2,1, 0,0,0,0,0,1,1, 0,0,0,0,0,0,1 }; cin>>t; while(t--){ cin>>n>>kk>>ll; if(n==1){ printf("%lld\n",kk); } else if(n==2){ printf("%lld\n",ll); } else{ mac res=Pow(a,n-2); LL ans=(ll*res.c[0][0]%mod+kk*res.c[0][1]%mod+81*res.c[0][2]%mod+27*res.c[0][3]%mod+9*res.c[0][4]%mod+3*res.c[0][5]%mod+res.c[0][6]%mod)%mod; printf("%lld\n",ans%mod); } } return 0; }
分类:
算法-数据结构 / 矩阵快速幂
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理