ABDELRHMAN_SAEED007

مسائلة قحبة3

Nov 7th, 2025
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #define rep(i, a, b) for(int i = a; i < (b); ++i)
  5. #define all(x) begin(x), end(x)
  6. #define sz(x) (int)(x).size()
  7. typedef long long ll;
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. #define ld long double
  11. #define int long long
  12. #define F first
  13. #define S second
  14. #define Lnode 2*node+1
  15. #define Rnode 2*node+2
  16. #define MID (l+r>>1)
  17. #define el '\n'
  18. #define coutf(x) for(auto v:(x)) cout<<v<<' '; cout<<el
  19. #define coutp(x) for(auto v:(x)) cout<<v.F<<' '<<v.S<<el
  20. #define cinl(x) for(auto &v:(x)) cin>>v;
  21. #define all(x)  x.begin(),x.end()
  22. #define ll long long
  23. #define sz(x)  (int)x.size()
  24. #define pi pair<ll,ll>
  25. #define pii pair<ll,pair<ll,ll>>
  26. #define vi vector<ll>
  27. #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
  28. const int N=5005,mod=1e9+7;
  29. using ull = unsigned long long;
  30. // order_of_key (k) : Number of items strictly smaller than k .
  31. // find_by_order(k) : K-th element in a set (counting from zero).
  32. struct aho_corasick {
  33.     struct trie_node {
  34.         vector<int> pIdxs; //probably take memory limit
  35.         map<char, int> next;
  36.         int fail;
  37.         trie_node() :  fail(0) {}
  38.         bool have_next(char ch) {
  39.             return next.find(ch) != next.end();
  40.         }
  41.         int& operator[](char ch) {
  42.             return next[ch];
  43.         }
  44.     };
  45.     vector<trie_node> t;
  46.     vector<string> patterns;
  47.     vector<int> end_of_pattern;
  48.     vector<bool>found;
  49.  
  50.     vector<vector<int>> adj;
  51.     int insert(const string &s, int patternIdx) {
  52.         int root = 0;
  53.         for (const char &ch : s) {
  54.             if (!t[root].have_next(ch)) {
  55.                 t.push_back(trie_node());
  56.                 t[root][ch] = t.size() - 1;
  57.             }
  58.             root = t[root][ch];
  59.         }
  60.         t[root].pIdxs.push_back(patternIdx);
  61.         return root;
  62.     }
  63.     int next_state(int cur, char ch) {
  64.         while (cur > 0 && !t[cur].have_next(ch))
  65.             cur = t[cur].fail;
  66.         if (t[cur].have_next(ch))
  67.             return t[cur][ch];
  68.         return 0;
  69.     }
  70.     void buildAhoTree() {
  71.         queue<int> q;
  72.         for (auto &child : t[0].next)
  73.             q.push(child.second);
  74.  
  75.         while (!q.empty()) {
  76.             int cur = q.front();
  77.             q.pop();
  78.             for (auto &child : t[cur].next) {
  79.                 int k = next_state(t[cur].fail, child.first);
  80.                 t[child.second].fail = k;
  81.                 vector<int> &idxs = t[child.second].pIdxs;
  82.                 //dp[child.second] = max(dp[child.second],dp[k]);
  83.                 idxs.insert(idxs.end(), all(t[k].pIdxs));
  84.                 q.push(child.second);
  85.             }
  86.         }
  87.     }
  88.  
  89.  
  90.     void buildFailureTree() {
  91.         adj = vector<vector<int>>(t.size());
  92.         for (int i = 1; i < t.size(); i++)
  93.             adj[t[i].fail].push_back(i);
  94.     }
  95.     aho_corasick(const vector<string> &_patterns) {
  96.         t.push_back(trie_node());
  97.         patterns = _patterns;
  98.         end_of_pattern = vector<int>(patterns.size());
  99.         found=vector<bool>(patterns.size(),false);
  100.         for (int i = 0; i < patterns.size(); i++)
  101.             end_of_pattern[i] = insert(patterns[i], i);
  102.         buildAhoTree();
  103.         //buildFailureTree();
  104.     }
  105.     vector<bool> match(const string &str) {
  106.         int k = 0;
  107.         int sum=0;
  108.  
  109.         vector<vector<int>> rt(patterns.size());
  110.         for (int i = 0; i < str.size(); i++) {
  111.             k = next_state(k, str[i]);
  112.             for (auto &it : t[k].pIdxs)
  113.                 found[it]=1;
  114.         }
  115.        return found;
  116.     }
  117. };
  118. ll n,k;
  119. ll dp[55][1005][2][2];
  120. string s1,s2;
  121. string s;
  122. vector<ll>num;
  123. ll digit(ll i,ll node,bool found,bool f,aho_corasick &ac)
  124. {
  125.     if (i==n)return (found>0);
  126.     ll &ret=dp[i][node][found][f];
  127.     if (~ret)return ret;
  128.     ll res=0;
  129.     int LMT;
  130.     if (f==0)LMT=num[i];
  131.     else LMT=9;
  132.     for (int dgt=0;dgt<=LMT;dgt++)
  133.     {
  134.         int nf=f;
  135.         if (f==0&&dgt<LMT)nf=1;
  136.         char ch=char('0'+dgt);
  137.         int nnode=ac.next_state(node,ch);
  138.         bool nfound =found;
  139.         if (!ac.t[nnode].pIdxs.empty())nfound=1;
  140.             res+=digit(i+1,nnode,nfound,nf,ac);
  141.         if (res>=mod)res-=mod;
  142.     }
  143.     return ret=res;
  144. }
  145. ll fun(string s,aho_corasick &ac){
  146.     num.clear();
  147.     for (auto it:s)
  148.     {
  149.         num.push_back(it-'0');
  150.     }
  151.  
  152.     n=s.size();
  153.  
  154.     memset(dp,-1,sizeof dp);
  155.     ll ans=digit(0,0,0,0,ac);
  156.     return ans;
  157. }
  158. string minus1(string s)
  159. {
  160.     int n=s.size();
  161.     int i=n-1;
  162.  
  163.     while (i>=0&&s[i]=='0')s[i]='9',i--;
  164.     if (i>=0)s[i]--;
  165.     else
  166.     {
  167.         for (int j=0;j<n;j++)s[j]='9';
  168.     }
  169.     return s;
  170. }
  171.  
  172. void solve()
  173. {
  174.     cin>>s;
  175.     cin>>s1>>s2;
  176.  
  177.     vector<string>pat;
  178.     k=(int)(s2.size()+1)/2;
  179.     for (int i=0;i+k<=(int)s.size();i++){
  180.         pat.push_back(s.substr(i,k));
  181.     }
  182.     aho_corasick ac(pat);
  183.  
  184.     bool zero=1;
  185.     for (int i=0;i<s1.size();i++)
  186.     {
  187.         if (s1[i]!='0')
  188.         {
  189.             zero=0;
  190.             break;
  191.         }
  192.     }
  193.     ll a,b;
  194.     if (zero==0)
  195.     {
  196.  
  197.     s1=minus1(s1);
  198.     b=fun(s1,ac);
  199.  
  200.     }
  201.     else b=0;
  202.     a=fun(s2,ac);
  203.     ll ans=(a-b)%mod;
  204.     if (ans<0)ans+=mod;
  205.     cout<<ans<<"\n";
  206. }
  207.  
  208. int32_t main() {
  209. #ifndef ONLINE_JUDGE
  210.     freopen("in.txt", "r", stdin);
  211.     freopen("out.txt", "w", stdout);
  212. #endif
  213.     ios_base::sync_with_stdio(false);
  214.     cin.tie(NULL);
  215.     int tc=1;
  216.     //cin>>tc;
  217.     for (int i=1;i<=tc;i++){solve();}
  218.     return 0;
  219. }
  220. /*
  221.  
  222.  */
Advertisement
Add Comment
Please, Sign In to add comment