luogu#P4925. [1007] Scarlet的字符串不可能这么可爱

[1007] Scarlet的字符串不可能这么可爱

Problem Description

Scarlet wants to construct a string with an alphabet size of kk and length LL, such that it has no palindromic contiguous substring with length greater than 11.

It seems there are too many such strings, so Scarlet casually adds a restriction: she fixes the ss-th character of the string to be ww.

Now Scarlet cannot solve it. Please help her compute how many strings satisfy the conditions. As usual, you only need to output the answer modulo pp.

Input Format

The first line contains three integers k,Lk, L and pp, representing the alphabet size, the string length, and the modulus.

The second line contains two integers s,ws, w, describing the restriction given by Scarlet.

Note: s=0s=0 means that in this test case Scarlet kindly did not add any restriction.

Output Format

Output one integer in a single line, representing the number of valid strings modulo pp.

3 3 233
1 1
2

Hint

Alphabet size: the number of distinct characters in a string. For example, if the alphabet size is 33, you may assume the string consists only of the three letters “A”, “B”, and “C”.

Sample explanation: if the first character is fixed to A, then the valid strings are ABC and ACB. The string AAB contains the palindromic substring AA, and ACA itself is a palindrome, and so on.

Constraints for 50%50\% of the testdata: k5,L10k\leq5, L\leq10.

Constraints for another 30%30\% of the testdata: s=0s=0.

Constraints for 100%100\% of the testdata: $1\leq k, L\leq 10^{18}, 0\leq s\leq L, 1\leq w\leq k, 1\leq p\leq 10^9$.

Translated by ChatGPT 5