Count Distinct Substrings of a String of a Single Repeated Character

HARD · C · 3000ms

The tool is now applied to a stress-test string made of a single repeated letter, and must answer: what is the length of the x-th lexicographically smallest distinct piece of that text? Given a string consisting only of the character 'a', repeated N times, and Q queries each giving an integer x, print the length of the x-th lexicographically smallest DISTINCT substring for each query. (This mirrors StringOFaaa directly — derive the closed-form using triangular numbers plus binary search, since N can be up to 10^9.)

Input

Line1: N Q. Next Q lines: one integer x each.

Output

Q lines: length of the x-th smallest substring, or -1 if x exceeds total distinct substrings.

Constraints

1 ≤ N ≤ 10^9, 1 ≤ Q ≤ 10^5, 1 ≤ x ≤ 10^18

Sample Input

4 2
1
5

Sample Output

1
3
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Count Distinct Substrings of a String of a Single Repeated Character · DIU ContestHub