zoukankan      html  css  js  c++  java
  • [bzoj4161]Shlw loves matrix I

    来自FallDream的博客,未经允许,请勿转载,谢谢。

    给定数列 {hn}前k项,其后每一项满足
    hn = a1*h(n-1) + a2*h(n-2) + ... + ak*h(n-k)
    其中 a1,a2...ak 为给定数列。请计算 h(n),并将结果对 1000000007 取模输出。
    n<=10^9,k<=2000
     
    很裸的特征多项式优化矩阵乘法,打个模版。
    #include<iostream>
    #include<cstdio>
    #define mod 1000000007
    #define MN 2000
    using namespace std;
    int X,F;char ch;
    inline int read()
    {
        X = 0 , F = 0 , ch = getchar();
        while(ch < '0' || ch > '9'){ if(ch == '-') F = 1;  ch = getchar();}
        while(ch >= '0' && ch <= '9'){X = X * 10 + ch - '0';ch = getchar();}
        return F?-X:X;
    }
    
    int n,k,ans=0;
    int h[MN+5],a[MN*2+5],b[MN*2+5],c[MN*2+5],t[MN*2+5];
    
    void mul(int*A,int*B)
    {
        for(int i=1;i<=k;i++)
            for(int j=1;j<=k;j++)
                c[i+j-1]=(c[i+j-1]+1LL*A[i]*B[j])%mod;
        for(int i=k<<1;i>k;c[i--]=0)
            for(int j=1;j<=k;j++)
                c[i-j]=(c[i-j]+1LL*c[i]*t[j])%mod;
        for(int i=k;i;i--) A[i]=c[i],c[i]=0;
    }
    
    int main()
    {
        n=read();k=read();a[2]=b[1]=1;
        for(int i=1;i<=k;i++)(t[i]=read())<0?t[i]+=mod:0;
        for(int i=1;i<=k;i++)(h[i]=read())<0?h[i]+=mod:0;
        if(n<=k)return 0*printf("%d
    ",h[n]);
        for(int i=n;i;i>>=1,mul(a,a))
            if(i&1)mul(b,a);
        for(int i=1;i<=k;i++)ans=(ans+1LL*b[i]*h[i])%mod;
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    初入水:vector
    Sort Colors
    Palindrome Partitioning II
    Search for a Range
    Container With Most Water
    Palindrome Partitioning
    Longest Consecutive Sequence
    简单写了一个堆排序
    Best Time to Buy and Sell Stock III
    4-7
  • 原文地址:https://www.cnblogs.com/FallDream/p/bzoj4161.html
Copyright © 2011-2022 走看看