zoukankan      html  css  js  c++  java
  • HDU 2256Problem of Precision(矩阵快速幂)

    题意

    求$(sqrt{2} + sqrt{3})^{2n} pmod {1024}$

    $n leqslant 10^9$

    Sol

    看到题解的第一感受:这玩意儿也能矩阵快速幂???

    是的,它能qwq。。。。

    首先我们把$2$的幂乘进去,变成了

    $(5 + 2sqrt{6})^n$

    设$f(n) = A_n + sqrt{6} B_n$

    那么$f(n+1) = (A_n + sqrt{6} B_n ) * (5 + 2sqrt{6})$

    乘出来得到

    $A_{n + 1} = 5 A_n + 12 B_n$

    $B_{n + 1} = 2A_n + B B_n$

    那么不难得到转移矩阵

    $$egin{pmatrix} 5 & 12 \ 2 & 5 end{pmatrix}$$

    这样好像就能做了。。

    但是实际上后我们最终会得到一个类似于$A_n + sqrt{6}B_n$的东西,这玩意儿还是不能取模

    考虑如何把$sqrt{6}$的影响消掉。

    $(5 + 2 sqrt{6})^n = A_n + sqrt{6}B_n$

    $(5 - 2 sqrt{6})^n = A_n - sqrt{6}B_n$

    相加得

    $(5 + 2 sqrt{6})^n + (5 - 2 sqrt{6})^n = 2A_n$

    考虑到$0 < (5 - 2sqrt{6})^n < 1$

    那么

    $$lfloor (5 + 2sqrt{6})^n floor = 2A_n - 1$$

    做完啦qwq

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define Pair pair<int, int> 
    #define MP(x, y)
    #define fi first
    #define se second 
    // #include<map>
    using namespace std;
    #define LL long long
    const LL MAXN = 101, mod = 1024;
    inline LL read() {
        char c = getchar(); LL x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int T, N;
    struct Matrix {
        LL m[5][5], N;
        Matrix() {N = 2; memset(m, 0, sizeof(m));}
        Matrix operator * (const Matrix &rhs) const {
            Matrix ans;
            for(int k = 1; k <= N; k++) 
                for(int i = 1; i <= N; i++)
                    for(int j = 1; j <= N; j++)
                        (ans.m[i][j] += 1ll * m[i][k] * rhs.m[k][j] % mod) % mod;
            return ans;
        }
    };
    
    Matrix fp(Matrix a, int p) {
        Matrix base; base.m[1][1] = 1; base.m[2][2] = 1;
        while(p) {
            if(p & 1) base = base * a; 
            a = a * a; p >>= 1;
        }
        return base;
    }
    int main() {
        T = read();
        while(T--) {
            N = read();
            Matrix a; 
            a.m[1][1] = 5; a.m[1][2] = 12;
            a.m[2][1] = 2; a.m[2][2] = 5;
            a = fp(a, N - 1);
            LL ans = (5 * a.m[1][1] + 2 * a.m[1][2]) % mod;
            printf("%I64d
    ", (2 * ans - 1) % mod);
        }
        return 0;
    }
    
    /**/
  • 相关阅读:
    欧拉工程第53题:Combinatoric selections
    540A: Combination Lock
    540C: Ice Cave
    540B :School Marks
    欧拉工程第52题:Permuted multiples
    欧拉工程第51题:Prime digit replacements
    C. Tourist's Notes
    B. Quasi Binary
    [LeetCode] Longest Substring Without Repeating Characters
    [hihoCoder] 后序遍历
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9633958.html
Copyright © 2011-2022 走看看