A generalization of IMO 1977 problem 2 Here is the IMO 1977 problem 2:

In a finite sequence of real numbers the sum of any seven successive terms is
  negative, and the sum of any eleven successive terms is positive. Determine
  the maximum number of terms in the sequence.

I would like to generalize this problem : seven -> $p$; eleven -> $q$. I have read that the result is $p+q-1-\gcd(p,q)$, I wonder how to prove it. Firstly, we shall assume that $\gcd(p,q)=1$. I proved that the number of terms is less than $p+q-2$, but I still have to prove that it can be equal to $p+q-2$. Can you exhibit an example?
Remark. It is sufficient to prove that the following square matrix is invertible:
$\newcommand{\block}[1]{
  \underbrace{1 \cdots 1}_{#1}
}$
$$\underbrace{\begin{pmatrix}
  \smash[b]{\block{p}} \\
&\ddots \\
& & \block{p}\\
\smash[b]{\block{q}} \\
&\ddots \\
& & \block{q}
  \end{pmatrix}
}_{p+q-2}$$
 A: I explain below an algorithmic construction which achieves the unique
solution to the problem obtained when one adds the further constraint
that all the consecutive $p$-sums be equal to $+1$ and all the consecutive
$q$-sums equal to $-1$. The algorithm is simple enough (although the
details are a little messy to write out), uses the Euclidean
algorithm and reminds one of the Cantor set construction. I do not know
if a simpler solution exists.
As you suggest, let us assume ${\sf gcd}(p,q)=1$. We may assume without loss that $p<q$. If you apply the Euclidean algorithm to the integers $p$ and
$q$, you obtain an increasing sequence $c_0=0<c_1=1<c_2<c_3< \ldots <c_{m-1}=p<c_m=q$ such that for every
$i$, $c_{i+1}$ is congruent to $c_{i-1}$ modulo $c_i$. We call such 
sequences $C$-sequences. The case $p=1$ is trivial, so we may assume 
$m\geq 3$. For any positive integers $i,j$, denote by
$\rho_i(j)$ the unique integer in $\lbrace 1,2, \ldots ,i\rbrace$
that’s congruent to $j$ modulo $i$. Also, define an equivalence
relation $\sim_i$ on integers by letting $x \sim_i y$ iff the two 
numbers $x$ and $y$ are both equal to $i$ or both not 
equal to $i$.
Fundamental lemma Let  $c_0=0<c_1=1<c_2<\ldots <c_m$ be a
$C$-sequence with $m\geq 3$. Then, for any $x\in[1,c_{m-1}-2]$ we have
$$
\rho_{c_2}\rho_{c_3}\ldots \rho_{c_{m-2}}\rho_{c_{m-1}}(x)
\sim_{c_2} \rho_{c_2}\rho_{c_3}\ldots \rho_{c_{m-2}}\rho_{c_{m-1}}(x+c_m) \tag{1}
$$
Proof of fundamental lemma By induction on $m$. When $m=3$, (1) reduces
to $ (**) : \rho_{c_2}(x) \sim_{c_2} \rho_{c_2}(x+c_3)$. Since $x\in [1,c_{2}-2]$, we have
$\rho_{c_2}(x)=x$, so that (**) is equivalent to 
$$\rho_{c_2}(x+c_3)\neq c_2 \Leftrightarrow c_2\not| x+c_3 
\Leftrightarrow c_2\not| x+c_1 \Leftrightarrow c_2\not| x+1 \tag{2}$$
and the rightmost condition is certainly true since $x+1\in[2,c_{2}-1]$.
Next, suppose that $m\geq 4$ and that the lemma is true at level $m-1$. Let 
$c_0=0<c_1<c_2<\ldots <c_m$ be a $C$-sequence and let $x\in[1,c_{m-1}-2]$. By hypothesis,
there is an integer $a$ with $c_m=ac_{m-1}+c_{m-2}$, so that to show (1) it will
suffice to show the following : 
$$
\rho_{c_2}\rho_{c_3}\ldots \rho_{c_{m-2}}\rho_{c_{m-1}}(x)
\sim_{c_1} \rho_{c_2}\rho_{c_3}\ldots \rho_{c_{m-2}}\rho_{c_{m-1}}(x+c_{m-2}) \tag{3}
$$
Note that $\rho_{c_{m-1}}(x)=x$. If $x \leq c_{m-1}-c_{m-2}$, then  we have
 $\rho_{c_{m-1}}(x+c_{m-2})=x+c_{m-2}$, so that 
 $\rho_{c_{m-2}}\rho_{c_{m-1}}(x)=\rho_{c_{m-2}}\rho_{c_{m-1}}(x+c_{m-2})$, and we see
 that the two numbers in (3) are equal. We may therefore assume that $x>c_{m-1}-c_{m-2}$ ;
 in that case, $y=x-(c_{m-1}-c_{m-2})$ satisfies $y\in[1,c_{m-2}-2]$, and (3)
 can be simplified to
$$
\rho_{c_1}\rho_{c_2}\ldots \rho_{c_{m-2}}(y+c_{m-1}-c_{m-2})
\sim_{c_1} \rho_{c_1}\rho_{c_2}\ldots \rho_{c_{m-2}}(y) \tag{4}
$$ 
  But $\rho_{c_{m-2}}(y+c_{m-1}-c_{m-2})=\rho_{c_{m-2}}(y+c_{m-1})$, so that (4)
  follows from the induction hypothesis at level $m-1$. This concludes the proof 
   of the lemma by induction. 
Once we have our lemma, say that an integer $x\in [1,p+q-2]$ is good if
$\rho_{c_2}\ldots \rho_{c_{m-2}}\rho_{c_{m-1}}(x) =c_2$.  Say that
$x$ is bad if it is not good. The lemma then shows that if any two numbers in the interval $I=[1,p+q-2]$  are  congruent
modulo $p$ or $q$, then they are both bad or both good. It follows that fro any $x\in[0,p+q-2]$, all the
subintervals of length $x$ in $I$ contain the same numbers of good elements (call it
$g(x)$). It follows from the fundamental lemma that
$$
g(qc_i+r)=qg(c_i)+g(r) \ \text{whenever} 0\leq r<c_i, qc_i+r\leq c_{i+1}. \tag{5}
$$
Let $i\geq 2$. There is an integer $a_i$ such that $c_i=a_ic_{i-1}+c_{i-2}$. By (5) we have
$g(c_i)=a_ig(c_{i-1})+g(c_{i-2})$, and hence
$$
c_{i-1}g(c_i)-c_ig(c_{i-1})=-(c_{i-2}g(c_{i-1})-c_{i-1}g(c_{i-2})) \tag{6}
$$
In other words $\delta_{i+1}=-\delta_i$ where $\delta_i=c_{i-1}g(c_i)-c_ig(c_{i-1})$.
As $\delta_2=1$ we deduce $\delta_i=(-1)^i$. Next, define a 
sequence $(a_1,a_2,\ldots,a_{p+q-2})$ by
$$
a_{x}=\left\lbrace\begin{array}{lcl}
 \frac{p+q-(g(p)+g(q))}{qg(p)-pg(q)}=\frac{p+q-(g(p)+g(q))}{(-1)^{m+1}} & \rm if & x \ \text{is good} \\
-\frac{g(p)+g(q)}{qg(p)-pg(q)}=\frac{g(p)+g(q)}{(-1)^m} & \rm if & x \ \text{is bad}
\end{array}\right. \tag{7}
$$ 
This sequence has the property that the sum of any successive $p$ elements is $1$,
and  the sum of any successive $q$ elements is $-1$.
A: This problem has a very simple resolution if we replace the terms $(u_k)_{k=1}^n$ by the sums $s_k=\sum_1^ku_i$ for $0 \le k \le n$ (with $s_0=0)$. Then the conditions become:
$s_{k+7} < s_k$ for $0 \le k \le n-7$
$s_{k+11} > s_k$ for $0 \le k \le n-11$  
So if $n \ge 17$, we have  
$0 = s_0 < s_7<s_{14}<s_{3} <s_{10} <s_{17}<s_{6}<s_{13}<s_{2}<s_{9}<s_{16}<s_5<s_{12}<s_1 <s_{8} <s_{15} <s_{4} <s_{11} <s_0 = 0,$
a contradiction. As a bonus, we immediately get a maximal-length sequence from this. Omitting the term $s_{17}$ gives:  
$s_{6}<s_{13}<s_{2}<s_{9}<s_{16}<s_5<s_{12}<s_1 <s_{8} <s_{15} <s_{4} <s_{11} <0 < s_7<s_{14}<s_{3} <s_{10}$
Set these equal to consecutive integers:
$s_6=-12,s_{13}=-11,\ldots,s_{11}=-1,s_7=1,\ldots,s_3=3,s_{10}=4$
Now just read off the values of $u_k$ from $u_k=s_k-s_{k-1}$ to get  
$\{5,5,−13,5,5,5,−13,5,5,−13,5,5,5,−13,5,5\}$  
This works for any $p,q$ with $(p,q)=1$. Just write down  
$s_{p-1} < \ldots < s_q < 0 < s_p < \ldots < s_{q-1}$
and assign consecutive integers to the terms. For instance, with $p=9, q=5$, we get  
$s_8 < s_3 < s_{12} < s_7 < s_2 < s_{11} < s_6 < s_1 < s_{10} < s_5 < 0 < s_9 < s_4$
So we put $s_8=-10,\ldots,s_4=2,$ and get  
$\{-3,-3,-3,11,-3,-3,-3,-3,11,-3,-3,-3\}$
