#include<bits/stdc++.h> using namespace std; /** //普通快速幂 LL pow_mod(LL a, LL x, LL p) { LL res = 1; while(x) { if (x & 1) res = (unsigned long long)res * a % p; x >>= 1; a = (unsigned long long)a * a % p; } return res; } **/ ///Miller-Rabin测试,时间复杂度O(logn) typedef long long LL; LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); } LL mul_mod(LL a, LL x, LL n) ///乘法快速幂 { LL t = 0; while(x) { if(x & 1) { t += a; if(t >= n) t -= n; } a <<= 1; if(a >= n) a -= n; x >>= 1; } return t; } ///直接的快速幂取模在数据很大的时候有平方,这个平方long long都会存不下,所以需要乘方快速幂 LL pow2_mod(LL a, LL x, LL n) ///乘方快速幂 { LL t = 1; a %= n; while(x) { if(x & 1) t = mul_mod(t, a, n); a = mul_mod(a, a, n); x >>= 1; } return t; } bool test(LL n, LL a, LL d) { if(n == 2) return true; if(n == a) return true; if((n&1) == 0) return false; while(!(d&1)) d /= 2; LL t = pow2_mod(a, d, n); while((d != n-1) && (t != 1) && (t != n-1)) { // t = (LL)t*t%n; t = mul_mod(t, t, n); d = d<<1; } return (t == n-1 || (d&1) == 1); } bool isPrime(LL n) { ///有些题目把1看作质数,但是负数不会是质数 if(n < 2) return false; LL a[] = {2, 3, 61}; for(int i = 0; i <= 2; i++) if(!test(n, a[i], n-1)) return false; return true; } int main() { ///freopen("in.txt","r",stdin); if(isPrime(3203431780337ll)) puts("YES"); else puts("NO"); return 0; }
浙公网安备 33010602011771号