洛谷 1865 A % B Promble 欧拉筛

来源:互联网 发布:交换机网络配置 编辑:程序博客网 时间:2024/06/18 04:22

题目:
https://www.luogu.org/problem/show?pid=1865

欧拉筛+前缀和;

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=4000001;bool not_p[MAXN];int cnt[MAXN],is_p[MAXN],tot,n,m;void euler(int n){    not_p[1]=true;    for(int i=1;i<=n;i++)    {        cnt[i]=cnt[i-1];        if(!not_p[i]) is_p[++tot]=i,cnt[i]++;        for(int j=1;j<=tot;j++)        {            if(is_p[j]*i>n) break;            not_p[is_p[j]*i]=true;            if(i%is_p[j]==0) break;        }    }    return;}void solve(){    scanf("%d%d",&m,&n);    euler(n);    while(m--)    {        int x,y;        scanf("%d%d",&x,&y);        if(x<1 || y>n) printf("Crossing the line\n");        else printf("%d\n",cnt[y]-cnt[x-1]);    }}int main(){    solve();    return 0;}
原创粉丝点击