Assume that $A$ has the minimal polynomial as $(x-1)(x-2)$ and c.p. as $(x-2)^2(x-1)$ 
Let $$A := \begin{pmatrix} 2 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 1\\ \end {pmatrix}$$ Find the number of matrices similar to $A$ whose entries are from $\mathbb{Z}/\mathbb{3Z}$.


Since $A$ is a $3 \times 3$ matrix and $A$ has the minimal polynomial as $(x-1)(x-2)$ and c.p. as $(x-2)^2(x-1)$.Then any matrix simialr to $A$ will have the same minimal and characteristic polynomial.
Any matrix similar to $A$ will be an upper triangular matrix or a lower triangular matrix with diagonals as $\{1,2,2\}$ and $\{2,2,1\}$.
Then number of upper triangular matrix with diagonals as $\{1,2,2\}$ is $3^3$ . Similarly number of lower triangular matrix is $3^3$.
Then the total numbers is $54$. So the total number of matrices as $2 \times 54 = 108$.The answer is $117$.What am i missing?
 A: Let consider the map
\begin{array}{lrcl}
f : & GL_3(\mathbb{Z}/3\mathbb{Z}) & \longrightarrow & GL_3(\mathbb{Z}/3\mathbb{Z}) \\
    & P & \longmapsto & PAP^{-1} \end{array}
We want to find the cardinality of the image of $f$.

*

*Let's begin by finding the cardinality of $\mathcal{C} = \lbrace B \in GL_3(\mathbb{Z}/3\mathbb{Z}), BA=AB \rbrace$.

By direct computation, one can see that a matrix
$\displaystyle{M=\begin{pmatrix}
a & b & c\\
d & e & f \\
g&h&i
\end{pmatrix} \in \mathcal{M}_3(\mathbb{Z}/3\mathbb{Z})}$ commutes to $A$ iff $c=f=g=h=0$, i.e. iff $M$ looks like $$\displaystyle{M=\begin{pmatrix}
a & b & 0\\
d & e & 0 \\
0&0&i
\end{pmatrix}}$$
For this matrix to belong to $GL_3(\mathbb{Z}/3\mathbb{Z})$, one needs $i \neq 0$ and $ae-db \neq 0$. So $\mathcal{C} \simeq GL_2(\mathbb{Z}/3\mathbb{Z}) \times \mathbb{Z}/2\mathbb{Z}$ so $\mathrm{Card}(\mathcal{C})=48 \times 2 = 96$.

*

*Now, let $P,Q \in GL_3(\mathbb{Z}/3\mathbb{Z})$. Then $f(P)=f(Q)$ iff $PAP^{-1} = QAQ^{-1}$, i.e. $f(P)=f(Q) \Longleftrightarrow Q^{-1}P \in \mathcal{C}$.

So if you fix $P$, then the set $\lbrace Q \in GL_3(\mathbb{Z}/3\mathbb{Z}), f(P)=f(Q) \rbrace$ is in bijection with $\mathcal{C}$, so for every $P$, the fiber
$$f^{-1}(f(P))$$
has exactly $96$ elements.
This gives you directly that the cardinality of the image of $f$ is equal to
$$\mathrm{Card}(\mathrm{Im}(f))=\frac{\mathrm{Card}(GL_3(\mathbb{Z}/3\mathbb{Z}))}{96} = \frac{11232}{96} = 117$$
so $\boxed{\text{there are exactly }117\text{ matrices that are similar to }A}$.
