poj 3641 Pseudoprime numbers
题目连接
http://poj.org/problem?id=3641
Pseudoprime numbers
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
快速幂。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<set>
using std::min;
using std::sort;
using std::pair;
using std::swap;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1 << 17;
const int INF = ~0u >> 1;
typedef unsigned long long ull;
bool isPrime(ull n) {
for(int i = 2; (ull)i * i <= n; i++ ) {
if(n % i == 0) {
return false;
}
}
return n != 1;
}
ull mod_pow(ull a, ull p) {
ull ans = 1, M = p;
while(p) {
if(p & 1) ans = ans * a % M;
a = a * a % M;
p >>= 1;
}
return ans;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
ull a, p;
while(~scanf("%lld %lld", &p, &a), a + p) {
if(isPrime(p)) { puts("no"); continue; }
puts(a % p == mod_pow(a, p) ? "yes" : "no");
}
return 0;
}
poj 3641 Pseudoprime numbers的更多相关文章
- 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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- 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 ...
- 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 Pseudoprime numbers 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10903 Accepted: 4 ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- POJ 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6044 Accepted: 24 ...
随机推荐
- 洛谷P1457 城堡 The Castle
P1457 城堡 The Castle 137通过 279提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 我们憨厚的USACO ...
- c++11 中的 move 与 forward
[update: 关于左值右值的另一点总结,请参看这篇] 一. move 关于 lvaue 和 rvalue,在 c++11 以前存在一个有趣的现象:T& 指向 lvalue (左传引用), ...
- Android layout属性大全
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 ...
- TRMM数据的读取
编辑下头文件,就有投影信息,然后再转换成你需要的投影方式1) From the command line, type in, ENVI, 2) Click File|Open External Fil ...
- 使用jquery时弹出[object HTMLImageElement]问题
本打算写一个轮播图,但是获取到图像后想改变图片地址,却jq方法各种不能实现,开始以为是jq文件弄错了,用百度的cdn试了下还是报错. 于是开始了alert调试. 我是用的jquery获取类 var a ...
- 误删ext3的恢复方法
1.Ext3文件系统结构的简单介绍在Linux所 用的Ext3文件系统中,文件是以块为单位存储的,默认情况下每个块的大小是1K,不同的块以块号区分.每个文件还有一个节点,节点中包含有文件所有者, 读写 ...
- .NET中使用log4net
一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...
- Silverlight读取Zip文件中的图片与视频
首先看看Demo的截图: 下面我将一步步展示实现这个Demo的过程,这个需求就是读出Zip文件中的图片与视频. Demo整体架构: 首先我们准备几张图片和视频,然后将其压缩至resource.zip文 ...
- Android判断横屏竖屏代码
// 判断Android当前的屏幕是横屏还是竖屏.横竖屏判断 if (this.getResources().getConfiguration().orientation == Configurati ...
- xml文件对应的DTD学习
DTD文件: 1.DTD文档主要由(元素,属性,实体,PCDATA,CDATA) 2.声明一个元素:<!ELEMENT 元素名称 (元素内容)> eg: <!ELEMENT pers ...