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

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

Source

 
 
快速幂
 
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std; typedef long long ll; int p,a; bool judge() {
for(int i = ; i * i <= p; i++) {
if(p % i == ) return false;
} return true;
}
bool mod_pow(ll x,ll n) {
ll res = ;
while(n > ) {
if(n & ) res = res * x % p;
x = x * x % p;
n >>= ;
} return res == a;
} int main() {
//freopen("sw.in","r",stdin); while(~scanf("%d%d",&p,&a) && p && a) {
if(!judge() && mod_pow(a,p)) printf("yes\n");
else printf("no\n"); } return ;
}

POJ 3641的更多相关文章

  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 快速幂+素数

    http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...

  3. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

  4. poj 3641 Pseudoprime numbers Miller_Rabin测素裸题

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

  5. poj 3641 ——2016——3——15

    传送门:http://poj.org/problem?id=3461 题目大意:给你两个字符串p和s,求出p在s中出现的次数. 题解:这一眼看过去就知道是KMP,作为模板来写是最好不过了.... 这道 ...

  6. POJ 3641 Oulipo KMP 水题

    http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...

  7. Mathematics:Pseudoprime numbers(POJ 3641)

     强伪素数 题目大意:利用费马定理找出强伪素数(就是本身是合数,但是满足费马定理的那些Carmichael Numbers) 很简单的一题,连费马小定理都不用要,不过就是要用暴力判断素数的方法先确定是 ...

  8. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  9. poj 3641 Pseudoprime numbers(快速幂)

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

随机推荐

  1. 【MediaKit】WPF项目中 调用摄像头拍照的开发包

    今天遇到一个 人事的项目,项目中需要调用摄像头给员工照相.如何解决这个问题呢? 介绍一个开发包给你,MediaKit.论坛里头的人都说好,但是黑兔觉得大家好才是真的好.你不妨试试~ 第一步:添加WPF ...

  2. 使用ResultSet,写了一个通用的查询方法

    此方法很烂,以后优化 /** * 通用的查询方法:SELECT */ @SuppressWarnings({ "unchecked", "rawtypes" } ...

  3. Linux下解决apache 报 403 forbidden 错

    三步搞定: 1. 打开终端 2. 输入 chcon -R -t httpd_sys_content_t /var/www/html # 后面的/var/www/html是网站的默认目录,可以根据自己的 ...

  4. ubuntu OPENCV移植

    Installing OpenCV 2.4.1 in Ubuntu 12.04 LTS 这是转载国外一篇文章  移植PC上的OPENCV  http://www.samontab.com/web/20 ...

  5. 常用sql时间字符转化

    这边主要用到2个函数  convert()  cast() cast是对数据字符类型的转化,例如: cast(date as datetime)   这样就将date字段转化成为时间类型了 因为常用到 ...

  6. 完美解决fixed 水平居中问题

    群里的朋友问的,发现自己没写过:就写了下,原理和网上的fixed上下左右四个角的原理一样! 1.防止页面振动: body{ _background-image: url(about:blank); _ ...

  7. Hadoop 的部署适用性(网上资料http://www.linuxidc.com/Linux/2013-10/92141.htm)

    近些年,Hadoop和“走向大数据分析引擎”一样,受到颇多赞誉.对很多人来说,Hadoop就意味着大数据技术.但其实开源的分布式处理框架未必能解决所有的大数据问题.这就要求想要部署Hadoop的公司慎 ...

  8. AngularJS(15)-依赖注入

    AngularJS 依赖注入 什么是依赖注入 wiki 上的解释是:依赖注入(Dependency Injection,简称DI)是一种软件设计模式,在这种模式下,一个或更多的依赖(或服务)被注入(或 ...

  9. 2013-07-26 IT 要闻速记快想

    ### ========================= ###传Google正在内测供用户买卖技能的电商平台Helpout,最早于下月上线该服务将依托Google强大的云服务和搜索能力,以实时视频 ...

  10. android 在标题栏加上按钮

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowF ...