2018 江苏省邀请赛 H

题目链接 https://nanti.jisuanke.com/t/28872

解析 递推 直接套杜教板子

AC代码

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cmath>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <string>
  7 #include <map>
  8 #include <set>
  9 #include <iostream>
 10 #include <cassert>
 11 using namespace std;
 12 #define rep(i,a,n) for (int i=a;i<n;i++)
 13 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 14 #define pb push_back
 15 #define mp make_pair
 16 #define all(x) (x).begin(),(x).end()
 17 #define fi first
 18 #define se second
 19 #define SZ(x) ((int)(x).size())
 20 typedef vector<int> VI;
 21 typedef long long ll;
 22 typedef pair<int,int> PII;
 23 const int maxn=1e6+20;
 24 const ll mod=1000000007;
 25 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 26 // head
 27 
 28 int _,n,k;
 29 int a[maxn],c[maxn];
 30 namespace linear_seq {
 31     const int N=10010;
 32     ll res[N],base[N],_c[N],_md[N];
 33 
 34     vector<int> Md;
 35     void mul(ll *a,ll *b,int k) {
 36         rep(i,0,k+k) _c[i]=0;
 37         rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
 38         for (int i=k+k-1;i>=k;i--) if (_c[i])
 39             rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
 40         rep(i,0,k) a[i]=_c[i];
 41     }
 42     int solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
 43 //        printf("%d\n",SZ(b));
 44         ll ans=0,pnt=0;
 45         int k=SZ(a);
 46         assert(SZ(a)==SZ(b));
 47         rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
 48         Md.clear();
 49         rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
 50         rep(i,0,k) res[i]=base[i]=0;
 51         res[0]=1;
 52         while ((1ll<<pnt)<=n) pnt++;
 53         for (int p=pnt;p>=0;p--) {
 54             mul(res,res,k);
 55             if ((n>>p)&1) {
 56                 for (int i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
 57                 rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
 58             }
 59         }
 60         rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
 61         if (ans<0) ans+=mod;
 62         return ans;
 63     }
 64     VI BM(VI s) {
 65         VI C(1,1),B(1,1);
 66         int L=0,m=1,b=1;
 67         rep(n,0,SZ(s)) {
 68             ll d=0;
 69             rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
 70             if (d==0) ++m;
 71             else if (2*L<=n) {
 72                 VI T=C;
 73                 ll c=mod-d*powmod(b,mod-2)%mod;
 74                 while (SZ(C)<SZ(B)+m) C.pb(0);
 75                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
 76                 L=n+1-L; B=T; b=d; m=1;
 77             } else {
 78                 ll c=mod-d*powmod(b,mod-2)%mod;
 79                 while (SZ(C)<SZ(B)+m) C.pb(0);
 80                 rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
 81                 ++m;
 82             }
 83         }
 84         return C;
 85     }
 86     int gao(VI a,ll n) {
 87         VI c=BM(a);
 88         c.erase(c.begin());
 89         rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
 90         return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
 91     }
 92 };
 93 
 94 int main() {
 95     while(scanf("%d%d",&n,&k)==2)
 96     {
 97         for(int i=1;i<=n;i++)scanf("%d",&a[i]);
 98         for(int i=1;i<=n;i++)scanf("%d",&c[i]);
 99         for(int i=n+1;i<=n*2;i++)
100         {
101             a[i]=0;
102             for(int j=1;j<=n;j++)
103                 a[i]=(a[i]+1ll*a[i-j]*c[j]%mod)%mod;
104         }
105         VI g;g.clear();
106         for(int i=1;i<=2*n;i++)g.pb(a[i]);
107         cout<<linear_seq::gao(g,k-1)<<endl;
108     }
109 }

猜你喜欢

转载自www.cnblogs.com/stranger-/p/9368468.html
今日推荐