First Solution: The answer is (n−1)n−(n−1) for part (a) and (n−1)((n−2)n−1−1) for part (b).
a. Observe that the number of good sequences is clearly the same for any choice of a0. For fixed a0, call the condition ai−ai−1≡i(modn) *condition (i)*. Now let Si be the set of sequences A={ai}i=1n,ai∈{1,2,…,n} such that A satisfies conditions (1), (2), \dots, (n−i) and fails to satisfy conditions (n−i+2), (n−i+3), \dots, (n). Note that there is no constraint on whether or not A satisfies condition (n−i+1). Finally, let F be the set of sequences that fails all conditions (1), (2), \dots, (n). Then we claim that the number of good sequences starting with a0 is
m=∣S1∣−∣S2∣+∣S3∣−⋯+∣Sn∣−∣F∣.
Consider the following table, representing the conditions on sequences in Si.
S1S2S3⋮SnF(1)(1)(1)⋮(2)(1)(2)(2)(2)⋮(3)(2)(3)(3)(3)⋮…(3)…………(n−2)…(n−2)(n−2)(n−1)⋮(n−1)(n−2)(n−1)(n)(n)⋮(n)(n−1)⋮(n)
Conditions that appear as (j) in row i are satisfied by sequences in Si, and those that appear as (j) are failed.
Now take any sequence A. If A is good, then A is counted exactly once in ∣S1∣ and is not counted anywhere else. Otherwise, if A is not good, let (i) be the first condition that A fails. If i>1, then A belongs to exactly the sets Sn−1,Sn−2. As one of ∣Sn−1∣,∣Sn−2∣ is added and one is subtracted, A has no net contribution to m. Finally, if i=1, then A belongs to Sn and F, so again A does not contribute to m. Therefore, m is indeed the number of good sequences.
We are left with computing the sizes of the Si's and F. This is easy, as the failure of the final i−1 conditions determines the terms an−1,an−2,…,an−i+1, whereas there are just n−1 ways to satisfy each of the conditions (1), (2), \dots, (n−i), producing (n−1)n−i subsequences a0,a1,…,an−i. As there is no constraint on an−i+1−an−i, we have found all sequences in Si. Hence, ∣Si∣=(n−1)n−i. As for F, there is clearly exactly one way to fail every single condition, because 1+2+⋯+(n−1)=2n(n−1) is divisible by n. Therefore, ∣Sn∣−F=0 and
m=∣S1∣−∣S2∣+⋯−∣Sn−1∣=(n−1)n−1−(n−1)n−2+⋯−(n−1)=n(n−1)n−(n−1).
As there were originally n choices for a0, multiplying by n gives the desired answer.
b. We can use the same argument as in part (a), but we must recompute the values of ∣Si∣ and ∣F∣. Note that for 1≤i≤n−1, there are exactly two ways to fail condition (i) (because n is odd) while condition (n) is degenerate and reduces to the single restriction an−an−1≡0(modn). Hence, we have
∣S1∣=(n−2)n−1and∣Si∣=2i−2(n−2)n−i,
for 2≤i≤n. Finding ∣F∣ is trickier in this case, however. Considering the differences bi=ai−ai−1, i=1,2,…,n, we wish to count the number of choices of {bi}i=1n such that
b1+b2+⋯+bn≡0(modn),
where bi=i or 2i(modn) for each i. Note that because of the degenerate condition (n), bn≡0(modn) is forced; hence, we are actually looking at 2n−1 sums b1+b2+⋯+bn−1 rather than 2n. Now, as n∣[1+2+⋯+(n−1)]=2n(n−1), we can equivalently consider sums of terms ci=bi−i. That is, we wish to find the number of choices of c1,c2,…,cn−1 such that
c1+c2+⋯+cn−1≡0(modn),
where ci=0 or i for each i. We now introduce an extra term cn that can be either 0 or n. The only effect of this extra term is to double the number of choices of {ci}i=1n compared to the number of choices of {ci}i=1n−1. That is, letting
F′={(c1,c2,…,cn)∣[t]ci=0 or i,c1+c2+⋯+cn≡0(modn)},
we have ∣F∣=21∣F′∣.
Observe that we can identify choices (c1,c2,…,cn) with subsets of T={1,2,…,n}; the elements of F′ are then simply the subsets of T that have sum divisible by n. The empty set and the set T are two sets that clearly satisfy this property. All remaining subsets U⊂T have from 1 to n−1 elements. Write U+k for the set {x+k∣x∈U} (taken mod n); this is the rotation of U by k shifts to the right. Then because n is prime and 0<∣U∣<n, the subsets U,U+1,U+2,…,U+(n−1) form an equivalence class of exactly n distinct subsets. Furthermore, the sums of the elements in U and U+k differ by k∣U∣(mod n), so exactly one element of each class has sum divisible by n. It follows that
∣F′∣=2+n2n−2,∣F∣=21∣F′∣=n2n−1+n−1.
Therefore,
m=∣S1∣−∣S2∣+∣S3∣−⋯+∣Sn∣−∣F∣=(n−2)n−1−(n−2)n−2+2(n−2)n−3−22(n−2)n−4+⋯+2n−2−n2n−1+n−1=(n−2)n−1−n(n−2)n−1−2n−1−n2n−1+n−1=n(n−1)(n−2)n−1−(n−1).
As before, multiplying by n gives the desired answer.
Second Solution: The answer is (n−1)n−(n−1) for part (a) and (n−1)((n−2)n−1−1) for part (b). Observe that the number of good sequences is clearly the same for any choice of a0. Fixing a0, we represent the choices for the differences ai−ai−1(modn) with the generating function
gi(x)=1+x+x2+⋯+xi−1+xi+1+⋯+xn−1=p(x)−xi,
where p(x)=1+x+x2+⋯+xn−1. Then the generating function representing all the possibilities for a1−a0,a2−a1,…,an−1−an−2 is
g(x)=i=1∏n−1gi(x)=i=1∏n−1(p(x)−xi).
The condition that a0−an−1≡0(modn) means that we wish to sum the coefficients of the xk terms of g(x) for n∣k.
Now the sum of all the coefficients is simply g(1)=(n−1)n−1. The sum of the coefficients of the xk terms for n∣k can be obtained by taking
n1j=0∑n−1g(ϵj),
where ϵ=e2πi/n is a primitive nth root of unity. As we noted above, g(1)=(n−1)n−1. For n>j>0, note that p(ϵj)=0, so
g(ϵj)=k=1∏n−1−ϵkj=(−1)n−1ϵn(n−1)/2j=1,
as n is odd. Therefore, the sum of the coefficients of the terms with powers not dividing n is
(n−1)n−1−n1((n−1)n−1+(n−1))=n1((n−1)n−(n−1)).
Remembering that we had n choices of a0 to begin with, we obtain (n−1)n−(n−1) as the answer to part (a).
For part (b), we change our generating functions to
hi(x)=p(x)−(xi+x2i),h(x)=i=1∏n−1hi(x).
Note that for 1≤i≤n−1, the terms xi and x2i both appear because i=2i(modn) as n is odd. For the final condition, however, n≡2n≡0(modn), so we are still counting those terms—and only those terms—with powers not divisible by n. Hence, using the same method as before, we have h(1)=(n−2)n−1,
h(ϵj)=k=1∏n−1−ϵkj(1+ϵkj)=k=1∏n−1(1+ϵk),
for 1≤j≤n−1, as n is prime. Now, 1+ϵi, i=0,1,…,n−1, are all of the roots of the polynomial (x−1)n−1. Therefore, their product is (−1)n=−1 times the constant coefficient of the polynomial, −2. Hence, the product of the roots not including i=0 (in which case 1+ϵ0=2) is 1. Therefore, h(ϵj)=1.
It follows that the number of sequences for a fixed a0 is
(n−2)n−1−n1((n−2)n−1+(n−1))=n1((n−1)((n−2)n−1−1)),
so the answer to part (b) is (n−1)((n−2)n−1−1).