Palindrome Smart
Time limit3sMemory limit128 MB
Count palindromic strings of length 1 to N built from lowercase letters that use at most K distinct letters, modulo 1234567891.
- Level
Medium7 of 10
- Topics
- Combinatorics, Math, Number theory, String
- Solved
- No attempts yet
Problem
A certain book gives the following task.
Consider every palindrome string whose length is at least 1 and at most N. Each string must consist only of lowercase English letters, and each string may contain at most K distinct letters.
Given N and K, compute how many palindrome strings satisfy these conditions. A palindrome reads the same from left to right and from right to left; strings such as wow and abba are palindromes.
Input
The first line contains two positive integers N and K.
- 1 <= N <= 1,000,000,000
- 1 <= K <= 26
Output
Print the number of palindrome strings satisfying the conditions, modulo 1234567891.