Finding dimension of a vector space, Hoffman and Kunze problem page 190 Let A be an $n\times n$ diagonal matrix with characteristic polynomial: 
$$\prod_{i=1}^{k}(x-c_{i})^{d_{i}}$$
where $c_{1},...,c_{k}$ are distinct.
Let $V$ be the space of all $n\times n$ matrices $B$ such that $AB=BA$.
Prove that the dimension of $V$ is $$\sum_{i=1}^{k}d_{i}^{2}.$$
I have no idea how to proceed. Thanks in advance.

Note that $A$ is diagonal so we have the direct sum decomoposition
$$ \mathbb{F}^n = \bigoplus_{i=1}^k \ker(A - c_i I) $$
where each $V_i := \ker(A - c_i I)$ is a $d_i$-dimensional eigenspace of $A$ associated to the eigenvalue $c_i$. If $BA = AB$ then each $\ker(A - c_i I)$ is $B$-invariant (so $BV_i \subseteq V_i$). On the other hand, if $BV_i \subseteq V_i$ then using the direct sum decomposition it is easy to see that $AB = BA$. 
Hence, the subspace $C_A$ you are looking for is $\{ B \in M_n(\mathbb{F}) \, | \, BV_i \subseteq V_i \}$. You can always conjugate $A$ by a permutation matrix to make sure that the diagonal entries of $A$ come as
$$ \underbrace{c_1, \dots, c_1}_{d_1 \text{ times}}, \dots, \underbrace{c_k, \dots, c_k}_{d_k \text{ times}} $$
and this won't change the dimension of the space $C_A$. Assuming this is the case, $C_A$ consists of block-diagonal matrices $\operatorname{diag}(B_1,\dots,B_k)$ where each $B_i$ is an arbitrary $d_i \times d_i$ matrix. Hence, 
$$ \dim C_A = \sum_{i=1}^k d_i^2. $$
