nyist 301 遞推求值(矩陣快速冪)

2021-07-11 21:14:33 字數 1099 閱讀 6881

思路:注意矩陣乘法不滿足交換律,所以要注意初始矩陣在右邊,要進行快速冪的矩陣在左邊

左邊的矩陣為    右邊的矩陣為

b a cf2 0 0

1 0 0f1 0 0

0 0 1 1 0 0

ac**:

#include #include #include #include #include #include #include #include #include #include #include const int inf = 0x3f3f3f3f;//1061109567

typedef long long ll;

const int maxn = 40000;

const int mod = 1000007;

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

using namespace std;

struct node

;node matrixmul(node a,node b)}}

return c;

}node quickmod(node s,int k)

return ans;

}int main()

if(n == 2)

node sa;

memset(sa.a,0,sizeof(sa.a));

sa.a[0][0]=e;

sa.a[0][1]=d;

sa.a[0][2]=f;

sa.a[1][0]=1;

sa.a[2][2]=1;

sa = quickmod(sa,n-2);

node sb;

memset(sb.a,0,sizeof(sb.a));

sb.a[0][0]=f2;

sb.a[1][0]=f1;

sb.a[2][0]=1;

sb = matrixmul(sa,sb);//注意順序

ll ans = (sb.a[0][0]+mod)%mod;

printf("%i64d\n",ans);

}return 0;

}

nyist 301 遞推求值

時間限制 1000 ms 記憶體限制 65535 kb 難度 4 描述給你乙個遞推公式 f x a f x 2 b f x 1 c 並給你f 1 f 2 的值,請求出f n 的值,由於f n 的值可能過大,求出f n 對1000007取模後的值。注意 1對3取模後等於2 輸入第一行是乙個整數t,表示...

nyoj 301 遞推求值(矩陣計算)

題目描述 給你乙個遞推公式 f x a f x 2 b f x 1 c 並給你f 1 f 2 的值,請求出f n 的值,由於f n 的值可能過大,求出f n 對1000007取模後的值。注意 1對3取模後等於2 解題思路 如果單純的用遞推來做的話鐵定超時,因為n可能很大。因此可以將遞推公式轉換成矩陣...

nyoj 301 遞推求值 矩陣冪

給你乙個遞推公式 f x a f x 2 b f x 1 c 並給你f 1 f 2 的值,請求出f n 的值,由於f n 的值可能過大,求出f n 對1000007取模後的值。注意 1對3取模後等於2 在濤神的協助下構造出來的矩陣 貌似發表了就格式亂了,空格用 表示 fn.b.a.1.fn 1 fn...