• HDU6198 number number number


    矩阵快速幂

    可以找到规律。。答案是第2 * k + 3项减1,直接矩阵加速就好了

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int ret = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
        return w ? -ret : ret;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template <typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template <typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template <typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int MOD = 998244353;
    int n;
    struct Matrix{
        ll m[2][2];
        Matrix(){
            full(m, 0);
        }
        Matrix operator * (Matrix &b){
            Matrix ret;
            for(int i = 0; i < 2; i ++){
                for(int j = 0; j < 2; j ++){
                    for(int k = 0; k < 2; k ++){
                        ret.m[i][j] += (m[i][k] * b.m[k][j]) % MOD;
                        ret.m[i][j] %= MOD;
                    }
                }
            }
            return ret;
        }
    };
    
    Matrix e, ori, t;
    
    Matrix fpow(Matrix &a, int p){
        Matrix res = e;
        for(; p; p >>= 1, a = a * a) if(p & 1) res = a * res;
        return res;
    }
    
    void init(){
        full(e.m, 0), full(ori.m, 0), full(t.m, 0);
        e.m[0][0] = 1, e.m[1][1] = 1;
        ori.m[0][1] = 1, ori.m[1][1] = 0;
        t.m[0][0] = t.m[0][1] = t.m[1][0] = 1;
    }
    
    int main(){
    
        while(~scanf("%d", &n)){
            init();
            t = fpow(t, 2 * n + 2);
            ori = t * ori;
            printf("%lld
    ", ((ori.m[0][1] - 1) % MOD + MOD) % MOD);
        }
        return 0;
    }
    
  • 相关阅读:
    Linux上查找最大文件的 3 种方法
    不念过去,不畏将来
    Weblogic/WAS之Full GC监控与计算
    EntityFramework:An error occurred while executing the command definition. See the inner exception for details.
    1051 复数乘法(C#)
    BACnet开发资料与调试工具
    JS设置cookie、读取cookie、删除cookie
    认识BACnet协议
    Unity WebGL请求Http接口出现的Cors跨域问题
    VS 2017 产品密钥
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/11201630.html
Copyright © 2020-2023  润新知