Jumping Frog
Time limit1sMemory limit1024 MB
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.
- Level
Hard8 of 10
- Topics
- Number theory, Math, String, Implementation
- Solved
- No attempts yet
Problem
Pog 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 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 to clockwise, which lets the judges record where every jump happened. Position is adjacent to position and to position .
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 with . Whenever he stands on the rock numbered , he aims his next jump at the position numbered . 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 with , he jumps from to , then to , then back to , and the session ends.
Given the state of the positions, count the distinct values of that Pog can choose for his practice sessions, where any rock may be the starting position.
Input
The first line contains a string of characters (). The -th character of () describes position : R means a rock and P means a pond.
Output
Print one line with the number of distinct jump lengths Pog can choose.