HDU6198 number number number

矩阵快速幂

能够找到规律。。答案是第2 * k + 3项减1,直接矩阵加速就行了html

#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\n", ((ori.m[0][1] - 1) % MOD + MOD) % MOD);
    }
    return 0;
}

转载于:https://www.cnblogs.com/onionQAQ/p/11201630.htmlios