All Tracks Problem

Avoid Prefix Palindromes
Tag(s):

Algorithms, Dynamic Programming, Introduction to Dynamic Programming 1, Medium, String

Problem
Editorial
Analytics

A special palindrome is a palindrome of size \(N \) which contains at most \(K \) distinct characters such that any prefix of size between \(2\) and \(N \) \(-1 \) is not a palindrome.

You need to count the number of special palindromes.

For example, abba is a special palindrome with N = 4 and K = 2 and ababa is not a special palindrome because aba is a palindrome and its a prefix of ababa.

If N = 3 and K = 3, possible special palindromes are aba, aca, bab, bcb, cac and cbc. So the answer will be 6.

Input format

A single line comprising two integers \(N\) and \(K\)

Output format

Print the answer to the modulo \(10^9 + 9 \).

Input Constraints

\(1 \le N \le 10^5\)

\(1 \le K \le 10^5 \)

 

SAMPLE INPUT
3 2

SAMPLE OUTPUT
2
Explanation

If we take our alphabet set as {a,b}, the two possible palindromes are aba and bab.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

Notifications
View All Notifications

?