Advertisement
Zeinab_Hamdy

Untitled

Oct 4th, 2023
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. // #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  solution  ================================ */
  51.  
  52.  
  53. const ll mod = 1e9+7;
  54.  
  55. ll m , d;
  56.  
  57. ll dp[2001][2001][2];
  58.  
  59.  
  60.  
  61. ll rec(string & s , int idx , int cnt , bool flag){
  62.    
  63.     if(idx >= sz(s) ){
  64.         // cout << cnt << " " << idx << nl;
  65.         return !cnt;
  66.     }
  67.    
  68.      
  69.     ll & ret = dp[idx][cnt][flag];
  70.     if(~ret)
  71.         return ret;
  72.  
  73.  
  74.     if(idx & 1 ){
  75.         if(!flag and (d+'0') > s[idx] ){
  76.             cout << "Here \n ";
  77.             return 0;
  78.         }
  79.        
  80.        return  ret = rec(s , idx+1 , (cnt * 10 + d) % m , flag or (d+'0') < s[idx]);
  81.     }
  82.    
  83.    
  84.     ret =0;
  85.     for(char c = '0' ; c <='9' ; c++){
  86.         if((c-'0') == d) continue;
  87.         if(!flag and c > s[idx]) continue;
  88.         ret += rec(s , idx+1 , ( cnt * 10 + (c-'0') ) % m  , flag or c < s[idx]);
  89.         ret %= mod;
  90.     }
  91.  
  92.     return ret ;
  93. }
  94.  
  95. void myCode(){
  96.  
  97. cin >> m >> d;
  98.  
  99.  
  100.  
  101. string s1 , s2;
  102. cin >> s1 >> s2;
  103.  
  104.  
  105. clr(dp,-1);
  106.  
  107. ll ans = rec(s2 , 0 , 0 , 0 );
  108.  
  109. if(s1.back()=='0'){
  110.     if(s1.front() == '1')  {
  111.         int cnt = sz(s1)-1;
  112.         s1="";
  113.         while(cnt--)  s1+='9';
  114.     }
  115.     else{
  116.      
  117.         s1[sz(s1) - 2]--;
  118.         s1.back()='9';
  119.     }
  120. }
  121. else {
  122.  s1.back() --;
  123. }
  124.  
  125. // cout << s1 << nl;
  126.  
  127.  
  128. ans+=mod;
  129. ans%=mod;
  130. // cout << ans << nl;
  131. // cout << nl << nl;
  132.  
  133. clr(dp,-1);
  134. ans-=rec(s1 , 0 , 0 , 0 );
  135.  
  136. ans+=mod;
  137. ans%=mod;
  138.  
  139. cout << ans << nl;
  140.  
  141.  
  142.  
  143. }
  144.  
  145.  
  146. int main(){
  147.                                    FastCode ;
  148.                                      Zainab() ;
  149.  
  150.         int testCase=1;
  151.             // cin >> testCase ;
  152.         for(int i=1 ; i<= testCase ; i++){
  153.             myCode();
  154.         }
  155.      
  156.  
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement