Ultraviolet A 239-Tempus et mobilius. time and motion (replacement cycle)

Source: Internet
Author: User
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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.