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>

poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题的更多相关文章

  1. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  2. POJ 3641 Pseudoprime numbers (数论+快速幂)

    题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...

  3. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

  4. poj 3641 Pseudoprime numbers(快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  5. POJ 3641 Pseudoprime numbers (miller-rabin 素数判定)

    模板题,直接用 /********************* Template ************************/ #include <set> #include < ...

  6. poj 3641 Pseudoprime numbers Miller_Rabin测素裸题

    题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为po ...

  7. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  8. HDU 3641 Pseudoprime numbers(快速幂)

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11336   Accepted: 4 ...

  9. 【UVA - 10006 】Carmichael Numbers (快速幂+素数筛法)

    -->Carmichael Numbers  Descriptions: 题目很长,基本没用,大致题意如下 给定一个数n,n是合数且对于任意的1 < a < n都有a的n次方模n等于 ...

随机推荐

  1. T100——q查询,子母查询(汇总——明细)练习笔记

    范例: 代码: #add-point:input段落 name="ui_dialog.input" INPUT BY NAME g_master.bdate,g_master.ed ...

  2. http请求之of_ordering_getmiditem

    //Public function of_ordering_getmiditem (string as_instr,string as_key) returns string //string as_ ...

  3. IDEA将项目打包为指定class文件的jar

    转自:https://blog.csdn.net/makang456/article/details/86699375 [背景]     公司最近要和某大公司合作,将本公司的产品提供给其它公司单独部署 ...

  4. 并不对劲的CF1239B&C&D Programming Task in the Train to Catowice City

    CF1239B The World Is Just a Programming Task 题目描述 定义一个括号序列s是优秀的,当且仅当它是以下几种情况的一种: 1.|s|=0 2.s='('+t+' ...

  5. stuff拼接字符串

    stuff stuff(param1,startIndex,length,param2) 说明:将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用 ...

  6. wpf 模板绑定父对象

    有两种方式可以实现在模板中元素绑定到父对象 1.<ContentPresenter Margin=”{TemplateBinding Padding}”/> 2.Color=”{Bindi ...

  7. js定时器 离开当前页面任然执行的问题

    今天在博客上看到有人问 js定时器-----离开当前页面原本匀速运动的div加速了,回到页面若干时间恢复匀速??? 他是js定时器控制一个盒子做旋转动画 离开页面后js还在执行 但是盒子这个dom却被 ...

  8. Databus&canal对比

    Databus和canal都能够提供实时从数据库获取变更,并提供给下游的实时消费流的功能. 本文针对两个系统实现和应用上的不同点,做了一个简单的对比: 对比项 Databus canal 结论 支持的 ...

  9. Solved: XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX

    esxi 出现XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX 问题. 回避方法: 将高级设置-->Us ...

  10. LVS (Linux虚拟服务器)-不同的负载均衡方法

    随着Internet用户的增长,基于Web的公司处理的通信量急剧增加.有各种解决方案来应对这种不断增长的流量. 一种解决方案是垂直扩展服务器(即:简单地向服务器添加更多的CPU和内存资源.)当然在一定 ...