### Errichto's blog

By Errichto, 2 weeks ago,

For the next few days, I will do live streams of virtual participation (or casual problem-solving) of CF div1 rounds. The first one is starting in less than 2 hours. You can watch on Twitch and Youtube.

I'm back to practicing because there's a big Polish competition in two weeks and I'm certainly out of shape.

maybe around Wednesday: 1770G - Koxia and Bracket, 1774G - Segment Covering, 1787I - Treasure Hunt

See you in the Twitch chat.

• +128

By Errichto, 7 months ago,

Hi. I'm organizing an online IOI-preparation camp in collaboration with Huawei. Every day will be a 5-hour virtual contest, then my problem analysis, and sometimes a lecture. There will be at least 4 contests and 2 lectures.

We will do some old IOI contests (2013?) from the IOI archive, CEOI (2015-2016?), or maybe JOI/JOISC — to be decided this weekend. I don't want to do recent years like 2021 because most participants already covered it. The example lecture topics are those from Range Queries or Trees in Usaco Guide Platinum.

The camp is free for IOI 2022 participants. For everybody else, the video recordings will be posted on Youtube after the camp. By participating you get the live experience (Discord, asking questions, competing with others, leaderboards).

Dates: 25.07-2.08 (updated)
Time of every analysis/lecture: 14:00 UTC / 16:00 CEST
Price: free for IOI participants, 40 USD for everybody else
Platform: Discord & Codeforces & uj.oz (?)

How to register?

Big thanks to Huawei UK R&D for sponsoring this camp.

EDIT: I'm now aware of CEOI collision. There's nothing I can do about it :(

Schedule:
25.07 — CEOI 2016 day 1
26.07 — CEOI 2022 day 1 + lecture
27.07 — CEOI 2016 day 2
28.07 — CEOI 2022 day 2
29.07 — lecture + homework
weekend off
1.08 — CEOI 2017 day 1
2.08 — CEOI 2017 day 2

Participants voted to cover CEOI instead of IOI to avoid problems that they already know.

• +448

By Errichto, 8 months ago,

When some country organizes an ICPC or IOI preparation camp, they often need a teacher from abroad. When they ask me, I usually decline because of a lack of time. I want to be able to link this blog so they would find a good teacher.

Feel free to advertise your 1:1 online coaching too. It's not the main purpose of this blog though.

• +210

By Errichto, 15 months ago,

Target audience: newbies and pupils (rating up to 1400).
Group link: https://codeforces.com/group/yg7WhsFsAp/contests (hit "join" on the right).

Hi. Enjoy a series of 8 problem lists for beginners. The example topics are strings, arrays, math, and binary search. You are allowed to discuss anything with others, or just look up solutions online. There are also 3 exams, each recommended for a 2-hour individual virtual participation. Use the displayed order, e.g. take exam 1 after day 3. It all should take you 2 weeks of intense bootcamp-like work (or a few months if you take your time).

The problems were originally used two years ago in a Saudi Arabia camp. It's a mix of around 70 existing CF problems and 30 new original problems, mainly prepared by kostka, with some help from me and mustafabar. I asked them for permission to publish everything.

I will put hints to some problems in this blog (or in the group? not sure). Expect a few videos and/or streams for beginners too. You should also read two first chapters of Competitive Programmer's Handbook.

UPD: On Sunday evening I'm making a stream with explanations to a few hard problems: P8, P11, P18, P30, P31, P33. You can try it with hints first:

P08. Cashier
P11. Queens attack!
P18. Mountain peaks
P30. Temporarily unavailable
P31. Shuffle Hashing
P31. Shuffle Hashing, hint 2
P33. Thanos Sort

• +251

By Errichto, 15 months ago,

This is my 100th CF blog!

This is a list of techniques with $O(\sqrt n)$ time complexity. Watch the lecture https://youtu.be/BJhzd_VG61k, with timestamps!

1. Square root decomposition — split the sequence into blocks of fixed size.
2. Splitting objects (e.g. vertices) into light and heavy.
3. Square root decomposition by the time of queries & rebuilding the structure.
4. Mo's algorithm — processing queries in proper order and updating the answer by erasing/inserting new elements. https://cp-algorithms.com/data_structures/sqrt_decomposition.html
5. Strings — if the sum of lengths is $S$ then there are at most $\sqrt{S}$ distinct lengths.
6. Birthday paradox & baby-step giant-step. See P4 and P6 here, and see https://cp-algorithms.com/algebra/discrete-log.html.

P1. 398D - Instant Messanger
P2. 220B - Little Elephant and Array
P3. 86D - Powerful array (actually, skip this one because it's boring)
P4. Count triangles in a graph, i.e. cycles of size 3.
P5. Given $N$ strings with total length $S$, find a pair where one string is a substring of the other, in $O(S \cdot \sqrt S)$.

Homework (will be discussed in a second stream soon)
P6. You are given $N$ positive integers $a_1, a_2, \ldots, a_N$ and target value $X$. Check if there is subset with sum equal to $X$, in $O(X \cdot \sqrt S)$ where $S = \sum a_i$.
P7. 13E - Holes
P8. 455D - Serega and Fun
P9. You're given a sequence $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$). It describes a functional graph: there is a directed edge from $i$ to $a_i$. Handle updates U i x (change $a_i$ to $x$) and answer queries Q v — if we start in $v$ and keep following edges, after how many steps do we get to an already visited node?

And two problems from recent contests: 1580C - Train Maintenance and ABC 219 G Propagation.

Thanks to krismaz for letting me use his list of techniques and problems.

• +463

By Errichto, 16 months ago,

Hello. Every week, I will discuss few nice recent problems from CF, AC, CC. I will alternate between div2 and div1 streams, with difficulties [1400, 1800] and ~[2200, 2800] respectively. Try to solve problems on your own before each stream!
You can suggest new cool/interesting/educational problems in comments below this blog.

#### 2.10.2021, Div2 Problems of the Week https://youtu.be/AbeEJvmsx0E

1. [1800] 1572A - Book
2. [1600] 1567C - Carrying Conundrum
3. [1800] 1556C - Compressed Bracket Sequence + O(N) solution

honorable mentions
- [1700] 1579E2 - Array Optimization by Deque

#### 9.10.2021, Div1 Problems of the Week https://youtu.be/7rtZEXAVzmk

1. [2400] 1592E - Bored Bakry
2. [2700] 1572C - Paint
3. [3000] 1572E - Polygon

• +329

By Errichto, 16 months ago,

Meet in the Middle lecture & problem-solving starts in an hour https://www.twitch.tv/errichto. See the problem list below. I will later update this blog with codes and written explanations.
UPD, video recording: https://youtu.be/18sJ3mK173s, some codes from the stream: https://ideone.com/1uScID

P1. Knapsack with $n \leq 40$ and values up to $10^9$ https://cses.fi/problemset/task/1628

P2. Given a sequence $a_1, a_2, \ldots, a_n$ ($n \leq 2000$), count increasing subsequences of length 3.

P3. 4-SUM, find four values that sum up to the target value https://cses.fi/problemset/task/1642

P4. Find a string with the standard polynomial hash equal to the target value $X$ modulo $10^9+7$. The hash is computed by converting characters a-z into 0-25 and multiplying every character by the next power of 26. Find a solution without just converting $X$ to base $26$.

P5. You're given a graph: $n \leq 300$, $m \leq n\cdot(n-1)/2$. Count paths made of 5 nodes. Nodes and edges can be repeated.
Bonus 1: Count simple paths only. No repetitions allowed.
Bonus 2: $n, m \leq 2000$
Similar problem: https://codeforces.com/blog/entry/94003

P6. You're given a sequence $n \leq 10^6, 0 \leq a_i < M = 10^9$. Find two subsequences with equal sums modulo $M$. https://quera.ir/problemset/olympiad/34090. The two subsequences can have common elements.

P7. Number Clicker https://codeforces.com/contest/995/problem/E. You start with the number $a$ and want to get $b$ in at most 200 moves. You can increment, decrement or change the current number into its modular inverse, all modulo prime $p$. Find any valid sequence of moves.

• +242

By Errichto, 18 months ago,

Hi. I think these are nice medium-hard interactive problems. I will discuss solutions in a stream today at https://www.twitch.tv/errichto.

UPD, recording is here https://youtu.be/9oEihYrAR5kUPD and I added text solutions in this blog.

P1. Mostly A — You're given two integers $N$ and $K$ ($N \leq 50\,000$, $K \leq 10$). You need to find a hidden string of length $N$ with lowercase English characters a-z. At most $K$ characters are different than 'a'. You can choose your own string of length $N$ and you will get info YES/NO whether your string is lexicographically smaller than the hidden one. There is no explicit limit on the number of queries. There's still some time limit (say, 2 seconds).
Hard version: Minimize the number of queries.

slow solution
hint
solution
hard version (optimal number of queries)

P2. Cloyster (https://codeforces.com/gym/102341/problem/C) — There's a grid $N \times N$ ($N \leq 2000$), each cell with some value. You can ask at most $3 \cdot n + 210$ queries "get value at cell (i, j)". Find the cell with the maximum value. For each of other $N^2 - 1$ cells, it's guaranteed that at least one adjacent cell has a greater value. Two cells are adjacent if they share a side or a corner.

hint

P3. Moving Car — A car on an infinite line starts at unknown position $X$ and unknown constant speed $V$ ($1 \leq X, V \leq 1000$). After $i$ seconds, it will be at $X + i \cdot V$. At time $0$ and then after every second, you get to ask about some position and you get a response whether the car is on the left, on the right, or at this position. Use at most 1000 queries to find values $X$ and $V$.
Hard version: $1 \leq X, V \leq 10^5$

hint
solution
cool improvement
hard version

If you know the source of problems 1 or 3, please let me know.

• +235

By Errichto, 20 months ago,

In a few hours, I will talk with guests about the upcoming IOI 2021. I will be joined (at least for 10-15 minutes each) by tmwilliamlin168, Petr, kostka and hopefully more.

We might talk winner predictions, stories, tips, training style in various countries. Feel free to post suggestions under this blog too.

See you at https://www.twitch.tv/errichto

UPD, recording: https://youtu.be/XNvcYFvFBls
Thanks to eduardische and jonathanirvings for joining the second part, when we discussed IOI from the organizers's point of view.

• +116

By Errichto, 2 years ago,

I and Radewoosh will virtually participate in Swiss-Slovak subregional (announcement, contest) on Tuesday at 16:00 CET. Watch my live stream on Twitch https://www.twitch.tv/errichto. Or later rewatch on YT https://youtu.be/M-xGjXwTXzQ.

Thanks to Xellos for the suggestion and majk for the contest.

This should be a nice warm-up before Petrozavodsk camp, which starts in a few days. I'm going to participate in half of the contests (as a member of mixed Polish Mafia team). I will record screencasts if allowed.

• +76

By Errichto, 2 years ago,

I'm going to solve CSES geometry section live on Saturday (tomorrow) while explaining and drawing stuff to make it educational. There are seven problems. We'll start with the cross-product recap and eventually get to Convex Hull and Closest Pair Of Points. No prewritten code.

Problems: https://cses.fi/problemset

Watch on Twitch or Youtube https://www.twitch.tv/errichto, https://youtu.be/G9QTjWtK_TQ. The latter will be available later to rewatch too.

• +124

By Errichto, 2 years ago,

Let's discuss the idea of IOI preparation in form of weekly contests and classes.

Many programming camps reuse problems from old national camps and contests. What about doing this on a bigger scale and conducting weekly mashup contests open for everybody for free? The problem analysis would be in English, in written or (live) video format, possibly combined with a lecture. Make a discord server for further discussion and questions, encourage students to upsolve, maintain yearly leaderboard and upsolving leaderboard. Once or twice a year, call it a 1-week online camp and organize these mashup contests daily.

I can take part in this initiative but I want to be just one of many teachers. When it's my turn, I would pick some old interesting POI or ONTAK problems because I either know them or can look up the solution in Polish. If I do this once per 1-2 months, I won't run out of good problems anytime soon, and I have enough time to prepare analysis and lecture, and later spend several hours just helping people with doubts and wrong codes. I can oversee this project at the beginning but then I'd prefer somebody else to take over.

#### Why?

There's clearly a need for IOI training. Almost every month, some country asks me if I can teach their IOI team by conducting a camp or mentoring them for a longer period. Even if a country has strong teachers or former participants, that usually just means one camp a year or classes for a single school. In my opinion, it's very important to train regularly, compete with others, and be able to ask questions. You can now participate regularly in Codechef Lunchtime or COCI but those are rare and neither of them emulates the camp experience.

#### Div2

If there are enough good subtasks, hard problems aren't a big issue for weak participants because they still have something to do and learn. Still, maybe once a month the contest should be just much easier to encourage everybody to participate? Or always add one more easy problem and recommend strong participants to skip it?

#### Codechef

I first thought that every contest should happen on its native platform like Szkopul for POI problems. It's easy to conduct such a contest made of old problems but the rules would be inconsistent and participants would need to create many accounts. A seemingly independent issue is money.

I then talked with Codechef and they agreed to pay teachers if it's all conducted on Codechef platform. Keeping track of everything would be much easier. Codechef staff will take care of adjusting the problem package format to the Codechef system but obviously, some problems might become unusable. (I think that CodeChef Lunchtime should then become part of this project too.)

By the way, you could find teachers who can work for free (like CSPrep by geniucos) but this is a time-consuming job, including the translation and helping students by answering their questions. Usually, camps exist because participants pay a fee or it's sponsored by a ministry of education, etc. I think that CodeChef is a good solution here. Nothing is decided yet so let me know if you disagree!

#### Possible Issues

1. Copyrights — Not every platform or old contest will allow reusing their problems in a different platform.
2. Language barrier — Some high school students don't know English well enough. From my teaching experience, strong participants usually don't have troubles, but weaker ones often prefer to resolve small doubts with a teacher or assistant who speaks their native language.
3. Timezones and collisions — We need a USACO-style long time window (it's supported by Codechef!) so you could choose when to start a 5-hour contest. If analysis or lecture is in live video format, it will still be tricky to find a 2-hour slot on Saturday or Sunday without collisions with regular contests. If somebody misses a live-stream, they should still be able to rewatch it and ask questions.
4. Quality — Just like for any camp, experienced teachers are necessary to ensure the quality of problems and analysis.

• +466

By Errichto, 2 years ago,

Hi. I have a few announcements.

Among Us stream on Sunday, 20:00 CEST (just after CF 675 div2). The current team is me, Egor, kostka, tmwilliamlin168, alexwice, xiaowuc1, SecondThread, Reyna, mesanu, the_white_devil, SlavicG, gupta_samarth, lior5654. See you in the chat! https://www.twitch.tv/errichto

I stream programming regularly now: every Tuesday (hard Atcoder upsolving), Thursday (coding interviews) and Saturday (easy Codeforces problems), always starting at 9:00 PST / 18:00 CEST / 21:30 IST. You can import my google calendar. I won't announce these streams in Codeforces anymore, so say goodbye to blogs like "My Coding Streams in Month X". I might do an exception for something unusual or guest episodes. The schedule will always be at the bottom of my Twitch page https://www.twitch.tv/errichto.
(So yeah, Saturday easy stream starts in 15 minutes!)

If you are high-rated and I know you, you can join me for a Tuesday stream and we would solve hard AGC problems together. For example, I might be soon joined my mnbvmar for such pair problem-solving. If anybody's interested too, let me know.

Also: past live streams are available here; I'm preparing Gaussian Elimination lessons for CF EDU; join my Discord server.

• +200

By Errichto, 2 years ago,

For the second time (part1) enjoy two videos by me and SecondThread.

I hope you like this format of taking an old cool problem and just discussing it.

• +63

By Errichto, 2 years ago,

You can watch me live on https://www.twitch.tv/errichto or see stored broadcasts on https://www.youtube.com/errichto2. I will start with a week of daily streams and then it's just when I have time.

I will stream regularly now: hard problems on Tuesday, coding interviews on Thursday, easy problems on Saturday

• +167

By Errichto, 2 years ago,

Hi. I invite you to AtCoder Grand Contest 047.

Statements are very short and I think that problems are interesting. Big thanks to rng_58 for discussing problems with me.

I will likely make a short live stream just after the contest ends. (update: watch the recording here)

UPD, congrats to tourist for winning and Benq for the second full score.

• +636

By Errichto, 3 years ago,

tl;dr — video tutorial https://www.youtube.com/watch?v=eMXNWcbw75E and codeforces GYM training https://codeforces.com/gym/102644 (register by finding this contest in GYM instead of using the link directly)
video editorial: part 1 (ABCDEF) and part 2 (GHI)
codes to all 9 problems: https://github.com/Errichto/youtube/tree/master/matrix-exponentiation

Prerequisites: binary exponentiation and iterative dp (you don't need to know matrices)

The youtube tutorial (link) focuses on intuition and graph-like visualization . Or, if you prefer, below is a shorter (less detailed) text tutorial instead. You can practice by solving a set of 9 educational problems in GYM https://codeforces.com/gym/102644. ABCD are easy, EF medium, GHI are hard. If you are stuck, see hints below or watch the full solution analysis — part 1 (ABCDEF) and part 2 (GHI).

#### Hints

C. Fibonacci
D. Count Paths
E. Knight Paths
F. Min Path
G. Recurrence With Square
I. Count Paths Qeuries

#### Matrix Exponentiation

Consider this problem:
String Mood — Limak can be either happy or sad. His mood changes (or stays same) when he reads an English uppercase letter. Letters S and D always make him sad, H makes him happy, and every vowel (AEIOU) flips his mood. Other letters do nothing.
Limak is happy right now. Among all $26^n$ possible strings of length $n$ ($n \leq 10^{18}$), count such strings that Limak will be happy after reading that string, modulo $10^9+7$.

If something can be solved with dp in $O(1)$ space, we can usually speed it up later with matrix exponentiation. This dp is easy — for length from $1$ to $n$ compute the number of strings making you happy and making you sad at the end.

dp in O(1) space

Let's visualize that by drawing vertices representing the two moods, and edges with the number of ways to move from one state to the other.

drawing 1

For example, if you are happy, there are 19 ways to make you happy and 7 ways to make you sad (SDAEIOU). Thin edges on the right represent the second letter of a string and the number there should be the same, because they are again 19 ways to make you happy if you were happy, etc. If we were asked about answer for $n = 2$, we would need to compute the number of ways to get from HAPPY state in first column to HAPPY state in third column, which is they yellow edge below.

drawing 2

That's $19 \cdot 19 + 7 \cdot 6 = 403$. In a similar way, we can compute all four counts for 2-letter strings: happy to happy (equal to $403$), happy to sad ($19\cdot7+7\cdot20=273$), sad to happy ($234$), sad to sad ($442$). We'll actually keep these four values $[ [403,273], [234, 442] ]$ in a 2-dimensional array, also called a matrix.

Starting from a matrix with four values describing 1-letter strings $[ [19,7],[6,20] ]$, in $O(1)$ we got a matrix describing 2-letter strings. We can do the same to get a matrix for 4-letter strings, then 8-letter strings, and so on. We can do binary exponentiation to find a matrix for any huge $n$. Formally, we compute $M^n$ where $M = [ [19,7],[6,20] ]$ and multiplying two matrices is exactly what we did above to compute a new matrix $[ [403,273],[234, 442] ]$, done with the following code:

for(int i = 0; i < s; i++) { // s is the number of states, s=2 in the String Mood problem
for(int j = 0; j < s; j++) {
for(int k = 0; k < s; k++) {
new_matrix[i][k] += matrix1[i][j] * matrix2[j][k];
}
}
}


The total time complexity is $O(s^3 \cdot \log n)$ where $s$ is the matrix size (also called order), which is equal to the number of states (variables) you need in space-efficient dp. We had $s=2$ in String Mood so the complexity is just $O(\log n)$.

full C++ solution

Now, try to find the $n$-th Fibonacci number for $n \leq 10^{18}$ (problem link) by first implementing space-efficient dp with transitions of form new_variable += old_variable * x where $x$ is some number you will need to put in the initial matrix (like $19$ or $7$ in String Mood).

#### Footnote

Thanks to tnowak for suggesting and creating one problem, and to mnbvmar for a few problem ideas. For the future, I'm looking for somebody to help me with the preparation of such training contests. If you want to help and you are high-rated and perfectly with experience in using polygon, please write to me. I can pay you a little if you want.

I hope you learned something thanks to this tutorial. Feel free to discuss problems from the GYM contest or give links to more matrix exponentiation problems or useful resources. If anybody wants that, I can make tests public. If you are a teacher and want to use some problems in your classes, I will give you access in Polygon.

• +606

By Errichto, 3 years ago,

I will host my first lockout stream on Sunday, June 28, 19:00 CEST / 20:00 MSK, featuring tourist, Radewoosh, majk and Egor. I'm not sure if we will use Codeforces or Atcoder problems.

You can watch it live on https://www.twitch.tv/errichto and later on youtube.

Watch previous streams organized by ecnerwala (not alone), more info in blogs one, two and format/rules.

EDIT Congratulations to Egor for the win! You can watch the recording on Youtube https://youtu.be/N7ec-OoxvHg
I was a great experience, you should expect the next lockout at the end of July :)

• +437

By Errichto, 3 years ago,

While I have mixed feelings about division 4, I think it perfectly matches introducing the rating lowerbond in division 2.

Maybe 1501 is a good idea, I'm not sure. Or maybe let new participants have 1400 initially (upper bound for div4) and lowerbound for div2 would be 1401.

The benefits are decreased load for the main and rare div1+2 rounds, and possibility to slightly increase the difficulty of div2-abc, thus get back to the format with 5 problems per division (3 are common). Because the contest is then for a group of people with more similar skill level. These arguments aren't that important for div2-only contests so it's ok if those are without lowerbound, I don't care. Or make those div2+3 instead of div2-only, I again don't care.

• +706

By Errichto, 3 years ago,

You can watch me on https://www.twitch.tv/errichto or https://www.youtube.com/errichto2. There is schedule with countdown on Twitch. Most likely I will stream in May on Tuesday mornings and Sunday evenings.

(updated on May 30)

two recent videos for beginners: Computations Modulo P, Binary Exponentiation
in works: Matrix Expo, Gaussian Elimination, EV in Chain-graph, Berlekamp-Massey

JUNE UPDATEJune 20, 19:00 CEST solving random problems suggested by viewers

• +168

By Errichto, 3 years ago,

On Tuesday, April 14, I will solve random problems live on Youtube, until I hit 100k subs. Suggestions in the chat are welcome.

https://youtu.be/rEGDNd-9PAU (there's countdown to start)

• +262

By Errichto, 3 years ago,

I recently made two mainstream videos which are a good introduction to CP, I think.

About Programming Competitions (Codeforces, Code Jam, ...) https://youtu.be/cpguolx2oms

UPD

What is Competitive Programming? https://youtu.be/ueNT-w7Oluw (by tmwilliamlin168)

HoW tO BEcOmE rEd cOdeR? https://youtu.be/y7169jEvb-Y

UPD2

Steps and Mistakes https://youtu.be/bVKHRtafgPc (by tmwilliamlin168 again)

• +150

By Errichto, 3 years ago,

At 18:00 CET on Saturday, as a warm-up before Google Hash Code qualification round, I will solve a practice problem (or just some old problem?) live together with my teammate kabuszki. You can watch us on YT or Twitch https://www.youtube.com/errichto2/live & https://www.twitch.tv/errichto. I know it collides with SRM but this time is convenient for me.

Maybe I will also record our qualification round performance on Thursday, we'll see.

Unrelated: expect a few short problem-solving streams next week, stay tuned.

• +45

By Errichto, 3 years ago,

I'm back to streaming and I plan the following three problem-solving streams, each lasting around 5 hours:

I will think out loud and talk about solutions, sometimes with implementation too. Streams 1 and 3 are meant to be educational, while stream 2 will be more of me thinking and struggling. I will not do virtual participation because the pressure of time doesn't go well with explaining anything.

You can watch me on my secondary Youtube channel https://www.youtube.com/errichto2/live or on Twitch https://www.twitch.tv/errichto.

See you in the chat :)

UPD — I will make shorter streams on Monday (the 17th) and Thursday (the 20th). It will be Codeforces div1C-D problems and Boring stream, respectively. I'm updating this old blog so I wouldn't spam with new one.

• +50

By Errichto, 3 years ago,

Part 1 (link) introduces basic bitwise operations. This is part 2 and it's mainly about (in)famous bitsets and example problems. Also, see links to very useful advanced stuff at the bottom. EDIT: here's video version of this blog (on my Youtube channel).

### Built-in functions

In C++, __builtin_popcount(x) returns popcount of a number — the number of ones in the binary representation of $x$. Use __builtin_popcountll(x) for long longs.

There are also __builtin_clz and __builtin_ctz (and their long long versions) for counting the number of leading or trailing zeros in a positive number. Read more here. Now, try to solve these two simple tasks in $O(1)$, then open the spoiler to check the solution:

Compute the biggest power of 2 that is a divisor of x. Example: f(12) = 4
Compute the smallest power of 2 that is not smaller than x. Example: f(12) = 16

While popcount is often needed, I rarely use the other two functions. During a contest, I would solve the two tasks above in $O(\log x)$ with simple while-loops, because it's easier and more intuitive for me. Just be aware that these can be done in $O(1)$, and use clz or ctz if you need to speed up your solution.

### Motivation behind bitsets

Consider this problem: There are $N \leq 5000$ workers. Each worker is available during some days of this month (which has 30 days). For each worker, you are given a set of numbers, each from interval $[1, 30]$, representing his/her availability. You need to assign an important project to two workers but they will be able to work on the project only when they are both available. Find two workers that are best for the job — maximize the number of days when both these workers are available.

You can compute the intersection of two workers (two sets) in $O(30)$ by using e.g. two pointers for two sorted sequences. Doing that for every pair of workers is $O(N^2 \cdot 30)$, slightly too slow.

We can think about the availability of a worker as a binary string of length $30$, which can be stored in a single int. With this representation, we can count the intersection size in $O(1)$ by using __builtin_popcount(x[i] & x[j]). The complexity becomes $O(N^2)$, fast enough.

What if we are given the availability for the whole year or in general for $D$ days? We can handle $D \leq 64$ in a single unsigned long long, what about bigger $D$?

We can split $D$ days into convenient parts of size $64$ and store the availability of a single worker in an array of $\frac{D}{64}$ unsigned long longs. Then, the intersection can be computed in $O(\frac{D}{64})$ and the whole complexity is $O(N^2 \cdot \frac{D}{64})$.

code

So, we can simulate a long binary number with multiple unsigned long longs. The implementation isn't that bad but doing binary shifts would be quite ugly. Turns out all of this can be done with bitsets easily.

### Bitsets

bitset<365> is a binary number with $365$ bits available, and it supports most of binary operations. The code above changes into simple:

code

Some functions differ, e.g. x.count() instead of __builtin_popcount(x) but it's only more convenient. You can read and print binary numbers, construct a bitset from int or string bitset<100> a(17); bitset<100> b("1010");. You can even access particular bits with b[i]. Read more in C++ reference https://en.cppreference.com/w/cpp/utility/bitset.

Note that the size of the bitset must be a constant number. You can't read $n$ and then declare bitset<n> john;. If $n$ is up to $100$, just create bitset<100>.

The complexity of bitwise operations is $O(\frac{size}{32})$ or $O(\frac{size}{64})$, it depends on the architecture of your computer.

### Problems

P1. Different numbers — You are given a sequence of $N \leq 10^7$ numbers, each from interval $[0, 10^9]$. How many different values appear in the sequence? Don't use set or unordered_set because they quite slow.

solution

P2. Knapsack — You are given $N \leq 1000$ items, each with some weight $w_i$. Is there a subset of items with total weight exactly $W \leq 10^6$?

solution

P3. Triangles in a graph — Given a graph with $n \leq 2000$ vertices and $m \leq n \cdot (n - 1) / 2$ edges, count triples of vertices $a, b, c$ such that there are edges $a-b$, $a-c$ and $b-c$.

hint

P4. Chef and Querieshttps://www.codechef.com/problems/CHEFQUE (easy)

P5. Odd Topichttps://www.codechef.com/AABH2020/problems/ODTPIC (medium), thanks to Not-Afraid for the suggestion

P6. Funny Gnomeshttps://www.codechef.com/problems/SHAIKHGN (hard)

### Bonuses

1) m & (m-1) turns off the lowest bit that was set to $1$ in a positive number $m$. For example, we get $24$ for $m = 26$, as $11010$ changes into $11000$. Explanation on quora
2) A quite similar trick allows us to iterate efficiently over all submasks of a mask, article on cp-algorithms / e-maxx. This article also explains why masks-submasks iteration is $O(3^n)$.
3) DP on broken profile (grid dp) — https://cp-algorithms.com/dynamic_programming/profile-dynamics.html
4) SOS dp (sum over subset) — https://codeforces.com/blog/entry/45223 & https://www.youtube.com/watch?v=Lpvsd8WpbWc&t=5m4s
5) _Find_next function and complexity notation for bitsets — https://codeforces.com/blog/entry/43718

I will add links to some problems in online judges, feel free to suggest some in the comments. I think that bonuses 3 and 4 lack some explanation with drawings, maybe I will make some soon.

• +326