1. 程式人生 > >1168: mxh對lfx的詢問(前綴和+素數表)

1168: mxh對lfx的詢問(前綴和+素數表)

color line tor names 素數表 sta all ext div

題目描述:

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\n",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), ‘\0‘, 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\n"); }else{ printf("YES\n"); } } return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == || ch == \n); 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; } } }

1168: mxh對lfx的詢問(前綴和+素數表)