MALD Contest 1 P5 - Scratch Cat and Desktop Backgrounds

View as PDF

Submit solution


Points: 17 (partial)
Time limit: 1.0s
Java 3.0s
Memory limit: 512M

Author:
Problem types
Who wears sunglasses in a dark room?

The Scratch Cat wants to be a cool coder. He searches for some coding desktop backgrounds, which naturally always contain 1s and 0s. The Scratch Cat prefers specifically balanced text consisting of 1s and 0s. He defines the imbalance function of string S as I(S) = \frac{|f_1-f_0|}{|S|} \times 100, where f_1 is the frequency of 1s, f_0 is the frequency of 0s, and |S| is the length of string S. The function I(S) represents the percentage of S that is extraneous.

The Scratch Cat determines the "coolness" of a background's text T by the number of substrings of T that have an imbalance between b_l and b_r, inclusive. The Scratch Cat asks you for help because you are the true cool coder.

Constraints

1 \le |T| \le 10^6

0 \le b_l \le b_r \le 100

Each character of T is either 1 or 0.

Subtask 1 [10%]

1 \le |T| \le 10^3

Subtask 2 [40%]

1 \le |T| \le 10^5

Subtask 3 [50%]

No additional constraints.

Input Specification

The first line will contain two space-separated integers b_l and b_r, the minimum and maximum imbalance.

The second line will contain the string T, the text in a desktop background. T only consists of 1 or 0.

Output Specification

Output the number of non-empty substrings with an imbalance in the range [b_l, b_r]. Two substrings are distinct if they are from different segments of T, even if the substrings themselves are identical.

Do not round the function I(S).

Sample Input

33 100
1001

Sample Output

7

Explanation

Below are the imbalances for all substrings of 1001:

  • [1, 1]: I(1) = 100
  • [1, 2]: I(10) = 0
  • [2, 2]: I(0) = 100
  • [1, 3]: I(100) = 33.33...
  • [2, 3]: I(00) = 100
  • [3, 3]: I(0) = 100
  • [1, 4]: I(1001) = 0
  • [2, 4]: I(001) = 33.33...
  • [3, 4]: I(01) = 0
  • [4, 4]: I(1) = 100

7 substrings have an imbalance between 33 and 100.


Comments

There are no comments at the moment.