poltomo
project by varun
Tuesday, August 19, 2025
King's Summit
Friday, August 1, 2025
Codeforces union find problem involving cycles
Codeforces Round 1040 (Div. 2) Problem C
Observations
Given n ranges, the problem involves maximally covering the range [1, 2n] with those ranges, while jointly minimizing cycles between the endpoints of those ranges/ undirected edges. [1, 2n] will be maximally covered by taking all ranges, but this does not necessarily avoid cycles. The important observation is that all cycles have a minimum and maximum point, e.g. 1 and 4 in S = {(1, 2), (2, 4), (1, 4)}. There are always two "paths" to get from the minimum to the maximum. In the case of S, {(1, 2), (2, 4)} or {(1, 4)}. Getting from the minimum to the maximum while avoiding a cycle is then just a matter of take one path and only one path.
Take ranges/ undirected edges greedily. Only take an edge (a, b) if b is not currently reachable from a, i.e. the other "path" has not already been taken
Thursday, July 24, 2025
Newton's method and the Jacobi method similarity part 1
Solve f(x) = 0. f is once differentiable.
1. f(x) = 0
2. f(x) + Jf(x)(x - x) = 0
Jf(x) is the jacobian of f evaluated at x.
3. f(xt) + Jf(xt)(xt+1 - xt) = 0
4. Jf(xt)(xt+1 - xt) = -f(xt)
Finding the step (xt+1 - xt) involves solving the system of linear equations Jf(xt)(xt+1 - xt) = -f(xt). This iterative scheme for solving nonlinear systems of equations is Newton's method.
The Jacobi method is also an iterative method, but its for solving diagonally dominant systems of linear equations.
Solve Ax = b. A is diagonally dominant.
1. Ax = b
2. (D + L + U)x = b
D is the diagonal of A. L is the lower triangular portion of A. U is the upper triangular portion of A.
3. Dxt+1 = b - Lxt - Uxt
4. xt+1 = D-1(b - (L+U)xt)
Notice the similarity between steps two and three in the derivation of Jacobi's method and Newton's method. Step two exploits an expansion/ decomposition, and then a "leap" is made to go from a system of variable(s) x to a system of xt+1 and xt.
But surely, one can't just use any decomposition they'd like. The iterative method has to be consistent and convergent.
In the next post, I will prove that this is the case for both methods, and then go back to these derivations to recover the train of thought that lead to performing step two and three in the first place. This will allow me to contrive whatever iterative methods, I would like. This is very important, because many systems of equations and optimization programs exhibit lots of structure that can be exploited.
Tuesday, July 8, 2025
Distinct Values Subsequences
#include<iostream>
#include<unordered_map>
using namespace std;
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
int main() {
const long long MOD = 1000000007;
int n;
cin >> n;
unordered_map<int, int> freq;
long long count = 1;
for (int i = 0;i < n;++i) {
int x;
cin >> x;
count = count + (count * binpow(freq[x] + 1, MOD - 2, MOD) % MOD) % MOD;
++freq[x];
}
count = count - 1 % MOD;
cout << count << endl;
}
Monday, July 7, 2025
Distinct Values Subarrays
observations
1. The number of subarrays of an array of length n is n(n + 1)/2. If each number in the array appears once, then this is the answer.
2. What if you find a covering of the array into disjoint segments each with distinct numbers, e.g. [1, 2, 3, 1] becomes [1, 2, 3], [1]. Can you apply the formula independently to the segments and sum the results? No, this fails to count segments that cross the split and would underestimate the correct answer. Also, such a covering might not be unique.
3. What if you forego disjoint-ness and expand the segments as much as possible while keeping the numbers distinct: [1, 2, 3, 1] becomes [1, 2, 3], [2, 3, 1]. This covering is unique, but now you overestimate the correct answer because the segments overlap. , that the amount of overshoot is equal to the number of subarrays of [2, 3]. A nice expression for the correct number of subarrays follows from this observation resembling the inclusion-exclusion principle formula.
4. If x1, ..., xk are distinct, but x1, ..., xk, xk+1 are not distinct, then one of the numbers in x1, ..., xk is xk+1. If xj = xk+1 with 1 ≤ j ≤ k, then xj+1, ..., xk+1 is distinct. This means that storing the index of incidence for each number in x1, ..., xk is enough to find out the start of the second segment in the construction from observation three.
You can compute the expression from observation three in a sliding window fashion by exploiting the structure from observation four.