GCD&&素筛&&快速幂 --A - Pseudoprime numbers
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
本题用到快速幂,素数判定、二者结合;
题意:输入两个数p,a.先判断p是否为素数,如果是,输出no。否则,再判断a的p次方取余p是否为a,是则yes,反之
则no。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
int isprime(ll n)
{
if(n<=3) return n>1;
int k;
k=sqrt(n);
if(n%6!= 1 && n%6!=5)
return 0;
for(int i=5;i<=k;i+=6)
{
if(n%i==0 || n%(i+2)==0)
return 0;
}
return 1;
}
ll qpow(ll a, ll n,ll mod)//计算a^n % mod
{
ll re = 1;
while(n)
{
if(n & 1)//判断n的最后一位是否为1
re = (re * a) % mod;
n >>= 1;//舍去n的最后一位
a = (a * a) % mod;//将a平方
}
return re;
}
int main()
{
ll p,a;
while(cin>>p>>a&&a&&p)
{
if(isprime(p))
cout<<"no"<<endl;
else
{
if(a==qpow(a,p,p))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
} }
return 0;
}
typedef
typedef long long ll;
快速幂模板
ll qpow(ll a, ll n,ll mod)//计算a^n % mod
{
ll re = 1;
while(n)
{
if(n & 1)//判断n的最后一位是否为1
re = (re * a) % mod;
n >>= 1;//舍去n的最后一位
a = (a * a) % mod;//将a平方
}
return re;
}
质数判定模板
int isprime(ll n)
{
if(n<=3) return n>1;
int k;
k=sqrt(n);
if(n%6!= 1 && n%6!=5)
return 0;
for(int i=5;i<=k;i+=6)
{
if(n%i==0 || n%(i+2)==0)
return 0;
}
return 1;
}
注意输入用cin,用scanf会wa
GCD&&素筛&&快速幂 --A - Pseudoprime numbers的更多相关文章
- 读入 并查集 gcd/exgcd 高精度 快速幂
ios_base::sync_with_stdio(); cin.tie(); ], nxt[MAXM << ], Head[MAXN], ed = ; inline void added ...
- Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)
/* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...
- hzau 1202 GCD(矩阵快速幂)
1202: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 201 Solved: 31[Submit][Status][Web Board] ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- 【快速幂】POJ3641 - Pseudoprime numbers
输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include ...
随机推荐
- 顺序表应用7:最大子段和之分治递归法(SDUT 3664)
#include <bits/stdc++.h> using namespace std; const int maxn = 50005; int num = 0; struct node ...
- MySQL查询top N记录
下面以查询每门课程分数最高的学生以及成绩为例,演示如何查询 top N记录.下图是测试数据,表结构和相关 insert 脚本见<常用SQL之日期格式化和查询重复数据>. 使用自连接[推荐] ...
- eclipse将web项目部署到tomcat
在 eclipse 中,选择 Window--->Preferences--->Server--->Runtime Environments,选择 Add 按钮 在弹出的对话框中,选 ...
- javaEE项目部署方式
1.手动部署 2.自动化部署 “自动化”的具体体现:向版本库提交新的代码后,应运服务器上自动部署
- 安装openssh-server
以前在服务器上装过openssh-server,今天突然想把台式机也打开ssh服务,结果忘了咋弄,稍微百度了一下就弄好了,备忘一下,以后就不用各种百度了. 第一步,安装openssh-server( ...
- go之构造体方法
package main import ( "fmt" "math" ) type Vertexs struct { X, Y float64 } //Abs ...
- Linux设备驱动程序 之 中断
中断 中断使得硬件可以发出通知给处理器,本质上是一种特殊的电信号,由硬件设备发向处理器,处理器接收到中断后,会马上向操作系统反应此信号的到来,然后就由操作系统负责处理这些新来的数据:硬件设备生成中断并 ...
- WebView调用js方法获取返回值的完美解决方案
在Android项目中我们或多或少会涉及到与js交互的问题,这其中WebView是必须掌握的控件,今天主要说说我们通过WebView调用js方法,然后如何很好的获取返回值.这里我总结了三种方式,大家可 ...
- <JavaScript>可枚举属性与不可枚举属性
在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. 一.怎么判断属性是否可枚举 js中基本包 ...
- Swift 变量
变量是一种使用方便的占位符,用于引用计算机内存地址. Swift 每个变量都指定了特定的类型,该类型决定了变量占用内存的大小,不同的数据类型也决定可存储值的范围. 包括整形Int.浮点数Double和 ...