adamant's blog

By adamant, history, 13 months ago, In English

Hi everyone!

As the Universal Cup stage 7 which mirrored my contest from the Osijek Competitive Programming Camp 2023 just concluded, I decided to also upload the contest to gym as OCPC 2023, Oleksandr Kulkov Contest 3, and post the tutorial here. I hope you enjoyed the contest!

My previous ICPC-style contests:

Compiled PDF Tutorial for all problems: link.

To make it worth a separate blog post, I will also add some brief meta comments :)

P.S. Congratulations to the team USA1 (ecnerwala, ksun48, scott_wu) for solving all the problems in-contest!

Full text and comments »

  • Vote: I like it
  • +76
  • Vote: I do not like it

By adamant, history, 13 months ago, In English

Hello everyone! The 7th Stage of the 1st Universal Cup: Zaporizhzhia, will be held on March 11th, 2023.

The contest is based on the Day 2: Oleksandr Kulkov Contest 3 from the Osijek Competitive Programming Camp 2023. Please, don't participate if you have seen these problems.

Authors of the problems: adamant, fedimser, I would also like to thank -is-this-fft- for the help with the preparation of the contest.

We hope you will like the problems!

You can participate in the contest in the following three time windows:

  • Mar 11th 13:00 — 18:00 (UTC +8)
  • Mar 11th 19:00 — 24:00 (UTC +8)
  • Mar 12th 02:00 — 07:00 (UTC +8)

Please note that you can see two scoreboards in DOMjudge. The 'Local Scoreboard' shows the standings ONLY IN THE CURRENT TIME WINDOW. And the 'Combined Scoreboard' shows all participants, including the onsite participants, and the cup participants in the previous time windows.

Contest link: https://domjudge.qoj.ac/

Universal Cup Scoreboard: https://qoj.ac/ucup/scoreboard

About Universal Cup:

Universal Cup is a non-profit organization dedicated to providing trainings for competitive programming teams. Up to now, there are more than 200 teams from all over the world registering for Universal Cup.

A more detailed introduction: https://codeforces.com/blog/entry/111672

Register a new team: https://ucup.ac/register

Full text and comments »

  • Vote: I like it
  • +247
  • Vote: I do not like it

By adamant, history, 13 months ago, In English

Hi everyone!

Sponsored by

The Osijek competitive programming camp (also see the announcement on Codeforces) just concluded last Sunday, on February 26, and I'd like to write this blog post as a wrap to this winter's iteration of the camp. Overall, 74 teams joined the camp, and 13 of them attended the camp onsite in Osijek. Tähvend (-is-this-fft-) and I (adamant), as organizers, were also there!

Thanks to pauldiac for all the photos!

The camp consisted of 7 contests, and 2 days off. On the first day off, the onsite participants had an opportunity to go to bowling, and on the second day off, to play paintball (see the photos). As for the contests, two of them were shared with the Universal Cup, so that our participants didn't have to choose what to attend between the camp contests and the UniCup. On top of that, three other contests will be used in the Universal Cup for later stages. Here is a brief contest summary:

Authors Contest Scoreboard UniCup
antontrygubO_o Day 1: Anton Trygub Contest scoreboard Stage 4: Ukraine
adamant, fedimser Day 2: Oleksandr Kulkov Contest 3 scoreboard Zaporizhia stage
rivalq Day 4: Jatin Garg Contest scoreboard
Adam_GS, ToxicPie9 Day 5: Adam Gąsienica-Samek Contest scoreboard
jqdai0815 Day 6: Yuhao Du Contest 11 scoreboard Zhejiang stage
conqueror_of_tourist Day 8: Dilhan Salgado Contest scoreboard Stage 5: Osijek
Savior-of-Cross Day 9: Magical Story of LaLa scoreboard Ranoa stage

Combined scoreboard, also ITMO rating table (thanks, awoo!). Congratulations to the top teams (by ITMO score):

And also to the top onsite teams (by ITMO score):

I can't express enough how happy I am that the idea worked out, and we gathered so many cool people to participate in this! I'm very optimistic towards the idea of conducting it again sometime, and as such I wanted to make an announcement:

Full text and comments »

  • Vote: I like it
  • +328
  • Vote: I do not like it

By adamant, history, 13 months ago, In English

Hi everyone!

This is the blog for infinite sums. For finite sums of power, see here.

As everybody knows, it is true that

$$$ \boxed{1+2+3+4+\dots = -\frac{1}{12}} $$$

Moreover, it is widely known that

$$$ \boxed{1+1+1+1+\dots = -\frac{1}{2}} $$$

Historically, it seems that the result was first discovered by Ramanujan, who described it in one of his letters to G. Hardy as

$$$ \text{"If I tell you this you will at once point out to me the lunatic asylum as my goal"} $$$

In this blog, I want to explain how this summation could be easily done with the competitive programmer's favorite tool: generating functions. Moreover, I will explain how to compute the infinite sums of form $$$1^k + 2^k + 3^k + 4^k + \dots = S_k$$$ for any non-negative integer $$$k$$$.

Full text and comments »

  • Vote: I like it
  • +68
  • Vote: I do not like it

By adamant, history, 14 months ago, In English

Hi everyone!

In my previous blog, I wrote about how generating functions can be used to enumerated labeled species. In this blog, I want to continue the topic by writing about how one can account for different kinds of symmetries when counting different combinatorial structures. Ultimately, we will end up deriving and hopefully understanding the analogue of Pólya enumeration theorem in species.

Difficulty: ★★★★☆

Prerequisites:

  • Familiarity with combinatorial species (see my prev. blog), OR
  • Very good intuition with enumerative combinatorics, genfuncs and recap below.

I will try to use plain language rather than formulas as much as possible, as it seems to be the preferred format for readers.

Recap

Below is a very brief recap of the most important things from the previous article. I tried to keep it as informal as possible.

Previously on combinatorial species

If $$$A(x)$$$ and $$$B(x)$$$ are the generating functions for species $$$A$$$ and $$$B$$$, then $$$A(B(x))$$$ is the generating function for their composition.

It is a fundamental result for labeled species. Unfortunately, there is no simple analogue with unlabeled structures, and deriving the composition formula in some reasonable formulation for them is the ultimate goal of this article.

Full text and comments »

  • Vote: I like it
  • +177
  • Vote: I do not like it

By adamant, history, 14 months ago, In English

Hi everyone!

This is yet another blog that I had drafted for quite some time, but was reluctant to publish. I decided to dig it up and complete to a more or less comprehensive state for the $300 contest.

Essentially, the blog tells how to combine CDQ technique for relaxed polynomial multiplication ("online FFT") with linearization technique from Newton method (similar approach is used in the first example of the ODE blog post by jqdai0815), so that the functions that typically require Newton's method can be computed online as well. I will try to briefly cover the general idea of "online FFT" too and provide some examples, in case you're not well familiar with it. That being said...

Full text and comments »

  • Vote: I like it
  • +161
  • Vote: I do not like it

By adamant, history, 15 months ago, In English

Hi everyone!

I had this blog drafter for quite a while, but the $300 contest motivated me to actually finish and publish it. I don't care about the prize that much as I think many other blogs published recently are talking about something even cooler. But I like to make people suffer, so I will apply anyway to make peltorator read even more stuff >:)

Besides, the contest makes it harder to stay on contribution top, so I got to do something to stay afloat. That being said...


Today I'd like to write about an algorithm that solves the following problem:

You're given a set of permutations $$$G = \{g_1, \dots, g_m\}$$$. Find the size $$$|\langle g_1, \dots, g_m \rangle|$$$ of the group generated by $$$G$$$.

At first, I wanted to get into a lot of group-theoretic details with proofs and all, but then I felt like it would make the article harder to comprehend, so I will try to keep group-theoretic stuff to minimum, while telling more about the high-level idea.

Prerequisites

It is strongly recommended to be familiar with some simpler basis constructing algorithms, e.g. Gauss elimination or XOR basis.

Familiarity with basic group theory (e.g. definitions) will also be beneficial.

Full text and comments »

  • Vote: I like it
  • +226
  • Vote: I do not like it

By adamant, history, 15 months ago, In English

Hi everyone!

I am happy to announce the Osijek Competitive Programming Camp that is scheduled to happen on 18-26 February 2023 in Osijek, Croatia. The camp is hosted by the Department of Mathematics at J. J. Strossmayer University of Osijek and is brought to you by the organizing team of adamant (that's me!), -is-this-fft-, mblazev, Tomx and antontrygubO_o.

The camp is inspired by various competitive programming camps that we attended during our active years in ICPC, and is aimed to help college students prepare for ICPC regional contests and finals. The camp will consist of 7 ICPC-style contests and 2 days off.

Up until now, most top competitive programming training camps have been organised in Russia, or by Russian entities. Due to the current situation in the world, attending such camps is not possible or desirable for a large number of participants, and we hope to offer a European alternative to Russian camps.

This is the first edition of the camp, and we all are very excited about it and hope that we may see you soon!

Details

Participation fee for onsite participants is 150€ per person. It does not include regular meals, travel or accommodation.

If you want to participate, but are unable to come onsite, we offer a reduced fee of 100€ per person for online participation. It is also possible to reduce fees individually if you are unable to attend some of the contests (e.g. because of the overlap with SWERC).

We support and empathize with those affected by the ongoing war in Ukraine, therefore we offer a 100€ discount for affected individuals and teams affiliated with Ukrainian institutions. In other words, the fees would be 50€ and 0€ per person for onsite and online participation correspondingly.

The expected starting time for the contests is 10am CET. For online participants, it is still a preferred starting time, but if it is not an option for you, we will also offer an opportunity to compete virtually within 24 hours after the main window is over.

Participants

If you are interested in participating, please fill the form here.

We ask you to register before February 10 if you want to participate online and before February 4 if you want to participate onsite.

If you want to participate onsite, especially if you require visa support, we would urge you to register as soon as possible, as visa processing may take 30+ days.

Also, if your university has a lively ICPC community that may be interested in attending the camp, and you have some contacts of people in charge (e.g. coaches) we would highly appreciate if you could fill the form here, so that we can send an invitation. Thanks!

Problemsetters

We'd like to praise the authors of the contests for the camp:

Sponsors

Last but not least, we would like to say special thanks to our sponsors, who make the camp possible:

If you are interested in sponsoring next editions of the camp or have any questions, please feel free to reach out at [email protected].

Finally, we would also like to thank Codeforces for guidance and promoting this announcement to the main page, eolymp for providing us an online judge for the contests and the Department of Mathematics at J. J. Strossmayer University of Osijek for all their organizational support and providing us a physical location to conduct the camp.

Full text and comments »

  • Vote: I like it
  • +465
  • Vote: I do not like it

By adamant, history, 17 months ago, In English

Hi everyone!

Today I saw a discussion in AC Discord server about how many problems some people made for different competitions. It was sparkled by this CF entry. I haven't keep track of this before, so it made me curious to go through everything that I made and publish in a comprehensive list. I'm doing it mostly out of curiosity, but maybe it might be interesting for someone else too :)

Full text and comments »

  • Vote: I like it
  • +104
  • Vote: I do not like it

By adamant, history, 19 months ago, In English

Hi everyone!

Today, I participate in AtCoder contest, which I quite rarely do. Of course, my performance was quite poor, but I stumbled with quite nice idea along the way. To those who didn't read it, the problem goes as follows:

AGC 058 — Yet Another ABC String. How many ternary strings are there such that they contain exactly $$$A$$$ characters a, exactly $$$B$$$ characters b and exactly $$$C$$$ characters c, and do not have any sub-string that is a cyclic shift of abc?

My approach is very different from the one in the editorial, and seems insightful to me, so I decided to share it in detail.

Besides, I can't reject the request by Um_nik to explain it further :D

Full text and comments »

  • Vote: I like it
  • +163
  • Vote: I do not like it

By adamant, history, 20 months ago, In English

Hi everyone!

Previously, I wrote a general introduction to linear programming duality. In this blog, I would like to write about several problems that could be solved with this technique. Familiarity with the first blog, or general knowledge of dual problems and how to construct them is generally expected to navigate in this one.

Thanks to brunovsky and Golovanov399 for problem suggestions!

And particularly special thanks to WeakestTopology for problem suggestions and all insightful discussions on the topic!

Full text and comments »

  • Vote: I like it
  • +169
  • Vote: I do not like it

By adamant, history, 20 months ago, In English

Hi everyone!

There is a well-known result about bipartite graphs which is formulated as follows:

In any bipartite graph, the number of edges in a maximum matching equals the number of vertices in a minimum vertex cover.

Knowing it you can easily find the size of a minimum vertex cover in a given bipartite graph. But what about actually recovering such cover? Well... There is an algorithm to it, but it's lengthy and not very motivated, so I constantly forget its details. However, there is a simple alternative, which is easily reproduced from scratch, even if you forget specific details.

Besides, there is another well-known result which is formulated as follows:

A bipartite graph has a perfect matching if and only if every subset $$$\alpha$$$ of one part has a neighborhood $$$\beta$$$ in the other part such that $$$|\beta| \geq |\alpha|$$$.

This result can be proven with a similar approach. It is likely known to those familiar with the topic, but still might be of interest to someone.

Full text and comments »

  • Vote: I like it
  • +135
  • Vote: I do not like it

By adamant, history, 20 months ago, In English

Hi everyone!

Previously I wrote about theoretical grounds of "aliens trick". Specifically, I introduced the concept of the Lagrangian duality and explained its connection with the trick. Today I want to elaborate a bit more on the concept of dual problems and their applications to linear programming as well as to common problems in competitive programming.

I initially wanted to also write about some applications in competitive programming problems, but given that the introduction is already quite lengthy, I decided to leave it for another blog, while using most common and well-known theoretical examples here, focusing more on how to construct and interpret dual problems to begin with, rather than how to use it in contests.

I think, it is a crucial first step towards using the duality in actual programming challenges.

Prerequisites

It is highly recommended to have some general understanding of basic mathematical optimization concepts (e.g. convexity, Lagrange multipliers), as well as basic understanding of flow algorithms and most well-known results around them, such as cycle-path decomposition of flows, maximum flow / minimum cut duality, minimum cost flows (ideally, the algorithm that computes it using Dijkstra with potentials). The article should hopefully shed some further light on these topics.

Full text and comments »

  • Vote: I like it
  • +250
  • Vote: I do not like it

By adamant, history, 21 month(s) ago, In English

Hi everyone!

Here's another collection of little tricks and general ideas that might make your life better (or maybe worse).

Most of them are somewhat well-known, but perhaps would still be useful to someone.

Full text and comments »

  • Vote: I like it
  • +214
  • Vote: I do not like it

By adamant, history, 21 month(s) ago, In English

Hi everyone!

In today's blog I'd like to write about the theorem that allows to efficiently solve the following problems:

  • What is the number of partitions of a non-negative number $$$n$$$ for $$$n \leq 10^5$$$?
  • What is the number of partitions of a non-negative number $$$n$$$ for $$$n \leq 10^5$$$, so that no summand repeats $$$d$$$ or more times?
  • What is the number of partitions of a non-negative number $$$n$$$ for $$$n \leq 10^5$$$, so that no summand is divisible by $$$d$$$?
  • HackerEarth — Perfect Permutations: What is the number of permutations of length $$$n$$$ with $$$k$$$ inversions for $$$k \leq n \leq 10^5$$$?
  • 102354E - Decimal Expansion: Let $$$\phi = \frac{9}{10} \cdot \frac{99}{100} \cdot \frac{999}{1000} \cdot \dots$$$, what is the $$$k$$$-th digit of $$$\phi$$$ for $$$k \leq 10^{18}$$$?
  • SNSS-2018 Round 5 — Investment Portfolio: You have $$$2n$$$ dollars and there are $$$n$$$ types of products in the market. There are $$$a_i$$$ instances of the $$$i$$$-th product, costing $$$i$$$ dollars each, such that $$$a_i > a_j$$$ when $$$i > j$$$. Besides that, there are also $$$m$$$ individual offers, $$$j$$$-th of them costing $$$b_j$$$. How many ways are there to spend all $$$2n$$$ dollars, while taking exactly one individual offer for $$$n, m \leq 10^5$$$?

The core fact that allows us to solve these problems efficiently is the pentagonal number theorem:

$$$ \large \boxed{\prod\limits_{k=1}^\infty (1-x^k) = 1 + \sum\limits_{k=1}^\infty (-1)^k\left(x^{\frac{k(3k+1)}{2}} + x^{\frac{k(3k-1)}{2}}\right)} $$$

Prerequisites

  • Familiarity with generating functions, formal power series and operations on them.
  • Basic notion of combinatorics.
  • Basic notion of number theory.

Full text and comments »

  • Vote: I like it
  • +172
  • Vote: I do not like it

By adamant, history, 21 month(s) ago, In English

Hi everyone!

There is a concept that is sometimes mentioned here and there. The Lagrange inversion theorem.

Let $$$x = f(y)$$$. We want to solve this equation for $$$y$$$. In other words, we want to find a function $$$g$$$ such that $$$y = g(x)$$$.

The Lagrange inversion theorem gives an explicit formula for the coefficients of $$$g(x)$$$ as a formal power series over $$$\mathbb K$$$:

$$$ \large k[x^k] g(x) = [x^{-1}] f^{-k} $$$

In a special case $$$y = x \phi(y)$$$, that is $$$f(y) = \frac{y}{\phi(y)}$$$, which is common in combinatorics, it can also be formulated as

$$$ \large k[x^k] g(x) = [x^{k-1}] \phi^k $$$

Finally, to avoid division by $$$k$$$ one may use (see the comment by Elegia) the following formula:

$$$ \large [x^k]g(x) = [x^{-2}] f' f^{-(k+1)}, $$$

which may be formulated for $$$y = x \phi(y)$$$ as

$$$ \large [x^k] g(x) = [x^{k-1}] (\phi - x \phi')\phi^{k-1}. $$$

Prerequisites

Familiarity with the following:

It is required to have knowledge in the following:

  • Polynomials, formal power series and generating functions;
  • Basic notion of analytic functions (e.g. Taylor series);
  • Basic concepts of graph theory (graphs, trees, etc);
  • Basic concepts of set theory (describing graphs, trees, etc as sets, tuples, etc).

I mention the concept of fields, but you're not required to know them to understand the article. If you're not familiar with the notion of fields, assume that we're working in real numbers, that is, $$$\mathbb K = \mathbb R$$$.

It is recommended (but not required) to be familiar with combinatorial species, as they provide lots of intuition to how the equations on generating functions below are constructed.

Full text and comments »

  • Vote: I like it
  • +101
  • Vote: I do not like it

By adamant, history, 21 month(s) ago, In English

Hi everyone!

The usage of generating functions is quite common in competitive programming these days. But it seems to happen so, that they're mostly presented as a way to simplify formal manipulation with polynomial coefficients, rather than something meaningful. But there actually is a meaning to all of this, and today I'm going to shed a light on it.

Alongside the blog post we'll uncover the combinatorial meaning of

  • The addition $$$F(x)+G(x)$$$,
  • The multiplication $$$F(x)G(x)$$$,
  • The exponent $$$\exp F(x)$$$,
  • The logarithm $$$\log F(x)$$$,
  • The sum of the infinite geometric progression $$$\frac{1}{1-F(x)}=1+F(x)+F^2(x)+\dots$$$,
  • The general composition $$$F(G(x))$$$

for the generating functions $$$F(x)$$$ and $$$G(x)$$$ and the underlying structures they represent.

Prerequisites

  • Basic notion of set theory (cardinality of sets, mappings, bijections, cartesian product, disjoint union, etc);
  • Polynomials and formal power series (representation, convolution formula, power series definitions of $$$\exp$$$ and $$$\log$$$);
  • Elementary combinatorics (combinations, partitions, etc).

Although combinatorial species are defined with category theory concepts, I will try to avoid them for simplicity. However, knowledge of some category theory concepts would greatly simplify the reading experience.

Some of definitions use pretty dense mathematical notation, I will follow up with informal explanation right away when it is the case.

Commutative diagrams

I will also use commutative diagrams to illustrate complicated identities. You may use the explanation below as a reference.

Explanation

Every diagram is also provided with a more verbose text description, so don't be afraid if the concept seems complicated.

Full text and comments »

  • Vote: I like it
  • +380
  • Vote: I do not like it

By adamant, history, 22 months ago, In English

Hi everyone!

There was once a problem named "Easy When You Know How". Unfortunately, I can't remember the contest it originated from.

You're given an array $$$a_0, a_1, \dots, a_{2^n-1}$$$. Answer $$$q$$$ queries that are:

  1. Given $$$m$$$, compute the sum over all $$$a_i$$$ such that $$$i$$$ is a submask of $$$m$$$;
  2. Given $$$m$$$ and $$$c$$$, add $$$c$$$ to all $$$a_m$$$.

Constraints were along the lines of $$$2^n, q \leq 10^5$$$.

I tried to find anything about this technique on Codeforces, but failed, so I thought it'd be nice to write a brief blog about this cute problem.

Full text and comments »

  • Vote: I like it
  • +105
  • Vote: I do not like it

By adamant, history, 22 months ago, In English

Hi everyone!

Today I'd like to write about the so-called Grundy numbers, or nimbers. I will start by providing a formal recap on the Sprague-Grundy theorem and then will advance to the topic that is rarely covered in competitive programming resources, that is I will write about nimber product and its meaning to the game theory.

I was asked to add the following to the blog: THANK SIR MASTER Adam_GS FOR GREAT AND VALUABLE REVIEW SIR

Prerequisites

Familiarity with the following concepts:

Familiarity with formal computational models (e.g. deterministic finite automata) is not required, but would be helpful.

Although the nimbers are typically constructed on top of ordinal numbers and transfinite nim, I will try to stick with natural numbers and classic nim. You may familiarize yourself with transfinite nim in e.g. this article by emorgan.

Full text and comments »

  • Vote: I like it
  • +204
  • Vote: I do not like it

By adamant, history, 22 months ago, In English

Hi everyone!

Let $$$R$$$ be a ring, $$$d_0, d_1, d_2, \dots \in R$$$ and $$$e_0, e_1, e_2, \dots \in R$$$ be linear recurrence sequences, such that

$$$\begin{gather} d_m = \sum\limits_{i=1}^k a_i d_{m-i}\text{ for }m \geq k, \\ e_m = \sum\limits_{i=1}^l b_i e_{m-i}\text{ for }m \geq l. \end{gather}$$$

In some applications, the following two sequences arise:

$$$\begin{gather} f_k &=& d_k e_k & \text{(Hadamard product)}, \\ f_k &=& \sum\limits_{i+j=k} \binom{k}{i} d_i e_j & \text{(binomial convolution)}. \end{gather}$$$

Today I'd like to write about the framework that allows to prove that both the sequences defined above are also linear recurrences. It would also allow to compute their characteristic polynomials in $$$O(kl \log kl)$$$, which is optimal as their degrees are $$$O(kl)$$$ in both cases.

Full text and comments »

  • Vote: I like it
  • +133
  • Vote: I do not like it

By adamant, history, 22 months ago, In English

Hi everyone!

You probably know that the primitive root modulo $$$m$$$ exists if and only if one of the following is true:

  • $$$m=2$$$ or $$$m=4$$$;
  • $$$m = p^k$$$ is a power of an odd prime number $$$p$$$;
  • $$$m = 2p^k$$$ is twice a power of an odd prime number $$$p$$$.

Today I'd like to write about an interesting rationale about it through $$$p$$$-adic numbers.

Hopefully, this will allow us to develop a deeper understanding of the multiplicative group modulo $$$p^k$$$.

Tl;dr.

For a prime number $$$p>2$$$ and $$$r \equiv 0 \pmod p$$$ one can uniquely define

$$$ \exp r = \sum\limits_{k=0}^\infty \frac{r^k}{k!} \pmod{p^n}. $$$

In this notion, if $$$g$$$ is a primitive root of remainders modulo $$$p$$$ lifted to have order $$$p-1$$$ modulo $$$p^n$$$ as well, then $$$g \exp p$$$ is a primitive root of remainders modulo $$$p^n$$$.

Finally, for $$$p=2$$$ and $$$n>2$$$ the multiplicative group is generated by two numbers, namely $$$-1$$$ and $$$\exp 4$$$.

Full text and comments »

  • Vote: I like it
  • +155
  • Vote: I do not like it

By adamant, history, 23 months ago, In English

Hi everyone!

Today I want to describe an efficient solution of the following problem:


Composition of Formal Power Series. Given $$$A(x)$$$ and $$$B(x)$$$ of degree $$$n-1$$$ such that $$$B(0) = 0$$$, compute $$$A(B(x)) \pmod{x^n}$$$.


The condition $$$B(0)=0$$$ doesn't decrease the generality of the problem, as $$$A(B(x)) = P(Q(x))$$$, where $$$P(x) = A(x+B(0))$$$ and $$$Q(x) = B(x) - B(0)$$$. Hence you could replace $$$A(x)$$$ and $$$B(x)$$$ with $$$P(x)$$$ and $$$Q(x)$$$ when the condition is not satisfied.

Solutions that I'm going to describe were published in Joris van der Hoeven's article about operations on formal power series. The article also describes a lot of other common algorithms on polynomials. It is worth noting that Joris van der Hoeven and David Harvey are the inventors of the breakthrough $$$O(n \log n)$$$ integer multiplication algorithm in the multitape Turing machine model.

Full text and comments »

  • Vote: I like it
  • +133
  • Vote: I do not like it

By adamant, history, 2 years ago, In English

Hi everyone!

Today I'd like to write a bit on the amazing things you can get out of a power series by putting roots of unity into its arguments. It will be another short story without any particular application in competitive programming (at least I don't know of them yet, but there could be). But I find the facts below amazing, hence I wanted to share them.

You're expected to know some basic stuff about the discrete Fourier transform and a bit of linear algebra to understand the article.

Full text and comments »

  • Vote: I like it
  • +72
  • Vote: I do not like it

By adamant, history, 2 years ago, In English

Hi everyone!

Today I'd like to finally talk about an algorithm to solve the following tasks in $$$O(n \log^2 n)$$$:

  • Compute the greatest common divisor of two polynomials $$$P(x)$$$ and $$$Q(x)$$$;
  • Given $$$f(x)$$$ and $$$h(x)$$$ find the multiplicative inverse of $$$f(x)$$$ modulo $$$h(x)$$$;
  • Given $$$F_0,F_1, \dots, F_m$$$, recover the minimum linear recurrence $$$F_n = a_1 F_{n-1} + \dots + a_d F_{n-d}$$$;
  • Given $$$P(x)$$$ and $$$Q(x)$$$, find $$$A(x)$$$ and $$$B(x)$$$ such that $$$P(x) A(x) + Q(x) B(x) = \gcd(P, Q)$$$;
  • Given $$$P(x)=(x-\lambda_1)\dots(x-\lambda_n)$$$ and $$$Q(x)=(x-\mu_1)\dots(x-\mu_m)$$$ compute their resultant.

More specifically, this allows to solve in $$$O(n \log^2 n)$$$ the following problems:


Library Checker — Find Linear Recurrence. You're given $$$F_0, \dots, F_{m}$$$. Find $$$a_1, \dots, a_d$$$ with minimum $$$d$$$ such that

$$$ F_n = \sum\limits_{k=1}^d a_k F_{n-k}. $$$


Library Checker — Inv of Polynomials. You're given $$$f(x)$$$ and $$$h(x)$$$. Compute $$$f^{-1}(x)$$$ modulo $$$h(x)$$$.


All tasks here are connected with the extended Euclidean algorithm and the procedure we're going to talk about is a way to compute it quickly. I recommend reading article on recovering minimum linear recurrence first, as it introduces some useful results and concepts. It is also highly recommended to familiarize yourself with the concept of continued fractions.

Full text and comments »

  • Vote: I like it
  • +179
  • Vote: I do not like it

By adamant, history, 2 years ago, In English

Hi everyone!

The task of finding the minimum linear recurrence for the given starting sequence is typically solved with the Berlekamp-Massey algorithm. In this article I would like to highlight another possible approach, with the use of the extended Euclidean algorithm.

Great thanks to nor for the proofreading and all the useful comments to make the article more accessible and rigorous.

Tl'dr.

The procedure below is essentially a formalization of the extended Euclidean algorithm done on $$$F(x)$$$ and $$$x^{m+1}$$$.

If you need to find the minimum linear recurrence for a given sequence $$$F_0, F_1, \dots, F_m$$$, do the following:

Let $$$F(x) = F_m + F_{m-1} x + \dots + F_0 x^m$$$ be the generating function of the reversed $$$F$$$.

Compute the sequence of remainders $$$r_{-2}, r_{-1}, r_0, \dots, r_k$$$ such that $$$r_{-2} = F(x)$$$, $$$r_{-1}=x^{m+1}$$$ and

$$$r_{k} = r_{k-2} \mod r_{k-1}.$$$

Let $$$a_k(x)$$$ be a polynomial such that $$$r_k = r_{k-2} - a_k r_{k-1}$$$.

Compute the auxiliary sequence $$$q_{-2}, q_{-1}, q_0, \dots, q_k$$$ such that $$$q_{-2} = 1$$$, $$$q_{-1} = 0$$$ and

$$$q_{k} = q_{k-2} + a_k q_{k-1}.$$$

Pick $$$k$$$ to be the first index such that $$$\deg r_k < \deg q_k$$$. Let $$$q_k(x) = a_0 x^d - \sum\limits_{k=1}^d a_k x^{d-k}$$$, then it also holds that

$$$ F_n = \sum\limits_{k=1}^d \frac{a_k}{a_0}F_{n-k} $$$

for any $$$n \geq d$$$ and $$$d$$$ is the minimum possible. Thus, $$$q_k(x)$$$ divided by $$$a_0$$$ is the characteristic polynomial of the minimum linear for $$$F$$$.

More generally, one can say for such $$$k$$$ that

$$$ F(x) \equiv \frac{(-1)^{k}r_k(x)}{q_k(x)} \pmod{x^{m+1}}. $$$

Full text and comments »

  • Vote: I like it
  • +175
  • Vote: I do not like it