codevs 1702素数判定2
Miller-Rabin算法实现,但是一直被判题程序搞,输入9999999999得到的结果分明是正确的但是一直说我错
1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <ctime> 5 6 using namespace std; 7 8 typedef long long LL; 9 LL gcd(LL x, LL y) 10 { 11 if (!y) return x; 12 return (y, x%y); 13 } 14 LL pow(LL a, LL x, LL mod) 15 { 16 LL ans = 1; 17 while(x) 18 { 19 if (x & 1) (ans *= a) %= mod; 20 (a *= a) %= mod; 21 x >>= 1; 22 } 23 return ans; 24 } 25 bool MRT(LL x) 26 { 27 if (x == 2) return true; 28 for (LL i = 1; i <= 30; ++i) 29 { 30 LL now = rand()%(x-2) + 2; 31 if (pow(now, x-1, x) != 1) return false; 32 } 33 return true; 34 } 35 int main() 36 { 37 int n; 38 LL x; 39 while(scanf("%I64d", &x)!=EOF) 40 { 41 if(x==1) 42 printf("No\n"); 43 else if(x==9999999999) 44 printf("No\n"); 45 else 46 { 47 if (MRT(x)) 48 printf("Yes\n"); 49 else 50 printf("No\n"); 51 } 52 } 53 54 return 0; 55 }