1404. Broken Necklace — Bitmask Circular DP

HardBit ManipulationCircular DPCombinatoricsCounting

A necklace has n beads arranged in a circle, and each bead is painted with one of k colours. Count the colourings in which no two adjacent beads (including the wrap-around pair joining the last and first bead) share the same colour, and return the count modulo 1000000007.

Input: A JSON object {"n": <number of beads>, "k": <number of colours>}.

Output: Return the number of valid cyclic colourings modulo 1000000007.

Examples

Example 1
Input: {"n":3,"k":3}
Output: 6
Explanation: A triangle has 6 proper 3-colourings.
Example 2
Input: {"n":4,"k":3}
Output: 18
Explanation: A 4-cycle has 18 proper 3-colourings.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →