<p>There are <code>8</code> prison cells in a row and each cell is either occupied or vacant.</p>

<p>Each day, whether the cell is occupied or vacant changes according to the following rules:</p>

<ul>
	<li>If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.</li>
	<li>Otherwise, it becomes vacant.</li>
</ul>

<p><strong>Note</strong> that because the prison is a row, the first and the last cells in the row can&#39;t have two adjacent neighbors.</p>

<p>You are given an integer array <code>cells</code> where <code>cells[i] == 1</code> if the <code>i<sup>th</sup></code> cell is occupied and <code>cells[i] == 0</code> if the <code>i<sup>th</sup></code> cell is vacant, and you are given an integer <code>n</code>.</p>

<p>Return the state of the prison after <code>n</code> days (i.e., <code>n</code> such changes described above).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> cells = [0,1,0,1,1,0,0,1], n = 7
<strong>Output:</strong> [0,0,1,1,0,0,0,0]
<strong>Explanation:</strong> The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cells = [1,0,0,1,0,0,1,0], n = 1000000000
<strong>Output:</strong> [0,0,1,1,1,1,1,0]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>cells.length == 8</code></li>
	<li><code>cells[i]</code>&nbsp;is either <code>0</code> or <code>1</code>.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

#include <vector>
#include <unordered_map>
#include <functional>

// Custom hash function for vector<int>
struct VectorHash {
    size_t operator()(const std::vector<int>& vec) const {
        std::hash<int> hasher;
        size_t result = 0;
        for (int v : vec) {
            result ^= hasher(v) + 0x9e3779b9 + (result << 6) + (result >> 2);
        }
        return result;
    }
};

class Solution {
public:
    void func(std::vector<int>& cells) {
        int n = cells.size();
        std::vector<int> temp = cells; // Use a temp vector to store the new state
        
        temp[0] = 0;
        temp[n-1] = 0;
        for (int i = 1; i < n - 1; i++) {
            if (cells[i-1] == cells[i+1]) {
                temp[i] = 1;
            } else {
                temp[i] = 0;
            }
        }
        cells = temp; // Update the original vector with the new state
    }
    
    std::vector<int> prisonAfterNDays(std::vector<int>& cells, int n) {
        std::unordered_map<std::vector<int>, int, VectorHash> seen; // Use custom hash function
        bool cycleFound = false;
        
        while (n > 0) {
            if (seen.find(cells) != seen.end()) {
                // Cycle detected
                n %= (seen[cells] - n); // Adjust n based on the cycle length
                cycleFound = true;
            }
            
            if (n > 0) {
                seen[cells] = n; // Store the state and remaining days
                func(cells); // Update the cells state
                n--;
            }
        }
        
        return cells;
    }
};

15700