#P7986. [USACO21DEC] HILO P
[USACO21DEC] HILO P
Problem Description
Bessie has a number , where is an integer between and ().
Elsie is trying to guess this number. She may ask questions of the following form for some integer between and : “Is too high or too low?” If is greater than , Bessie answers "HI!". If is less than , she answers "LO!".
Elsie comes up with the following strategy for guessing Bessie’s number. Before making any guesses, she creates a sequence containing integers in which every number from to appears exactly once (in other words, the sequence is a permutation of length ). Then she goes through this list and makes guesses in the order of the numbers in the list. However, Elsie will skip all unnecessary guesses. That is, if Elsie is about to guess some number , and earlier she has already guessed some and Bessie answered "HI!", then Elsie will not guess and will instead move on to the next number in the sequence. Similarly, if she is about to guess some number , and earlier she has already guessed some and Bessie answered "LO!", then Elsie will not guess and will instead move on to the next number in the sequence. It can be proven that using this strategy, for any sequence Elsie creates, she can uniquely determine .
If we concatenate all of Bessie’s answers "HI" or "LO" into a string , then the number of times Bessie says "HILO" is the number of length substrings of that are equal to "HILO".
Bessie knows that Elsie will use this strategy and has already chosen the value , but she does not know what permutation Elsie will use. Your goal is to compute, over all permutations Elsie could choose, the sum of the number of times Bessie says "HILO", modulo .
Input Format
The input consists of one line containing and .
Output Format
Output the total number of "HILO", modulo .
4 2
17
60 10
508859913
Hint
[Sample Explanation 1]
In this test case, Bessie’s number is .
For example, if Elsie’s permutation is , then Bessie will say "HILOHILO", for a total of occurrences of "HILO". As another example, if Elsie’s permutation is , then Bessie will say "HILOLO", for a total of occurrence of "HILO".
[Sample Explanation 2]
Make sure to output the sum modulo .
[Constraints]
- Test cases - satisfy .
- Test cases - satisfy .
- Test cases - have no additional constraints.
Translated by ChatGPT 5