Monday, February 7, 2011

Facebook Hacker Cup 2011 Round 1C, N-Factorful

When I opened Facebook Hacker Cup 2011 Round 1C's problems, this is the first problem that I open because I didnt have enough time to do all three.
When I thought I did this problem right because it gave me the right output for the sample problem, It turned out I got something wrong in my code because facebook told so. Then I checked my code, and apparently I forgot to change sieve of eratostheness upper limit (sqrt(n)) to n/2 for this problem, so i didnt pass to the next round :(


N-Factorful


A number is called n-factorful if it has exactly n distinct prime factors. Given positive integers a, b, 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 a, b, and n as described above.

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

Constraints
T = 20
1 ≤ a ≤ b ≤ 107
0 ≤ n ≤ 10

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <cstring>
using namespace std;
typedef long long int lli;
typedef long unsigned long llu;
#define mem_prep(varx) memset(varx,0,sizeof(varx))
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define repe(i,a,n)  for(int i=(a);i<=(n);i++)
#define forsz(i,a,v) rep(i,a,sz(v))
#define repsz(i,v) rep(i,0,sz(v))
#define vi vector<int>
#define vs vector<string>

void prepare( )
{
  freopen("nfactorful.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
int primenum;
int a,b ,n;
vector<char> prime;
vector<char> num;
void eratos(llu n){
  primenum=0;
  prime.resize(n+1,1);
  num.resize(n+1, 0);
  prime[0] = prime[1] = 0;
  for(llu i=2; i<=n; i++){
    if(i<=n/2)
      if(prime[i]){
for(llu j=i*2;j<=n;j+=i){
 prime[j]=0;
 num[j]+=1;
}
      }
    if(prime[i])primenum++;
  }
}

int main(){
  //prepare();
  int nTests;
  scanf("%d ",&nTests);
  eratos(10000000);
  repe(test,1,nTests) {
    cin>>a>>b>>n;
    int ans = 0;
    if(n==0 && a == 1){cout<<"1"<<endl;continue;}
    for (int i = a; i <= b; ++i){
      if(!prime[i] && n!= 1)if(num[i]==n){ans++;}
      if(n==1 && prime[i]) ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

No comments:

Post a Comment