IDENTIFICATION DIVISION.
PROGRAM-ID. 158B.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(100).
01 N          PIC 9(19).
01 A          PIC 9(18).
01 B          PIC 9(18).
01 X          PIC 9(18).
01 Y          PIC 9(18).
01 SUM-A      PIC 9(19).
01 SUM-B      PIC 9(19).
01 CNT        PIC 9(18).
01 BK-CNT     PIC 9(19).

01 OUT-CNT    PIC Z(18)9.
 
PROCEDURE DIVISION.
MAIN-001.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ALL ' '
      INTO N A B.
    
  EVALUATE TRUE
    WHEN A = 0
      MOVE 0 TO OUT-CNT
    WHEN B = 0
      MOVE N TO OUT-CNT
    WHEN OTHER
      MOVE 0 TO SUM-A SUM-B
*>
          
      COMPUTE X = N ** (1 / A) 
      COMPUTE Y = N ** (1 / B)
      EVALUATE TRUE 
        WHEN (A = 1) AND (B = 1)
          COMPUTE CNT = N ** 0.5
        WHEN (A = 1)
          MOVE Y TO CNT
        WHEN (B = 1)
          MOVE X TO CNT
        WHEN OTHER
          IF (X < Y)
            MOVE X TO CNT
          ELSE
            MOVE Y TO CNT
          END-IF  
      END-EVALUATE
*>
      IF (CNT > 1)
        COMPUTE CNT = CNT - 1
      END-IF
*>
      COMPUTE SUM-A = A ** CNT
      COMPUTE SUM-B = B ** CNT
      PERFORM UNTIL (N <= SUM-A + SUM-B)
        COMPUTE SUM-A = SUM-A + A
        COMPUTE SUM-B = SUM-B + B
      END-PERFORM

      COMPUTE BK-CNT = SUM-A + SUM-B - N
      IF (BK-CNT <= B)
        MOVE SUM-A TO OUT-CNT 
      ELSE
        COMPUTE OUT-CNT = SUM-A - (BK-CNT - B)
      END-IF
  END-EVALUATE.

  DISPLAY OUT-CNT.
 
MAIN-EXIT.
  STOP RUN.
