POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641
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 and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)
Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.
Input
Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.
Output
For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".
Sample input
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
Sample output
no
no
yes
no
yes
yes
Solution
题意
给定 \(p\) 和 \(a\),判断 \(p\) 是否为合数且满足 \(a^p\equiv a(mod\ p)\)。
题解
水题 快速幂 + 素数判断
Code
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
bool is_prime(ll a) {
for(ll i = 2; i <= a / i; ++i) {
if(a % i == 0) return 0;
}
return 1;
}
ll qmod(ll a, ll b, ll p) {
if(!b) return 1 % p;
ll ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % p;
a = (a * a) % p;
b >>= 1;
}
return ans;
}
int main() {
ll a, p;
while(~scanf("%lld%lld", &p, &a) && a + p) {
if(is_prime(p) == 0 && qmod(a, p, p) == a) {
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}
POJ 3641 Pseudoprime numbers (数论+快速幂)的更多相关文章
- poj 3641 Pseudoprime numbers(快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- poj 3641 Pseudoprime numbers Miller_Rabin测素裸题
题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为po ...
- POJ 3641 Pseudoprime numbers (miller-rabin 素数判定)
模板题,直接用 /********************* Template ************************/ #include <set> #include < ...
- POJ 1995 Raising Modulo Numbers(快速幂)
嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...
- poj 3070 && nyoj 148 矩阵快速幂
poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...
随机推荐
- 《图解设计模式》读书笔记6-1 VISITOR模式
目录 1. Visitor模式简介 2. 示例 2.1 类图 2.2 代码 3. 模式的角色和类图 4. 思路拓展 4.1 双重分发 4.2 开闭原则 4.3 难以增加ConcreteElement角 ...
- [Codeforces 729F] Financiers Game
题意: 两个人分别从长度为n的数列的两端开始取数,如果前一 个人取了k个数,后一个人必须取k或k+1个. 第一个人最 开始可以取1个或2个,不能操作时结束. 两个人都希望自 己取到的数字之和尽量大,并 ...
- maven scope 作用域
1.test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖 2.compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去 3.provided依赖:在编译和测试的过程有效,最后 ...
- HDFS 工具类
读取HDFS上文件数据 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import ...
- a标签的锚点链接
<a href="#creditor" class="clearfix nav_creditor"> <div class="sec ...
- 不要62(数位dp)
题目传送门 不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 牛客网挑战赛24 青蛙(BFS)
链接:https://www.nowcoder.com/acm/contest/157/E来源:牛客网 有一只可爱的老青蛙,在路的另一端发现了一个黑的东西,想过去一探究竟.于是便开始踏上了旅途 一直这 ...
- 使用VS 2019发布.net core程序并部署到IIS的最新教程
不管你是使用.net core开发的是web api还是网站类的程序,如果你是部署到IIS,那么下面的内容都适合于你,不会将.net core程序部署到IIS的朋友,可以看看这篇手把手教你部署.net ...
- ant打包遇到的问题
\build\build.xml:350: Problem: failed to create task or type foreach Cause: The name is undefined. A ...
- 42.Flatten Binary Tree to Linked List
Level: Medium 题目描述: Given a binary tree, flatten it to a linked list in-place. For example, given ...