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