Non-Prime Factors
In many programming competitions, we are asked to find (or count the number of) Prime Factors of an integer ii. This is boring. This time, let’s count the number of Non-Prime Factors of an integer ii, denoted as NPF(i).
For example, integer 100100 has the following nine factors: {1,2⎯⎯,4,5⎯⎯,10,20,25,50,100}{1,2_,4,5_,10,20,25,50,100}. The two which are underlined are prime factors of 100100 and the rest are non-prime factors. Therefore, NPF(100)= 77.
Input
The first line contains an integer QQ (1≤Q≤3⋅1061≤Q≤3⋅106) denoting the number of queries. Each of the next QQ lines contains one integer ii (2≤i≤2⋅1062≤i≤2⋅106).
Output
For each query ii, print the value of NPF(i).
Warning
The I/O files are large. Please use fast I/O methods.
Sample Input 1 | Sample Output 1 |
---|---|
| |
https://www.cnblogs.com/Remilia-Scarlet/p/10733424.html
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
bool vis[2000005];//记录这个数是不是素数,vis=1表示该数不是质数
int ans[2000005];//记录这个数的非质数因子数量
void init()
{
//开始筛,求出所有的素数
vis[1]=1;
int m=sqrt(2000002+0.5);
for(int i=2; i<=m; ++i)
{
if(!vis[i])
{
for(int j=i*i; j<=2000002; j+=i)
{
vis[j]=1;
}
}
}//筛选结束
for(int i = 1; i <= 2000000; ++i)
{
int rt = 2000000/i;
for(int j = i; j <= rt; ++j)
{
if(vis[i]) //非质数(因为题目说求出不包含素数的因子数量)
{
++ans[i*j];//i×j包含i这个因子,所以++
}
if(vis[j] && i!=j)//这里i != j 防止重复计算(如9×9 = 81)
{
++ans[i*j];//i×j包含j这个因子,所以++
}
}
}
}
int main()
{
init();//先预处理答案
int Q;
scanf("%d",&Q);
while(Q--)
{
int n;
scanf("%d",&n);
printf("%d\n",ans[n]);
}
return 0;
}