POJ3641(快速幂)
2024-10-21 11:53:10
Pseudoprime numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8529 | Accepted: 3577 |
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
思路:问p是不是伪素数。伪素数条件:①p不是素数。② ap = a (mod p)。
#include <iostream>
using namespace std;
typedef unsigned long long ull;
ull a,p;
bool testPrime(ull x)
{
for(ull i=;i*i<=x;i++)
{
if(x%i==)
{
return false;
}
}
return true;
}
ull mpow(ull x,ull n,ull mod)
{
ull res=;
while(n>)
{
if(n&)
{
res=(res*x)%mod;
}
x=(x*x)%mod;
n>>=;
}
return res;
}
int main()
{
while(cin>>p>>a)
{
if(a==&&p==) break;
if(testPrime(p))
{
cout<<"no"<<endl;
}
else
{
if(mpow(a,p,p)==a%p)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
}
return ;
}
最新文章
- 一次sql注入中转
- Ubuntu14.10安装Eclipse
- Git 常用命令行
- SQL Server Management Studio 已停止工作 异常错误
- 安装R语言扩展包vegan
- Java实验一
- android——创建camera应用(译)
- OO ALV 学习参考
- Office2007图标变成白框,但是还能使用问题解决办法
- 一个经典的js中关于块级作用域和声明提升的问题
- java类的初始化
- 学习笔记——抽象工厂模式Abstract Factory
- Android组件生命周期(一)
- Windows 10下Markdown不能显示预览
- android离线缓存技术
- js-ES6学习笔记-Class
- 深入理解Java虚拟机 #01# 自己编译JDK
- CSS 0.5px 细线边框的原理和实现方式
- 【刷题】BZOJ 1061 [Noi2008]志愿者招募
- Hash开散列 拉链法
热门文章
- MySQL-LRU_List Free_List Flush_List
- IntelliJ Idea 快捷键精选
- java深入探究12-框架整合
- Effective java第一章引言
- node操作mongdb的常用函数示例
- 智课雅思词汇---十五、前缀co-com-con-col-cor-是什么意思
- spring: 使用嵌入式数据源 EmbeddedDatabaseBuilder
- 免费获得 NTFS for Mac 12. Special Edition 激活码活动
- JProfiler远程监控 -转
- Struts2(2)