[Solution] Shifting String Codeforces Solution
Polycarp found the string and the permutation . Their lengths turned out to be the same and equal to .
A permutation of elements — is an array of length , in which every integer from to occurs exactly once. For example, and are permutations, but , and are not.
In one operation he can
Solution Click Below:- CLICK HERE
multiply by , so he replaces with string , in which for any from to it is true that . For example, with and , after operation the string will turn to .
Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter.
It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type.
The first line of input contains one integer () — the number of test cases in input.
The first line of each case contains single integer () — the length of string and permutation.
The second line of each case contains a string of length , containing lowercase Latin letters.
The third line of each case contains integers — permutation (), all are different.
Output lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer — the minimum number of operations, after which the string will become the same as it was before operations.
No comments:
Post a Comment