Spoj NDIV Solution
This is the solution for spoj n-divisors
#include<bits/stdc++.h>
using namespace std;
int mx=32000;
vector<int> prime;
void sieve()
{
int isprime[mx],i,j;
for(i=0;i<=mx;i++)
isprime[i]=1;
isprime[0]=0;
isprime[1]=0;
for(i=4;i<=mx;i+=2)
isprime[i]=0;
for(i=3;i*i<=mx;i+=2)
{
if(isprime[i])
{
for(j=i*i;j<=mx;j+=i*2)
isprime[j]=0;
}
}
for(i=2;i<=mx;i++)
{
if(isprime[i]==1)
prime.push_back(i);
}
}
int main()
{
sieve();
int a,b,c,i,j,fans=0;
cin>>a>>b>>c;
for(i=a;i<=b;i++)
{
int n=i;
int ans=1;
int k=0;
for(j=prime[k]; j*j<=n ; j=prime[++k])
{
int cnt=0;
while(n%j==0)
{
n/=j;
cnt++;
}
ans*=cnt+1;
}
if(n!=1)
ans*=2;
if(ans==c)
fans++;
}
cout<<fans<<"\n";
return 0;
}
Comments
Post a Comment