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 ...
随机推荐
- (Matlab)GPU计算简介,及其与CPU计算性能的比较
1.GPU与CPU结构上的对比 2.GPU能加速我的应用程序吗? 3.GPU与CPU在计算效率上的对比 4.利用Matlab进行GPU计算的一般流程 5.GPU计算的硬件.软件配置 5.1 硬件及驱动 ...
- sqlite中的时间
插入时间的sql语句 ','-61') 时间格式'2014-11-17T19:37:32' 年月日和时分秒之间多了一个字母T,保存到数据库的时候,会自动给时间加8个小时. 保存的结果为2014-11- ...
- UVa 10491 奶牛和轿车(全概率公式)
https://vjudge.net/problem/UVA-10491 题意: 假设有a头牛,b辆车,在最终选择前主持人会替你打开c个有牛的门,输出"总是换门"的策略下,赢得车的 ...
- java 普通容器,同步容器,并发容器,同步工具
同步容器,如HashTable,提供独占访问. 并发容器,ConcurrentHashMap,有着更好的并发性能,但是不能独占访问. --putIfAbsent 同步工具: 闭锁:CountDownL ...
- python 字典获取最大和最小的value
my_dict = {, , } key_max = max(my_dict.keys(), key=(lambda k: my_dict[k])) key_min = min(my_dict.key ...
- Java网络编程学习A轮_07_基于Buffer的Socket编程
示例代码: https://github.com/gordonklg/study,socket module A. LineSeparate 基于 Buffer 实现逐行读取的 EchoServer ...
- 在am中定义消息集束,并在CO中验证之后抛出异常。
需求:在页面上点某个按钮的时候,需要收集所有异常并抛出. -------------------------------------------方式1:参考 EBS OAF开发中的错误/异常处理(Er ...
- git 基础入门操作
前言: 介绍基础的git入门级指令,虽然git指令非常多,但是实际工作中,我们会用到的非常少,小项目中甚至只需要用到2.3个.而且大部分人都会采用gui,而不是每次都打开终端然后输一长串难记的指令. ...
- Linux jdk环境配置模板
export JAVA_HOME=/opt/JAVA/jdk1.8.0_191export JRE_HOME=${JAVA_HOME}/jreexport CLASSPATH=.:${JAVA_HOM ...
- 201621123005《Java程序设计》第十次实验总结
201621123005<Java程序设计>第十周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 1. 常用异常 1.1 自己 ...