日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P1939 【模板】矩阵加速(数列)

發(fā)布時間:2025/4/16 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷P1939 【模板】矩阵加速(数列) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述

a[1]=a[2]=a[3]=1

a[x]=a[x-3]+a[x-1] (x>3)

求a數(shù)列的第n項對1000000007(10^9+7)取余的值。

輸入格式

第一行一個整數(shù)T,表示詢問個數(shù)。

以下T行,每行一個正整數(shù)n。

輸出格式

每行輸出一個非負整數(shù)表示答案。

輸入輸出樣例

輸入 #1

3
6
8
10

輸出 #1

4
9
19

說明/提示

對于30%的數(shù)據(jù) n<=100;

對于60%的數(shù)據(jù) n<=2*10^7;

對于100%的數(shù)據(jù) T<=100,n<=2*10^9;

解析:

\(\displaystyle \begin{array}{{>{\displaystyle}l}} \ 現(xiàn)在我需要求的矩陣是:\\ \begin{bmatrix} f[ i]\\ f[ i-1]\\ f[ i-2] \end{bmatrix}\\ 根據(jù)題目中給出的條件:f[ x] =f[ x-1] +f[ x-3]\\ 而我們下一步要求出f[ i+1]\\ 所以f[ i+1] =f[ i] +f[ i-2]\\ 所以求初始矩陣為\\ \begin{bmatrix} 1 & 0 & 1\\ 1 & 0 & 0\\ 0 & 1 & 0 \end{bmatrix}\\ 對初始矩陣進行矩陣快速冪然后輸出a[ 1][ 1] \end{array}\)

#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #include <queue> #include <stack> #define re register #define Max 200000012 #define int long long int n; const int mod=1000000007; struct Mat {int a[4][4];Mat() {memset(a,0,sizeof a);}inline void build() {memset(a,0,sizeof a);for(re int i = 1 ; i <= 3 ; ++ i) a[i][i]=1;} }; Mat operator*(Mat &a,Mat &b) {Mat c;for(re int k = 1 ; k <= 3 ; ++ k)for(re int i = 1 ; i <= 3 ; ++ i)for(re int j = 1 ; j <= 3 ; ++ j)c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;return c; } Mat ans,a,A; void quick_Mat(int x) {ans.build();while(x) {if(x & 1 == 1) ans=ans*a;a=a*a;x>>=1;} } signed main() {scanf("%lld",&n);int x;A.a[1][1]=1;A.a[2][1]=1;A.a[3][1]=1;for(re int i = 1 ; i <= n ; ++ i) {scanf("%lld",&x);if(x<=3 && x>=1) {printf("1\n");continue;}a.a[1][1]=1;a.a[1][2]=0;a.a[1][3]=1;a.a[2][1]=1;a.a[2][2]=0;a.a[2][3]=0;a.a[3][1]=0;a.a[3][2]=1;a.a[3][3]=0;quick_Mat(x-3);ans = ans * A;printf("%lld\n",ans.a[1][1]);}return 0; }

轉載于:https://www.cnblogs.com/handsomegodzilla/p/11299441.html

總結

以上是生活随笔為你收集整理的洛谷P1939 【模板】矩阵加速(数列)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。