I have implemented sieve approach and it runs correctly for my test cases. I used a counter rather than a flag for all numbers and the times the numbers been accessed. When i run it on SPOJ it gives wrong answer. Here is the link http://www.spoj.com/problems/NDIV/
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int main(){
vector<long int> vec;
long int sum=0,n,i,j,k,a,b,c;
double root;
cin>>a>>b>>n;
root = sqrt(b);
for(i=1;i<=b;i++) vec.push_back(1);
for(i=2;i<=root;i++){
k=i;
c=0;
for(j=2;j<=b && c<=b;j++){
c=k*j;
if(c<=b){
vec[c-1]++;
}
}
}
for(i=a;i<=b;i++){
if(sqrt(i)-floor(sqrt(i))!=0){
if(vec[i-1]*2 == n) {
sum++;
}
}
else {
if(vec[i-1]*2-1 == n) {
sum++;
}
}
}
cout<<sum;
}