## Editorial for LCC '22 Contest 2 S5 - Nutty Necklaces

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

In general, "stateful" transitions are hard to calculate in a loop. Hence, we begin by "unravelling" the necklace into a sequence of integers from to such that no two adjacent elements are equal. Additionally, the first and last element cannot be the same, and we must also account for overcounting (since in a necklace is equivalent to as rotations are considered the same).

We start by calculating the number of such sequences. Because of the additional constraint of the last element not being equal to the first, we will handle the two cases separately. Let be the number of sequences of length such that no two adjacent elements are the same and the first element is **different** from the last element. Let by the number of sequences of length such that no two adjacent elements are the same but the first is the **same** as the last element.

Then, (If the first and last elements are the same, just take one that isn't the last element; if the first and last elements are different, we can't choose the first or last element)

and (since no two elements are adjacent, we must have a sequence where the last element isn't the same as the first add a bead of the first color to the sequence).

Now, we go back to our original problem. We must convert our sequences to a necklace. However, we must now consider rotations. Let's say the shortest period is of length . Then, there are overcounted sequences and we must divide by .

For example, if is and is , then the sequences , , and are all equivalent (they're rotations of each other, but each counted as a separate sequence).

Now, we must solve the problem separately once again. How many sequences of length are there such that the shortest period is of length ? Consider this: Take a sequence of length , and clone it times to get a sequence of length . Obviously, this sequence must have a period of length , so the number of sequences of length with a period of is equal to . However, it might not be the shortest, but if the shortest period isn't of length , then it must be a divisor of . So, we can subtract all all sequences with a length equal to a divisor of .

More formally, if is the number of sequences of length with a shortest period of , then

Now finally, there are possible necklaces (since each necklace has "rotations" that are counted in the sequences).

So, to reiterate, we wish to find the sum of , we can actually optimize this by only considering such that , since is the period. Additionally, can be calculated if we know and for all . Notice that if , then as well. So, really only relies on other values of . So, we only need to calculate the value of such that . In general, there really aren't that many divisors of a number - in fact it works out to be roughly divisors at most. So, we can run an algorithm, where is the number of divisors. For each value of , we will first calculate and iterate through all of the remaining divisors of , subtracting if . Then, once is calculated, we add to our total.

Now, firstly, to get the list of divisors, we can iterate from to . If , then we add and to our list of divisors.

Next, to calculate , we must use matrix exponentiation. To begin, we define the transition:

and

So, . We then take and multiply that by to get . And we are done.

## Comments