Counting Solutions of Constraint Satisfiability Problems:Exact Phase Transitions and Approximate Algorithm