This page is still under construction.

Parts of this page are still being built. What you see may change.

Joyful KMP

Time limit1sMemory limit1024 MB

Summary
Count the lowercase strings that have the same failure function as the given string, and print the K-th one in lexicographic order or OVER.
Level

Hard8 of 10

Topics
String matching, Combinatorics, Greedy
Solved
No attempts yet

Problem

Hongjun recently learned the KMP (Knuth-Morris-Pratt) algorithm and is stuck on understanding the failure function. For a string S=s1s2⋯sNS = s_1s_2\cdots s_N, its failure function consists of NN values f[1],f[2],⋯ ,f[N]f[1], f[2], \cdots, f[N], where f[i]f[i] is the length of the longest string that is both a proper prefix and a suffix of s1s2⋯sis_1s_2\cdots s_i. If no such string exists, f[i]f[i] is 00. For example, the failure function of abcabd is shown in the table below.

ii112233445566
sis_iabcabd
f[i]f[i]000000112200

To understand the failure function better, Hongjun picked a string and wants to list, in lexicographic order, every string made only of lowercase English letters that has the same failure function. You decide to help him. Write a program that counts the strings with the same failure function as the given string and finds the KK-th one in lexicographic order.

Input

The first line contains a string of length between 1 and 10610^6, inclusive. The string consists only of lowercase English letters.

The second line contains a single positive integer KK (1≤K≤9×10181 \le K \le 9 \times 10^{18}).

Output

On the first line, print the number of lowercase strings that have the same failure function as the input string, modulo 1 000 000 0071\,000\,000\,007. This number can be very large.

On the second line, print the string that comes KK-th in lexicographic order among those strings. If there are fewer than KK such strings, print OVER.

Hint

Only strings in which the first and third characters are equal, the second and fourth characters are equal, and the first and second characters differ have the same failure function as abab. So the number of such strings is 26×25=65026 \times 25 = 650.

The 100th string in lexicographic order is dzdz.

Examples2

  1. Example 1

    Input
    abab
    100
    
    Expected output
    650
    dzdz
    
  2. Example 2

    Input
    abab
    1000
    
    Expected output
    650
    OVER