Advertisement
yuhung94

Problem 3. Piling Papers

Mar 6th, 2023
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #pragma GCC optimize("O3,unroll-loops")
  2. #pragma GCC target("avx,popcnt,sse4,abm")
  3. #include<bits/stdc++.h>
  4. #define int long long
  5. #define quick ios::sync_with_stdio(0);cin.tie(0);
  6. #define rep(x,a,b) for(int x=a;x<=b;x++)
  7. #define repd(x,a,b) for(int x=a;x>=b;x--)
  8. #define lowbit(x) (x&-x)
  9. #define sz(x) (int)(x.size())
  10. #define F first
  11. #define S second
  12. #define all(x) x.begin(),x.end()
  13. #define mp make_pair
  14. #define eb emplace_back
  15. using namespace std;
  16. typedef complex<int> P;
  17. #define X real()
  18. #define Y imag()
  19. typedef pair<int,int> pii;
  20. void debug(){
  21.     cout<<"\n";
  22. }
  23. template <class T,class ... U >
  24. void debug(T a, U ... b){
  25.     cout<<a<<" ",debug(b...);
  26. }
  27. const int N=3e2+7;
  28. const int D=19;
  29. const int INF=1e18;
  30. const int Mod=1e9+7;
  31. int dp[N][D][D][3];
  32. int a[N];
  33. int pref[N][N];
  34. int query(int i,int l,int r,int c){
  35.     if(l>r){
  36.         return (c==1);
  37.     }
  38.     return dp[i][l][r][c];
  39. }
  40. void add(int &x,int val){
  41.     x+=val;
  42.     if(x>=Mod) x%=Mod;
  43. }
  44. void cal(int n,int st,const string&s){
  45.     int d=sz(s);
  46.     rep(l,0,d-1){
  47.         int sum=0;
  48.         rep(r,0,d-1){
  49.             rep(c,0,2) dp[st][l][r][c]=0;
  50.             if(l==r){
  51.                 if(a[st]<s[l]-'0'){
  52.                     dp[st][l][r][0]=2;
  53.                 }
  54.                 else if(a[st]>s[l]-'0') dp[st][l][r][2]=2;
  55.                 else dp[st][l][r][1]=2;
  56.             }
  57.         }
  58.     }
  59.     rep(i,st+1,n){
  60.         rep(l,0,d-1){
  61.             rep(r,l,d-1){
  62.                 rep(c,0,2) dp[i][l][r][c]=dp[i-1][l][r][c];
  63.                 if(a[i]<s[l]-'0'){
  64.                     rep(c,0,2){
  65.                         add(dp[i][l][r][0],query(i-1,l+1,r,c));
  66.                     }
  67.                 }
  68.                 else if(a[i]==s[l]-'0'){
  69.                     rep(c,0,2) add(dp[i][l][r][c],query(i-1,l+1,r,c));
  70.                 }
  71.                 else{
  72.                     rep(c,0,2) add(dp[i][l][r][2],query(i-1,l+1,r,c));
  73.                 }
  74.                 if(a[i]<s[r]-'0'){
  75.                     add(dp[i][l][r][0],query(i-1,l,r-1,1)+query(i-1,l,r-1,0));
  76.                     add(dp[i][l][r][2],query(i-1,l,r-1,2));
  77.                 }
  78.                 else if(a[i]==s[r]-'0'){
  79.                     rep(c,0,2) add(dp[i][l][r][c],query(i-1,l,r-1,c));
  80.                 }
  81.                 else{
  82.                     add(dp[i][l][r][0],query(i-1,l,r-1,0));
  83.                     add(dp[i][l][r][2],query(i-1,l,r-1,1)+query(i-1,l,r-1,2));
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }
  89. int cnt(int j,int d){
  90.     int ret=1;
  91.     rep(L,0,d-2){
  92.         rep(c,0,2){
  93.             add(ret,dp[j][0][L][c]);
  94.         }
  95.     }
  96.     add(ret,dp[j][0][d-1][0]+dp[j][0][d-1][1]);
  97.     return ret;
  98. }
  99. signed main(){
  100.     quick
  101.     int n,A,B;
  102.     cin>>n>>A>>B;
  103.     rep(i,1,n) cin>>a[i];
  104.     string B2=to_string(B);
  105.     string A2=to_string(A-1);
  106. //  cal(n,1,B2);debug("cnt",cnt(1,sz(B2)));return 0;
  107.     rep(st,1,n){
  108.         cal(n,st,B2);
  109.     //  debug(st,"st");
  110.         rep(j,st,n){
  111.             pref[st][j]=cnt(j,sz(B2));
  112.     //      cout<<pref[st][j]<<" \n"[j==n];
  113.         }
  114.         cal(n,st,A2);
  115.         rep(j,st,n){
  116.     //      cout<<-cnt(j,sz(A2))<<" \n"[j==n];
  117.             pref[st][j]=(pref[st][j]-cnt(j,sz(A2))+Mod)%Mod;
  118.         }
  119.     }
  120.     /*rep(i,1,n){
  121.         rep(j,i,n){
  122.             cout<<pref[i][j]<<" \n"[j==n];
  123.         }
  124.     }*/
  125.     int q;
  126.     cin>>q;
  127.     while(q--){
  128.         int l,r;
  129.         cin>>l>>r;
  130.         cout<<pref[l][r]<<"\n";
  131.     }
  132.     return 0;
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement