Advertisement
prog3r

https://codeforces.com/gym/102893/problem/H digit dp

Dec 6th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using vbo = vector<bool>;
  4. using ll = long long;
  5. using ull = unsigned long long;
  6. using pll = pair<ll, ll>;
  7. using ld = long double;
  8. using qll = queue<ll>;
  9. using vll = vector<ll>;
  10. using vvll = vector<vector<ll>>;
  11. using vvpll = vector<vector<pll>>;
  12. using qld = queue<ld>;
  13. using vld = vector<ld>;
  14. using qpll = queue<pll>;
  15. using vpll = vector<pll>;
  16. #include <ext/pb_ds/assoc_container.hpp>
  17. #include <ext/pb_ds/tree_policy.hpp>
  18. using namespace __gnu_pbds;
  19. template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  20. ostream& endl(ostream& os) {
  21.     return os << '\n';
  22. }
  23. //define all(xxx) xxx.begin(), xxx.end()
  24. //define watch(xxx) cout << "value of " << #xxx << " is " << xxx << endl
  25. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,no-stack-protector,fast-math")
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27. uniform_int_distribution<ll> distrib(0ll, 10ll);
  28. //constexpr ll MOD = 819356875157278019ll;
  29. constexpr ll MOD = 1e9+7;
  30.  
  31. void solve(){
  32.     string l, r; cin >> l >> r;
  33.     reverse(l.begin(), l.end());
  34.     reverse(r.begin(), r.end());
  35.     while (l.size() != r.size()) {
  36.         l.push_back('0');
  37.     }
  38.     ll dp[10][20][2][2][2]; // dp[][][][][] = kolvo_sposobov_popast_v_sostoyaniye
  39.     ll been[10][20][2][2][2][20]; // dp[][][][][][x] = dlya dliny x — eto kolvo chisel u kotorih est otrezok iz x odinakovih cifr
  40.     ll dp_nw[10][20][2][2][2];
  41.     ll been_nw[10][20][2][2][2][20];
  42.     reverse(l.begin(), l.end());
  43.     reverse(r.begin(), r.end());
  44.     clog << l << "," << r << endl;
  45.     ll n = l.size();
  46.     memset(dp, 0, sizeof(dp));
  47.     memset(been, 0, sizeof(been));
  48.     dp[0][1][0][0][0] = 1;
  49.     been[0][1][0][0][0][1] = 1;
  50.     vector<ll> kol_by_length(20);
  51.     for (ll i = 0; i < n; i += 1) {
  52.         clog << "i = " << i << ".." << endl;
  53.         memset(dp_nw, 0, sizeof(dp_nw));
  54.         memset(been_nw, 0, sizeof(been_nw));
  55.         for (ll last = 0; last <= 9; last += 1) {
  56.             for (ll kol = 0; kol <= 19; kol += 1) {
  57.                 for (ll more_than_l = 0; more_than_l < 2; more_than_l += 1) {
  58.                     for (ll less_than_r = 0; less_than_r < 2; less_than_r += 1) {
  59.                         for (ll was_non_zero = 0; was_non_zero < 2; was_non_zero += 1) {
  60.                             if (!dp[last][kol][more_than_l][less_than_r][was_non_zero]) continue;
  61.                             for (ll nw = 0; nw <= 9; nw += 1) {
  62.                                 if (nw < (l[i]-'0') && !more_than_l) continue;
  63.                                 if (nw > (r[i]-'0') && !less_than_r) continue;
  64.                                 ll nwkol = 1;
  65.                                 if (nw == last && (nw != 0 || was_non_zero)) nwkol += kol;
  66.                                 dp_nw[nw][nwkol][more_than_l||nw>(l[i]-'0')][less_than_r||nw<(r[i]-'0')][was_non_zero || nw != 0] += dp[last][kol][more_than_l][less_than_r][was_non_zero];
  67.                                 for (ll trkol = 0; trkol <= 19; trkol += 1) {
  68.                                     been_nw[nw][nwkol][more_than_l||nw>(l[i]-'0')][less_than_r||nw<(r[i]-'0')][was_non_zero || nw != 0][trkol] += been[last][kol][more_than_l][less_than_r][was_non_zero][trkol];
  69.                                 }
  70.                                 been_nw[nw][nwkol][more_than_l||nw>(l[i]-'0')][less_than_r||nw<(r[i]-'0')][was_non_zero || nw != 0][nwkol] += dp[last][kol][more_than_l][less_than_r][was_non_zero]-been[last][kol][more_than_l][less_than_r][was_non_zero][nwkol];
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.         swap(dp, dp_nw);
  78.         swap(been, been_nw);
  79.     }
  80.     for (ll last = 0; last <= 9; last += 1) {
  81.         for (ll kol = 0; kol <= 19; kol += 1) {
  82.             for (ll more_than_l = 0; more_than_l < 2; more_than_l += 1) {
  83.                 for (ll less_than_r = 0; less_than_r < 2; less_than_r += 1) {
  84.                     for (ll was_non_zero = 0; was_non_zero < 2; was_non_zero += 1) {
  85.                         for (ll been_sz = 0; been_sz <= 19; been_sz += 1) {
  86.                             ll shit = been[last][kol][more_than_l][less_than_r][was_non_zero][been_sz];
  87.                             kol_by_length[been_sz] += shit;
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.     }
  94.     for (ll i = 19; i >= 0; i -= 1) {
  95.         if (kol_by_length[i] != 0) {
  96.             cout << i << " " << kol_by_length[i] << endl;
  97.             return;
  98.         }
  99.     }
  100.     assert(false);
  101. }
  102.  
  103. int32_t main(int32_t argc, char* argv[]) {
  104. //    ifstream cin("distance.in");
  105. //    ofstream cout("distance.out");
  106.     cout << fixed << setprecision(17);
  107.     bool use_fast_io = true;
  108.     for (int32_t i = 1; i < argc; ++i) {
  109.         if (string(argv[i]) == "-local-no-fast-io") {
  110.             use_fast_io = false;
  111. //            cout << "No fastIO" << endl;
  112.             break;
  113.         }
  114.     }
  115.     if (use_fast_io) {
  116.         ios::sync_with_stdio(false);
  117.         cin.tie(nullptr);
  118.         cout.tie(nullptr);
  119.         cerr.tie(nullptr);
  120.         clog.tie(nullptr);
  121.     }
  122.     ll tt = 1;
  123.     cin >> tt;
  124.  
  125.     while (tt--) {
  126.         solve();
  127.     }
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement