• P2461 [SDOI2008]递归数列 矩阵乘法+构造


    还好$QwQ$


    思路:矩阵快速幂

    提交:1次

    题解:

    如图:

    注意$n,m$如果小于$k$就不要快速幂了,直接算就行、。。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #define ull unsigned long long
    #define ll long long
    #define R register ll
    using namespace std;
    #define pause (for(R i=1;i<=10000000000;++i))
    #define In freopen("NOIPAK++.in","r",stdin)
    #define Out freopen("out.out","w",stdout)
    namespace Fread {
    static char B[1<<15],*S=B,*D=B;
    #ifndef JACK
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    #endif
    inline ll g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);}
    inline void gs(char* s) {
        register char ch; while(isempty(ch=getchar()));
        do *s++=ch; while(!isempty(ch=getchar()));
    }
    } using Fread::g; using Fread::gs;
    namespace Luitaryi {
    const int N=20;
    int k,mod,b[N],c[N];
    ll n,m,sumn,summ,sum[N];
    ll ans[N],s[N],a[N][N],mem[N][N];
    inline void mul(ll a[][N],ll b[][N]) {
        R tmp[N][N]; memset(tmp,0,sizeof(tmp));
        for(R i=1;i<=k+1;++i) for(R l=1;l<=k+1;++l) for(R j=1;j<=k+1;++j)
            tmp[i][j]=(tmp[i][j]+a[i][l]*b[l][j])%mod;
        memcpy(a,tmp,sizeof(tmp));
    }
    inline void qpow(ll p) {
        R ret[N][N]; memset(ret,0,sizeof(ret));
        for(R i=1;i<=k+1;++i) ret[i][i]=1;
        for(;p;p>>=1,mul(a,a)) if(p&1) mul(ret,a);
        memcpy(a,ret,sizeof(a));
    }
    inline void main() {
        k=g(); for(R i=1;i<=k;i++) b[i]=g();
        for(R i=1;i<=k;++i) c[i]=g();
        m=g()-k-1,n=g()-k,mod=g();
        const int M=mod;
        for(R i=1;i<=k;++i) sum[i]=(b[i]+sum[i-1])%M;
        for(R i=1;i<=k;++i) s[i]=b[i]%M; s[k+1]=sum[k]%M;
        for(R i=1;i<k;++i) a[i+1][i]=1;
        for(R i=1;i<=k;++i) a[i][k]=a[i][k+1]=c[k-i+1]%M; a[k+1][k+1]=1;
        if(n<=0) return (void)printf("%lld
    ",((sum[k+n-1]-sum[k+m-1])%M+M)%M);
        memcpy(mem,a,sizeof(a));
        qpow(n); for(R i=1;i<=k+1;++i) for(R j=1;j<=k+1;++j) ans[j]=(ans[j]+s[i]*a[i][j])%M; sumn=ans[k+1];
        if(m<=0) return (void)printf("%lld
    ",((sumn-sum[k+m])%M+M)%M);
        memset(ans,0,sizeof(ans)); memcpy(a,mem,sizeof(a));
        qpow(m); for(R i=1;i<=k+1;++i) for(R j=1;j<=k+1;++j) ans[j]=(ans[j]+s[i]*a[i][j])%M; summ=ans[k+1];
        printf("%lld
    ",((sumn-summ)%M+M)%M);
    }
    }
    signed main() {
        Luitaryi::main();
        return 0;
    }

    2019.07.21

  • 相关阅读:
    Oracle 创建dblink
    好的博客
    Java项目导出war包 security alert:integrity check error”
    tomcat7.0 处理问题
    项目支持Servlet3.0的新特性
    oracle replace函数
    JavaWeb项目连接Oracle数据库
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
  • 原文地址:https://www.cnblogs.com/Jackpei/p/11223469.html
Copyright © 2020-2023  润新知