Sherlock and his girlfriend CodeForces - 776B (数论)
一个数若是合数则必定可以分解为多个质数相乘。有了这个定理分类讨论一下就行了。
#include<bits/stdc++.h>
using namespace std;
int a[100200],n,cnt;
void getprim()
{
memset(a,0,sizeof(a));
for(int i=2;i<=n+1;i++)
{
if(!a[i])
{
for(int j=i+i;j<=n+1;j=j+i)
a[j]=1;
}
}
}
int main()
{
cin>>n;
if(n==1)
{
cout<<1<<endl<<1<<endl;
}
else
if(n==2)
{
cout<<1<<endl<<1<<" "<<1<<endl;
}
else
{
getprim();
cout<<2<<endl;
for(int i=2;i<=n+1;i++)
{
if(!a[i])
cout<<1<<" ";
else
cout<<2<<" ";
}
cout<<endl;
}
return 0;
}