Under construction

Still being built. Things may change

Finding a Momentum Sequence

Time limit1sMemory limit1024 MB

Summary
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 A={a1,⋯ ,an}A=\{a_1, \cdots, a_n\} of length nn a momentum sequence with momentum fAf_A if it satisfies all of the following conditions.

  • n≥3n \ge 3.
  • For every integer ii with 1≤i≤n1 \le i \le n, aia_i is an integer and 1≤ai<1091 \le a_i < 10^9.
  • There exists a positive integer dd such that ai+1=ai+da_{i+1}=a_i+d for every 1≤i≤n−21 \le i \le n-2. In other words, after removing the last term ana_n, the sequence {a1,⋯ ,an−1}\{a_1, \cdots, a_{n-1}\} is an arithmetic progression with positive integer common difference.
  • There exists an integer fA≥2f_A \ge 2 such that an=an−1⋅fAa_n=a_{n-1}\cdot f_A.

As an illustration, A={2,3,4,8}A=\{2, 3, 4, 8\} is a momentum sequence because it satisfies all conditions with d=1d=1 and fA=2f_A=2.

A momentum sequence {2,3,4,8}\{2, 3, 4, 8\} gaining momentum

Given a string SS consisting only of digits, determine whether there exists a momentum sequence A={a1,⋯ ,an}A=\{a_1, \cdots, a_n\} such that SS is exactly the concatenation of a1,⋯ ,ana_1, \cdots, a_n with no spaces.

When writing any aia_i as a string, leading zeros are not allowed. If more than one valid sequence exists, find one with the smallest possible fAf_A.

Input

The first line contains the string SS. The length of SS is at least 33 and at most 2 3482\,348, and every character of SS is one of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

The first character of SS is never 0.

Output

If there exists a momentum sequence AA satisfying the conditions for SS, print fAf_A as an integer. If more than one such AA exists, print the smallest possible value of fAf_A.

If no such momentum sequence exists, print 0.

Examples3

  1. Example 1

    Input
    2348
    
    Expected output
    2
    
  2. Example 2

    Input
    100000000010000000012000000002
    
    Expected output
    0
    
  3. Example 3

    Input
    123
    
    Expected output
    0