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

  1. POJ Pseudoprime numbers( Miller-Rabin素数测试 )

    链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...

  2. poj 3641 Pseudoprime numbers

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

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

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

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

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

  5. HDU 3641 Pseudoprime numbers(快速幂)

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

  6. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

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

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

  8. poj 3641 Pseudoprime numbers Miller_Rabin测素裸题

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

  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. WPF DataGridCheckBoxColumn需要点两次才能修改checkbox状态

    如题,如果必须要用DataGridCheckBoxColumn使用一下方式就可以解决需要点击两次才能改状态的问题 <DataGridCheckBoxColumn> <DataGrid ...

  2. window.addEventListener介绍说明

    原型 public override function addEventListener(type:String, listener:Function, useCapture:Boolean = fa ...

  3. 用C#(ASP.Net)在Exchange Server环境下发送邮件

    普通的邮件, 用System.Net.Mail 类 或 System.Web.Mail 类 处理即可, 但是Exchange Server 环境下, 这两个类起不了作用-------至少目前我看到的情 ...

  4. Bootstrap 下拉菜单(dropdown)插件

    使用下拉菜单的插件,您可以向任何组件(比如:导航栏,标签页,胶囊式导航,按钮)添加下拉菜单 用法 您可以切换下拉菜单(dropdown)插件隐藏内容 1.通过data属性,向链接或按钮添加data-t ...

  5. iview Tooltip换行及应用

    第一种: <Tooltip placement="bottom"> <Button>Multiple lines</Button> <di ...

  6. 快速启动mongodb服务

    在桌面创建一个mongodb.bat文件 输入以下内容: D:cd D:\mongodb\binstart mongod --dbpath D:\mongodb\data\dbcd D:\robot\ ...

  7. 我如何解决Centos下cannot find a valid baseurl for repo的问题的

    刚刚安装完centos,进入命令行模式后,发现所有的命令都不能使用,最后一行显示:Error:Cannot find a valid baseurl for repo:base,如何解决? 在cent ...

  8. LeetCode之Weekly Contest 92

    第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...

  9. webpack4搭建Vue开发环境笔记~~持续更新

    项目git地址 一.node知识 __dirname: 获取当前文件所在路径,等同于path.dirname(__filename) console.log(__dirname); // Prints ...

  10. Vue表单输入绑定

    <h3>基础用法</h3> <p>你可以用<strong>v-model</strong>指令在表单input,textarea以及sele ...