From d241cd9cccdd697b530d9c248a4201643f390870 Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Sat, 19 Jul 2025 20:52:09 +0200 Subject: [PATCH 1/8] Add Solution: Split Array by Prime Indices --- split-array-by-prime-indices/answer.cpp | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 split-array-by-prime-indices/answer.cpp diff --git a/split-array-by-prime-indices/answer.cpp b/split-array-by-prime-indices/answer.cpp new file mode 100644 index 0000000..957ceb7 --- /dev/null +++ b/split-array-by-prime-indices/answer.cpp @@ -0,0 +1,59 @@ +struct PrimeFactorization { + + // primes.size() <= sqrt(n) + vector primes; + vector p; + + PrimeFactorization(int n) { + p.resize(n); + p.assign(n, true); + + p[0] = p[1] = 0; + for (int i = 4; i < n; i += 2) + p[i] = 0; + for (int i = 3; i * i < n; i += 2) { + if (!p[i]) + continue; + for (int j = i * i; j < n; j += i) + p[j] = 0; + } + + primes = {2}; + for (int i = 3; i < n; i += 2) + if (p[i]) + primes.push_back(i); + } + + vector> getFactors(int n) { + vector> ans; + for (int i = 0; n > 1 && i < (int)primes.size(); i++) { + int cnt = 0; + while (n % primes[i] == 0) { + n /= primes[i]; + cnt++; + } + if (cnt) + ans.emplace_back(primes[i], cnt); + } + if (n > 1) + ans.emplace_back(n, 1); + return ans; + } +} pf(1e5 + 1); + +using int64 = long long; + +class Solution { +public: + long long splitArray(vector &a) { + const auto &p = pf.p; + int64 ans = 0; + for (int i = 0; i < a.size(); i++) { + if (p[i]) + ans += a[i]; + else + ans -= a[i]; + } + return abs(ans); + } +}; From 0b8fa8b1280a544ce7c9a3b71a496461a1fb9fc4 Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Sat, 19 Jul 2025 21:02:09 +0200 Subject: [PATCH 2/8] Add Solution: Count Islands With Total Value Divisible by K --- .../answer.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 count-islands-with-total-value-divisible-by-k/answer.cpp diff --git a/count-islands-with-total-value-divisible-by-k/answer.cpp b/count-islands-with-total-value-divisible-by-k/answer.cpp new file mode 100644 index 0000000..917a69e --- /dev/null +++ b/count-islands-with-total-value-divisible-by-k/answer.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int countIslands(vector> &a, int k) { + int n = a.size(), m = a[0].size(); + vector> vis(n, vector(m, 0)); + + function dfs = [&](int x, int y, int &mod) { + vis[x][y] = 1; + assert(a[x][y] > 0); + mod = (mod + a[x][y]) % k; + if (y + 1 < m and !vis[x][y + 1] and a[x][y + 1] > 0) + dfs(x, y + 1, mod); + if (y > 0 and !vis[x][y - 1] and a[x][y - 1] > 0) + dfs(x, y - 1, mod); + if (x + 1 < n and !vis[x + 1][y] and a[x + 1][y] > 0) + dfs(x + 1, y, mod); + if (x > 0 and !vis[x - 1][y] and a[x - 1][y] > 0) + dfs(x - 1, y, mod); + }; + + int ans = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!vis[i][j] and a[i][j] > 0) { + int mod = 0; + dfs(i, j, mod); + if (mod == 0) + ans++; + } + } + } + return ans; + } +}; From 27f505afed363fa24095afdecee44d3365fbd284 Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Sat, 19 Jul 2025 21:12:06 +0200 Subject: [PATCH 3/8] Add Solution: Network Recovery Pathways --- network-recovery-pathways/answer.cpp | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 network-recovery-pathways/answer.cpp diff --git a/network-recovery-pathways/answer.cpp b/network-recovery-pathways/answer.cpp new file mode 100644 index 0000000..b60d8f9 --- /dev/null +++ b/network-recovery-pathways/answer.cpp @@ -0,0 +1,56 @@ +using int64 = long long; + +template bool umax(T &a, const T b) { return a < b ? a = b, 1 : 0; } + +class Solution { +public: + int findMaxPathScore(vector> &edges, vector &online, + long long k) { + int n = online.size(); + vector>> adj(n); + for (auto e : edges) + adj[e[0]].push_back({e[1], e[2]}); + + vector dis(n); + auto Test = [&](int val) -> bool { + dis.assign(n, LLONG_MAX); + using State = pair; + priority_queue, greater> q; + dis[0] = 0; + q.push({0, 0}); + + while (!q.empty()) { + auto [d, u] = q.top(); + q.pop(); + // skip invalid node + if (dis[u] != d) + continue; + + for (auto [v, weight] : adj[u]) { + if (!online[v]) + continue; + if (weight < val) + continue; + int64 w = dis[u] + weight; + if (w < dis[v]) { + dis[v] = w; + q.push({dis[v], v}); + } + } + } + return dis[n - 1] <= k; + }; + + int l = 0, r = 0, ans = -1; + for (auto e : edges) + umax(r, e[2]); + while (l <= r) { + int mid = (l + r) / 2; + if (Test(mid)) + ans = mid, l = mid + 1; + else + r = mid - 1; + } + return ans; + } +}; From a62ebb52709bc636798ac5a221dd0ff2cc22f51f Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Sat, 19 Jul 2025 22:24:23 +0200 Subject: [PATCH 4/8] Add Solution: Number of Integers With Popcount-Depth Equal to K I --- .../answer.cpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 number-of-integers-with-popcount-depth-equal-to-k-i/answer.cpp diff --git a/number-of-integers-with-popcount-depth-equal-to-k-i/answer.cpp b/number-of-integers-with-popcount-depth-equal-to-k-i/answer.cpp new file mode 100644 index 0000000..3169b83 --- /dev/null +++ b/number-of-integers-with-popcount-depth-equal-to-k-i/answer.cpp @@ -0,0 +1,66 @@ +#define bitCount(n) __builtin_popcountll((n)) + +using int64 = long long; + +class Solution { +public: + std::string toBinary(int64 n) { + if (n == 0) + return "0"; + + std::string s = ""; + while (n > 0) { + s += (n % 2) ? '1' : '0'; + n /= 2; + } + + // Reverse since we built it backwards + std::reverse(s.begin(), s.end()); + return s; + } + + long long popcountDepth(long long n, int k) { + if (k == 0) + return 1; + + vector cnt(65, 0); + cnt[1] = 0; + for (int i = 2; i <= 64; i++) + cnt[i] = cnt[bitCount(i)] + 1; + + string s = toBinary(n); + int m = s.size(); + + // dp[pos][ones][equal] + vector>> dp( + m, vector>(65, vector(2, 0))); + dp[0][0][0] = 1; + dp[0][1][1] = 1; + for (int i = 1; i < m; i++) { + int d = s[i] - '0'; + for (int j = 0; j <= i; j++) { + // choose 0 + dp[i][j][0] += dp[i - 1][j][0]; + if (d == 1) + dp[i][j][0] += dp[i - 1][j][1]; + else + dp[i][j][1] += dp[i - 1][j][1]; + + // choose 1 + dp[i][j + 1][0] += dp[i - 1][j][0]; + if (d == 1) + dp[i][j + 1][1] += dp[i - 1][j][1]; + } + } + + int64 ans = 0; + for (int i = 1; i <= m; i++) { + if (cnt[i] != k - 1) + continue; + ans += dp[m - 1][i][0] + dp[m - 1][i][1]; + } + if (k == 1) + return ans - 1; + return ans; + } +}; From c9c5a2f599f2124c9d0d36031d429f91f199bd35 Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Sun, 20 Jul 2025 19:53:59 +0200 Subject: [PATCH 5/8] Add Solution: Check Divisibility by Digit Sum and Product --- .../answer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 check-divisibility-by-digit-sum-and-product/answer.cpp diff --git a/check-divisibility-by-digit-sum-and-product/answer.cpp b/check-divisibility-by-digit-sum-and-product/answer.cpp new file mode 100644 index 0000000..5747002 --- /dev/null +++ b/check-divisibility-by-digit-sum-and-product/answer.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + bool checkDivisibility(int n) { + int p = 1, sum = 0; + for (int m = n; m > 0; m /= 10) { + sum += m % 10; + p *= m % 10; + } + return n % (sum + p) == 0; + } +}; From aea78971b95173f5429191302f3a301cabfd26da Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Sun, 20 Jul 2025 20:14:19 +0200 Subject: [PATCH 6/8] Add Solution: Number of Integers With Popcount-Depth Equal to K II --- .../answer.cpp | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 number-of-integers-with-popcount-depth-equal-to-k-ii/answer.cpp diff --git a/number-of-integers-with-popcount-depth-equal-to-k-ii/answer.cpp b/number-of-integers-with-popcount-depth-equal-to-k-ii/answer.cpp new file mode 100644 index 0000000..f512eb1 --- /dev/null +++ b/number-of-integers-with-popcount-depth-equal-to-k-ii/answer.cpp @@ -0,0 +1,79 @@ +template struct Fenwick { + vector c; + int n; + + Fenwick(int _n) { init(_n); } + + void init(int _n) { + n = _n; + c.resize(_n + 1); + for (int i = 1; i <= n; i++) + c[i] = 0; + } + + inline int lowbit(int x) { return x & (-x); } + + void update(int x, T val) { + for (int i = x; i <= n; i += lowbit(i)) + c[i] = c[i] + val; + } + + // sum of [1 ... x] + T get(int x) { + assert(0 <= x && x <= n); + T ret = 0; + for (int i = x; i > 0; i -= lowbit(i)) + ret = ret + c[i]; + return ret; + } +}; + +#define bitCount(n) __builtin_popcountll((n)) + +using int64 = long long; + +class Solution { +public: + vector popcountDepth(vector &a, + vector> &queries) { + vector cnt(65, 0); + cnt[1] = 0; + for (int i = 2; i <= 64; i++) + cnt[i] = cnt[bitCount(i)] + 1; + + auto Get = [&](int64 x) -> int { + if (x == 1) + return 0; + return cnt[bitCount(x)] + 1; + }; + + int n = a.size(); + vector> fes(6, Fenwick(n)); + for (int i = 0; i < n; i++) { + int pos = Get(a[i]); + if (pos < 6) + fes[pos].update(i + 1, +1); + } + + int q = queries.size(); + vector ans; + for (auto query : queries) { + if (query[0] == 1) { + int l = query[1] + 1, r = query[2] + 1, k = query[3]; + int res = fes[k].get(r) - fes[k].get(l - 1); + ans.push_back(res); + } else { + int i = query[1]; + int pos = Get(a[i]); + if (pos < 6) + fes[pos].update(i + 1, -1); + + a[i] = query[2]; + pos = Get(a[i]); + if (pos < 6) + fes[pos].update(i + 1, +1); + } + } + return ans; + } +}; From e9a88bf7f12e7781a03d02317098fb58b43c39d6 Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Tue, 29 Jul 2025 21:25:22 +0200 Subject: [PATCH 7/8] Add Solution: Count Number of Trapezoids I --- count-number-of-trapezoids-i/answer.cpp | 147 ++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 count-number-of-trapezoids-i/answer.cpp diff --git a/count-number-of-trapezoids-i/answer.cpp b/count-number-of-trapezoids-i/answer.cpp new file mode 100644 index 0000000..4e8e2de --- /dev/null +++ b/count-number-of-trapezoids-i/answer.cpp @@ -0,0 +1,147 @@ +template struct modnum { + typedef long long int64; + + static constexpr int MOD = MOD_; + static_assert(MOD_ > 0, "MOD must be positive"); + +private: + int v; + static int minv(int a, int m) { + a %= m; + assert(a); + return a == 1 ? 1 : int(m - int64(minv(m, a)) * int64(m) / a); + } + +public: + modnum() : v(0){}; + modnum(int64 v_) : v(int(v_ % MOD)) { + if (v < 0) + v += MOD; + } + explicit operator int() const { return v; } + friend std::ostream &operator<<(std::ostream &out, const modnum &n) { + return out << int(n); + } + friend std::istream &operator>>(std::istream &in, modnum &n) { + int64 v_; + in >> v_; + n = modnum(v_); + return in; + } + friend bool operator==(const modnum &a, const modnum &b) { + return a.v == b.v; + } + friend bool operator!=(const modnum &a, const modnum &b) { + return a.v != b.v; + } + + modnum inv() const { + modnum res; + res.v = minv(v, MOD); + return res; + } + friend modnum inv(const modnum &m) { return m.inv(); } + modnum neg() const { + modnum res; + res.v = v ? MOD - v : 0; + return res; + } + friend modnum neg(const modnum &m) { return m.neg(); } + + modnum pow(int64 m) { + assert(m >= 0); + modnum res = 1, p = modnum(*this); + while (m) { + if (m & 1) + res *= p; + p *= p; + m /= 2; + } + return res; + } + + modnum operator-() const { return neg(); } + modnum operator+() const { return modnum(*this); } + + modnum &operator++() { + ++v; + if (v == MOD) + v = 0; + return *this; + } + modnum &operator--() { + if (v == 0) + v = MOD; + --v; + return *this; + } + modnum &operator+=(const modnum &o) { + v += o.v; + if (v >= MOD) + v -= MOD; + return *this; + } + modnum &operator-=(const modnum &o) { + v -= o.v; + if (v < 0) + v += MOD; + return *this; + } + modnum &operator*=(const modnum &o) { + v = int(int64(v) * int64(o.v) % MOD); + return *this; + } + modnum &operator/=(const modnum &o) { return *this *= o.inv(); } + + friend modnum operator++(modnum &a, int) { + modnum r = a; + ++a; + return r; + } + friend modnum operator--(modnum &a, int) { + modnum r = a; + --a; + return r; + } + friend modnum operator+(const modnum &a, const modnum &b) { + return modnum(a) += b; + } + friend modnum operator-(const modnum &a, const modnum &b) { + return modnum(a) -= b; + } + friend modnum operator*(const modnum &a, const modnum &b) { + return modnum(a) *= b; + } + friend modnum operator/(const modnum &a, const modnum &b) { + return modnum(a) /= b; + } +}; +using mint = modnum<1000000007>; +// using mint = modnum<998244353>; + +class Solution { +public: + int countTrapezoids(vector> &points) { + unordered_map ycnt; + for (auto p : points) + ycnt[p[1]]++; + + mint total = 0; + for (auto [y, cnt] : ycnt) { + if (cnt == 1) + continue; + mint val = mint(cnt) * (cnt - 1) / 2; + total += val; + } + + mint ans = 0; + for (auto [y, cnt] : ycnt) { + if (cnt == 1) + continue; + mint val = mint(cnt) * (cnt - 1) / 2; + ans += val * (total - val); + } + ans /= 2; + return int(ans); + } +}; From 7310efbd3aca9e627a83e7b5590115e53f584143 Mon Sep 17 00:00:00 2001 From: Jiapeng Chen Date: Tue, 29 Jul 2025 21:44:26 +0200 Subject: [PATCH 8/8] Add Solution: Count Number of Trapezoids II --- count-number-of-trapezoids-ii/answer.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 count-number-of-trapezoids-ii/answer.py diff --git a/count-number-of-trapezoids-ii/answer.py b/count-number-of-trapezoids-ii/answer.py new file mode 100644 index 0000000..02674d8 --- /dev/null +++ b/count-number-of-trapezoids-ii/answer.py @@ -0,0 +1,25 @@ +class Solution: + def countTrapezoids(self, A: List[List[int]]) -> int: + slopes = Counter() + lines = Counter() + mids = Counter() + midlines = Counter() + + for (x1, y1), (x2, y2) in combinations(A, 2): + dx, dy = x2 - x1, y2 - y1 + g = gcd(dx, dy) + dx, dy = dx // g, dy // g + if dx < 0 or (dx == 0 and dy < 0): + dx, dy = -dx, -dy + + inter = dx * y1 - dy * x1 + slopes[dx, dy] += 1 + lines[dx, dy, inter] += 1 + mids[x1 + x2, y1 + y2] += 1 + midlines[x1 + x2, y1 + y2, dx, dy, inter] += 1 + + ans = sum(comb(v, 2) for v in slopes.values()) + ans -= sum(comb(v, 2) for v in lines.values()) + ans -= sum(comb(v, 2) for v in mids.values()) + ans += sum(comb(v, 2) for v in midlines.values()) + return ans