Nice Pairs
Time limit2sMemory limit512 MB
Count pairs (x, y) with A <= x < y <= B where y is a rotation of x's digits (a suffix moved to the front), counting duplicates only once.
- Level
Medium5 of 10
- Topics
- String, Math, Hash map, Brute force
- Solved
- No attempts yet
Problem
Two natural numbers and form a nice pair when the following holds.
- Cutting some digits off the back of and placing them, in the same order, in front of what is left gives .
For example, cutting off the back of and placing it in front gives , so is a nice pair.
You are given two natural numbers and with the same number of digits. Count the pairs with such that is a nice pair.
Two different cut lengths can produce the same . In that case the pair is counted once.
Input
The first line contains two natural numbers and separated by a space. , and and have the same number of digits.
Output
Print the number of nice pairs as a single integer.