Hdoj 1905.Pseudoprime numbers 题解
Problem Description
Fermat's theorem states that for any prime number p and for any integer a > 1, a^p == 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 ≤ 1,000,000,000 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
Author
Gordon V. Cormack
Source
思路
就是判断\(a^p\%p==a\),计算\(a^p\)可以用快速幂的方法,快速幂本质也是二分不断加速
代码
#include<bits/stdc++.h>
using namespace std;
typedef __int64 ll;
bool isprime(ll x)
{
for(int i=2;i<sqrt(x);i++)
if(x%i==0)
return false;
return true;
}//判断是否为质数
ll quickpower(ll a,ll b,ll c)
{
ll ans =1;
while(b)
{
if(b&1)
ans = (ans*a) % c;
a = (a*a) % c;
b >>= 1;
}
return ans;
}//返回a^b%c的结果
int main()
{
int a,p;
while(cin>>p>>a)
{
if(p==0 && a==0) break;
if(isprime(p))
cout << "no" << endl;
else
{
int ans_power = quickpower(a,p,p);
if(ans_power==a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
}
return 0;
}
Hdoj 1905.Pseudoprime numbers 题解的更多相关文章
- Hdoj 1058.Humble Numbers 题解
Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...
- hdu 1905 Pseudoprime numbers
#include<stdio.h> #include<math.h> #define ll long long ll mod; bool Judge(int x) { ;i&l ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- 找规律/数位DP HDOJ 4722 Good Numbers
题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- poj Pseudoprime numbers 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10903 Accepted: 4 ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
随机推荐
- Day15 Python基础之logging模块(十三)
参考源:http://www.cnblogs.com/yuanchenqi/articles/5732581.html logging模块 (****重点***) 一 (简单应用) import lo ...
- semantic-ui 图标
semantic-ui提供了很多的图标,基本常用的在官网上面都能找到.要想记住这么多图标是不可能的,但是也是有简便方法记忆. 首先,图标其实和按钮的区别基本没有,要说有的话,也就是基础样式的大小不同吧 ...
- asp.net mvc 三种过滤器
前几天面试遇到这个问题,发现不是很了解,学习了下,这里记录下来 经常需要将用户的操作记录到日志中,或者是验证用户是否登录了网站, 面对这样的需求,以前的操作是自定义一个统一的全局方法,然后做处理, 在 ...
- C#的修饰符
C#的修饰符 废话少说,直接上总结: 一.在命名空间下: 类:默认修饰符为internal 接口:默认的修饰符为internal 结构体:默认的修饰符为internal 枚举:默认的修饰符为inter ...
- Laravel认证模块开发
菜鸟学Laravel(二) Laravel认证模块开发 laravel内部已经做好了一个简单的登录模块,我们可以用如下命令来生成: 1 php artisan make:auth 我们查看一下路由 ...
- 如何让pl/sql developer记住密码,实现快速登录
前两天,有同事使用plsql的时候,切换数据库的时候需要不断的重复输入密码,这样太麻烦了. 下面,我这里说下如何的实现plsql不需要输入密码就能快速登录的方法: 1.一开始登录,首先像往常那样输入密 ...
- maven中jar下载失败
关键词:maven jar 错误描述:反编译时遇到 invalid LOC header (bad signature)的问题 aether-89969cb8-5741-44e3-be2c-74f90 ...
- java 中Excel的导入导出
部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字 的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...
- Dom4j解析
dom4j-1.6.1.jar, 这个包提供了xml解析相关的方法. 这里做一个记录,微信公众号里需要对HttpServletRequest做解析,实际上也可以用dom4j提供的方法进行解析转换. 这 ...
- 建议3---理解Python与C语言的不同之处
我们都知道,Python的底层是用C语言实现的,但切忌用C语言的思维和风格来编写Python代码.Python与其他语言有很多不同,以下来进行简单的分析: (1)"缩进"与“{}” ...