洛谷 P1865 A%B

emmm

线性筛+前缀和水题

代码

#include
#include
#include
#include
using namespace std;
int n,m,emm[1000500];
int sum[1000500];
inline void xxss(int a)
{
    sum[2]=1;
    for (int i=2;i<=a;++i)
    {
        if (!emm[i])
        {
            for (int j=1;j*i<=a;++j) emm[j*i]=1;
            	sum[i]=sum[i-1]+1;
        }
        else sum[i]=sum[i-1];
    }	
    return ;
}
inline int read()
{
    int wc=0,emm=1;
    char c=getchar();
    while(c<'0'||c>'9') 
    {
        c=getchar();if (c=='-') emm=-1;
    }
    while(c>='0'&&c<='9') wc=wc*10+c-'0',c=getchar();
    return wc*emm;
}
int main()
{
    int x,y;
    scanf("%d%d",&n,&m);
    xxss(m);
    while (n--)
    {
    	int ans=0; 
    	x=read();y=read();
    	if (x<1||y>m) printf("Crossing the line\n");
    	else
    	{
   		 	printf("%d\n",sum[y]-sum[x-1]);
        }
    }
    return 0;
}

你可能感兴趣的:(数论&&组合数学)