HDU5391——素数筛——Zball in Tina Town
http://acm.hdu.edu.cn/showproblem.php?pid=5391
/* 求(n-1)!%n 先打个表发现与素数有关,而且4特判,然后只要尽可能降低复杂度就行 */ /************************************************ * Author :Powatr * Created Time :2015-8-16 20:48:39 * File Name :HDU5391.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int prim[MAXN] = {2}; bool is_prime(int x) { for(int i = 0 ; prim[i]*prim[i] < x; i++){ if(x % prim[i] == 0) return false; } return true; } int main(){ int T, n; scanf("%d", &T); int cnt = 1; for(int i = 3; i <= MAXN; i++) if(is_prime(i)) prim[cnt++] = i; while(T--){ scanf("%d", &n); int i; if(n == 4) printf("2\n"); else{ if(!is_prime(n)) printf("0\n"); else printf("%d\n", n-1); } } return 0; }