Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
傳送門
題目
fn=c2?n?6fn?1fn?2fn?3\begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} ?fn?=c2?n?6fn?1?fn?2?fn?3???
思路
我們通過迭代發現fnf_nfn?其實就是由ct1,f1t2,f2t3,f3t4c^{t_1},f_1^{t_2},f_2^{t_3},f_3^{t_4}ct1?,f1t2??,f2t3??,f3t4??相乘得到,因此我們可以分別用矩陣快速冪求出t1,t2,t3,t4t_1,t_2,t_3,t_4t1?,t2?,t3?,t4?,最后用快速冪求得答案。
對于n<=3n<=3n<=3的我們直接輸出即可,n>3n>3n>3的我們先將nnn減去333,然后進行求解。
對f1,f2,f3f_1,f_2,f_3f1?,f2?,f3?的指數,我們可以推出xn=xn?1+xn?2+xn?3x_n=x_{n-1}+x_{n-2}+x_{n-3}xn?=xn?1?+xn?2?+xn?3?:
(xnxn?1xn?2)=(xn?1xn?2xn?3)[110101100]\begin{aligned} (x_n&&x_{n-1}&&x_{n-2})=(x_{n-1}&&x_{n-2}&&x_{n-3}) \left[ \begin{matrix} 1 & 1 & 0\\ 1 & 0 & 1\\ 1 & 0 & 0\\ \end{matrix} \right] \end{aligned} (xn???xn?1???xn?2?)=(xn?1???xn?2???xn?3?)???111?100?010?????
對ccc的指數,我們可以推出xn=xn?1+xn?2+xn?3+2n=xn?1+xn?2+xn?3+2(n?1)+2x_n=x_{n-1}+x_{n-2}+x_{n-3}+2n=x_{n-1}+x_{n-2}+x_{n-3}+2(n-1)+2xn?=xn?1?+xn?2?+xn?3?+2n=xn?1?+xn?2?+xn?3?+2(n?1)+2:
(xnxn?1xn?2n1)=(xn?1xn?2xn?3n?11)[1100010100100002001020011]\begin{aligned} (x_n&&x_{n-1}&&x_{n-2}&&n&&1)=(x_{n-1}&&x_{n-2}&&x_{n-3}&&n-1&&1) \left[ \begin{matrix} 1 & 1 & 0 & 0 & 0\\ 1 & 0 & 1 & 0 & 0\\ 1 & 0 & 0 & 0 & 0\\ 2 & 0 & 0 & 1 & 0\\ 2 & 0 & 0 & 1 & 1\\ \end{matrix} \right] \end{aligned} (xn???xn?1???xn?2???n??1)=(xn?1???xn?2???xn?3???n?1??1)???????11122?10000?01000?00011?00001?????????
注意,由于我們處理出來的x1,x2,x3,x4x_1,x_2,x_3,x_4x1?,x2?,x3?,x4?都是指數部分,這里如果膜1e9+71e9+71e9+7的話是不對的,我們還需要對其進行歐拉降冪。
代碼實現
#include <set> #include <map> #include <deque> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <bitset> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std;typedef long long LL; typedef pair<LL, LL> pLL; typedef pair<LL, int> pLi; typedef pair<int, LL> pil;; typedef pair<int, int> pii; typedef unsigned long long uLL;#define lson rt<<1 #define rson rt<<1|1 #define lowbit(x) x&(-x) #define name2str(name) (#name) #define bug printf("*********\n") #define debug(x) cout<<#x"=["<<x<<"]" <<endl #define FIN freopen("D://code//in.txt","r",stdin) #define IO ios::sync_with_stdio(false),cin.tie(0)const double eps = 1e-8; const int mod = 1000000007; const int maxn = 2e5 + 7; const double pi = acos(-1); const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3fLL;int f[10], a[10][10];void mulself(int a[10][10]) {int c[10][10];memset(c, 0, sizeof(c));for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {for(int k = 0; k < 3; k++) {c[i][j] = (c[i][j] + (long long)a[i][k] * a[k][j] % (mod - 1)) % (mod - 1);}}}memcpy(a, c, sizeof(c)); }void mul(int f[10], int a[10][10]) {int c[10];memset(c, 0, sizeof(c));for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {c[i] = (c[i] + (long long)f[j] * a[j][i] % (mod - 1)) % (mod - 1);}}memcpy(f, c, sizeof(c)); }void mulself1(int a[10][10]) {int c[10][10];memset(c, 0, sizeof(c));for(int i = 0; i < 5; i++) {for(int j = 0; j < 5; j++) {for(int k = 0; k < 5; k++) {c[i][j] = (c[i][j] + (long long)a[i][k] * a[k][j] % (mod - 1)) % (mod - 1);}}}memcpy(a, c, sizeof(c)); }void mul1(int f[10], int a[10][10]) {int c[10];memset(c, 0, sizeof(c));for(int i = 0; i < 5; i++) {for(int j = 0; j < 5; j++) {c[i] = (c[i] + (long long)f[j] * a[j][i] % (mod - 1)) % (mod - 1);}}memcpy(f, c, sizeof(c)); }int qpow(int x, int n) {int res = 1;while(n) {if(n & 1) res = 1LL * res * x % mod;x = 1LL * x * x % mod;n >>= 1;}return res; }LL n; int f1, f2, f3, c;int main(){scanf("%lld%d%d%d%d", &n, &f1, &f2, &f3, &c);if(n == 1) return printf("%d\n", f1) * 0;if(n == 2) return printf("%d\n", f2) * 0;if(n == 3) return printf("%d\n", f3) * 0;n -= 3;LL ans = 1;f[0] = 1, f[1] = 0, f[2] = 0;a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;a[1][0] = 1, a[1][1] = 0, a[1][2] = 1;a[2][0] = 1, a[2][1] = 0, a[2][2] = 0;LL x = n;while(x) {if(x & 1) mul(f, a);mulself(a);x >>= 1;}ans = ans * qpow(f3, f[0]) % mod;f[0] = 0, f[1] = 1, f[2] = 0;a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;a[1][0] = 1, a[1][1] = 0, a[1][2] = 1;a[2][0] = 1, a[2][1] = 0, a[2][2] = 0;x = n;while(x) {if(x & 1) mul(f, a);mulself(a);x >>= 1;}ans = ans * qpow(f2, f[0]) % mod;f[0] = 0, f[1] = 0, f[2] = 1;a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;a[1][0] = 1, a[1][1] = 0, a[1][2] = 1;a[2][0] = 1, a[2][1] = 0, a[2][2] = 0;x = n;while(x) {if(x & 1) mul(f, a);mulself(a);x >>= 1;}ans = ans * qpow(f1, f[0]) % mod;if(n == 1) f[0] = 2;if(n == 2) f[0] = 6;if(n == 3) f[0] = 14;if(n > 3) {n -= 3;f[0] = 14, f[1] = 6, f[2] = 2, f[3] = 3, f[4] = 1;memset(a, 0, sizeof(a));a[0][0] = a[0][1] = 1;a[1][0] = a[1][2] = 1;a[2][0] = 1;a[3][0] = 2, a[3][3] = 1;a[4][0] = 2, a[4][3] = a[4][4] = 1;while(n) {if(n & 1) mul1(f, a);mulself1(a);n >>= 1;}}ans = ans * qpow(c, f[0]) % mod;printf("%lld\n", ans);return 0; }總結
以上是生活随笔為你收集整理的Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用python画数学函数图像教程_你知道
- 下一篇: Celery理解