Ultraviolet (a) 239-Tempus et mobilius. Time and Motion
Question Link
Question: This question is also very difficult. After reading it for a long time, it is probably like this:
There is a ball-dropping queue, and three tracks (in other words, it is a stack), one containing five or one 12, one 12, each 1 minute the team list a small ball, put into the stack, if 5 is full, put 5 back into the queue, and put the first one into 12. If 12 is full, put 12 back into the queue, the first one is placed in the stack of another 12. If it is full again, it will all be put back into the queue (the first one is last put back). After how many days, the ball in the queue will return to the original status.
Train of Thought: First simulate the situation of a day, corresponding to a replacement, and then find the maximum public multiple of the cycle in the replacement.
Code:
#include <stdio.h>#include <string.h>#include <queue>#include <stack>using namespace std;const int N = 7005;int n, next[N], vis[N];long long gcd(long long a, long long b) {if (!b) return a;return gcd(b, a % b);}long long lcm(long long a, long long b) {return a / gcd(a, b) * b;}int main() {while (~scanf("%d", &n) && n) {queue<int> Q;stack<int> mins, fives, hours;for (int i = 0; i < n; i++)Q.push(i);for (int t = 0; t < 1440; t++) {int now = Q.front();Q.pop();if (mins.size() == 4) {for (int i = 0; i < 4; i++) {Q.push(mins.top());mins.pop(); } if (fives.size() == 11) { for (int i = 0; i < 11; i++) { Q.push(fives.top()); fives.pop(); } if (hours.size() == 11) { for (int i = 0; i < 11; i++) { Q.push(hours.top()); hours.pop(); } Q.push(now); } else hours.push(now); } else fives.push(now); } else mins.push(now); } for (int i = 0; i < n; i++) { next[i] = Q.front(); Q.pop();}memset(vis, 0, sizeof(vis));long long ans = 1;for (int i = 0; i < n; i++) {if (!vis[i]) {long long cnt = 1;vis[i] = 1;int t = next[i];while (!vis[t]) {cnt++;vis[t] = 1;t = next[t]; } ans = lcm(ans, cnt); } } printf("%d balls cycle after %lld days.\n", n, ans); }return 0;}