5 mysterious programming problems
5 mysterious programming problems
Here are five mysterious programming problems and their solutions.
1. Fib^2
fib(20) is only 10946, so we can just figure out fib(i). Then figure out fib of fib(i)
1 |
|
2. The sustainable development of the research group
It’s easy to think of a greedy strategy: If the number of teacher A’s enrollment is x, the number of scientific research is y, they can produce (x+1)*y papers at most, using the first x*C time to enroll students and the last y*B time to do scientific research.
Because the K is less than 10001, so we can just enumerate x from 1 to K. Use $y = ceil(K / (x + 1) )$ to find the minimum value of y, while the x determined.
1 |
|
3. The date of the scholarship
The range of n is so small that we can calculate it month by month: we know the day of the week on the 1st of the month, then we can calculate the day of the week on the 13th of the month. And then calculate the day of the week on the 1st of the next month based on the number of days in this month.
1 |
|
4. Horse traversal
This is a classic breadth-first search problem. Just put the initial position in your queue and start your bfs.
1 |
|
5. [COCI ‘21 Contest 2 #4] Magneti
problem link: https://dmoj.ca/problem/coci21c2p4
solution link: https://dmoj.ca/problem/coci21c2p4/editorial
Little Marko is bored of playing with shady cryptocurrencies such as Shiba Inu or XRC, which is why he decided to play with magnets. He has $n$ different magnets and a board which has $l$ available empty slots in a row, in which the magnets can be placed. Each pair of adjacent slots is exactly one centimeter apart. Each of the magnets has a radius of activity that is equal to $r_i$. This means that it will attract all magnets that are located strictly less than centimeters away (regardless of the radius of activity of the other magnet). It is possible that some magnets have the same radius of activity, but they are considered as different magnets.
Marko doesn’t like it when the magnets attract each other, so he is interested in the number of ways to place the magnets on the board so that no magnet attracts any other. All of the magnets should be placed on the board, and each empty slot may contain at most one magnet. Two ways of placing the magnets are considered different if there is a magnet which is at a different position in the first way than in the second way. As the required number can be quite large, you should output it modulo $10^9+7$.
$1 \leq n \leq 50$ and $n \leq l \leq 10000$
We sort the magnets by increasing radius and build the permutation with the following dp:
dp[i][j][d]: number of ways to arrange the first i magnets in j groups such that the sum of the lengths of the groups is d.
One group represents a segment of the permutation that is being built and is comprised of magnets and the least amount of empty space between them. The transition of the dp actually consists of adding a new magnet to one of the groups, which can be done in three ways:
- creating a new group that is made just from this magnet
- adding a magnet to one of the ends of one of the already existing groups
- connecting two existing groups by placing the new magnet between them
The solution has been explained very clearly in the link, but the state transition equation is not provided.
Here is the state transition equation, which might help you understand:
creating a new group that is made just from this magnet:
$dp[i+1][j+1][d+1] += dp[i][j][d]$
adding a magnet to one of the ends of one of the already existing groups:
$dp[i+1][j][d+r_{i+1}] += dp[i][j][d] \times j \times 2$
connecting two existing groups by placing the new magnet between them:
$dp[i+1][j-1][d + 2 \times r_{i+1} - 1] += 2 \times C^{2}_{j} \times dp[i][j][d]$
The answer is:
$\sum_{d=1}^{l}{dp[n][1][d] \times C_{l-d+n}^{n}}$
boundary condition:
$dp[0][0][0] = 1$
we can find inverse elements with linear complexity:
$P = k * x + r$
$x^{-1} \equiv -\lfloor \frac{P}{i} \rfloor \times r^{-1} \ \ (mod \ P)$
Then preprocess the factorials and their inverse elements.
So we calcuate any combination number within O(1) time.
1 |
|