POJ3641 Pseudoprime numbers (幂取模板子)

Posted chen-tian-yuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3641 Pseudoprime numbers (幂取模板子)相关的知识,希望对你有一定的参考价值。

给你两个数字p,a。如果p是素数,并且ap mod p = a,输出“yes”,否则输出“no”。

很简单的板子题。核心算法是幂取模(算法详见《算法竞赛入门经典》315页)。

幂取模板子:

 

技术图片
1 int pow_mod(int a,int n,int m)
2 {
3     if(n==0) return 1;
4     int x = pow_mod(a, n / 2, m);
5     long long ans = (long long)x * x % m;
6     if(n%2) ans = ans * a % m;
7     return (int)ans;
8 }
View Code

 

题目代码也比较简单,有一个坑点是如果用筛素数打表,数组开不了这么大。

报错:error: total size of array must not exceed 0x7fffffff bytes

报错代码:

 

技术图片
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <iomanip>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <stack>
 7 #include <functional>
 8 #include <queue>
 9 #include <cmath>
10 using namespace std;
11 typedef long long ll;
12 ll pow_mod(ll a, ll n, ll m)
13 {
14     if (n == 0)
15         return 1;
16     ll x = pow_mod(a, n / 2, m);
17     ll ans = x * x % m;
18     if (n % 2)
19         ans = ans * a % m;
20     return ans;
21 }
22 const long long maxn = 1000000000 + 1;
23 int *vis = new int[maxn];
24 void prime()
25 {
26     memset(vis, 0, sizeof(vis));
27     int len = sqrt(maxn * 1.0);
28     for (int i = 2; i <= len; i++)
29         if (!vis[i])
30             for (int j = i * 2; j <= maxn; j += i)
31                 vis[j] = 1;
32 }
33 int main()
34 {
35     int p, a;
36     prime();
37     while (cin >> p >> a)
38     {
39         if (!vis[p])
40             cout << "no
";
41         else
42         {
43             if (pow_mod(a, p, p) == a)
44                 cout << "yes
";
45             else
46                 cout << "no
";
47         }
48     }
49     delete[] vis;
50     return 0;
51 }
View Code

 

AC代码:

技术图片
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <iomanip>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <stack>
 7 #include <functional>
 8 #include <queue>
 9 #include <cmath>
10 using namespace std;
11 typedef long long ll;
12 ll pow_mod(ll a, ll n, ll m)
13 {
14     if (n == 0)
15         return 1;
16     ll x = pow_mod(a, n / 2, m);
17     ll ans = x * x % m;
18     if (n % 2 == 1)
19         ans = ans * a % m;
20     return ans;
21 }
22 bool prime(ll a)
23 {
24     if (a == 1)
25         return 1;
26     if (a == 2)
27         return 1;
28     for (int i = 2; i * i <= a; i++)
29         if (a % i == 0)
30             return 0;
31     return 1;
32 }
33 int main()
34 {
35     ll a, p;
36     while (cin >> p >> a)
37     {
38         if (a == 0 && p == 0)
39             break;
40         else
41         {
42             if (prime(p))
43             {
44                 cout << "no
";
45                 continue;
46             }
47             if (pow_mod(a, p, p) == a)
48                 cout << "yes
";
49             else
50                 cout << "no
";
51         }
52     }
53 }
View Code

 

以上是关于POJ3641 Pseudoprime numbers (幂取模板子)的主要内容,如果未能解决你的问题,请参考以下文章

POJ3641 Pseudoprime numbers(快速幂+素数判断)

POJ-3641 Pseudoprime numbers---快速幂

Pseudoprime numbers POJ - 3641

POJ 3641 Pseudoprime numbers (快速幂)

Pseudoprime numbers POJ 3641(快速幂)

POJ3641 Pseudoprime numbers (幂取模板子)