6

On continued fractions. Part 3: In competitive programming

 2 years ago
source link: http://codeforces.com/blog/entry/101460
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hi everyone!

It's been quite some time since I wrote two previous articles in the cycle:

Part 1: Introduction
Part 2: Properties and interpretation
Part 3: In competitive programming

This time I finally decided to publish something on how one can actually use continued fractions in competitive programming problems.

Few months ago, I joined CP-Algorithms as a collaborator. The website also underwent a major design update recently, so I decided it would be great to use this opportunity and publish my new article there, so here it is:

CP-Algorithms — Continued fractions

It took me quite a while to write and I made sure to not only describe common competitive programming challenges related to continued fractions, but also to describe the whole concept from scratch. That being said, article is supposed to be self-contained.

Main covered topics:

  1. Notion of continued fractions, convergents, semiconvergents, complete quotients.
  2. Recurrence to compute convergents, notion of continuant.
  3. Connection of continued fractions with the Stern-Brocot tree and the Calkin-Wilf tree.
  4. Convergence rate with continued fractions.
  5. Linear fractional transformations, quadratic irrationalities.
  6. Geometric interpretation of continued fractions and convergents.

I really hope that I managed to simplify the general story-telling compared to previous 2 articles.

Here are the major problems that are dealt with in the article:

  • Given a1,…,ana1,…,an, quickly compute [al;al+1,…,ar][al;al+1,…,ar] in queries.
  • Which number of A=[a0;a1,…,an]A=[a0;a1,…,an] and B=[b0;b1,…,bm]B=[b0;b1,…,bm] is smaller? How to emulate A−εA−ε and A+εA+ε?
  • Given A=[a0;a1,…,an]A=[a0;a1,…,an] and B=[b0;b1,…,bm]B=[b0;b1,…,bm], compute the continued fraction representations of A+BA+B and A⋅BA⋅B.
  • Given 01≤p0q0<p1q1≤1001≤p0q0<p1q1≤10, find pqpq such that (q,p)(q,p) is lexicographically smallest and p0q0<pq<p1q1p0q0<pq<p1q1.
  • Given xx and kk, xx is not a perfect square. Let x−−√=[a0;a1,…]x=[a0;a1,…], find pkqk=[a0;a1,…,ak]pkqk=[a0;a1,…,ak] for 0≤k≤1090≤k≤109.
  • Given rr and mm, find the minimum value of qr(modm)qr(modm) on 1≤q≤n1≤q≤n.
  • Given rr and mm, find pqpq such that p,q≤m−−√p,q≤m and pq−1≡r(modm)pq−1≡r(modm).
  • Given pp, qq and bb, construct the convex hull of lattice points below the line y=px+bqy=px+bq on 0≤x≤n0≤x≤n.
  • Given AA, BB and CC, find the maximum value of Ax+ByAx+By on x,y≥0x,y≥0 and Ax+By≤CAx+By≤C.
  • Given pp, qq and bb, compute the following sum:
∑x=1n⌊px+bq⌋.∑x=1n⌊px+bq⌋.

So far, here is the list of problems that are explained in the article:

And an additional list of practice problems where continued fractions could be useful:

There are likely much more problems where continued fractions are used, please mention them in the comments if you know any!

Finally, since CP-Algorithms is supposed to be a wiki-like project (that is, to grow and get better as time goes by), please feel free to comment on any issues that you might find while reading the article, ask questions or suggest any improvements. You can do so in the comments below or in the issues section of the CP-Algorithms GitHub repo. You can also suggest changes via pull request functionality.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK