5. Divide four students, A, B, C, and D, into three different classes, with each class having at least one student, and students A and B cannot be in the same class. The number of different ways to do this is ( ).
(A) 24
(B) 30
(C) 36
(D) 81
5. B.

First, choose two classes out of three to arrange A and B, which can be done in $\mathrm{C}_{3}^{2} \mathrm{~A}_{2}^{2}$ ways. Then arrange C and D: if both C and D are in the class without A or B, there is 1 way to choose; if one is in the class without A or B, and the other is in a class with A or B, there are $\mathrm{C}_{2}^{1} \mathrm{C}_{2}^{1}$ ways to choose.
Thus, the number of methods required is
$$
\mathrm{C}_{3}^{2} \mathrm{~A}_{2}^{2}\left(1+\mathrm{C}_{2}^{1} \mathrm{C}_{2}^{1}\right)=30
$$