Skip to content

Instantly share code, notes, and snippets.

@erjiaqing
Created February 3, 2014 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erjiaqing/8783802 to your computer and use it in GitHub Desktop.
Save erjiaqing/8783802 to your computer and use it in GitHub Desktop.
线性欧拉筛+素数筛
#include <cstdio>
using namespace std;
const int maxn=1e7+7;
int prime[maxn],phi[maxn],n,totprime;
int main()
{
scanf("%lld",&n);
phi[1]=1;
for (int i=2;i<=n;i++)
{
if (!phi[i])
{
prime[totprime++]=i;
phi[i]=i-1;
}
for (int j=0;j<totprime&&i*prime[j]<=n;j++)
{
if (i%prime[j])
phi[i*prime[j]]=phi[i]*(prime[j]-1);
else
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment