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 }
时间: 2024-08-03 04:04:00

codevs 1702素数判定2的相关文章

Miller-Rabin算法 codevs 1702 素数判定 2

转载自:http://www.dxmtb.com/blog/miller-rabbin/ 普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(slog³n)的算法. 定理一:假如p是质数,且(a,p)=1,那么a^(p-1)≡1(mod p).即假如p是质数,且a,p互质,那么a的(p-1)次方除以p的余数恒等于1.(费马小定理) 该定理的逆命题是不一定成立的,但是令人可喜的是大多数情况是成立的. 于是我们就得到了一个定理的直接应用,对于待验证的数p,我们不断取a∈[1,p-1]且a∈

Miller_Rabin codevs 1702 素数判定2

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #define ll long long #define T 10 using namespace std; ll slow_mul(ll a,ll b,ll c)//防止爆掉 { ll ans=0; a=a%c;b=b%c; while(b) { if(b&1)

1702 素数判定 2

1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 一个数,他是素数么? 设他为P满足(P<=263-1) 输入描述 Input Description P 输出描述 Output Description Yes|No 样例输入 Sample Input 2 样例输出 Sample Output Yes 数据范围及提示 Data Size & Hint 算法导论——数论那一节注意Carmich

1702 素数判定 2[[一中数论随堂练]

1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 一个数,他是素数么? 设他为P满足(P<=263-1) 输入描述 Input Description P 输出描述 Output Description Yes|No 样例输入 Sample Input 2 样例输出 Sample Output Yes 数据范围及提示 Data Size & Hint 算法导论——数论那一节注意Carmich

codevs——1430 素数判定

1430 素数判定 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 质数又称素数.指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数. 素数在数论中有着很重要的地位.比1大但不是素数的数称为合数.1和0既非素数也非合数.质数是与合数相对立的两个概念,二者构成了数论当中最基础的定义之一.基于质数定义的基础之上而建立的问题有很多世界级的难题,如哥德巴赫猜想等.算术基本定理证明每个大于1的正整数都可以写成素

【数论】【素数判定】CODEVS 2851 菜菜买气球

素数判定模板. 1 #include<cstdio> 2 #include<map> 3 using namespace std; 4 int a[2001],ans=-2147483647,l,r,n,sum[2001]; 5 bool is_prime(const int &x) 6 { 7 for(int i=2;i*i<x;i++) if(x%i==0) return false; 8 return true; 9 } 10 int main() 11 { 1

数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&amp;2429

素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: http://blog.csdn.net/maxichu/article/details/45459533 然后是参考了kuangbin的模板: http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html 模板如下: //快速乘 (a

素数判定(给你两个数a、b,现在的问题是要判断这两个数组成的区间内共有多少个素数)

1 #include<stdio.h> 2 #include<math.h> 3 int func(int x)//自定义函数实现寻找素数功能 4 { 5 int i, flag = 1; 6 for (i = 2; i <= (int)sqrt((float)x); i++) //取到平方根就好,(float)x,强制将int x型转化成float型,再将平方根转化为int型 7 { 8 if (x%i == 0) //是合数,则标记 9 flag = 0; 10 } 11

素数判定 AC 杭电

素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 87861    Accepted Submission(s): 30699 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数. Input 输入数