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 ...
随机推荐
- 数据结构实验之数组二:稀疏矩阵(SDUT 3348)
Problem Description 对于一个n*n的稀疏矩阵M(1 <= n <= 1000),采用三元组顺序表存储表示,查找从键盘输入的某个非零数据是否在稀疏矩阵中,如果存在则输出O ...
- LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)
题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...
- super关键字和调用父类构造方法
表示父类对象的默认引用 如果子类要调用父类被覆盖的实例方法,可用super作为调用者调用父类被覆盖的实例方法. 使用super调用父类方法 使用super调用父类的构造方法 调用构造方法 本类中调用另 ...
- Java并发指南5:JMM中的final关键字解析
本文转载自互联网,侵删 与前面介绍的锁和volatile相比较,对final域的读和写更像是普通的变量访问.对于final域,编译器和处理器要遵守两个重排序规则: 在构造函数内对一个final域的 ...
- PyTricks-使用namedtuple以及dataclass的方式定义类
from collections import namedtuple from dataclasses import dataclass # 以前简单的类可以使用namedtuple实现. Car = ...
- 查看memcache版本
window: cmd中用telnet 127.0.0.1 11211这样的命令连接上memcache,然后直接输入stats就可以得到memcache服务器的版本. 注意:memcache的默认端口 ...
- 解决python 保存json到文件时 中文显示16进制编码的问题
python 2.7 import codecs import json with codecs.open('Options.json', 'w', encoding='utf-8') as f: j ...
- 【Java/JDBC】利用ResultSetMetaData从数据库的某表中获取字段信息并存到csv文件
代码下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-2.rar 这篇还不够完善,请看更完善的续篇 ...
- Java同步数据结构之LinkedTransferQueue
前言 LinkedTransferQueue是Java并发包中最强大的基于链表的无界FIFO阻塞传输队列.从JDK7开始出现,Doug Lea说LinkedTransferQueue是Concurre ...
- C++ STL Heap算法
#include <iostream>#include <algorithm>#include <vector> using namespace std; int ...