[BJOI2018] binary solution to a problem

Topic Link

Subject description:

pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不是 的倍数。他想研究对于二进制,是否也有类似的性质。
于是他生成了一个长为n 的二进制串,希望你对于这个二进制串的一个子区间,能求出其有多少位置不同的连续子串,满足在重新排列后(可包含前导0)是一个3 的倍数。
两个位置不同的子区间指开始位置不同或结束位置不同。
由于他想尝试尽量多的情况,他有时会修改串中的一个位置,并且会进行多次询问。

Consider first a sub-problem: determine a substring satisfy a condition.
First, the method of binary into decimal bits × corresponding bit weight.
Can be found, the bit weight \ (mod3 \) values are \ (1,2,1,2 \) alternately.
The substring provided there are x 1 on 2, y 1 on the two should meet \ ((2x + Y) mod3 = 0 \) , i.e. \ (x = Y (mod3) \) .
The length of the substring is disposed len, there are s 1.
So, greedy want, x and y difference of 0 or 3.
Points of discussion:

  1. \ (len \% 2 = 0 \) , \ (S = 2% 0 \) . Need to satisfy \ (S / 2 <= len / 2 \) , it will be apparent.
  2. \ (len = 0% 2 \) , \ (S = 2%. 1 \) . That \ (X + Y = S, = XY. 3 \) , solve for \ (X = (S +. 3) / 2 \) . Need to satisfy \ ((S +. 3) / 2 <= len / 2 \) , i.e. \ (S <=. 3-len \) .
  3. \ (len = 2%. 1 \) , \ (S = 2% 0 \) . Need to satisfy \ (S / 2 <= (len-. 1) / 2 \) , it will be apparent.
  4. \ (len = 2%. 1 \) , \ (S = 2%. 1 \) . Similarly, \ (X = (S +. 3) / 2 \) . Need to satisfy \ ((S +. 3) / 2 <= (len +. 1) / 2 \) , i.e. \ (S <= len-2 \) .
    Can be found, it will be when s is an even number. s is odd complex, difficult to count (because even if there are many len determined to meet the requirements of s, the interval can not be merged).
    Consider the inclusion-exclusion, ask not legitimate.
    If not valid, then the \ (S = 2%. 1 \) .

Guess you like

Origin www.cnblogs.com/lnzwz/p/11348249.html