poj_3641_Pseudoprime numbers
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 题意:费马定理给出a^p=a mod p(p为素数),一些合数也有类似的状况,判断输入p,a
先判断 p是否为素数,后判断是否满足定理
#include<iostream>
#include<cstdio>
#define LL long long
#define N 100000
using namespace std;
int prime[N];
int pn=0;
bool vis[N];
LL pow(LL a,LL n,LL mod)
{
LL base=a,ret=1;
while(n)
{
if(n&1) ret=(ret*base)%mod;
base=(base*base)%mod;
n>>=1;
}
return ret%mod;
}
bool judge(int n)
{
for(int i=0;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==0)
return 1;
}
return 0;
}
int main()
{
for (int i = 2; i < N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (int j = i; j < N; j += i)
vis[j] = 1;
}
int a,p;
while(~scanf("%d%d",&p,&a),a&&p)
{
if(!judge(p)){
puts("no");
continue;
}
if(pow(a,p,p)%p==a)
puts("yes");
else
puts("no"); }
}
poj_3641_Pseudoprime numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- 怎么为android控件边缘添加阴影
为控件设置一个有阴影感的背景图片即可,可以使用shape 在自定义shape中增加一层或多层,并错开,即可显示阴影效果.为增加立体感,按钮按下的时候,只设置一层.我们可以通过top, bottom, ...
- 解决dede图集上传图片时跳出302错误
错误.以前从来没遇到过,想了半天也没想出是哪里出了错误,郁闷~ 没辙,去论坛搜了一下,还真有同命相连的兄弟,同样爆出这个错误.往下拉了几楼,还是找到了答案. 解决办法是: 在include/userl ...
- Oracle安装后忘记用户名或密码+创建新登陆用户
新安装的Oracle11g,不料在使用的时候没记住安装时的用户名和密码. 不用担心,打开sqlplus. 按如下步骤,新建一个登陆用户: 第一步:以sys登陆 sys/密码 as sysdba 此 ...
- Unity C# ref与out
ref和out 都是按地址传递的,使用后都将改变原来的数值.ref 方法参数关键字使方法引用传递到方法的同一个变量.当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中.若要使用 r ...
- Unity C# string 与byte 直接的转换
指纹模版的转换 可还原字节数组byt string str = Convert.ToBase64String(bytes); byte[] bytes = Convert.FromBase64Stri ...
- Props 和 IActorRef 3
在Actor模式中我们使用IActorRef通过ActorSystem来发送消息数据.这么做有两个理由 1.ActorSystem会在消息中封装一些元数据(metadata),这些数据我们可以通过上下 ...
- nopCommerce如何支持MySQL
此方法支持nopCommerce2.4以上版本(缺少的代码,可参照nopCommerce2.6源码) nopCommerce 4.1 如何支持Mysql 请看 Url: http://www.nop ...
- (生产)js-base64 - 转码
参考:https://github.com/dankogai/js-base64 安装 $ npm install --save js-base64 使用 var Base64 = require(' ...
- window下隐藏apache版本和PHP脚本等敏感信息
隐藏Apache信息 1.1 主配置中启用httpd-default.conf 文件: conf/httpd.Conf 找到httpd-default.conf,删除前面的注释“#”,改成如下 Inc ...
- unity工具介绍
MemoryProfiler内存分析工具 行为树 behaviac github 场景动画编辑器 Snapdragon Profiler性能分析工具 VTune性能分析器 protobuf数据交换格式 ...