首页 > 其他分享 >洛谷-2044

洛谷-2044

时间:2022-11-14 14:35:02浏览次数:72  
标签:2044 end mat int begin pmatrix 洛谷 mod

洛谷-2044

思路

首先对与递推式\(x_n = (a * x_{n - 1} + c)\) % \(mod\),发现\(c\)的存在使得不能直接使用整数快速幂求出\(x_n\),因为\(x_n\)是关于\(a, c, x0\)的\(n\)次多项式。并且\(n\)是\(1e18\)大小的。

于是考虑使用矩阵快速幂加速,但是显然不能使用一维矩阵(没意义),考虑通过二维矩阵
\(\begin{pmatrix} x_n & c \end{pmatrix}\)
来得到关系式。(\(x_{n-1}\)和\(c\)均与\(x_n\)有关)

首先有

\[\begin{pmatrix} x_n & c \end{pmatrix} = \begin{pmatrix} x_{n - 1} & c \end{pmatrix} * \begin{pmatrix} A & B \\ C & D \end{pmatrix} \tag{1} \]

将\((1)\)展开,有

\[\begin{cases} x_n = x_{n - 1} * A + c * C \\ c = x_{n - 1} * B + c * D \end{cases} \tag{2} \]

由于\(x_n = a * x_{n - 1} + c\),替换\((2)\)中第一个方程的\(x_n\),得到

\[\begin{cases} a * x_{n - 1} + c = x_{n - 1} * A + c * C \\ c = x_{n - 1} * B + c * D \end{cases} \tag{3} \]

由\((3)\)得到

\[\begin{pmatrix} A = a & B = 0 \\ C = 1 & D = 1 \end{pmatrix} \]

再由于

\[\begin{pmatrix} x_n & c \end{pmatrix} = \begin{pmatrix} x_{n - 1} & c \end{pmatrix} * \begin{pmatrix} a & 0 \\ 1 & 1 \end{pmatrix} = \begin{pmatrix} x_{0} & c \end{pmatrix} * \begin{pmatrix} a & 0 \\ 1 & 1 \end{pmatrix} ^{n} \]

可知,只需要求出
\( \begin{pmatrix} a & 0 \\ 1 & 1 \end{pmatrix} ^{n} \)
即可得到答案。

Code

#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}

using ll = long long;
#define int long long
int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 2, M = 5010;
const int inf = 0x3f3f3f3f;

struct mat {
    int m[N][N];
};

ll n, c, x0,  g;

int mul(int a,int b) {
    int res = 0;
    a %= mod;
    b %= mod;
    while(b) {
        if (b & 1) {
            res = (res + a) % mod;
        }
        a = (a + a) % mod;
        b >>= 1;
    }
    return res;
}

mat operator * (const mat&a, const mat&b) {
    mat c;
    memset(c.m, 0, sizeof c.m);
    for (int i = 0;i < N;i++) {
        for (int j = 0;j < N;j++) {
            for (int k = 0;k < N;k++) {
                c.m[i][j] = (c.m[i][j] + mul(a.m[i][k] , b.m[k][j])) % mod;
            }
        }
    }
    return c;
}

mat qpow(mat a, int b ) {
    mat c;
    memset(c.m, 0,sizeof c.m);
    for (int i = 0;i < N;i++) c.m[i][i] = 1;
    while (b) {
        if (b & 1) {
            c = c * a;
        }
        a = a * a;
        b >>= 1;
    }
    return c;
}

inline void _A_A_() {
    #ifdef LOCAL
    freopen("in.in", "r", stdin);
    #endif
    _u_u_;
    mat s;
    memset(s.m, 0, sizeof s.m);
    s.m[1][0] = s.m[1][1] = 1;
    cin >> mod >> s.m[0][0] >> c >> x0 >> n >> g;
    s = qpow(s, n);
    int xn = (mul(s.m[0][0] , x0) + mul(c , s.m[1][0])) % mod;// 注意xn可能会大于mod,要取模。
    cout << (xn) % g << "\n";
}

标签:2044,end,mat,int,begin,pmatrix,洛谷,mod
From: https://www.cnblogs.com/FanWQ/p/16888946.html

相关文章

  • 洛谷 P6142
    先对\(k\)进行二分,将最值问题转化成判定问题。判定一个\(k\)是否合法时,贪心去考虑,一个节点下面的若干条链在合并时,一条链肯定和另一条使它合并后恰好满足长度限制的链......
  • 洛谷-1939
    洛谷-1939思路原来我是打算按照斐波那契数列的方法,找到一个二维矩阵,因为\(a_x=a_{x-1}+a_{x-3}\),只有两个元素。但是错了,得不到正确答案。再看题解,全都是三维矩阵,......
  • 洛谷 P6147
    和这题有点类似。首先不难发现,如果当前check的值不是\(n-1\)的约数,一定无解。然后进行一遍DFS,每次用一个multiset保存子树传来的残链长度,然后贪心的配对。最后如......
  • 洛谷 P3523
    感觉和COCI2021-2022#4的T4一模一样。显然考虑二分,设当前二分出的值是\(lim\)。那么就是称一个点能覆盖另一个点当且仅当它被选中且它与那个点的距离不大于\(lim......
  • 洛谷 P1002 [NOIP2002 普及组] 过河卒
    第一个dp(动态规划)题纪念一下先尝试暴力写一个递归,由于x与y只能增加,不存在回路。#include<iostream>usingnamespacestd;inta_x,a_y,h_x,h_y,sum=0;//a_x,a_y代表目标地......
  • 洛谷P8849 『JROI-7』hibernal 二分法题解
    题目链接题目大意:交互题,给定N=2或18或64或512或1000,其中你要通过19次以内的询问在数列1-N中找到给定的未知的两个数x和y(本题解中设x<y),每次询问......
  • 洛谷-1052
    洛谷-1052思路文字版视频版Code#include<bits/stdc++.h>usingnamespacestd;#define_u_u_ios::sync_with_stdio(false),cin.tie(nullptr)#definecfint_o_o......
  • 洛谷P5309 Ynoi 2011 初始化 题解
    题面。我也想过根号分治,但是题目刷得少,数组不敢开,所以还是看题解做的。这道题目要用到根号分治的思想,可以看看这道题目和我的题解。题目要求处理一个数组a,支持如下操作......
  • 洛谷 P4135 作诗 题解
    题面。之前做过一道很类似的题目洛谷P4168蒲公英,然后看到这题很快就想到了解法,做完这题可以对比一下,真的很像。题目要求区间内出现次数为正偶数的数字的数量。数据范......
  • 洛谷刷题_P217 [USACO1.5]回文质数 Prime Palindromes
    题目P217[USACO1.5]回文质数PrimePalindromes题目链接https://www.luogu.com.cn/problem/P1217知识点埃氏筛原理:要得到自然数n以内的全部素数,必须把不大于根号n......