#include using namespace std; int CountNextRandomNumber(int Z, int L, int I, int M) { return (Z * L + I) % M; } int GetCycleLength(int* head, int* pivot, int checkValue) { if(head == pivot) return 0; else { int *ptr = head; while(ptr != pivot) { //It's a cycle if(checkValue == *ptr) { int cycleLength = 0; while(ptr != pivot) { cycleLength++; ptr++; } return cycleLength; } else ptr++; } return 0; } } void PrintAnswer(int caseNumber, int cycleLength) { cout << "Case " << caseNumber << ": " << cycleLength << "\n"; } int main() { int Z, I, M, L; int caseNumber = 1; cin >> Z >> I >> M >> L; while(Z != 0 && I != 0 && M != 0 && L != 0) { // New a M size array to keep random numbers that have already shown int* randomNumbers = new int[M]; int* pivot = randomNumbers; while(true) { int cycleLength = GetCycleLength(randomNumbers, pivot, L); if(cycleLength != 0) { PrintAnswer(caseNumber, cycleLength); break; } else { *pivot++ = L; L = CountNextRandomNumber(Z, L, I, M); } } cin >> Z >> I >> M >> L; caseNumber++ ; } return 0; }
文章標籤
全站熱搜
創作者介紹
創作者 eric30 的頭像
eric30

*Our Blog*

eric30 發表在 痞客邦 留言(0) 人氣(0)