SPOJ PON - Prime or Not
阿新 • • 發佈:2017-09-11
www integer esp span sca mes puts max bsp
題目鏈接:http://www.spoj.com/problems/PON/
題目大意:判斷N是不是素數,N<264-1.
解題思路:需要用到拉賓-米勒素性判定。
(選自數論書籍)合數的拉賓-米勒測試:設n是奇素數,記n-1=2kq,q為奇數。對不被n整除的某個a,如果下述兩個條件都成立,則n是合數。
a): aq !≡ 1 (mod n), 即a的q次方不與1模n同余
b): 對所有i = 0,1,2,……,k-1, a2^i * q !≡ -1 (mod n)
所以可以打出100個素數表然後叠代判定。其中需要用的:快速冪取模,大數乘取模。
代碼:
1 const int maxn = 1e5 + 10; 2 VI primes; 3 bool vis[maxn]; 4 5 ll mul_mod(ll a, ll b, ll m){ 6 ll ans = 0, base = a; 7 while(b){ 8 if(b & 1) ans = (ans + base) % m; 9 base %= m; 10 base = base * 2 % m; 11 b >>= 1; 12 } 13 return ans; 14 } 15 ll pow_mod(ll a, ll b, ll m){16 ll ans = 1, base = a % m; 17 while(b){ 18 if(b & 1) ans = mul_mod(ans, base, m); 19 base %= m; 20 base = mul_mod(base, base, m); 21 b >>= 1; 22 } 23 return ans % m; 24 } 25 void dowork(){ 26 memset(vis, 0, sizeof(vis)); 27 for(int i = 2; i < maxn / 2; i++) 28 for(int j = i * 2; j > 0 && j < maxn; j += i) 29 vis[j] = 1; 30 for(int i = 2; i < 1e5; i++) 31 if(!vis[i]) primes.push_back(i); 32 } 33 bool check(ll x){ 34 if(x == 2) return true; 35 if(x % 2 == 0) return false; 36 ll q = x - 1; 37 int k = 0; 38 while(!(q & 1)){ 39 k++; q >>= 1; 40 } 41 bool flag = true; 42 for(int i = 0; i < 100; i++){ 43 ll a = primes[i]; 44 if(a == x) return true; 45 if(pow_mod(a, q, x) == 1) 46 continue; 47 bool none = true; 48 for(int j = 0; j < k; j++){ 49 ll qi = 1 << j; 50 ll tmp = pow_mod(a, qi * q, x); 51 if(tmp == x - 1){ 52 none = false; 53 break; 54 } 55 else if(tmp == 1) 56 break; 57 } 58 if(none) { 59 flag = false; 60 break; 61 } 62 } 63 return flag; 64 } 65 int main(){ 66 dowork(); 67 int t; 68 scanf("%d", &t); 69 while(t--){ 70 ll n; 71 scanf("%lld", &n); 72 if(check(n)) puts("YES"); 73 else puts("NO"); 74 } 75 }
題目:
PON - Prime or Not
#cryptography #prime-numbers-1 #primality-test
Given the number, you are to answer the question: "Is it prime?"
Solutions to this problem can be submitted in C, C++, Pascal, Perl, Python, Ruby, Lisp, Hask, Ocaml, Prolog, Whitespace, Brainf**k and Intercal only.
Input
t – the number of test cases, then t test cases follows. [t <= 500]
Each line contains one integer: N [2 <= N <= 2^63-1]
Output
For each test case output string "YES" if given number is prime and "NO" otherwise.
Example
Input: 5 2 3 4 5 6 Output: YES YES NO YES NOSubmit solution!
SPOJ PON - Prime or Not