POJ 1879 Tempus et mobilius time and motion

Source: Internet
Author: User
Tags greatest common divisor

Link: Click to open link

The main idea:

There is a timer, consisting of a ball numbered from 1 to N, with three tracks, corresponding to 1 minutes, 5 minutes, and 1 hours respectively. These three tracks can be thought of as stacks, each minute out of the heap of a ball, a ball heap can be considered a queue, the capacity of three tracks is 4,11,11. The ball first enters the 1-minute orbit, and when the fifth ball enters, the ball of the one-minute track is all out of the stack and into the team row. While the fifth ball enters the 5-minute orbit, the 5-minute track fills the ball into the hour track, and the 5-minute track ball all goes out of the stack into the team row. The hour is full and so is the track. At this time after half a day. Q: How many celestial spheres are returned to the original state.

Problem Solving Ideas:

Every half-day ball completes a sequence exchange, but asks how many days it takes to simulate the sequence exchange of a day's ball and then the least common multiple of the loop. (I can only simulate the sequence exchange of the ball in a day, but how to find out how many days it takes to get back to the original state or not to understand). For details, see the code:

#include <stdio.h> #include <string.h> #include <stack> #include <queue>using namespace Std;int NEXT[7005],MARK[7005]; Long Long gcd (long long A,long long B)//greatest common divisor {if (!b) return A;elsereturn gcd (b, a% b);} Long long LCM (long long X,long long y)//least common multiple {return x/gcd (x, y) * y;} int main () {int n;while (scanf ("%d", &n), n) {queue <int> q;stack <int> min,five,hour;for (int i=1;i<=n; i++) Q.push (i);//mark the ball into the queue for (int t=0;t<12*60*2;t++)//simulate the movement of the ball in the day {int now=q.front ();//each time it comes out is the ball of the first team Q.pop (); if ( Min.size () < 4)//If the 1 minute track is not full, enter this track Min.push (now), else{for (int i=1;i<=4;i++)//If this track is full, the ball into the team column {Q.push (Min.top ( )); Min.pop ();} if (Five.size () < 11)//Then determine whether the 5 minute ball is full, not full, then enter this track Five.push (now), else{for (int i=1;i<=11;i++)//If 5 minutes the ball is full, this track ball into the team column { Q.push (Five.top ()); Five.pop ();} if (Hour.size () < 11)//To determine the hour track Hour.push (now), else{for (int i=1;i<=11;i++) {Q.push (Hour.top ()); Hour.pop ();} Q.push (now);}}} for (int i=1;i<=n;i++)//Save the position of the ball in the day to the next array {next[i]=q.front (); Q.pop ();} MemseT (mark,0,sizeof (Mark)); Long long sum=1;for (int i=1;i<=n;i++)//Find minimum number of days to complete a loop {if (!mark[i]) {Long Long cnt=1;mark[i ]=1;int t=next[i];//The marking of the first ball to T while (!mark[t]) {mark[t]=1;cnt++;t=next[t];} SUM=LCM (sum,cnt);}} printf ("%d balls cycle after%lld days.\n", n,sum);}  return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1879 Tempus et mobilius time and motion

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.