Using globals is not usually a good idea. It is especially bad in recursive functions, which should preferably be re-entrant. Of course you can fix your function by resetting the count in the loop, like this:
while(t--)
{
cin>>n;
count = 0; // Reset count before the recursive call
cout << factors(n) << endl;
}
You could also make factors
"wrapper" that resets the count
to free the callers from the need to reset count
before calling factors
, like this:
long long factors(long n) {
count = 0;
return factors(n, 1);
}
long long factors(long n,long f /* Remove the default */) {
... // the rest of your code
}