Advertisement
pb_jiang

T4 TLE

Oct 27th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. using ll = long long;
  2. constexpr int mod = 1e9 + 7;
  3. class Solution {
  4. public:
  5.     int subsequencePairCount(vector<int>& ns) {
  6.         int n = ns.size();
  7.         using a3i = array<int, 3>;
  8.         // map<a3i, ll> cache;
  9.         // vector<vector<vector<int>>> cache(201, vector<vector<int>>(201, vector<int>(201, -1)));
  10.         // map<int, map<int, map<int, int>>> cache;
  11.         using a2i = array<int, 2>;
  12.         vector<map<a2i, int>> cache(201);
  13.        
  14.         function<int(int, int, int)> get = [&](int i, int j, int k) {
  15.             if (i < 0) return int(j == k);
  16.             // if (cache[i][j][k] != -1) return cache[i][j][k];
  17.             // if (cache.count(i) && cache[i].count(j) && cache[i][j].count(k)) return cache[i][j][k];
  18.             if (cache[i].count({j, k})) return cache[i][{j, k}];
  19.             int v = ns[i];
  20.             //return cache[i][j][k] = (get(i-1, j, k) + get(i-1, gcd(j, v), k) + get(i-1, j, gcd(k, v))) % mod;
  21.             return cache[i][{j,k}] = (ll(get(i-1, j, k)) + ll(get(i-1, gcd(j, v), k)) + ll(get(i-1, j, gcd(k, v)))) % mod;
  22.         };
  23.  
  24.         return (get(n-1, 0, 0) + mod - 1) % mod;
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement