A technique that is particularly helpful in problems like the ones you've given is to construct the cartesian product of two finite automata. (The relevant part of the link starts at slide 26.) In your situation, you'll construct two FA, one, $M_1$, for the language
$$
L_1 = \{a^k \mid k\equiv 0\pmod X\}\quad\text{i.e, strings of $a$s with length a multiple of $X$}
$$
One such machine will have $X$ states, $p_n,\ (0\le n < X)$, each corresponding to the situation where the number of $a$s seen so far is congruent to $n\bmod X$. It's clear that the transitions in $M_1$ will be $\delta(p_n, a) = p_{n+1 \pmod X}$.
In a similar way, construct another FA, $M_2$ for the language
$$
L_2 = \{b^k \mid k\equiv 0\pmod Y\}\quad\text{i.e, strings of $b$s with length a multiple of $Y$}
$$
As above, denote the states of $M_2$ by $q_n,\ (0\le n < Y)$ with transitions defined to be $\delta(q_n, a) = q_{n+1 \pmod Y}$.
Now here's where the product construction comes in. Make a new FA whose states are pairs of states $(p_i, q_j)$ each component of which acts like the ones in $M_1$ and $M_2$. In other words, we'll have $X\cdot Y$ such pairs, with transitions
$$\begin{align}
\delta((p_i, q_j), a) &= (p_{i+1\pmod X}, q_j) \\
\delta((p_i, q_j), b) &= (p_i, q_{j+1\pmod X})
\end{align}$$
For example, with $X=2, Y=3$ the product machine will have 6 states, $(0,0),(0,1),(0,1),(1,0),(1,1),(1,2)$, and if we happen to be in state $(1,1)$ and see input $abb$ our transitions will be
$$
(0, 1)\stackrel{a}{\longrightarrow}(1, 1)\stackrel{b}{\longrightarrow}(1, 2)\stackrel{b}{\longrightarrow}(1, 0)
$$
Now what should the final state of this machine be? In your first example, it's easy to see that it should be $(0,0)$, namely when we've seen an input where the number of $a$s is divisible by $X$ and the number of $b$s is divisible by $Y$. Similarly, in your second example, the final state should be $(P, Q)$.
[It's worth noting that the construction of this product machine, $M_1\times M_2$, works for any FA $M_1, M_2$ and, by suitably choosing the final states, can be used to construct machines for the union, intersection, and difference of any regular languages.]