The following counts Cohen-Macaulay modules in a certain Gorenstein algebra. I search for a closed formula, see also [Elementary interpretation of a homological result](https://mathoverflow.net/questions/266800/elementary-interpretation-of-a-homological-result) .
Let $n \geq 4$ and $w >3$ and let $w$ be an unit in $\mathbb{Z}/\mathbb{Z}n$.


Let $r:=2 \inf \{ s \geq 0 | sw+1 \equiv 0 $ mod $n \}$.


How many tuples (a,b) are there with $a \in \mathbb{Z}/\mathbb{Z}n$ and $b \in \{1,...,w-1 \}$ such that $a+\frac{1+(-1)^l}{2} b + w [\frac{l+1}{2}] \neq 0 $ mod $n$ for all $ l=1,2,...,r$?([-] denotes the floor function: [2]=2, [3/2]=1) Can those tuples even be nicely parametrised? The number should be one iff $w$ divides n+1.
Here some values with the computer, the first entry of a tuple is w and the second the number of existing tuples for given n and w:


n=4: [ [ 3, 4 ], [ 5, 1 ], [ 7, 13 ], [ 9, 2 ], [ 11, 22 ], [ 13, 3 ] ]


n=5:[ [ 3, 1 ], [ 4, 9 ], [ 6, 1 ], [ 7, 10 ], [ 8, 5 ], [ 9, 25 ], [ 11, 2 ], [ 12, 19 ], 
 [ 13, 9 ], [ 14, 41 ], [ 16, 3 ] ]


n=6:[ [ 5, 16 ], [ 7, 1 ], [ 11, 41 ], [ 13, 2 ], [ 17, 66 ], [ 19, 3 ] ]


n=7:[ [ 3, 6 ], [ 4, 1 ], [ 5, 4 ], [ 6, 25 ], [ 8, 1 ], [ 9, 17 ], [ 10, 31 ], [ 11, 5 ], 
 [ 12, 13 ], [ 13, 61 ], [ 15, 2 ], [ 16, 33 ], [ 17, 56 ], [ 18, 9 ], [ 19, 22 ], 
 [ 20, 97 ], [ 22, 3 ] ]


n=8:[ [ 3, 1 ], [ 5, 11 ], [ 7, 36 ], [ 9, 1 ], [ 11, 10 ], [ 13, 36 ], [ 15, 85 ], [ 17, 2 ], 
 [ 19, 19 ], [ 21, 61 ], [ 23, 134 ], [ 25, 3 ] ]


n=9:[ [ 4, 15 ], [ 5, 1 ], [ 7, 9 ], [ 8, 49 ], [ 10, 1 ], [ 11, 26 ], [ 13, 64 ], [ 14, 5 ], 
 [ 16, 25 ], [ 17, 113 ], [ 19, 2 ], [ 20, 51 ], [ 22, 113 ], [ 23, 9 ], [ 25, 41 ], 
 [ 26, 177 ], [ 28, 3 ] ]



Counting certain tuples
First, notice that $s\equiv (-w)^{-1}\pmod{n}$ and thus all elements in the set
$$M = \{ (-wk)\bmod n\mid 1\leq k\leq s \}$$
are distinct (i.e., $|M|=s$).


Second, the restrictions on $a,b$ can be restated as
\begin{split}
a &\not\equiv -wk\pmod{n},\\
a+b &\not\equiv -wk\pmod{n}
\end{split}
for all $k\in\{1,2,\dots,s\}$.


Thinking of $[a,a+b]$ as an interval in $\mathbb{Z}/n\mathbb{Z}$, it should not start or end at elements $-wk$, and its length ($=b$) is at most $w-1$.


The total number of intervals of length at most $w-1$ is $n(w-1)$. The number of intervals starting or ending at a particular element $-wk$ is $2(w-1)$. And the number of intervals starting at elements of the form $-wk$, denoted $m$, equals the number of pairs of elements (considered as residues modulo $n$) at distance below $w$ in the set $M$.


By inclusion-exclusion, it follows that the number of pairs $(a,b)$ equals 
$$n(w-1) - 2(w-1)s + m.$$


*Examples:*


For $n=7$ and $w=3$, we have $s=2$ and $M=\{1,4\}$. So, here $m=0$, and the number of pairs $(a,b)$ is $7\cdot 2 - 2\cdot 2\cdot 2 = 6$.


For $n=8$ and $w=5$, we have $s=3$ and $M=\{ 1, 3, 6\}$. So, here $m=3$ and the number of pairs $(a,b)$ is $8\cdot 4 - 2\cdot 4\cdot 3 + 3 = 11$.




---


**UPDATE.** Let us find the number of pairs $(-k\_1w\bmod n)$ and $(-k\_2w\bmod n)$ from $M$ with $1\leq k\_1 < k\_2\leq s$ at a fixed distance $t\leq w-1$. We have $(k\_2-k\_1)(-w)\equiv t\pmod{n}$, implying that $k\_2-k\_1\equiv st\pmod{n}$ and thus $k\_2-k\_1 = (st\bmod n)$. The number of such pairs equals $\max\{s-(st\bmod n),0\}$. Similarly, for $1\leq k\_2 < k\_1\leq s$, we have $k\_1-k\_2 = n-(st\bmod n)$, and the number of such pairs is $\max\{s-n+(st\bmod n),0\}$.
It follows that
$$m = \sum\_{t=1}^{w-1} \max\{s-(st\bmod n),0\} + \max\{s-n+(st\bmod n),0\}.$$




---


*Example.* Consider the case when $w$ divides $n+1$. Then $n+1=(n-s)w$ or $n(w-1)=sw+1$, and thus $w-1$ divides $s+1$. Hence, for $t=1,2,\dots,w-1$, we have $st = n(t-1) + s - \frac{s+1}{w-1}(t-1)$.
Notice that $s - \frac{s+1}{w-1}(t-1)$ as a function of $t$ is decreasing, integer-valued, and takes values $s$ at $t=1$ and $-1$ at $t=w$. It follows that for $t\leq w-1$, it's nonnegative and thus $(st\bmod n) = s - \frac{s+1}{w-1}(t-1)$.
So, 
$$s - (st\bmod n) = \frac{s+1}{w-1}(t-1) \geq 0.$$ 
We also have 
$$s-n+(st\bmod n) = 2s - \frac{s+1}{w-1}(t-1) - n = s - \frac{s+1}{w-1}t,$$
which is nonnegative for $t\leq w-2$ (similarly to the above).


Therefore,
$$m = \sum\_{t=1}^{w-1} \frac{s+1}{w-1}(t-1) + \sum\_{t=1}^{w-2} (s - \frac{s+1}{w-1}t) = sw - 2s.$$
Correspondingly, the number of $(a,b)$ equals
$$n(w-1) - 2(w-1)s + m = sw + 1 - 2sw + 2s + sw - 2s = 1$$
as expected.


