program main implicit none integer*8::N character*800000,allocatable::S(:) integer*8,allocatable::i(:),j(:) integer*8::k,M,x,d,summ read *,N allocate(S(N)) do k=1,N read *,S(k) end do allocate(i(N),j(N)) read *, M, x, d call query(N,M,x,d,i,j) summ = 0 do k=1,M summ = summ + get_lcp(S,i(k),j(k)) end do print '(i0)',summ contains integer*8 function get_lcp(S,ik,jk) result(lcp) character*800000,allocatable,intent(in)::S(:) integer*8::ik,jk integer*8::silen,sjlen,ii lcp = 0 do ii=1,MIN(LEN_TRIM(S(ik)),LEN_TRIM(S(jk))) if(S(ik)(ii:ii).eq.S(jk)(ii:ii)) then lcp = lcp + 1 else exit end if end do end function get_lcp subroutine query(N,M,x,d,i,j) integer*8::tmp,k integer*8,intent(in)::M,d,N integer*8,intent(inout)::x integer*8,allocatable::i(:),j(:) do k=1, M i(k) = (x/(N-1))+1 j(k) = MOD(x,(N-1))+1 if(i(k).gt.j(k)) then tmp = j(k) j(k) = i(k) i(k) = tmp else j(k) = j(k) + 1 end if x = MOD((x+d),N*(N-1)) end do end subroutine query subroutine aprinter(array) integer*8::array(:) character*32::cformat='(i0, (1x,i0))' write(cformat(5:9),'(i0)'),size(array) write(*,cformat) array end subroutine aprinter end program main