Solution to the CountDiv problem in C++