Battle Droids
Time limit2sMemory limit1024 MB
Count contiguous segments where the sum of 2^a_i over the segment is a power of two, since equal AIQ values merge like binary addition.
- Level
Medium7 of 10
- Topics
- Math, Hash map, Bit manipulation
- Solved
- No attempts yet
Problem
Count Dooku wants to send a squad of droids on an important mission. There are droids standing in a row, numbered from 1 to from left to right. Dooku picks a contiguous segment of the row as the squad, that is, all droids with numbers from to for some . The AIQ (artificial intelligence quotient) of droid is .
Droids in the same squad can merge into a more advanced droid. If the squad has two droids with the same AIQ , they can merge into one droid with AIQ .
Dooku wants a squad where all droids can become a single droid after some number of merges. Count the number of different segments of the row that he can choose as the squad.
Input
The first line contains one integer , the number of droids (). The second line contains integers , the AIQ values of the droids ().
Output
Print one integer, the number of segments Dooku can choose as the desired squad.
Note
In the first example, besides the three segments of length 1, the segments and also fit. In the second example, besides the seven segments of length 1, all four segments of length 4 fit, and so do the two segments .