Algorithm


Problem link- https://www.spoj.com/problems/NFACTOR/

NFACTOR - N-Factorful

 

A number is called n-factorful if it has exactly n distinct prime factors. Given positive integers ab, and n, your task is to find the number of integers between a and b, inclusive, that are n-factorful. We consider 1 to be 0-factorful.

Input

Your input will consist of a single integer T followed by a newline and T test cases. Each test cases consists of a single line containing integers ab, and n as described above.

T > 10000
1 ≤ a ≤ b ≤ 106
0 ≤ n ≤ 10

Output

Output for each test case one line containing the number of n-factorful integers in [ab].

Example

Input:
5
1 3 1
1 10 2
1 10 3
1 100 3
1 1000 0

Output:
2
2
0
8
1

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
 
#define MOD (ll)1000000007
#define pb 	push_back
#define EPS 1e-9
#define FOR(i, n)	for(int i = 0;i < n; i++)
#define pi(a)   printf("%d\n", a)
#define all(c)  c.begin(), c.end()
#define tr(container, it)   for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

void si(int &x){
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}

int numFactors[1000005];
vector<int> arr[11];	//arr[0] contains numbers which are 0 factorful.

void precal(){
	for(int i = 2;i < 1000005; i++){
		if(numFactors[i] == 0){
			numFactors[i] = 1;
			for(int j = 2*i; j < 1000005; j+=i){
				numFactors[j]++;
			}
		}
	}
}

int main(){
    int t;
    si(t);
    precal();
    for(int i = 1; i < 1000001; i++){
    	if(numFactors[i] <= 10){
    		// cout<<"HE"<<endl;
    		arr[numFactors[i]].pb(i);
    	}
    }
    while(t--){
    	int a, b, n;
    	si(a);
    	si(b);
    	si(n);
    	int res = 0;
    	//do binary search on arr[n] to find the number of numbers that lies between a and b.
    	//find first occurence of a
    	int begin = lower_bound(arr[n].begin(), arr[n].end(), a)-arr[n].begin();
    	int end = upper_bound(arr[n].begin(), arr[n].end(), b)-arr[n].begin();
    	res = end-begin;
    	pi(res);
    	// cout<<"karan"<<endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
1 3 1
1 10 2
1 10 3
1 100 3
1 1000 0

Output

x
+
cmd
2
2
0
8
1
Advertisements

Demonstration


SPOJ Solution-N-Factorful-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python