poj 3641 Pseudoprime numbers(快速幂)
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 < p ≤ and < 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
Sample Output
no
no
yes
no
yes
yes
Source
感觉好久没A题了,脑子都快生锈了,所有赶紧做做题。
求(a^p)%p==a,数据大所有用long long
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define N 1000000
#define inf 1e12
ll pow_mod(ll a,ll n,ll MOD)
{
if(n==)
return %MOD;
ll tt=pow_mod(a,n>>,MOD);
ll ans=tt*tt%MOD;
if(n&)
ans=ans*a%MOD;
return ans;
}
int main()
{
ll p,a;
while(scanf("%I64d%I64d",&p,&a)==){
if(p== && a==){
break;
}
int flag=;
for(int i=;i<(int)sqrt(p+0.5);i++){
if(p%i==){
flag=;
break;
}
}
if(flag==){
printf("no\n");
continue;
}
ll ans=pow_mod(a,p,p); //printf("%I64d\n",ans);
if(ans==a){
printf("yes\n");
}else{
printf("no\n");
}
}
return ;
}
poj 3641 Pseudoprime numbers(快速幂)的更多相关文章
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- poj 3641 Pseudoprime numbers Miller_Rabin测素裸题
题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为po ...
- POJ 3641 Pseudoprime numbers (miller-rabin 素数判定)
模板题,直接用 /********************* Template ************************/ #include <set> #include < ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- pojPseudoprime numbers (快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
随机推荐
- bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...
- Implement Queue using Stacks 解答
Question Implement the following operations of a queue using stacks. push(x) -- Push element x to th ...
- js跳转页面代码用法
一:window.location.href='https://www.baidu.com'; 需要加上http或者https,否则会查找项目内htm打开. 二:window.history.bac ...
- ajax+json+java
1.首先下载json所以依赖的包, Json-lib 最新版 json-lib-2.3-jdk15.jar,其官方网站是:http://json-lib.sourceforge.net/可以直接dow ...
- mock server相关解决方案
前后端分离之后 前后端分离后, 大家从此进入了所谓的并行开发时代. 一旦完成前后端的(边界)分工, 大家就可以各司其职了. 前端在与后端交互时, 要想有效地提高工作效率, 后端的接口文档就是重中之重了 ...
- 判断Table表中是否含有某一列
if (row.Table.Columns.Contains("DealRecord_GiftCost")) { if (row["DealRecord_Gift ...
- Jquery时间段选择器
效果(有给小bug, 在时间的大小比较上.): HTML: <html> <head> <title>测试DatePicker</title> < ...
- Uva 10652 Board Wrapping(计算几何之凸包+点旋转)
题目大意:给出平面上许多矩形的中心点和倾斜角度,计算这些矩形面积占这个矩形点形成的最大凸包的面积比. 算法:GRAHAM,ANDREW. 题目非常的简单,就是裸的凸包 + 点旋转.这题自己不会的地方就 ...
- Hadoop学习之常用命令
HADOOP基本操作命令 在这篇文章中,我们默认认为Hadoop环境已经由运维人员配置好直接可以使用. 假设Hadoop的安装目录HADOOP_HOME为/home/admin/hadoop. 启动与 ...
- php的DOMDocument 如何判断xml是否存在某节点
在做xml解释时,需要判断是否存在某个节点,查了手册函数,硬是没有发现那个函数有这个功能(如果你知道的话,请留言,博主感激涕零). 下面,就说下博主,经过N多博文,论坛查阅后,得到的一种方法(看到的博 ...