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 }

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

报错: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\n";
41         else
42         {
43             if (pow_mod(a, p, p) == a)
44                 cout << "yes\n";
45             else
46                 cout << "no\n";
47         }
48     }
49     delete[] vis;
50     return 0;
51 }

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\n";
45                 continue;
46             }
47             if (pow_mod(a, p, p) == a)
48                 cout << "yes\n";
49             else
50                 cout << "no\n";
51         }
52     }
53 }

原文地址:https://www.cnblogs.com/chen-tian-yuan/p/10652381.html

时间: 2024-10-27 00:57:51

POJ3641 Pseudoprime numbers (幂取模板子)的相关文章

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

POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g

POJ3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6644   Accepted: 2696 Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power

【快速幂】POJ3641 - Pseudoprime numbers

输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 typedef long long ll; 7 ll p,a; 8 9 int whether(int p) 10 { 1

POJ3641:Pseudoprime numbers(判断是否为Carmichael numbers,挑战P122)

题意:判断P是否为素数,是即输出no,不是就计算a的p次方是否等于a,是就输出yes,否则输出no: key:快速幂,判断素数,两个函数即可: /*快速幂, Carmicharl numbers*/ #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; typedef long long ll; ll a, p; bool ok_prime(ll t) //如果是

poj 3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6645   Accepted: 2697 Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power

模板 快速幂取模

[模板]快速幂取模 1 long long quickmod(long long a,long long b,long long m) 2 { 3 long long ans = 1; 4 while(b)//用一个循环从右到左便利b的所有二进制位 5 { 6 if(b&1)//判断此时b[i]的二进制位是否为1 7 { 8 ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m 9 b--;//把该为变0 10 } 11 b/=2; 12 a = a*a%m; 13 } 1

HDU - 2817 - A sequence of numbers (快速幂取模!)

A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3494    Accepted Submission(s): 1073 Problem Description Xinlv wrote some sequences on the paper a long time ago, they migh

Pseudoprime numbers poj3641

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8682   Accepted: 3645 Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power

hdu 2817 A sequence of numbers(快速幂取余)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 题目大意:给出三个数,来判断是等差还是等比数列,再输入一个n,来计算第n个数的值. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #define m 200907 5 6 using namespace std; 7 8 __int64 fun(__int64 j,__int64 k) 9