1001
直接模拟
1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(a) cerr<<#a<<"=="<<a<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int> pii; 7 8 const int maxn=1e5+10; 9 const int mod=1e9+7; 10 11 string str,ans,tmp; 12 13 void solve() 14 { 15 int now=0,len=str.size(); 16 tmp=""; 17 for(int i=0; i<len; i++) 18 { 19 char ch=str[i]; 20 if(ch=='(') 21 { 22 now=1; 23 } 24 else if(ch==')') 25 { 26 int k=str[i+3]-'0'; 27 while(k--) ans+=tmp; 28 tmp=""; 29 i+=4; 30 now=0; 31 } 32 else 33 { 34 if (now==1) 35 { 36 tmp+=ch; 37 } 38 else 39 { 40 ans+=ch; 41 } 42 } 43 } 44 } 45 46 int main() 47 { 48 int T; 49 scanf("%d",&T); 50 while(T--) 51 { 52 ans=""; 53 cin>>str; 54 solve(); 55 ll last=0; 56 for(auto i:ans) 57 { 58 last*=10; 59 last%=mod; 60 last+=(i-'0'); 61 last%=mod; 62 } 63 cout<<last<<endl; 64 } 65 return 0; 66 }/* 67 68 4 69 1 70 (1)#(3) 71 (12)#(2)4(2)#(3) 72 (12)#(5) 73 74 */
1003
原题:NYOJ980(比赛之后才知道
直接递推
打表查询
1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(a) cerr<<#a<<"=="<<a<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int> pii; 7 8 const int maxn=1e5+10; 9 const int mod=1e9+7; 10 11 ll a[maxn],b[maxn]; 12 13 void init() 14 { 15 cl(a,0),cl(b,0); 16 a[1]=1,a[2]=6; 17 b[1]=1; 18 for(int i=2;i<=maxn;i++) 19 { 20 b[i]=b[i-1]*2; 21 b[i]%=mod; 22 } 23 for(int i=3;i<=maxn;i++) 24 { 25 a[i]+=2*a[i-1]; 26 a[i]%=mod; 27 a[i]+=4*a[i-2]; 28 a[i]%=mod; 29 a[i]+=b[i]; 30 a[i]%=mod; 31 } 32 } 33 34 ll cal(ll n) 35 { 36 if(n==1) return 2; 37 if(n==2) return 24; 38 if(n==3) return 96; 39 ll sum=4*a[n]; 40 for(int i=2;i<n; i++) 41 { 42 sum+=8*b[n-i]*a[i-1]; 43 sum%=mod; 44 sum+=8*b[i-1]*a[n-i]; 45 sum%=mod; 46 } 47 return sum; 48 } 49 50 int main() 51 { 52 init(); 53 int T; 54 scanf("%d",&T); 55 while(T--) 56 { 57 ll n; 58 scanf("%lld",&n); 59 printf("%lld ",cal(n)); 60 } 61 return 0; 62 }/* 63 64 4 65 1 66 2 67 3 68 4 69 70 */
1005
数位dp模板题
1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(a) cerr<<#a<<"=="<<a<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int> pii; 7 8 const int maxn=100+10; 9 const int mod = 1e9+7; 10 11 char str[maxn]; 12 int dp[maxn][10][3][3][3]; 13 int l,num[maxn]; 14 15 ll cal(int len,int now,int sta,int limit,int lead) 16 { 17 if(len==l) return 1; 18 if(!limit&&!lead&&dp[len][now][sta][limit][lead]!=-1) 19 { 20 return dp[len][now][sta][limit][lead]; 21 } 22 ll ans=0; 23 int ed=(limit?num[len]:9); 24 for(int i=0;i<=ed;i++) 25 { 26 if(sta==1) 27 { 28 ans+=cal(len+1,(i==0&&lead?9:i),(i<=now?1:0),limit&&i==num[len],lead&&i==0); 29 } 30 else if(i>=now) 31 { 32 ans+=cal(len+1,(i==0&&lead?9:i),2,limit&&i==num[len],lead&&i==0); 33 } 34 ans%=mod; 35 } 36 ans%=mod; 37 return dp[len][now][sta][limit][lead]=ans; 38 } 39 40 void init() 41 { 42 l=strlen(str); 43 cl(dp,-1); 44 for(int i=0;i<l;i++) 45 { 46 num[i]=str[i]-'0'; 47 } 48 return ; 49 } 50 51 int main() 52 { 53 int T; 54 scanf("%d",&T); 55 while(T--) 56 { 57 58 scanf("%s",str); 59 init(); 60 ll ans=cal(0,9,1,1,1)-1; 61 printf("%lld ",ans); 62 } 63 return 0; 64 }/* 65 66 3 67 3 68 14 69 120 70 71 */