Блог пользователя SydneySweeneyFan

Автор SydneySweeneyFan, история, 9 месяцев назад, По-английски

First they came for the Newbies, and I did not speak out— Because I was not a Newbie.

Then they came for the Specialists, and I did not speak out— Because I was not a Specialist.

Then they came for the Masters, and I did not speak out— Because I was not a Masters.

Then they came for the Grandmasters, and I did not speak out— Because I was not a Grandmaster.

Then they came for me—and there was no one left to speak for me.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

Автор SydneySweeneyFan, история, 11 месяцев назад, По-английски

I am exploring the following combinatorial question about binary strings and would like insights, ideas, or references from the community.

For a binary string S of length N, define its border sequence as the strictly increasing sequence of integers x (1 ≤ x ≤ N) such that the prefix of S of length x equals the suffix of S of length x.

Formally: x is in the sequence if S[1..x] = S[N−x+1..N]

Note that x = N is always included.

Example: S = "01010" (N = 5) Border sequence = (1, 3, 5)

Now define f(N) as the number of distinct border sequences that occur among all binary strings of length N. We count only unique sequences, not strings.

https://oeis.org/A005434

Is there an optimal way to find this for given N rather than brute enumeration?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -2
  • Проголосовать: не нравится

Автор SydneySweeneyFan, история, 13 месяцев назад, По-английски

Congrats Dominater069 for becoming LGM and being first Indian LGM ever.

Good Luck for ICPC World Finals Next year, hoping to see first Indian ICPC Medal too soon.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -61
  • Проголосовать: не нравится

Автор SydneySweeneyFan, история, 14 месяцев назад, По-английски

I was thinking about the following problem:

Construct an array $$$A$$$ of positive integers such that exactly $$$K$$$ subsets of $$$A$$$ have $$$\gcd = 1$$$.

Formally, let $$$f(A)$$$ be the number of non-empty subsets $$$S \subseteq A$$$ for which $$$\gcd(S) = 1$$$. The task is to design $$$A$$$ so that $$$f(A) = K$$$.

  • Shorter arrays are preferable.
  • I am not looking for a brute force "search until found" method — I want a clean construction idea.
  • For example, if $$$K = 10^9 + 7$$$, how would you construct such an array?

Also, is it true that such an array always exists for any $$$K$$$? If yes, can we formally prove it?

Any insights or clever constructions would be appreciated!

Полный текст и комментарии »

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

Автор SydneySweeneyFan, история, 17 месяцев назад, По-английски

In the NSUBSTR problem on SPOJ, we are given a string $$$s$$$ of length $$$n$$$, and for every $$$k = 1$$$ to $$$n$$$, we are asked to find the maximum number of times any substring of length $$$k$$$ appears in $$$s$$$ (including overlapping occurrences).


1. Rephrasing the Problem

We need to compute, for each possible substring length $$$k$$$, the highest frequency among all substrings of length $$$k$$$. For example, if $$$s = "ababab"$$$:

  • $$$k=1$$$: substrings are {"a", "b"}, frequencies {3,3} → answer 3.
  • $$$k=2$$$: substrings are {"ab", "ba"}, frequencies {3,2} → answer 3.
  • etc.

A brute-force sliding-window plus hashing approach would be $$$O(n^2)$$$ or worse, which is too slow for $$$n$$$ up to $$$10^5$$$.


2. Key Insight: Suffix Array + LCP

We transform the problem into counting identical substrings by sorting all suffixes and using their common prefixes.

  1. Suffix Array: an array $$$sa$$$ of length $n$ that lists the starting positions of all suffixes of $s$ in lexicographical order.
  2. LCP Array: an array $$$lcp$$$ of length $n-1$ where $$$lcp[i]$$$ = length of the longest common prefix of the suffixes starting at $$$sa[i]$$$ and $$$sa[i+1]$$$.

Once we have $$$sa$$$ and $$$lcp$$$, any group of $$$c$$$ consecutive suffixes in sorted order share a common prefix of length at least $$$L = \min(lcp[i..i+c-2])$$$. That prefix corresponds to some substring of length $L$ occurring $$$c$$$ times.


3. From LCP to Maximum Frequencies

Let’s imagine the $$$lcp$$$ array as heights of bars in a histogram of size $$$n-1$$$. For each index $$$i$$$:

  • The bar at $$$i$$$ has height $$$h = lcp[i]$$$.
  • We want to know the largest span $$$[L[i], R[i]]$$$ around $$$i$$$ where all bars are at least height $h$.
  • That span corresponds to $$$c = (R[i] - L[i] + 2)$$$ suffixes sharing a common prefix of length $$$h$$$.

So for each $$$i$$$, we can update:

$$$ ans[h] = \max(ans[h], c) \quad\text{where } c = R[i]-L[i]+2. $$$

After processing all $$$i$$$, we have for each exact length $$$h$$$ the maximum count of some substring of length $$$h$$$. However, note that if a substring of length $$$h$$$ appears $$$c$$$ times, then all its prefixes (length $$$ \lt h$$$) appear at least $$$c$$$ times. Thus we perform a final pass:

$$$ \text{for } x = n-1 \downarrow 1:\quad ans[x] = \max(ans[x], ans[x+1]). $$$

This ensures the answer is non-decreasing as substring length shrinks.


4. Efficient Computation of $$$L[i], R[i]$$$

To find, for each bar $$$i$$$, the next smaller element on the left and right, we use a classic monotonic stack in two passes:

  1. Left pass: iterate $$$i=0$$$ to $$$n-2$$$, maintain a stack of indices with strictly increasing $$$lcp$$$ heights. When $$$lcp[stack.top()] \ge lcp[i]$$$, pop. Then:
  • If stack empty, set $$$L[i]=0$$$; else $$$L[i]=stack.top()+1$$$.
  • Push $$$i$$$.
  1. Right pass: similarly from $$$i=n-2$$$ down to 0, find next smaller on the right and set:
  • If stack empty, $$$R[i]=n-2$$$; else $$$R[i]=stack.top()-1$$$.

Each index is pushed/popped at most once ⇒ $$$O(n)$$$ total.


5. Putting It All Together

Optimal C++ Solution

6. Suffix Array & LCP: A Quick Primer

  • Suffix Array in $$$O(n\log n)$$$: sort cyclic shifts by doubling technique (or SA-IS in $$$O(n)$$$).
  • LCP Array in $$$O(n)$$$ with Kasai’s algorithm: scans adjacent suffixes, reuses previous LCP to avoid recomputation.

If you’re unfamiliar, many tutorials walk through building suffix arrays by sorting starting characters, then doubling the length each round, reassigning classes.


7. Complexity

  • Building SA: $$$O(n\log n)$$$
  • Building LCP: $$$O(n)$$$
  • Two stack passes: $$$O(n)$$$
  • Final propagation & output: $$$O(n)$$$

Overall: $$$O(n\log n)$$$, suitable for $$$n$$$ up to $$$10^5$$$.


8. Conclusion

By reducing the substring-frequency problem to histogram spans over the LCP array, we elegantly count the maximum occurrences of each length in $$$O(n\log n)$$$. This approach highlights the power of suffix arrays and range queries with stacks for common-prefix problems.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

Автор SydneySweeneyFan, история, 17 месяцев назад, По-английски

Introduction

In this article I’ll write about techniques in black box linear algebra that are useful in competitive programming—especially the approach due to Wiedemann.


Motivation

Counting problems form one large genre in competitive programming. In many cases we compute answers mod a prime like 1,000,000,007; here we’ll focus on the prime-modulus case.

Among the many ways to solve counting problems, one major family uses matrices—i.e. linear algebra. Two representative examples we’ll cover:

  1. Speeding up DP via matrix exponentiation
  2. Fast determinant computation

1. Speeding Up DP with Matrix Exponentiation

Matrix exponentiation is extremely common in counting problems. We encode the state‑transition as a matrix. If the initial state is a vector b and the transition matrix is A, then after K steps the state is

$$$ A^K b. $$$

Using binary exponentiation, this takes

$$$ \Theta(N^3 \log K) $$$

time for an $$$N\times N$$$ matrix.

Sometimes special structure lets us do even better. For example, if A is circulant, multiply two such matrices in $$$O(N^2)$$$ (or faster via FFT‑based polynomial multiplication), and exponentiation in $$$O(N^2\log K)$$$. Triangular Toeplitz matrices also admit similar speedups.

In this article we’ll vastly generalize these special‑case speedups to get faster “general” matrix exponentiation.


2. Computing Determinants Quickly

Determinant computations also appear frequently in contests. By Gaussian elimination one can compute $$$\det(A)$$$ of an $$$N\times N$$$ matrix in $$$\Theta(N^3)$$$. Special cases—e.g. a banded matrix with bandwidth $$$K$$$ — drop to $$$\Theta(N K^2)$$$.

We’ll show how to speed up determinant calculation for a much larger class of matrices.


Preliminaries

Linear Recurrences and Their Minimal Polynomial

Let $$$V$$$ be a vector space over a field $$$\mathbb F$$$. An infinite sequence $$${a_i}_{i=0}^\infty$$$ in $$$V$$$ is linearly recurrent if there is a polynomial

$$$ c(X) = \sum_{j=0}^n c_j\,X^j \quad(c_j \in \mathbb F) $$$

such that for every $$$i\ge0$$$,

$$$ \sum_{j=0}^n c_j\,a_{i+j} \;=\;0. $$$

We say $$$c$$$ annihilates the sequence. The set of all annihilating polynomials is an ideal in $$$\mathbb F[X]$$$, and since $$$\mathbb F[X]$$$ is a PID there is a unique monic generator (unless the ideal is zero—in which case we take the zero polynomial). That generator is the minimal polynomial of the sequence.

If $$$f$$$ is a linear map between two vector spaces, then the minimal polynomial of $$$f(a)$$$ divides that of $$$a$$$.


Matrix Minimal Polynomial vs. Characteristic Polynomial

For a square matrix $$$A$$$, consider the sequence of powers $$${A^i}_{i=0}^\infty$$$. Its minimal polynomial is called the minimal polynomial of $$$A$$$. The characteristic polynomial is

$$$ \chi_A(X) = \det(XI - A). $$$

By Cayley–Hamilton, $$$A$$$ satisfies its own characteristic polynomial, so the minimal polynomial divides the characteristic polynomial, and its degree is at most $$$N$$$. Moreover, they share the same set of roots (eigenvalues).


Finding the Minimal Polynomial

Scalar Sequences

If the input sequence lives in the field $$$\mathbb F$$$, one can use the Extended Euclidean Algorithm or the Berlekamp–Massey algorithm to compute the minimal polynomial in $$$O(n^2)$$$ time. Berlekamp–Massey is especially simple to implement.

Vector Sequences

If the sequence $$${b_i}$$$ lives in $$$\mathbb F^m$$$, pick a random vector $$$u\in\mathbb F^m$$$ by selecting each coordinate from some finite subset $$$U\subset\mathbb F$$$. Compute the scalar sequence

$$$ a_i = u^\top\,b_i. $$$

With high probability the minimal polynomial of $$${a_i}$$$ equals that of $$${b_i}$$$. This takes $$$O(mn + n^2)$$$ time overall.

Matrix Case

To find the minimal polynomial of an $$$N\times N$$$ matrix $$$A$$$, pick a random vector $$$b$$$ and form the sequence $$${A^i b}_{i=0}^{2N-1}$$$. Reduce to the vector case above, giving high‑probability correctness in $$$O(N^2 + N\,T(N))$$$ time, where $$$T(N)$$$ is the cost of multiplying $$$A$$$ by a vector.


Applications of the Minimal Polynomial

Fast General Matrix Exponentiation

Given $$$A$$$, $$$b$$$, and $$$K$$$, we want $$$A^K b$$$. Because the sequence $$${A^i b}$$$ is linearly recurrent, let its minimal polynomial be $$$c(X)$$$. Then using polynomial‑recurrence techniques, one can express the $$$K$$$-th term as a linear combination of the first $$$N$$$ terms. Computing that takes

$$$ O\bigl(N^2 + N\,T(N) + M(N)\log K\bigr), $$$

where $$$M(N)$$$ is the cost of multiplying degree-$$$N$$$ polynomials. For a dense matrix ($$$T(N)=\Theta(N^2)$$$), this is $$$O(N^3 + M(N)\log K)$$$, which beats plain exponentiation. For sparse or structured matrices it can be much faster.


Fast Determinant Computation

Recall $$$\det(A) = (-1)^N \chi_A(0)$$$. If the minimal polynomial has degree $$$N$$$, it equals the characteristic polynomial. Otherwise, we “preprocess” $$$A$$$ so that its minimal polynomial jumps to degree $$$N$$$. Concretely, if $$$A$$$ is nonsingular, choose a random diagonal matrix $$$D$$$ with $$$\det(D)\neq 0$$$. With high probability the minimal polynomial of $$$AD$$$ has degree $$$N$$$. Compute its minimal polynomial, then recover

$$$ \det(A) = \frac{\det(AD)}{\det(D)}. $$$

This takes $$$O(N^2 + N\,T(N))$$$ time.


Conclusion

Even when a matrix isn’t given explicitly, as long as you have a “black box” that multiplies any vector by that matrix, these algorithms work. Such methods fall under black box linear algebra.

Beyond what we’ve covered, many more black‑box algorithms exist—for example, solving linear systems $$$Ax=b$$$ quickly via similar ideas.

In competitive programming, linear‑algebraic problems are abundant. Even if this approach isn’t the “intended” solution, it’s a powerful tool worth mastering.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится