       PROGRAM MIXZSVDW

C   K IS THE LENGTH OF MONOCHROMATIC AP 
C   K2 IS THE LENGTH OF ZERO-SUM AP
C   R IS THE NUMBER OF COLORS:  0,1,2,...,R-1 ARE THE COLORS
   

       IMPLICIT NONE
       INTEGER*2  R,K,K2,MAXLENGTH
       PARAMETER (K=5,K2=5,R=4,MAXLENGTH=200)
     
       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:  KLMZSR, WHERE K=LENGTH, R=NUMBER COLORS

       FILENUM='55MZS4'
  
       ADDPOINT=1
       MAXL=0



 1111  NUMUSEDCOLOR(ADDPOINT)=0
         
       

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

       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 MZSAP(',K, ',',K2,',',R,')=' , MAXL+1
       ELSE
          GO TO 2222
       END IF
       STOP
       END



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

       MN=MIN(K,K2)

  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.MN) THEN
            D=PROG(COLORING,ADDPOINT,CL,K,K2,R,MAXLENGTH)
          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,K2,R,MAXLENGTH)
       IMPLICIT NONE
       INTEGER*2 ADDPOINT,MAXLENGTH,CL
       INTEGER*2 COLORING(MAXLENGTH),K,R,K2
       INTEGER*2 TALLY,G,I,J


C   RETURNS 0 IF NO ZERO SUM K2-TERM AP AND NO
C   K-TERM MONOCHROMATIC AP OF COLOR NEQ TO 0 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
C   AND ALSO KEEPS TRACK OF NUMBER OF SAME COLORED ELEMENT IN AP


C   FIRST CHECK IS ZERO-SUM PART

       TALLY=CL

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

C   NOW CHECK MONOCHROMATIC

       IF (CL.NE.0) THEN

       TALLY=1

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

       END IF

       PROG=0

       END



