1168: mxh对lfx的询问(前缀和+素数表)

题目描述:

      AS WE ALL KNOW, lfx是咱们组的神仙,但是mxh想考一考lfx一个简单的问题,以此看一下lfx到底是不是神仙。但是lfx要准备补考,于是请你来帮忙回答问题:

给定一个整数N,和N个整数ai(1<=i<=n),mxh有n个询问,即第i个询问(1<=i<=n)是数组a从1~i这i个数中,奇数的个数是否是一个质数?< span="">

如果是请输出”YES”,没有引号。反之输出”NO”。

正如你想的那样,你输出有N个输出。

数据范围:

1<=n<=1e6

1<=ai<=1e6

非常easy的一个题目吧,为什么没人过,,如果a[i] 是奇数就把a[i]赋值为1,否则赋值为0。然后求a[i]数组的前缀和,

预处素数表之后,1~n扫一下前缀和,如果是质数就输出YES反而反之就好了。题目真没什么可以说的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d
",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int sum[maxn];
const int   N = 1e7+50;
bool noprime[N+50];// true -> 不是素数
vector <int> p;
void getPrime()
{
    // 华丽的初始化
    memset(noprime,false,sizeof(noprime));
    p.clear();
 
    int m=(int)sqrt(N+0.5);
    // 多姿的线性筛
    for(int i=2;i<=m;i++)
    {
        if(!noprime[i])
        {
            for(int j=i*i;j<=N;j+=i)
            {
                noprime[j] = true;
            }
        }
    }
    noprime[1]=1;
//    // 把素数加到vector里
//    for(int i=2;i<=maxn;i++)
//    {
//        if(!noprime[i])
//        {
//            p.push_back(i);
//        }
//    }
//    //返回vector的大小
//    return p.size();
 
}
int main()
{
//    freopen("D:\common_text\code_stream\in.txt","r",stdin);
//    freopen("D:\common_text\code_stream\out.txt","w",stdout);
    getPrime();
    scanf("%d",&n);
    repd(i,1,n)
    {
        scanf("%d",&a[i]);
        if(a[i]&1)
        {
            a[i]=1;
        }else
        {
            a[i]=0;
        }
        sum[i]=sum[i-1]+a[i];
    }
    repd(i,1,n)
    {
        if(noprime[sum[i]])
        {
            printf("NO
");
        }else{
            printf("YES
");
        }
    }
    return 0;
}
 
inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '
');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/10393337.html