poj 3641 快速幂
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
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
ll quick_pow(ll a,ll b,ll mod) {
ll ans=;
while(b) {
if(b&)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=;
}
return ans;
}
bool isprime(int x) {
for(int i=;i*i<=x;i++) {
if(x%i==) return false;
}
return true;
}
int main() {
// freopen("input.txt","r",stdin);
int a,p;
while(scanf("%d%d",&p,&a)!=EOF) {
if(a==&&p==) break;
if(isprime(p)) {
printf("no\n");
continue; }
ll z=quick_pow(a,p,p);
if(z==a) printf("yes\n");
else printf("no\n");
}
return ;
}
poj 3641 快速幂的更多相关文章
- POJ 3641 快速幂+素数
http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...
- Pseudoprime numbers(POJ 3641 快速幂)
#include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...
- POJ 1995 快速幂模板
http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...
- POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)
Sumdiv Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Raising Modulo Numbers(POJ 1995 快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5934 Accepted: ...
- poj 1995 快速幂
题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11 11=2^0+2^1+2^3 => 3^1*3 ...
- POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)
题意: 给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路: 感觉很赞的一个题目,据说证明是什 ...
- POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
随机推荐
- html布局(盒子)
在body里面放置两个盒子,里面盒子设置margin-top,外层盒子生效?在里面盒子上面加一个块元素,设置高度 表单 form action="地址" method=" ...
- 关于Ubuntu使用笔记
Ubuntu vm tools 安装 sudo apt install open-vm-tools-desktop 在安装程序时Ubuntu会将安装目录锁定,安装结束后会解除锁定,中断安装后无法再安装 ...
- SQL语句:如何让字符串转化数字
和前端联调的时候,突然出现一个状况,新增数据的时候,一直报系统错误,写下此文,留以后反复温习.菜鸟程序员一名~ 项目内容:新增产品信息 具体实现:1 获取基础信息,创建产品(调用接口传入的产品类型,如 ...
- 类 Arrays StringBuilder 跟 StringBuffer 的异同 SimpleDateFormat
类 String 同:起连接字符串类型作用 异: StringBuffer //线程安全 效率慢 StringBuilder //线程不安全 效率快 类 Arrays copyOf ( ...
- 小练习_num1
题目:将一个正整数分解质因数.例如:输入90,打印输出90=2*3*3*5. /* 分解质因数 */ import java.util.*; class num1 { public static vo ...
- Python元组的一点用法
#python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 元组可以理解为不可变动的列表,访问元组中元素的方式和列表相同,但是需要注意的是元组中的元素是不可修改(增删)的 1.列表的 ...
- Linux监控
第三十次课 Linux监控 目录 一. Linux监控平台介绍 二. zabbix监控介绍 三. 安装zabbix 四. 忘记Admin密码如何做 五. 主动模式和被动模式 六. 添加监控主机 七. ...
- 二、redis持久化
一.redis持久化 1 RDB持久化(定redis的数据定时dump到磁盘上的RDB持久化)RDB持久化是指在指定的时间间隔内将内存中的数据集快照写入磁盘,实际操作过程是fork一个子进程,先将数据 ...
- 20175224 2018-2019-2 《Java程序设计》第九周学习总结
教材学习内容总结 第11章 JDBC与MySOLz数据库 MySQL数据库管理系统,简称MySQL. 使用步骤: 启动MySQL数据库服务 器建立连接: 建立数据库: 创建表等操作. JDBC:为专门 ...
- 常忽略的css技巧
1.利用 CSS 的伪类中的content属性获取attr中的信息 效果图:鼠标放上去出现提示 css代码: .box{position:relative;display:inline-block;m ...