       PROGRAM ZSVDW2

C   K IS THE LENGTH OF THE AP 
C   COLORS FIXED AT 2 (DON'T CHANGE R)
C   MD IS THE MODULO FOR THE ZERO SUM AP   

       IMPLICIT NONE
       INTEGER*2  R,K,MAXLENGTH,MD
       PARAMETER (K=10,MD=5,R=2,MAXLENGTH=2000)
     
       CHARACTER*16 FILENUM      
       INTEGER*2  ADDPOINT,MAXL,I,J,L,S,C,FLGSTOP,PROG
       INTEGER*2  FLG4444,T,MEMBER,USEDCOLOR(R,MAXLENGTH)
       INTEGER*2  COLORING(MAXLENGTH),NUMUSEDCOLOR(MAXLENGTH)

       
C FORMAT FOR FILENAME:  KZSTwoMD, WHERE K=LENGTH, MD=MODULO

       FILENUM='10ZSTwo5'
  
       ADDPOINT=1
       MAXL=0



 1111  NUMUSEDCOLOR(ADDPOINT)=0
         
       

 2222  CALL NEWCOL(COLORING,ADDPOINT,R,MAXLENGTH,
     *USEDCOLOR,NUMUSEDCOLOR,FLG4444,K,MD)

       IF (FLG4444.EQ.1) THEN
          GO TO 4444
       END IF



 3333  CALL ACCEPT(COLORING,ADDPOINT,MAXL,
     *MAXLENGTH,FILENUM,R,FLGSTOP)

       IF (FLGSTOP.EQ.1) THEN
         ADDPOINT=2
         NUMUSEDCOLOR(1)=R
         GO TO 4444
       ELSE
         GO TO 1111
       END IF



 4444  ADDPOINT=ADDPOINT-1
       IF (ADDPOINT.EQ.1.AND.NUMUSEDCOLOR(1).EQ.R) THEN
          OPEN(UNIT=10,FILE=FILENUM)
          WRITE(10,*) 'ANSWER IS ZSAP(',K, ',',R,')=' , MAXL+1
       ELSE
          GO TO 2222
       END IF
       STOP
       END



       SUBROUTINE NEWCOL(COLORING,ADDPOINT,R,MAXLENGTH,
     *USEDCOLOR,NUMUSEDCOLOR,FLG4444,K,MD)
       IMPLICIT NONE
       INTEGER*2  ADDPOINT,R,MAXLENGTH,J,D,K,MD
       INTEGER*2  COLORING(MAXLENGTH),NUMUSEDCOLOR(MAXLENGTH)
       INTEGER*2  CL,PROG,UC(R),I,FLG4444
       INTEGER*2  USEDCOLOR(R,MAXLENGTH),N,MEMBER


  55   N=NUMUSEDCOLOR(ADDPOINT)
       
       IF (N.EQ.R) THEN
         FLG4444=1
         RETURN
       END IF

       IF (N.EQ.0) THEN
          J=0
          GO TO 77
       END IF

       DO I=1,N
          UC(I)=USEDCOLOR(I,ADDPOINT)
       END DO  

       FLG4444=0
       
       J=-1
       DO I=1,R
          IF (MEMBER(I,UC,N).EQ.0) THEN
             J=I
             GO TO 77
          END IF
       END DO
      
  77   IF (J.EQ.-1) THEN
          FLG4444=1
          RETURN
       ELSE
          NUMUSEDCOLOR(ADDPOINT)=N+1
          USEDCOLOR(N+1,ADDPOINT)=J


          CL=J

          IF (ADDPOINT.GE.K) THEN
            D=PROG(COLORING,ADDPOINT,CL,K,R,MAXLENGTH,MD)
          ELSE
            D=0
          END IF
       END IF

       IF (D.EQ.1) THEN
          GO TO 55
       ELSE
          COLORING(ADDPOINT)=J
       END IF 
       END



       SUBROUTINE ACCEPT(COLORING,ADDPOINT,MAXL,
     *MAXLENGTH,FILENUM,R,FLGSTOP)
       IMPLICIT NONE
       INTEGER*2 ADDPOINT,MAXL,MAXLENGTH,R,FLGSTOP
       INTEGER*2 COLORING(MAXLENGTH),I
       CHARACTER*8 FILENUM

       FLGSTOP=0

       IF (ADDPOINT.GT.MAXL) THEN
          MAXL=ADDPOINT
          OPEN(UNIT=10,FILE=FILENUM)
          WRITE(10,*) 'MAXL UP TO' , MAXL
          WRITE(10,*) 'A VALID COLORING:',(COLORING(I),I=1,MAXL)
          IF (MAXL.GT.MAXLENGTH) THEN
            WRITE(10,*) 'OUT OF BOUNDS'
            FLGSTOP=1
          END IF
          CALL FLUSH() 
       END IF

       ADDPOINT=ADDPOINT+1
       
       END



       INTEGER*2 FUNCTION MEMBER(ELT,LST,LNGTH)
       IMPLICIT NONE
       INTEGER*2 LNGTH,F,ELT,LST(LNGTH)

       DO F=1,LNGTH
            IF (ELT.EQ.LST(F)) THEN
	      MEMBER=1
	      RETURN
	    END IF
       END DO
       MEMBER=0
       RETURN
       END



       INTEGER*2 FUNCTION PROG(COLORING,ADDPOINT,CL,K,R,MAXLENGTH,MD)
       IMPLICIT NONE
       INTEGER*2 ADDPOINT,MAXLENGTH,CL,MD
       INTEGER*2 COLORING(MAXLENGTH),K,R
       INTEGER*2 TALLY,G,I


C   RETURNS 0 IF NO ZERO SUM K-TERM AP ENDING WITH ADDPOINT
C   COLORING IS THE CURRENT COLORING WITHOUT ADDPOINT
C   CL IS THE PROPOSED COLOR OF ADDPOINT
C   TALLY IS THE FUNCTION THAT BUILDS THE SUM OF COLORS OF AP


       TALLY=CL

       DO G=1,(ADDPOINT-1)/(K-1)
         DO I=1,K-1
           TALLY=TALLY + COLORING(ADDPOINT-I*G)
         END DO
         IF (MOD(TALLY,MD).EQ.0) THEN
           PROG=1
           RETURN
         ELSE
           TALLY=CL
         END IF
       END DO

       PROG=0

       END


