Problem 1393. A (wrong) place for everything, and everything in its (wrong) place
You have an equal number of cups and balls, each labelled from one to N. You randomly place one ball in each cup. Determine the number of possible combinations such that no balls are in the cup with a matching number. For example, if you have three balls and three cups, there are two valid solutions:
- 2, 3, 1
- 3, 1, 2
The following permutations do not meet the criteria for the reasons listed:
- 1, 2, 3 (all three balls are in the correct cups)
- 1, 3, 2 (ball 1 is in cup 1)
- 3, 2, 1 (ball 2 is in cup 2)
- 2, 1, 3 (ball 3 is in cup 3)
Good luck!
Solution Stats
Problem Comments
-
2 Comments
Jean-Marie Sainthillier
on 8 Apr 2013
I think that with this kind of problem, you can process in two steps.
A first easy problem with small N (to test perms for example). And a harder problem with big N, which
oblige to find another algorithm.
Binbin Qi
on 27 Oct 2014
http://oeis.org/A000166
Solution Comments
Show commentsGroup

Sequences & Series I
- 14 Problems
- 87 Finishers
- Sum all integers from 1 to 2^n
- Integer sequence - 2 : Kolakoski sequence
- Sum all integers from 1 to 2^n
- Project Euler: Problem 2, Sum of even Fibonacci
- Sum of first n terms of a harmonic progression
- Project Euler: Problem 6, Natural numbers, squares and sums.
- Integer Sequence - II : New Fibonacci
- Project Euler: Problem 2, Sum of even Fibonacci
- Project Euler: Problem 6, Natural numbers, squares and sums.
- Find the stride of the longest skip sequence
- Sum the Infinite Series
- Return the Fibonacci Sequence
- Sum of first n terms of a harmonic progression
- "Look and say" sequence
- Is X a Fibonacci Matrix?
- Square Digits Number Chain Terminal Value (Inspired by Project Euler Problem 92)
- Golomb's self-describing sequence (based on Euler 341)
- Alternating sum
- Integer sequence - 2 : Kolakoski sequence
- Integer Sequence - II : New Fibonacci
Problem Recent Solvers54
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!