Given a circular string of rocks and ponds, count the step sizes K (1 to N-1) for which some rock's K-step cycle stays entirely on rocks.
Hard8Number theoryMathStringImplementationNo attempts yetTime limit1sMemory limit1024 MBPog the Frog wants to compete in the World Frog Jump competition held in Nlogonia. Each frog performs a sequence of acrobatic jumps in a specially built arena. The arena has N positions spaced evenly around a circle, so the arc between two adjacent positions always has the same length, and each position is either a rock or a pond. The positions are numbered from 0 to N−1 clockwise, which lets the judges record where every jump happened. Position 0 is adjacent to position 1 and to position N−1.
The rules say that a frog's sequence of jumps starts on a rock, always goes from a rock to another rock, and ends on the position where it started. A frog does not have to use every rock in the arena.
Pog is practicing for the competition. At the start of a practice session he picks a starting rock and an integer jump length K with 1≤K≤N−1. Whenever he stands on the rock numbered i, he aims his next jump at the position numbered (i+K)modN. He stops once he lands back on the starting rock. Landing on a pond or outside the marked positions means disqualification, so every position he lands on must be a rock. For example, if the arena has 3 positions and all of them are rocks, and Pog starts at position 0 with K=2, he jumps from 0 to 2, then to 1, then back to 0, and the session ends.
Given the state of the N positions, count the distinct values of K that Pog can choose for his practice sessions, where any rock may be the starting position.
The first line contains a string S of N characters (3≤N≤105). The i-th character of S (i=0,1,…,N−1) describes position i: R means a rock and P means a pond.
Print one line with the number of distinct jump lengths Pog can choose.