program main implicit none integer*8::N,D,i,curr,next,total integer,allocatable::ashiba(:) read *,N,D allocate(ashiba(0:D)) ashiba = 1 curr = 0 do i=1,D ashiba(curr) = 0 total = (N-1)/D + total next = MOD(curr+(N/D+1)*D,N) if(ashiba(next).eq.0) exit curr = next end do print '(i0)',total end program main