#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int table[1000009];
void print_table2()//table[i]==0,i为质数
{
int x=sqrt(1000010)+1;
for(int p=2;p<=x;p++)
{
if(table[p]!=0)
continue;
for(int q=p;q*p<=1000010;q++)
{
if(table[p]!=0)
continue;
for(int k=p*q;k<=1000010;k=k*p)
{
table[k]=1;
}
}
}
}
int main()
{
memset(table,0,sizeof(table));
print_table2();
int x;
while(~scanf("%d",&x))
printf("%d\n",table[x]);
}
说明:解决这个问题的诀窍是如何安排删除的次序,使得每一个非
质数都只被删除一次。 中学时学过一个因式分解定理,他说任何一个非质(合)数都可以分解成质数的连乘积。例如,16=2^4,18=2 * 3^2,691488=2^5 * 3^2 * 7^4等。如果把因式分解中
最小质数写在最左边,有16=4^2,18=2*9,691488=2^5 * 21609,;换句话说,把合数N写成N=p^k * q,此时q当然是大于p的,因为p是因式分解中最小的质数。由于因式分解的唯一性,任何一个合数N,写成N=p^k * q;的方式也是唯一的。 由于q>=p的关系,因此在删除非
质数时,如果已知p是质数,可以先删除p^2,p^3,p^4,... ,再删除pq,p^2*q,p^3*q,...,(q是比p大而没有被删除的数),一直到pq>N为止。
因为每个非质数都只被删除一次,时间复杂度为线性时间。