While I don't know of an explicit formula giving what you want, there are good ways to simplify the problem.
First, let $\tau(n)$ be the number of divisors of $n$. If $n=ab$ with $\operatorname{GCD}(a,b)=1$, then every divisor of $n$ can be uniquely factored as a divisor of $a$ and a divisor of $b$. Because of this, $\tau(ab)=\tau(a)\tau(b)$. By induction, if we factor $n$ as a product of powers of primes $n=\prod p_i^{a_i}$, then
$\tau(n)=\prod \tau(p_i^{a_i})=\prod (a_i+1)$ because $p^k$ has divisors $1,p,p^2,\ldots p^k$. Thus, the number of divisors a number has is determined by the exponents of its prime factorization (and not the individual factors).
With this in mind, we can solve the original problem as follows.
- Find all possible factorizations of $K$. Note that there will be $\tau(K)$ of them.
- For each factorization of $K$, subtract $1$ from each of the factors to write $K=\prod (a_i+1)$ for some collection of positive integers $a_i$.
- For each collection of $a_i$, look at the possible assignment of primes such that $\prod p_i^{a_i}$ is less than $N$.
This is dependent on having a good way to generate all the primes, and the third step probably needs to be fleshed out a little, especially if one wants to be efficient, but overall, this is probably the best approach to the problem.
Note that (as mentioned in the other comments), if you let $K=2$, the problem simplifies to counting the number of primes less than $N$, and so a general formula is going to be more complicated than computing $\pi(N)$. I don't think anything less involved than the procedure above is likely to be fruitful (although you might be able to get asymptotics for specific values of $K$).