poj Pseudoprime numbers 3641
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10903 | Accepted: 4710 |
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 题意:判断伪质数,即非质数,并且满足:存在a,使得a^p==a mod(p)的p称为伪质数。
思路:快速幂运算验证即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
#define MOD 1000000000
typedef long long ll;
const int N_MAX = ;
ll p, a;
ll ll_mult(ll a,ll x,ll p) {
ll res = ; bitset<>tmp = static_cast<bitset<>>(x);//前面低位
for (int i = ; i < tmp.size();i++) {
if (tmp[i])res += a*( << i);
res %= p;
} return res;
} ll mod_pow(ll x,ll n,ll p) {
ll res = ;
while (n) {
if (n & )res = ll_mult(res,x,p);
x = ll_mult(x, x,p);
n >>= ;
}
return res;
} bool is_prime(ll n) {
for (int i = ; i*i <= n;i++) {
if (n%i == )return false;
}
return n!=;
} int main() {
while (scanf("%lld%lld",&p,&a)&&(p||a)) {
if (is_prime(p)) { puts("no"); continue; }
if (mod_pow(a, p, p) == a)puts("yes");
else puts("no");
} return ;
}
poj Pseudoprime numbers 3641的更多相关文章
- POJ Pseudoprime numbers( Miller-Rabin素数测试 )
链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...
- 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 ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- 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 Miller_Rabin测素裸题
题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为po ...
- poj 3641 Pseudoprime numbers(快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
随机推荐
- JVM内存模型与GC算法(简介)
JVM内存模型如上图,需要声明一点,这是<Java虚拟机规范(Java SE 7版)>规定的内容,实际区域由各JVM自己实现,所以可能略有不同.以下对各区域进行简短说明. 1.1程序计数器 ...
- const,static,extern,#define
一.const // 简单定义变量,可以修改变量的值 ; a = ; // const的用法 // 用法一: ; ; // 不允许修改,因为 const 修饰 b/c,指定 b/c 为常量!! // ...
- 设置通过Maven创建的工程的JDK的版本,更改conf/settings.xml
eclipse提示警告如下: Build path specifies execution environment J2SE-1.5. There are no JREs installed in t ...
- Python爬虫系列-BeautifulSoup详解
安装 pip3 install beautifulsoup4 解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(markup,'html,parser') Pyth ...
- Linux网络编程:客户端/服务器的简单实现
一. Socket的基本知识 1. socket功能 Socket层次 Socket实质上提供了进程通信的端点,进程通信之前,双方必须首先各自创建一个端点,否则是没有办法建立联系并相互通信的. 每一个 ...
- Hive学习笔记(二)
Hive内部表跟外部表之间的区别 创建外部表 先删除上面创建的表,指定location 此时在hdfs根目录下就有一个hivedata文件夹 上传文本数据到hivedata目录下 查询表中数据 删除上 ...
- springboot-vue-前后端数据交互
前端项目: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- 大数据学习——spark学习
计算圆周率 [root@mini1 bin]# ./run-example SparkPi [root@mini1 bin]# ./run-example SparkPi [root@mini1 bi ...
- python - 字符串的内建函数
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_3_str_内建函数.py@ide: PyCharm Commu ...
- 思路清奇:通过 JavaScript 获取移动设备的型号
我们一般在浏览器里识别用户的访问设备都是通过 User Agent 这个字段来获取的,但是通过它我们只能获取一个大概的信息,比如你用的是 Mac 还是 Windows,用的是 iPhone 还是 iP ...