HDU 3641 Pseudoprime numbers(快速幂)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11336 | Accepted: 4891 |
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
题意:p不是素数,且a^p对p取模等于a,输出yes,其他的输出no。
题解:判断p是否是素数那部分直接蛮力求就好。
#include <iostream>
using namespace std;
typedef long long ll;
bool is_prime(ll x)
{
int i;
if (x == ) return ;
if (x == ) return ;
for (i = ; i*i < x; i++)
{
if (x %i == )
return ;
}
return ;
}
int main()
{
ll a, p;
while (cin >> p>>a)//p=n
{
if (a == && p == ) break;
if (is_prime(p))
{
cout << "no" << endl;
continue;
}
ll ans = ;
ll k = p;
ll x = a;
while (p > )
{
if (p & ) ans = (ans * a)%k;
a = (a * a)%k;
p >>= ; }
if (ans%k == x) cout << "yes" << endl;
else cout << "no" << endl;
}
return ;
}
HDU 3641 Pseudoprime numbers(快速幂)的更多相关文章
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- pojPseudoprime numbers (快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
随机推荐
- [Pytorch]Pytorch 细节记录(转)
文章来源 https://www.cnblogs.com/king-lps/p/8570021.html 1. PyTorch进行训练和测试时指定实例化的model模式为:train/eval eg: ...
- [BZOJ1257][CQOI2007]余数之和
题目大意 给你 \(n, k\),计算 $ \sum_{i=1}^n k \bmod i$ 解析 注意到 $ k\bmod i=k-[k/i] \times i$ 则上式等于 $ n \times k ...
- shell 按行读取文件
#!/bin/bash count= //赋值语句,不加空格 cat test | while read line //cat 命令的输出作为read命令的输入,read读到的值放在line中 do ...
- 带宽检测工具iftop
1.安装 # yum install iftop –y 2.使用 # iftop -i eth0 -n # iftop -i eth0 -P 说明: 中间的<= =>这两个左右箭头,表示的 ...
- Spring IOC 源码简单分析 01 - BeanFactory
### 准备 ## 目标 了解 Spring IOC 的基础流程 ## 相关资源 Offical Doc:http://docs.spring.io/spring/docs/4.3.9.RELEASE ...
- Spring4 MVC RESTFul WebServices CRUD实例+RestTemplate
简单介绍REST REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP ...
- python自动化运维之路2
list列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 #!/usr/bin/env python # _*_ encoding:utf-8 _*_ # a ...
- Mysql04
mysql: dbs 数据库系统 bdms 数据库管理系统 bda 数据库管理员 db 数据库 dba通过dbms来操作db! 关系型数据库和非关系型数据库 登录mysql mysql -h主机地址 ...
- 用django发送异步邮件
太阳底下没有新鲜事,github是一个神奇的地方,你有什么想法,需求,点子.其实别人早就想到,而且也已经做到. 所以不要高估自己,有什么想法还是GITHUB一下,免得成了井底之娃. 这几天一直在研究p ...
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks
class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...