题目地址:http://acm.timus.ru/problem.aspx?space=1&num=2070
思路:质数一定满足题意(满足条件一,因子数为2为质数)。所以只需求出l到r中的合数且因子数为质数的数的个数。该数质因子只能为1(若大于一,则因子数为合数),所以枚举每个质数,若该质数的指数+1(因子数)为质数,则ans--。
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxx=1e6;
LL l,r;
int v[maxx];
vector<int> prime;
void prepare()
{
for(int i=2; i<maxx; i++)
{
if(!v[i])
{
prime.push_back(i);
for(int j=2*i; j<maxx; j+=i) v[j]=1;
}
}
}
LL solve()
{
LL ans=r-l+1;
for(int i=0; i<prime.size(); i++)
{
LL now=1,tot=0;
while(now<l) now*=prime[i],tot++;
while(now<=r)
{
if(tot>1&&!v[tot+1])ans--;
now*=prime[i],tot++;
}
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(0);
prepare();
cin>>l>>r;
solve();
return 0;
}