10人を5部屋に割り当てるときに空室があってもよいが1部屋の定員は3とすると 4229400通り数えられた。

library(gtools)
n=5
r=10
c=3
perm=permutations(n,r,rep=T)
tail(perm)
h <- function(x) max(tabulate(x))<=c
idx3=which(apply(perm,1,h))
perm3=perm[idx3,]
tail(perm3) #  定員3空室可
> tail(perm3)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[4229398,] 5 5 5 4 4 4 3 3 2 3
[4229399,] 5 5 5 4 4 4 3 3 3 1
[4229400,] 5 5 5 4 4 4 3 3 3 2
>

g <- function(x) all(1:n %in% x)
system.time(apply(perm3[1:1e5,],1,g)) # fast
idx35=which(apply(perm3,1,g))
perm35=perm3[idx35,]
tail(perm35) # 定員3空室不可
> tail(perm35)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[3137398,] 5 5 5 4 4 4 3 2 3 1
[3137399,] 5 5 5 4 4 4 3 3 1 2
[3137400,] 5 5 5 4 4 4 3 3 2 1

g <- function(x) all(1:n %in% x) #
system.time(apply(perm[1:1e5,],1,g)) # fast
i=which(apply(perm,1,g)) # lengthy
perm5=perm[i,] #定員なし空室不可
> tail(perm5)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[5102998,] 5 5 5 5 5 5 4 2 3 1
[5102999,] 5 5 5 5 5 5 4 3 1 2
[5103000,] 5 5 5 5 5 5 4 3 2 1