Finding a Momentum Sequence
Time limit1sMemory limit1024 MB
Given a digit string, decide if it can be split into an arithmetic progression followed by a term equal to the last progression term times some integer f, and output the minimal possible f.
- Level
Hard8 of 10
- Topics
- String, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
Call a sequence of length a momentum sequence with momentum if it satisfies all of the following conditions.
- .
- For every integer with , is an integer and .
- There exists a positive integer such that for every . In other words, after removing the last term , the sequence is an arithmetic progression with positive integer common difference.
- There exists an integer such that .
As an illustration, is a momentum sequence because it satisfies all conditions with and .
A momentum sequence gaining momentum
Given a string consisting only of digits, determine whether there exists a momentum sequence such that is exactly the concatenation of with no spaces.
When writing any as a string, leading zeros are not allowed. If more than one valid sequence exists, find one with the smallest possible .
Input
The first line contains the string . The length of is at least and at most , and every character of is one of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
The first character of is never 0.
Output
If there exists a momentum sequence satisfying the conditions for , print as an integer. If more than one such exists, print the smallest possible value of .
If no such momentum sequence exists, print 0.