#K45657. Counting Palindromic Strings
Counting Palindromic Strings
Counting Palindromic Strings
Eli is fascinated by palindromes—strings that read the same forwards and backwards. Given a set of characters and an integer length L, your task is to count how many palindromic strings of length L can be constructed using only the characters from the provided set.
Note that for a palindrome, only the first half (and the middle character if L is odd) determines the entire string. Mathematically, if there are m distinct characters in the set, the number of palindromes is given by:
- If L is even: \( m^{\frac{L}{2}} \)
- If L is odd: \( m^{\frac{L+1}{2}} \)
For example, if the set is abc
and L = 3, the answer would be \( 3^{\frac{3+1}{2}} = 3^2 = 9 \).
Your program should read input from stdin and produce output to stdout.
inputFormat
The input consists of two values separated by whitespace:
1. A non-empty string representing the available set of characters.
2. An integer L representing the required length of the palindromic strings.
Input is read from standard input (stdin).
outputFormat
Output a single integer which is the number of palindromic strings that can be formed using the given set of characters and length. The result should be written to standard output (stdout).## sample
abc
1
3