TITLE: A generalization of IMO 1977 problem 2
QUESTION [7 upvotes]: 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}$$

REPLY [2 votes]: 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\}$