[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;
}
原文地址:https://www.cnblogs.com/shandongs1/p/8150240.html