hdoj1905 Pseudoprime numbers (基础数论)
Problem Description
Fermat's theorem states that for any prime number p and for any integer a > 1, a^p == 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 ≤ 1,000,000,000 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,a;如果a的p次方对p取余等于a,并且p不是素数,则输出“yes”,否则输出“no”.
这里用到快速幂求余技巧
#include <iostream>
#include <stdio.h>
using namespace std;
bool isprime(long long n){
for (long long i = ; i*i <= n; i++){
if (n%i == )
return false;
}
return true;
}
long long qmod(long long a, long long r, long long m){
long long res = ;
while (r){
if (r & )
res = res*a%m;
a = a*a%m;
r >>= ;
}
return res;
}
int main(){
long long p, a;
while (scanf("%I64d%I64d", &p, &a) && p&&a){
if (!isprime(p) && qmod(a, p, p) == a)
printf("yes\n");
else
printf("no\n");
}
return ;
}
hdoj1905 Pseudoprime numbers (基础数论)的更多相关文章
- LightOJ1214 Large Division 基础数论+同余定理
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- poj Pseudoprime numbers 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10903 Accepted: 4 ...
- HDU-1576 A/B 基础数论+解题报告
HDU-1576 A/B 基础数论+解题报告 题意 求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973) (我们给定的A必能被B整除,且gcd(B,9973) = 1). 输入 数据 ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
随机推荐
- python 运行环境
Python 是一种半编译半解释型运行环境.首先,它会在模块 "载入" 时将源码编译成字节码 (ByteCode).而后,这些字节码会被虚拟机在一个 "巨大" ...
- ALINX公众号
请大家加一下ALINX公众号,后续FPGA资料更新,活动信息,新产品发布将通过微信公众号进行第一时间通知.
- Install Local SQL In Mac OS
extends:http://www.cnblogs.com/maxinliang/p/3583702.html 一.安装 到MySQL官网上http://dev.mysql.com/download ...
- namecheap 添加二级域名
namecheap Docs Nginx 添加一个server server { listen 80; server_name video.ajanuw.fun; location / { root ...
- mybatis13--2级缓存
验证内置的2级缓存 Ehcache缓存的配置 01.引入需要的ehcache 和mybatis-ehcache 两个jar包 02.在mapper文件中增加 <cache type=" ...
- 让wampserver2.5.exe支持sql server数据库的方法
将D:\wamp\bin\php\php5.5.12\ext路径下 这两个文件复制到php.ini中 链接数据库方法 <?php $serverName = "."; $co ...
- word-break和word-wrap的使用和区别
问题起源: 中文是一个字就是一个单词,而英文字母要有一个空格才将他们分割为一个单词:文字换行没事,主要是英文 <!DOCTYPE html> <html> <head&g ...
- css小知识 2
效果为 为什么还出现出现不同的效果? 浏览器在解析第二个p的时候,因为第二个字母见没有空格,它会认为这是一个单词没有写完,所以不会换行 列表 1.无序列表ul 第二,内部必须有子代标签<li&g ...
- python全栈开发 * 18 面向对象知识点汇总 * 180530
18 面向对象初识1class person: level="高级动物" mind="有思想" def __init__(self,name,age,gent, ...
- 关闭shift中英文切换 英文代码/中文注释随意切换着写。
x 背景 写代码的时候总是意外的就切成中文了,特别是代码中大小写切换的这种情况... 例如:"public static TimeZone CurrentTime..."publi ...