zoukankan      html  css  js  c++  java
  • bzoj4161: Shlw loves matrixI

    传送门

    多项式取模优化k阶常系数线性递推的模板

    刚才的板子改了一下,wa了,,,先码这吧,下次再改。

     1 //Achen
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<queue>
     9 #include<cmath>
    10 #include<set>
    11 #include<map>
    12 #define Formylove return 0
    13 #define For(i,a,b) for(int i=(a);i<=(b);i++)
    14 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    15 const int N=4007,p=1000000007;
    16 typedef long long LL;
    17 typedef double db;
    18 using namespace std;
    19 LL a[N],f[N],n,k;
    20 
    21 template<typename T>void read(T &x)  {
    22     char ch=getchar(); x=0; T f=1;
    23     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    24     if(ch=='-') f=-1,ch=getchar();
    25     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    26 }
    27 
    28 LL rs[N],bs[N];
    29 void mul(LL a[],LL b[],LL c[]) {
    30     static LL tp[N];
    31     int up=(k<<1);
    32     For(i,0,up) tp[i]=0;
    33     For(i,0,k-1) For(j,0,k-1)
    34         (tp[i+j]+=a[i]*b[j]%p)%=p; 
    35     Rep(i,up,k) { 
    36         For(j,1,k) (tp[i-j]+=f[j]*tp[i]%p)%=p;
    37         tp[i]=0;
    38     }
    39     For(i,0,up) c[i]=tp[i]; 
    40 }
    41 
    42 void ksm(LL b) {
    43     while(b) {
    44         if(b&1) mul(rs,bs,rs);
    45         mul(bs,bs,bs);
    46         b>>=1;    
    47     }
    48 }
    49 
    50 //#define ANS
    51 int main() {
    52 #ifdef ANS
    53     freopen("shanghai.in","r",stdin);
    54     freopen("shanghai.out","w",stdout);
    55 #endif
    56     read(n); read(k);
    57     For(i,1,k) read(f[i]);
    58     For(i,1,k) { read(a[i]); (a[i]+=p)%=p; }
    59     if(n<k) { 
    60         printf("%lld
    ",a[n+1]); 
    61         return 0;
    62     }
    63     rs[0]=1; bs[1]=1;
    64     ksm(n);
    65     LL ans=0;
    66     For(i,0,k-1) (ans+=rs[i]*a[i+1]%p)%=p;
    67     printf("%lld
    ",ans);
    68     Formylove;
    69 }
    View Code
  • 相关阅读:
    jQuery 语法
    jQuery 简介
    把数据存储到 XML 文件
    XML 注意事项
    XML DOM (Document Object Model) 定义了访问和操作 XML 文档的标准方法。
    通过 PHP 生成 XML
    XML 命名空间(XML Namespaces)
    XML to HTML
    XMLHttpRequest 对象
    使用 XSLT 显示 XML
  • 原文地址:https://www.cnblogs.com/Achenchen/p/9507826.html
Copyright © 2011-2022 走看看