       PROGRAM AB 

C   R IS THE NUMBER OF COLORS:  1,2,...,R ARE THE COLORS
C   NUMCOLOR(J) IS THE NUMBER OF INTEGERS OF COLOR J
C   IN THE CURRENT COLORING

       IMPLICIT NONE
       INTEGER*2  R,A,B,MAXLENGTH
       PARAMETER (R=4,A=2,B=2,MAXLENGTH=1000)
     
       CHARACTER*8 FILENUM      
       INTEGER*2  ADDPOINT,MAXL,I,J,S,C,FLG4444,T,X
       INTEGER*2  TEMPSET(MAXLENGTH)
       INTEGER*2  COLORING(R,MAXLENGTH),NUMUSEDCOLOR(MAXLENGTH)
       INTEGER*2  MAXCOLS(R,MAXLENGTH),NUMCOLOR(R)
       INTEGER*2  USEDCOLOR(R,MAXLENGTH),NUMCOLMAX(R)
       INTEGER*2  PROG,COLOR,MEMBER,ADDPNTCOLOR(MAXLENGTH)
       
       FILENUM='22-4'

       DO J=1,R
          NUMCOLOR(J)=0
       END DO      
       ADDPOINT=1
       MAXL=0

 1111  NUMUSEDCOLOR(ADDPOINT)=0

 2222  CALL NEWCOL(COLORING,ADDPOINT,R,MAXLENGTH,
     *USEDCOLOR,NUMUSEDCOLOR,FLG4444,FILENUM,
     *NUMCOLOR,ADDPNTCOLOR,A,B)

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

 3333  CALL ACCEPT(COLORING,NUMCOLOR,ADDPOINT,MAXL,MAXCOLS,
     *MAXLENGTH,FILENUM,R,NUMCOLMAX)

       GO TO 1111

 4444  ADDPOINT=ADDPOINT-1
       IF (ADDPOINT.EQ.1) THEN
          OPEN(UNIT=10,FILE=FILENUM)
          WRITE(10,*) 'ANSWER IS W(',A, ',' , B , ')=' , MAXL+1
       ELSE
         IF (NUMCOLOR(ADDPNTCOLOR(ADDPOINT)).GE.1) THEN
          NUMCOLOR(ADDPNTCOLOR(ADDPOINT))=
     * NUMCOLOR(ADDPNTCOLOR(ADDPOINT))-1
         END IF
         GO TO 2222
       END IF
       STOP
       END

       SUBROUTINE NEWCOL(COLORING,ADDPOINT,R,MAXLENGTH,
     *USEDCOLOR,NUMUSEDCOLOR,FLG4444,FILENUM,
     *NUMCOLOR,ADDPNTCOLOR,A,B)
       IMPLICIT NONE
       INTEGER*2  ADDPOINT,R,MAXLENGTH,J,D
       INTEGER*2  COLORING(R,MAXLENGTH)
       INTEGER*2  COLOR,PROG,UC(R),I,FLG4444
       INTEGER*2  USEDCOLOR(R,MAXLENGTH),NUMUSEDCOLOR(MAXLENGTH)
       INTEGER*2  TEMPMSET(R),MEMBER,NUMCOLOR(R),N,A,B
       INTEGER*2  CCLASS(MAXLENGTH),ADDPNTCOLOR(MAXLENGTH)
       CHARACTER*8 FILENUM

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

       IF (N.EQ.0) THEN
          J=1
          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
          DO I=1,NUMCOLOR(J)
            CCLASS(I)=COLORING(J,I)
          END DO
          D=PROG(CCLASS,A,B,ADDPOINT,R,MAXLENGTH,NUMCOLOR(J))
       END IF

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

       SUBROUTINE ACCEPT(COLORING,NUMCOLOR,ADDPOINT,MAXL,MAXCOLS,
     *MAXLENGTH,FILENUM,R,NUMCOLMAX)
       IMPLICIT NONE
       INTEGER*2 ADDPOINT,MAXL,MAXLENGTH,R
       INTEGER*2 COLORING(R,MAXLENGTH),MAXCOLS(R,MAXLENGTH)
       INTEGER*2 J,L,NUMCOLOR(R),NUMCOLMAX(R),X,I
       CHARACTER*8 FILENUM

       IF (ADDPOINT.GT.MAXL) THEN
          MAXL=ADDPOINT
          DO J=1,R
             NUMCOLMAX(J)=NUMCOLOR(J)
             DO L=1,NUMCOLOR(J)
               MAXCOLS(J,L)=COLORING(J,L)
             END DO
          END DO
          OPEN(UNIT=10,FILE=FILENUM)
          WRITE(10,*) 'MAXL UP TO' , MAXL
          WRITE(10,*) 'VALID COLORING:'
            DO X=1,R
              WRITE(10,*) 'COLOR' , X ,': ',(MAXCOLS(X,I),I=1,
     * NUMCOLMAX(X))
            END DO
          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(CCLASS,A,B,ADDPOINT,
     *R,MAXLENGTH,NUMCCLASS)
       IMPLICIT NONE
       INTEGER*2 NUMCCLASS,ADDPOINT,R,MAXLENGTH,MEMBER,I 
       INTEGER*2 CCLASS(MAXLENGTH),A,B,C,D,M,X,Y

       IF (NUMCCLASS.LT.2) THEN
          PROG=0
          RETURN 
       END IF 

       M=ADDPOINT
       DO I=1,NUMCCLASS-1
         X=CCLASS(I)
         Y=B*X
         IF (Y.GE.M-1) THEN
           PROG=0
           RETURN
         ELSE
           C=M-Y
           IF (2*(C/2).EQ.C) THEN
             D=C/2
             IF (MEMBER(A*X+D,CCLASS,NUMCCLASS).EQ.1) THEN
               PROG=1
               RETURN
             END IF
           END IF
         END IF
       END DO 

       PROG=0
       RETURN
       END
