Peter-007's blog

By Peter-007, history, 9 months ago, In English

About two months ago I invented this (probably well known though) when trying to solve problem that I made. Haven't seen any blogs on this, so decided to make one.

Main idea is that we can use meet-in-the-middle here.

In an naive algorithm to insert a value or erase a value, we will create a vector of size $$$2^k$$$, where $$$dp_{mask}$$$ denotes how many elements with such mask are there, and just do $$$dp_{mask}++$$$ (or do $$$dp_{mask}--$$$), so it will take $$$O(1)$$$ time, and to make a query we will iterate over all submasks of $$$mask$$$ in $$$O(2^k)$$$. It seems obvious that we can balance this.

So we can split bits in half, to add a value or erase it we will iterate over all UPmasks (in UPmask, we are replacing some zeroes with ones) of the first half of the bits (name it $$$maskup$$$) and add the second half here (name it $$$masksec$$$), so do $$$dp_{maskup+masksec}++$$$ (in case we want to add a value, but if we want to erase, substract one).

To make a query we will do an inverse of that: first half is fixed, and we iterate over all SUBmasks of the second one.

The only issue here is that we still use $$$O(2^k)$$$ memory, we can have map or unordered_map, but that's pretty slow, or alternitevly some clever of vectors.

Code (using vectors with O(2^(k/2)) memory)
Problem (some other solutions not using that idea might exist)

Full text and comments »

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

By Peter-007, history, 20 months ago, In English

Here's a simple problem for you.

You are given $$$n=200$$$ elements and you have to iterate over all possible quadriplets (4) of them, the order of elements of each quadriplet doesn't matter, and you can take same element more than one time. Give quick approximation of how many iterations will algorithm run.

Answer

Full text and comments »

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

By Peter-007, history, 21 month(s) ago, translation, In English

Yesterday on BSUIR Open i found this problem: Given array $$$a$$$ size $$$n$$$ and $$$q$$$ queries:

  1. Given two integers $$$l,r,$$$ calculate $$$\sum_i^{r-l+1} a_{l+i}*fib_i$$$ modulo $$$10^9+7$$$, where $$$fib_i$$$ — $$$i$$$-th number in Fibonacci sequence.
  2. Given three integers $$$l,r,x$$$, add $$$x$$$ on segment from $$$l$$$ to $$$r$$$.

But while solving I never used the fact that given coeficients are Fibonacci numbers, and was solving general case, which I couldn't do, and now I become intrested, is it possible to solve general case problem faster $$$O(nq)$$$, or prove the reverse?

More formally: Given array $$$a$$$, and also array $$$c$$$, both size $$$n$$$ and $$$q$$$ queries:

  1. Given two integers $$$l,r,$$$ find $$$\sum_i^{r-l+1} a_{l+i}*c_i$$$ modulo $$$10^9+7$$$.
  2. Given three integers $$$l,r,x$$$, add $$$x$$$ on segment from $$$l$$$ to $$$r$$$.

Can you help?

Full text and comments »

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

By Peter-007, 2 years ago, In English

I started codeforces nearly a month ago and recently got a question: how to "actually" sort problems by difficulty?

I understand that difficulty is subjective, but for an average Codeforces user some problems of the same Codeforces difficulty still would be much much harder than others. And sorting by Codeforces difficulty and looking for the problem with average amount of solves doesn't help either, since older problems have less solves, because Codeforces was less popular.

Full text and comments »

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