51nod 1106 質數檢測
阿新 • • 發佈:2017-08-09
eof class can name %d scanf rime rim sin
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 1e5+10; bool s[maxn]; void is_prime() { memset(s,true,sizeof(s)); s[0] = s[1] = 0; for(int i=2; i*i <= maxn;i++){ if(s[i]){ for(int j=i*i; j <= maxn;j += i) s[j]= 0; } } } int main () { scanf("%d" ,&n); is_prime(); int x; for(int i=1; i <= n;i++){ scanf("%d", &x); if( x <= maxn){ if( s[x] ) printf("Yes\n"); else printf("No\n"); }else{ bool ok = 1; for(int i=2;i*i <= maxn;i++){ if(s[i] && x%i==0){ ok = 0; break; } } if(ok) printf("Yes\n"); else printf("No\n"); } } return 0; }
暴力出奇跡
51nod 1106 質數檢測