Advertisement
pb_jiang

ABC311F TLE

Jul 22nd, 2023
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. constexpr int mod = 998244353;
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     int n, m;
  20.     cin >> n >> m;
  21.     vector<string> vs(n);
  22.     for (auto &s : vs)
  23.         cin >> s;
  24.  
  25.     ll ans = 0;
  26.     auto get_first_row = [&](int col) {
  27.         for (int i = 0; i < n; ++i)
  28.             if (vs[i][col] == '#')
  29.                 return i;
  30.         return n;
  31.     };
  32.     vector<int> fbs(m, n);
  33.     for (int i = 0; i < m; ++i)
  34.         fbs[i] = get_first_row(i);
  35.  
  36.     map<pii, ll> cache;
  37.     function<ll(int, int)> search = [&](int col, int fb) {
  38.         if (cache.count({col, fb}))
  39.             return cache[{col, fb}];
  40.         if (col == -1)
  41.             return 1ll;
  42.         int lb = fb, ub = fbs[col] + 1;
  43.         ll ans = 0;
  44.         for (int i = ub - 1; i >= lb; --i) {
  45.             ans = (ans + search(col - 1, max(i - 1, 0))) % mod;
  46.             cache[{col, i}] = ans;
  47.         }
  48.  
  49.         // dbg(col, fb, ans);
  50.         return cache[{col, fb}] = ans;
  51.     };
  52.  
  53.     ans = search(m - 1, 0);
  54.     cout << ans << endl;
  55.     return 0;
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement