zoukankan      html  css  js  c++  java
  • [luogu] zpl的数学题1

    https://www.luogu.org/problemnew/show/U16887

    $f[1] + f[2] + f[3] + .... + f[n] = f[n + 2] - 1$

    矩阵快速幂求出第n+2项即可

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define gc getchar()
    #define LL long long
    
    const LL mod = 100000007;
    
    LL n, s;
    
    inline LL read(){
        LL x = 0; char c = gc;
        while(c < '0' || c > '9') c = gc;
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
        return x;
    }
    
    struct Node{
        LL m[3][3];
        Node() { memset(m,0,sizeof m);}
        void clear(){for(int i = 1; i <= n; i ++) m[i][i] = 1;}
    };
    
    Node operator *(Node a, Node b){
        Node ret;
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                for(int k = 1; k <= n; k ++)
                    ret.m[i][j] = (ret.m[i][j] + a.m[i][k] * b.m[k][j]) % mod;
        return ret;
    }
    
    Node ksm(Node a, LL p){
        Node ret; ret.clear();    
        while(p){
            if(p & 1) ret = ret * a;
            a = a * a;
            p >>= 1;
        }
        return ret;
    }
    
    int main()
    {
         s = read();
         s += 2;
        n = 2;
        Node a, b;
        a.m[1][1] = 0; a.m[1][2] = 1; a.m[2][1] = 1; a.m[2][2] = 1;
        b.m[1][1] = 1; b.m[2][1] = 1;
        Node Ans;
        Ans = ksm(a, s - 1);
        Node answer = Ans * b;
        cout << answer.m[1][1] - 1;
    
        return 0;
    }
  • 相关阅读:
    重载
    两数之和
    求二叉树层序遍历
    最小的K的个数
    二分查找
    实现二叉树先序,中序,后序
    判断 链表中是否有环
    设计LRU缓存结构
    排序
    反转链条
  • 原文地址:https://www.cnblogs.com/shandongs1/p/8150240.html
Copyright © 2011-2022 走看看