poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
2024-09-02 02:27:40
Pseudoprime numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7954 | Accepted: 3305 |
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-apseudoprimes, 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 anda.
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
<span style="font-size:32px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long long a,p;
long long power(long long a,long long p)
{
long long ret=1,temp=p;
while(temp)
{
if(temp&1)
ret=(ret*a)%p;
a=(a*a)%p;
temp>>=1;
}
return ret%p;
}
bool prime(long long m)
{
for(long long i=2;i*i<=m;i++)
if(m%i==0)
return false;
return true;
}
int main()
{
long long a,p;
while(~scanf("%lld %lld",&p,&a))
{
if(a==0&&p==0) return 0;
if(power(a,p)==a%p&&!prime(p))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
</span>
最新文章
- mybatis3.2.3+spring3 控制台打印sql解决办法
- [MAC]OS X Mavericks 10.9.5 / 10.10.2 VMWare vmdk镜像,解压就能用!
- MYSQL性能调优: 对聚簇索引和非聚簇索引的认识
- setup 桌面化设置网卡
- iOS UIWebView 捕获403 、404错误
- linux 多核
- 说一说window.parent
- su Authentication failure解决
- Asp.Net MVC 2.0 Filter基本用法
- JavaScript中Null和Undefined的深渊
- TheFourthJavaText
- 【转】JavaScript中使用ActiveXObject操作本地文件夹的方法
- 使用mongoose和bcrypt实现用户密码加密
- 《实战Nginx》读书笔记
- mysql 合并left join 数据条目
- Vue笔记整理——第一天
- quora 的东西就是不一样
- supervisorctl 常用命令
- oracle 创建create user 及授权grant 查看登陆的用户
- 2013-7-27 802.1X学习
热门文章
- Elastic Search中DSL Query的常见语法
- Spring 容器中 Bean 的生命周期
- 怎样解决在执行 vue init 时提示 ";vue : 无法加载文件"; 的问题?
- L1-025. 正整数A+B 简单复习一下,。
- z-index神奇的失效了!!!
- JS ES5
- Linux系统介绍及部署
- 阿里巴巴开源框架java诊断工具--Arthas
- 【踩坑经历】SQLSTATE[HY000] [2002] Connection refused
- Hacklab WebIDE在线调试ESP32笔记