We present an analytical algorithm for deriving the shapes of the deformable mirrors to be used for multiconjugate adaptive correction on a large telescope. The algorithm is optimal in the limit where the overlap of the wave-front contributions from relevant atmospheric layers probed by the guide stars is close to the size of the pupil. The fundamental principle for correction is based on a minimization of the sum of the residual power spectra of the phase fluctuations seen by the guide stars after correction. On the basis of the expressions for the mirror shapes, so-called layer transfer functions describing the distribution of the correction of a single atmospheric layer among the deformable mirrors and the resulting correction of that layer have been derived. It is shown that for five guide stars distributed in a regular cross, two- and three-mirror correction will be possible only up to a maximum frequency defined by the largest separation of the conjugate altitudes of the mirrors and by the angular separation of the guide stars. The performance of the algorithm is investigated in the K band by using a standard seven-layer atmosphere. We present results obtained for two guide-star configurations: a continuous distribution within a given angular radius and a five-star cross pattern with a given angular arm length. The wave-front fluctuations are subjected to correction using one, two, and three deformable mirrors. The needed mirror dynamic range is derived as required root-mean-square stroke and actuator pitch. Finally the performance is estimated in terms of the Strehl ratio obtained by the correction as a function of field angle. No noise has been included in the present analysis, and the guide stars are assumed to be at infinity.