Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

本博客主要介绍如何利用矩阵快速幂解决递推关系fn=c2*n-6*fn-1*fn-2*fn-3的问题。在理解递推规律后,通过计算t1, t2, t3, t4的矩阵形式,再结合快速幂算法求解。对于n<=3的情况直接输出,否则先将n减3再进行计算。同时,注意对指数部分进行欧拉降幂处理,以确保正确性。" 125872607,13220608,数据库单表行数限制:从2千万到1亿的真相,"['数据库', 'mysql', '索引']
摘要由CSDN通过智能技术生成

传送门

题目

f n = c 2 ∗ n − 6 f n − 1 f n − 2 f n − 3 \begin{aligned} &amp;f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&amp;\\ \end{aligned} fn=c2n6fn1fn2fn3

思路

我们通过迭代发现 f n f_n fn其实就是由 c t 1 , f 1 t 2 , f 2 t 3 , f 3 t 4 c^{t_1},f_1^{t_2},f_2^{t_3},f_3^{t_4} ct1,f1t2,f2t3,f3t4相乘得到,因此我们可以分别用矩阵快速幂求出 t 1 , t 2 , t 3 , t 4 t_1,t_2,t_3,t_4 t1,t2,t3,t4,最后用快速幂求得答案。
对于 n &lt; = 3 n&lt;=3 n<=3的我们直接输出即可, n &gt; 3 n&gt;3 n>3的我们先将 n n n减去 3 3 3,然后进行求解。
f 1 , f 2 , f 3 f_1,f_2,f_3 f1,f2,f3的指数,我们可以推出 x n = x n − 1 + x n − 2 + x n − 3 x_n=x_{n-1}+x_{n-2}+x_{n-3} xn=xn1+xn2+xn3
( x n x n − 1 x n − 2 ) = ( x n − 1 x n − 2 x n − 3 ) [ 1 1 0 1 0 1 1 0 0 ] \begin{aligned} (x_n&amp;&amp;x_{n-1}&amp;&amp;x_{n-2})=(x_{n-1}&amp;&amp;x_{n-2}&amp;&amp;x_{n-3}) \left[ \begin{matrix} 1 &amp; 1 &amp; 0\\ 1 &amp; 0 &amp; 1\\ 1 &amp; 0 &amp; 0\\ \end{matrix} \right] \end{aligned} (xnxn1xn2)=(xn1xn2xn3)111100010
c c c的指数,我们可以推出 x n = x n − 1 + x n − 2 + x n − 3 + 2 n = x n − 1 + x n − 2 + x n − 3 + 2 ( n − 1 ) + 2 x_n=x_{n-1}+x_{n-2}+x_{n-3}+2n=x_{n-1}+x_{n-2}+x_{n-3}+2(n-1)+2 xn=xn1+xn2+xn3+2n=xn1+xn2+xn3+2(n1)+2:
( x n x n − 1 x n − 2 n 1 ) = ( x n − 1 x n − 2 x n − 3 n − 1 1 ) [ 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 ] \begin{aligned} (x_n&amp;&amp;x_{n-1}&amp;&amp;x_{n-2}&amp;&amp;n&amp;&amp;1)=(x_{n-1}&amp;&amp;x_{n-2}&amp;&amp;x_{n-3}&amp;&amp;n-1&amp;&amp;1) \left[ \begin{matrix} 1 &amp; 1 &amp; 0 &amp; 0 &amp; 0\\ 1 &amp; 0 &amp; 1 &amp; 0 &amp; 0\\ 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0\\ 2 &amp; 0 &amp; 0 &amp; 1 &amp; 0\\ 2 &amp; 0 &amp; 0 &amp; 1 &amp; 1\\ \end{matrix} \right] \end{aligned} (xnxn1xn2n1)=(xn1xn2xn3n11)1112210000010000001100001
注意,由于我们处理出来的 x 1 , x 2 , x 3 , x 4 x_1,x_2,x_3,x_4 x1,x2,x3,x4都是指数部分,这里如果膜 1 e 9 + 7 1e9+7 1e9+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;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
To solve this recurrence relation, we can use the Master Theorem. The recurrence relation can be written in the form: T(n) = aT(n/b) + f(n) where a = 2, b = 2, and f(n) = f(n). We can use the following steps to apply the Master Theorem: 1. Calculate the value of logb(a): log2(2) = 1 2. Compare f(n) with nlogb(a): If f(n) = O(nlogb(a)-ε) for some ε > 0, then T(n) = Θ(nlogb(a)). If f(n) = Θ(nlogb(a)), then T(n) = Θ(nlogb(a) log n). If f(n) = Ω(nlogb(a)+ε) for some ε > 0, and if af(n/b) ≤ cf(n) for some constant c < 1 and sufficiently large n, then T(n) = Θ(f(n)). In this case, we have f(n) = f(n), which is not comparable to nlogb(a) = n. Therefore, we cannot use the Master Theorem to determine the asymptotic growth rate of T(n). However, we can still solve the recurrence relation by using the substitution method. Let T(n) = 2T(n/2) + f(n) Assume that T(n) = O(nlog n). Then, we have: T(n) ≤ 2T(n/2) + f(n) ≤ 2(c(n/2)log(n/2)) + f(n) = cnlogn - cn + f(n) Since f(n) is a non-negative function, we can further simplify: T(n) ≤ cnlogn + f(n) Therefore, T(n) = O(nlogn) is a valid upper bound. Now, assume that T(n) = Ω(nlogn). Then, we have: T(n) ≥ 2T(n/2) + f(n) ≥ 2(c(n/2)log(n/2)) + f(n) = cnlogn - cn + f(n) Since f(n) is a non-negative function, we can further simplify: T(n) ≥ cnlogn + f(n) Therefore, T(n) = Ω(nlogn) is a valid lower bound. Combining the upper and lower bounds, we get: T(n) = Θ(nlogn) Therefore, the solution to the recurrence relation is T(n) = Θ(nlogn).
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值