Hmm, it was some time ago I read discrete mathematics... but I'll try .

The 5 pencils can only be distributed in 1 way since they are identical, and have the same amount as the students.

The placement of the eight books can be split into 3 parts, as shown in the figure.

In part 1, the books can be distributed in 8! ways. But since the order of the books isn't important in column 5, you must divide by 4! so you get 8! / 4!. Also note that the fifth column can be in any of the five students, so multiply that by 5.

You get (8! / 4!) * 5.

In part 2, the books can be distributed in 8! ways. But the order of the books isn't important in the two last columns so you have to divide by 2! and 3!. You get 8! / (2! * 3!). The last two columns can be in any of the five students, so you have to multiply by 5 * 4 (the last column can be in any of the 5, the second last can be in any of the 5 minus the one the last one is in, giving 4).

You get (8! / (2! * 3!)) * (5 * 4).

In part 3, the books can be distributed in 8! ways. But the order of the books isn't important in the three last columns, so you have to divide by 2! three times. You get (8! / (2! * 2! * 2!)). The last three columns can be in any of the five students, so you have to multiply by 5 * 4 * 3.

You get (8! / (2! * 2! * 2!)) * (5 * 4 * 3).

Then sum all those:

(8! / 4!) * 5 +

(8! / (2! * 3!)) * (5 * 4) +

(8! / (2! * 2! * 2!)) * (5 * 4 * 3)

You might want to shorten that expression:

8! * ((5 / 24) + (10 / 6) + (15 / 2))

And the final result:

378000