UVA Solution 239 - Tempus et mobilius. Time and motion - Solution in C++ | Volume 2
UVA Online Judge Solution 239- Tempus et mobilius| Volume 2
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=175
Problem Name: Tempus et mobilius
Problem Number : UVA - 239
Online Judge : UVA Online Judge Solution
Volume: 2
Solution Language : C plus plus
UVA Solution 239Code in CPP:
#include <stdio.h> #include <stack> #include <queue> using namespace std; long long gcd(long long x, long long y) { for(long long t; x%y; t = x, x = y, y = t%y); return y; } int main() { int n; while(scanf("%d", &n) == 1 && n) { // simulation queue<int> Q; stack<int> minute, five, hour; for(int i = 0; i < n; i++) Q.push(i); for(int i = 0; i < 24 * 60; i++) { int x = Q.front(); Q.pop(); if(minute.size() < 4) { minute.push(x); } else { while(!minute.empty()) Q.push(minute.top()), minute.pop(); if(five.size() < 11) five.push(x); else { while(!five.empty()) Q.push(five.top()), five.pop(); if(hour.size() < 11) hour.push(x); else { while(!hour.empty()) Q.push(hour.top()), hour.pop(); Q.push(x); } } } } // permutation group int M[8192], visited[8192] = {}; for(int i = 0; i < n; i++) M[i] = Q.front(), Q.pop(); long long ret = 1; for(int i = 0; i < n; i++) { if(visited[i] == 0) { int j, len = 1; visited[i] = 1; for(j = M[i]; j != i; j = M[j]) { visited[j] = 1, len++; } ret = ret / gcd(ret, len) * len; } } printf("%d balls cycle after %lld days.\n", n, ret); } return 0; }
No comments:
Post a Comment