5. On a piece of paper, the positive integers $1,2, \cdots, n$ are written. In the first step, the first 4 numbers are crossed out, and the sum of these 4 numbers, 10, is written at the end of the sequence; in the second step, the next 4 numbers $5,6,7,8$ are crossed out, and their sum, 26, is written at the end; and so on (i.e., in each step, the first 4 numbers are crossed out, and the sum of these 4 numbers is written at the end).
(1) If only one number remains at the end, what is the necessary and sufficient condition for $n$?
(2) For $n=2002$, what is the total sum of all the numbers written (including the original $1,2, \cdots, 2002$) until only one number remains?
(2002 Shanghai High School Competition Problem)
5.(1) With each step, the number on the paper decreases by 3. Assuming after $p$ steps, only 1 number remains on the paper, then $n-3p=1$, which means $n=3p+1$. Therefore, the necessary and sufficient condition for $n$ is that the remainder when $n$ is divided by 3 is 1.
(2) Suppose there are $4^{k}$ numbers initially, with their sum being $S$. When $4^{k}$ numbers are crossed out, it takes $4^{k-1}$ steps, leaving $4^{k-1}$ numbers on the paper. The sum of these $4^{k-1}$ numbers equals the sum $S$ of the original $4^{k}$ numbers. Therefore, when only one number remains, the total sum of the numbers written is $(k+1)S$.
$2002=4^{5}+978$, the original numbers, after $\frac{978}{3}=326$ steps, leave $4^{5}$ numbers, with $326 \times 4=1304$ numbers being crossed out, which are $1,2,3, \cdots, 1304$, and the sum of the $4^{5}$ numbers left on the paper is $1+2+3+\cdots+2002$. Therefore, when only one number remains, the total sum of the numbers written is $(1+2+3+\cdots+1304)+6(1+2+3+\cdots+2002)=12880878$.