【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 ...
随机推荐
- metal feature and specification
https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf 宝贝 https://developer.apple.com/metal/ ...
- springmvc处理一个请求的全流程
首先,用户的浏览器发出了一个请求,这个请求经过互联网到达了我们的服务器. Servlet 容器首先接待了这个请求,并将该请求委托给 DispatcherServlet 进行处理. 接着 Dispatc ...
- Thread setUncaughtExceptionHandler
setUncaughtExceptionHandler 用于获取线程运行时异常 线程在执行时是不能抛出 checked 异常的,IDE 只会提示你用 try-catch 包裹起来.因此主线程无法直接获 ...
- laravel常用响应操作
- mongodb命令---花样查询语句
闲言少叙 查出价格低于200的商品信息----包含商品名称,货物编号,价格,添加信息等 db.goods.find( {}}, {,,,} ) 商品分类不为3的商品 db.goods.find( {} ...
- Codeforces Round #588 (Div. 2) C. Anadi and Domino(思维)
链接: https://codeforces.com/contest/1230/problem/C 题意: Anadi has a set of dominoes. Every domino has ...
- nginx 端口转发 (proxy_pass反向代理)
第一种(访问IP转发到IP+端口) server{ listen ; server_name 192.168.1.114; index index.php index.html index.htm; ...
- Laravel 事件侦听的几个方法 [Trait, Model boot(), Observer Class]
1 Trait 1.1 可以在 Trait 中定义一个静态的 bootFooBar() 方法,注:FooBar 是你的 Trait 名称 namespace App\Traits; use App\A ...
- [Luogu] 子串
https://www.luogu.org/problemnew/show/P2679 DP f(k,i,j)f(k,i,j)表示分了k段,用了第一个串中的前i个数字,已经构成了第二个串的前j个的方案 ...
- delphi将两个Strlist合并,求并集
Function StrList_Merge(StrListA,StrListB:String):String; //将两个Strlist合并,求并集 var SListA,SListB,SListC ...