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. JavaScript 全局

    JavaScript 全局 JavaScript 全局属性和方法可用于创建Javascript对象. JavaScript 全局属性 属性 描述 Infinity 代表正的无穷大的数值. NaN 指示 ...

  2. spring IOC经典理解

    不多解释,直接上图片!

  3. SQL server基本操作(一)

      --1.create database CREATE DATABASE MyDB GO --2.use database USE MyDB GO --3.create table CREATE T ...

  4. zedboard U盘挂载+交叉编译helloworld

    交叉编译环境见http://blog.csdn.net/xiabodan/article/details/22717175 1:编写hello.c文件 #include<stdio.h> ...

  5. 第四届蓝桥杯C/C++A组题目:振兴中华

    首先把题目贴上来吧! 小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见图1) 从我做起振 我做起振兴 做起振兴中 起振兴中华 图1 比 ...

  6. php中如何防止表单的重复提交

    在php中如何防止表单的重复提交?其实也有几种解决方法. 下面小编就为大家介绍一下吧.需要的朋友可以过来参考下 代码: <?php /* * php中如何防止表单的重复提交 * by www.j ...

  7. Delphi XE5教程7:单元引用和uses 子句

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  8. oracle数据库一些问题

    1.oracle12安装的过程中需要注意字符的选择(特别注意 选择ZHS16GBK 国内企业大多选这个   否则创建数据的时候会出现中文变成问号) 2.设置密码是必须是大小写混合的 pl/sql32链 ...

  9. linux终端io笔记

    简介 终端的两种工作模式:以行为单位的工作模式,以字符数或时间为单位自定义模式 终端判断函数: int isatty(int fd) 终端属性的获取与设置: int tcgetattr(int fd, ...

  10. MySQL参数调优最佳实践

    前言很多时候,RDS用户经常会问如何调优RDS MySQL的参数,为了回答这个问题,写一篇blog来进行解释: 哪一些参数不能修改,那一些参数可以修改:这些提供修改的参数是不是已经是最佳设置,如何才能 ...