标签:span div 问题 sample ace freopen tput inline blog
正解:倍增。
比较裸的题,直接对于每一段路,倍增算出答案即可。
1 #include <bits/stdc++.h> 2 #define il inline 3 #define RG register 4 #define ll long long 5 6 using namespace std; 7 8 int f[26][31][10010],v[26][31][10010],n,k,len,rhl,ans; 9 char s[150010]; 10 11 il int gi(){ 12 RG int x=0,q=1; RG char ch=getchar(); 13 while ((ch<‘0‘ || ch>‘9‘) && ch!=‘-‘) ch=getchar(); 14 if (ch==‘-‘) q=-1,ch=getchar(); 15 while (ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-48,ch=getchar(); 16 return q*x; 17 } 18 19 int main(){ 20 #ifndef ONLINE_JUDGE 21 freopen("OItree.in","r",stdin); 22 freopen("OItree.out","w",stdout); 23 #endif 24 n=gi(),k=gi(); 25 for (RG int i=1;i<=n;++i) 26 for (RG int j=0;j<k;++j) 27 f[j][0][i]=gi(),v[j][0][i]=gi(); 28 scanf("%s",s+1),len=strlen(s+1),rhl=gi(); 29 for (RG int p=0;p<k;++p) 30 for (RG int j=1;j<=30;++j) 31 for (RG int i=1;i<=n;++i){ 32 f[p][j][i]=f[p][j-1][f[p][j-1][i]]; 33 v[p][j][i]=v[p][j-1][f[p][j-1][i]]+v[p][j-1][i]; 34 if (v[p][j][i]>=rhl) v[p][j][i]-=rhl; 35 } 36 for (RG int i=1,jp,p,S=1;i<=len;){ 37 while (s[i]==‘[‘) ++i; jp=0; 38 while (s[i]>=‘0‘ && s[i]<=‘9‘) jp=jp*10+s[i]-48,++i; 39 if (!jp) jp=1; p=s[i]-‘A‘,++i; while (s[i]==‘]‘) ++i; 40 for (RG int j=30;j>=0;--j) 41 if (jp>=(1<<j)){ 42 jp-=1<<j,ans+=v[p][j][S],S=f[p][j][S]; 43 if (ans>=rhl) ans-=rhl; 44 } 45 if (i>len) break; 46 } 47 cout<<ans; return 0; 48 }
标签:span div 问题 sample ace freopen tput inline blog
原文地址:http://www.cnblogs.com/wfj2048/p/7603443.html