This sounds like an application for genetic algorithm:
- Select a random permutation of the 40 guests - this is one seating arrangement
- Repeat the random permutation N time (n is how many times you are to switch seats in the night)
- Combine the permutations together - this is the chromosome for one organism
- Repeat for how ever many organisms you want to breed in one generation
- The fitness score is the number of people each person got to see in one night (or alternatively - the inverse of the number of people they did not see)
- Breed, mutate and introduce new organisms using the normal method and repeat until you get a satisfactory answer
You can add in any other factors you like into the fitness, such as male/female ratio and so on without greatly changing the underlying method.