trungore10

Untitled

Apr 11th, 2021
459
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 300;
  5. int dp[33][N][N][2];
  6.  
  7. vector<int> ExtractDigit(int value) {
  8.     vector<int> ans;
  9.     while (value > 0) {
  10.         ans.push_back(value % 10);
  11.         value /= 10;
  12.     }
  13.     reverse(ans.begin(), ans.end());
  14.     return ans;
  15. }
  16.  
  17. int calc(vector<int> lsDigit, int mod, int i, int modSumDigit, int modNumber, bool flag) {
  18.     if ( i >= (int) lsDigit.size() ) {
  19.         if (modSumDigit == 0 && modNumber == 0) return 1;
  20.         return 0;
  21.     }
  22.     if ( dp[i][modSumDigit][modNumber][flag] != -1 ) {
  23.         return dp[i][modSumDigit][modNumber][flag];
  24.     }
  25.  
  26.     int &foo = dp[i][modSumDigit][modNumber][flag];
  27.     foo = 0;
  28.  
  29.     int BeginDigit = 0;
  30.     int EndDigit = (!flag) ? lsDigit[i] : 9;
  31.  
  32.     for (int digit = BeginDigit; digit <= EndDigit; ++digit) {
  33.         int nModNumber = (modNumber * 10 % mod + digit) % mod;
  34.         int nModSumDigit = (modSumDigit + digit) % mod;
  35.         int nFlag = flag | (digit < lsDigit[i]);
  36.         foo += calc(lsDigit, mod, i+1, nModSumDigit, nModNumber, nFlag);
  37.     }
  38.  
  39.         // cerr << "/?? " << i << " " << modSumDigit << " " << modNumber << " " << flag << " --> " << foo << '\n';
  40.  
  41.     return foo;
  42. }
  43.  
  44. int sol(int bound, int mod) {
  45.     if (bound <= 0) {
  46.         return 1;
  47.     }
  48.     memset(dp, -1, sizeof(dp));
  49.  
  50.     vector<int> lsDigit = ExtractDigit(bound);
  51.     int res = calc(lsDigit, mod, 0, 0, 0, false);
  52.     return res;
  53. }
  54.  
  55. int main() {
  56.     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  57.     if (fopen("input.txt", "r")) {
  58.         freopen("input.txt", "r", stdin);
  59.         freopen("output.txt", "w", stdout);
  60.     }
  61.  
  62.     int numTest;
  63.     cin >> numTest;
  64.  
  65.     for (int iTest = 1; iTest <= numTest; ++iTest) {
  66.         int L, R, K;
  67.         cin >> L >> R >> K;
  68.  
  69.         int res = -1;
  70.         if (K >= 300) {
  71.             res = 0;
  72.         }
  73.         else {
  74.             res = sol(R, K)- sol(L-1, K);
  75.         }
  76.        
  77.         cout << res << '\n';
  78.     }
  79.  
  80.     return 0;
  81. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×