• [SDOI2008]递归数列


    嘟嘟嘟


    裸的矩阵快速幂,构造一个((k + 1) * (k + 1))的矩阵,把sum[n]也放到矩阵里面就行了。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 18;
    inline ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), last = ' ';
    	while(!isdigit(ch)) last = ch, ch = getchar();
    	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    	if(last == '-') ans = -ans;
    	return ans;
    }
    inline void write(ll x)
    {
    	if(x < 0) x = -x, putchar('-');
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    
    ll l, r, mod, sum[maxn];
    int K, Max;
    int b[maxn], c[maxn];
    struct Mat
    {
        ll a[maxn][maxn];
        In Mat operator * (const Mat& oth)const
        {
            static Mat ret; Mem(ret.a, 0);
            for(int i = 0; i <= Max; ++i)
                for(int j = 0; j <= Max; ++j)
                    for(int k = 0; k <= Max; ++k) ret.a[i][j] += a[i][k] * oth.a[k][j], ret.a[i][j] %= mod;
            return ret;
        }
    }f;
    
    In void init()
    {
    	for(int i = 1; i <= K; ++i) sum[i] = (sum[i - 1] + b[i]) % mod;
    	Max = K; Mem(f.a, 0); f.a[0][0] = 1;
    	for(int i = 1; i <= Max; ++i) f.a[0][i] = f.a[1][i] = c[i];
    	for(int i = 2; i <= K; ++i) f.a[i][i - 1] = 1;
    }
    
    In Mat quickpow(Mat A, ll b)
    {
        Mat ret; Mem(ret.a, 0);
        for(int i = 0; i <= Max; ++i) ret.a[i][i] = 1;
        for(; b; b >>= 1, A = A * A)
            if(b & 1) ret = ret * A;
        return ret;
    }
    
    In ll solve(ll n)
    {
    	if(n <= K) return sum[n];
    	n -= K;
    	Mat A = quickpow(f, n);
    	ll ret = sum[K];
    	for(int i = 1; i <= K; ++i) ret = (ret + A.a[0][i] * b[K - i + 1] % mod) % mod;
    	return ret;
    }
    
    int main()
    {
    	K = read();
    	for(int i = 1; i <= K; ++i) b[i] = read();
    	for(int i = 1; i <= K; ++i) c[i] = read();
    	l = read(), r = read(), mod = read();
    	init();
    	write((solve(r) - solve(l - 1) + mod) % mod), enter;
    	return 0;
    }
    
  • 相关阅读:
    「SDOI2014」向量集 解题报告
    HNOI 2014 世界树
    LOJ #2205. 「HNOI2014」画框 解题报告
    KM模板
    洛谷 [HNOI2014]道路堵塞 解题报告
    [AHOI2014/JSOI2014] 解题报告
    AndroidStudio中添加依赖的三种方式以及如何引入so文件和arr文件
    android 四大组件详解
    Android—— ListView 的简单用法及定制ListView界面
    Android Studio 使用VCS版本控制
  • 原文地址:https://www.cnblogs.com/mrclr/p/10453575.html
Copyright © 2020-2023  润新知