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 ...
随机推荐
- 【转】Selenium 加载Chrome/Firefox浏览器配置文件
原文地址:https://www.cnblogs.com/eastonliu/p/9083982.html Selenium启动浏览器时,默认是打开一个新用户,不会加载原有的配置以及插件.但有些时候我 ...
- Linux下实现MySQL数据库每天定时自动备份
使用MySQL自带的备份工具+ crontab 的方式来实现备份 1.查看磁盘挂载信息(选一个容量合适的) #df -h 2.创建备份目录 为了方便,在/home保存备份文件: cd /home/ga ...
- day 90 RBAC
参考博客 -陈晓梅 http://www.cnblogs.com/c-x-m/p/9025478.html 登录view from django.shortcuts import render,red ...
- FileInputStream_FileOutputStream
Writer的基本方法 //向输出流中写入一个字符数据,该字节数据为参数b的低16位 void write(int c) throws IOException //将一个字符类型的数组中的数据写入输出 ...
- “希希敬敬对”队软件工程第九次作业-beta冲刺第三次随笔
“希希敬敬对”队软件工程第九次作业-beta冲刺第三次随笔 队名: “希希敬敬对” 龙江腾(队长) 201810775001 杨希 201810812008 何敬 ...
- centos7 安装 gitlab 服务器
https://www.cnblogs.com/wenwei-blog/p/5861450.html 标签: centos7 git安装 git gitlab 我这里使用的是centos 7 64bi ...
- 阿里Druid连接池的坑。。
Druid的坑 当查询数据库的Clob转换为Oracle Clob类型的时候. java.lang.ClassCastException: com.alibaba.druid.proxy.jdbc.C ...
- python删除某一行
整理了网络上的一些方法,一般有两种方法:第一种:是先把文件读入内存,在内存中修改后再写入源文件. 例子:将内容包含“123”的所有行删去: with open('C:/Users/lai/Deskto ...
- 常用Message Queue对比
目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量 ...
- Android关于界面一定时间无操作自动跳转到指定界面的实现
主要用到的功能,自定义一个定时器CountTimer继承CountDownTimer. public class CountTimer extends CountDownTimer { private ...