题目连接

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-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的更多相关文章

  1. POJ 3641 Pseudoprime numbers (数论+快速幂)

    题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...

  2. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

  3. poj 3641 Pseudoprime numbers Miller_Rabin测素裸题

    题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为po ...

  4. poj 3641 Pseudoprime numbers(快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  5. POJ 3641 Pseudoprime numbers (miller-rabin 素数判定)

    模板题,直接用 /********************* Template ************************/ #include <set> #include < ...

  6. HDU 3641 Pseudoprime numbers(快速幂)

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11336   Accepted: 4 ...

  7. poj Pseudoprime numbers 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10903   Accepted: 4 ...

  8. 【POJ - 3641】Pseudoprime numbers (快速幂)

    Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以  ...

  9. POJ 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6044   Accepted: 24 ...

随机推荐

  1. 设置Eclipse的字体风格方式

    1:Eclipse版本号:3.7.0(汉化版) 假设eclipse已经打开 窗口--->首选项--->常规--->外观--->颜色和字体--->基本--->文字字体 ...

  2. android反编译工具总结

    序:总结反编译主要的目的在于学习.利用反编译进行相关的汉化或修改,都是不道德的! 大家都知道,将apk文件解压后有两部分文件需要处理,一种是xml文件,另一种一个dex文件(.dex),我们可以从.d ...

  3. JavaScript创建表格的两种方式

    方式一: var data = [ { id: 1, name: "first", age: 12 }, { id: 2, name: "second", ag ...

  4. ionic localstorage

    angular.module('locals',[]) .factory('ls', ['$window', function($window) { return { set: function(ke ...

  5. 把jqmobi 變成jQuery 的插件 從此使用jQuery

    因為項目的要求,需要用到jQuery 的一些插件 ,而我又是用jqmobi的....o(╯□╰)o 看看文檔.....把jqmobi  變成jQuery的插件  問題就解決了 O(∩_∩)O哈哈~ 官 ...

  6. idea 14 svn安装

    安装SVN客户端. 打开IDEA 14 File-setting-Version coltorl-Subversion-General 填入安装路径 打开"VCS"菜单项然后点击& ...

  7. SQL Server 索引和视图

    Ø 索引 1. 什么是索引 索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度. 2. 索引分类 数据库中索引主要分为两类:聚集索引和非聚集索引.SQL Serv ...

  8. c++ 创建 socket server

    下面一段代码是创建socket server的代码片段: 需要引用的库包括: #include <sys/types.h> #include <sys/socket.h> #i ...

  9. CSS文件中第一行@charset "utf-8";的作用

    使用UTF-8编码唯一的好处是,国外的用户如果使用Windows XP英文版,浏览UTF-8编码的任何网页,无论是中文.还是日文.韩文.阿拉伯文,都可以正常显示,UTF-8是世界通用的语言编码,而如果 ...

  10. ecshop常用语句

    ecshop之中的IF语句: <select name="product_cat" id="product_cat" class="form-c ...