【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers
Descriptions
费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) 。也就是说,a的 p 次幂除以 p 的余数等于 a 。p 的某些 (但不是很多) 非素数的值,被称之为以 a 为底的伪素数,对于某个 a 具有该特性。并且,某些 Carmichael 数,对于全部的 a 来说,是以 a为底的伪素数。
给定 2 < p ≤ 1000000000 且 1 < a < p ,判断 p 是否为以 a 为底的伪素数。
输入
输入包含多个测试用例,以 "0 0" 表示输入结束。每个测试用例,由包含 p 和 a 的一行组成。
输出
对于每个测试用例,如果 p 是以 a 为底的伪素数,则输出 "yes",否则输出 "no" 。
示例输入
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
示例输出
no
no
yes
no
yes
yes
题目链接
https://vjudge.net/problem/POJ-3641
简单的说满足两个条件
1、p不是素数
2、a的p次幂除以p的余数等于a
就输出yes
否则输出no
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100000+100
using namespace std;
ll a,p;
ll mod;
ll qpow(ll a, ll n)//计算a^n % mod
{
ll re = ;
while(n)
{
if(n & )//判断n的最后一位是否为1
re = (re * a) % mod;
n >>= ;//舍去n的最后一位
a = (a * a) % mod;//将a平方
}
return re % mod;
}
int main()
{
while(cin>>p>>a,a+p)
{
ll sum=;
for(ll i=; i*i<p; i++)//判断是否是素数
{
if(p%i==)
sum++;
}
if(sum==)//p是素数
cout<<"no"<<endl;
else//p不是素数
{
mod=p;
if(qpow(a,p)==a)//a的p次幂除以p的余数是否等于a
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}
return ;
}
【POJ - 3641】Pseudoprime numbers (快速幂)的更多相关文章
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- poj 3641 Pseudoprime numbers(快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
- 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 < ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- pojPseudoprime numbers (快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
随机推荐
- MySQL基准测试和sysbench工具
参考https://www.cnblogs.com/kismetv/archive/2017/09/30/7615738.html 一.基准测试的作用 sysbench是一个开源的.模块化的.跨平台的 ...
- 19-SQLServer定期自动导入数据的dtsx部署
一.注意点 1.登录Integration Service必须使用windows用户,并且只能在本地服务器登录. 2.SQLServer2000以前,叫dts,全程Data Transformatio ...
- vector简单使用
在刷ccf题的时候日常做完去网上查看别的同学怎么做的 发现有使用vector后三十几行代码就写出来的,虽然时间复杂度和我一样十几毫秒,but !我看中了它的代码量啊!多么的少啊! 所以百度了vecto ...
- Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...
- windows下mysql 主库从库同步
今天先讲讲数据库的主从同步,两个好处: 一是读写分离可以用上.比如 写操作就写到主数据库,读就再从库读取 二是纯粹给数据库备份,以防硬盘彻底崩了 主从数据库操作准备: 两台电脑,都安装好mysql 5 ...
- Selenium定位class包含空格的元素-复合class节点
在HTML中, 节点有三种常见属性, 分别是id, name和class, 其中class是一个特殊的属性, 支持多个类名, 以空格隔开, 如下图所示: 你是否注意到, 为什么selenium中的fi ...
- 事件处理机制与Handler消息传递机制
一.基于监听的事件处理机制 基于监听的时间处理机制模型: 事件监听机制中由事件源,事件,事件监听器三类对象组成 处理流程如下: Step 1:为某个事件源(组件)设置一个监听器,用于监听用户操作 St ...
- 怎样用linux命令知道系统是ubuntu还是redhat或者其它的系统?
1.第一种方法:# lsb_release -aLSB Version: :core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4. ...
- sql 同一张表查询不同数据合并之后关联查询
SELECT t.articleId articleId, comments.`comments` parentComment, t.commentId commentsId, comments.`i ...
- MySQL之MyISAM和InnoDB
一.区别 1.MySQL默认采用的是MyISAM. 2.MyISAM不支持事务和外键,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提 ...