classSolution { public: intnumberOfPoints(vector<vector<int>>& nums){ sort(nums.begin(), nums.end()); int ans = 0; int start = 0, end = 0; for (auto v : nums) { if (v[0] > end) { ans += end - start; start = v[0]; end = v[1] + 1; } else { end = max(end, v[1] + 1); } } ans += end - start; return ans; } };
classSolution { public: boolisReachableAtTime(int sx, int sy, int fx, int fy, int t){ int x = abs(sx - fx); int y = abs(sy - fy); if (x == 0 && y == 0) { return t != 1; } if (x > t || y > t) { returnfalse; } returntrue; } };
int tot = accumulate(valid.begin(), valid.end(), 0); vector<vector<LL>> mat(2, vector<LL>(2)); mat[0][0] = n - tot - 1; mat[0][1] = tot; mat[1][0] = n - tot; mat[1][1] = tot - 1; auto res = matrixPow(mat, k); if (t == s) { return res[0][0]; } else { return res[0][1]; } }
vector<vector<LL>> multiply(vector<vector<LL>> &a, vector<vector<LL>> &b) { int n = a.size(); int m = a[0].size(); int q = b[0].size(); vector<vector<LL>> res(n, vector<LL>(q)); for (int i = 0; i < n; i++) { for (int j = 0; j < q; j++) { for (int k = 0; k < m; k++) { res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % Mod; } } } return res; };
vector<vector<LL>> matrixPow(vector<vector<LL>> &a, longlong k) { int n = a.size(); vector<vector<LL>> res(n, vector<LL>(n, 0)); for (int i = 0; i < n; i++) res[i][i] = 1LL; for (; k != 0; k >>= 1) { if (k & 1) { res = multiply(res, a); } a = multiply(a, a); } return res; }; };
intnumberOfWays(string s, string t, longlong k){ int n = s.size(); vector<int> pat = find_pattern(t, s + s.substr(0, n - 1)); int tot = pat.size(); vector<vector<LL>> mat(2, vector<LL>(2)); mat[0][0] = n - tot - 1; mat[0][1] = tot; mat[1][0] = n - tot; mat[1][1] = tot - 1; auto res = matrixPow(mat, k); if (t == s) { return res[1][1] ; } else { return res[0][1]; } }
vector<vector<LL>> multiply(vector<vector<LL>> &a, vector<vector<LL>> &b) { int n = a.size(); int m = a[0].size(); int q = b[0].size(); vector<vector<LL>> res(n, vector<LL>(q)); for (int i = 0; i < n; i++) { for (int j = 0; j < q; j++) { for (int k = 0; k < m; k++) { res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % Mod; } } } return res; }
vector<vector<LL>> matrixPow(vector<vector<LL>> &a, longlong k) { int n = a.size(); vector<vector<LL>> res(n, vector<LL>(n, 0)); for (int i = 0; i < n; i++) res[i][i] = 1LL; for (; k != 0; k >>= 1) { if (k & 1) { res = multiply(res, a); } a = multiply(a, a); } return res; }
vector<int> find_pattern(const string& pattern, const string& text){ int n = pattern.size(); int m = text.size(); string t = pattern + "$" + text; vector<int> result; vector<int> prefix(m + n + 1, 0); int j = 0; for (int i = 1; i < t.size(); i++) { while (j > 0 && t[j] != t[i]) { j = prefix[j - 1]; } if (t[i] == t[j]) { j++; } else { j = 0; } prefix[i] = j; if (i > n && prefix[i] == n) { result.push_back(i - 2 * n); } } return result; } };