POJ3641 Pseudoprime numbers (幂取模板子)
给你两个数字p,a。如果p是素数,并且ap mod p = a,输出“yes”,否则输出“no”。
很简单的板子题。核心算法是幂取模(算法详见《算法竞赛入门经典》315页)。
幂取模板子:
int pow_mod(int a,int n,int m)
{
if(n==) return ;
int x = pow_mod(a, n / , m);
long long ans = (long long)x * x % m;
if(n%) ans = ans * a % m;
return (int)ans;
}
题目代码也比较简单,有一个坑点是如果用筛素数打表,数组开不了这么大。
报错:error: total size of array must not exceed 0x7fffffff bytes
报错代码:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll pow_mod(ll a, ll n, ll m)
{
if (n == )
return ;
ll x = pow_mod(a, n / , m);
ll ans = x * x % m;
if (n % )
ans = ans * a % m;
return ans;
}
const long long maxn = + ;
int *vis = new int[maxn];
void prime()
{
memset(vis, , sizeof(vis));
int len = sqrt(maxn * 1.0);
for (int i = ; i <= len; i++)
if (!vis[i])
for (int j = i * ; j <= maxn; j += i)
vis[j] = ;
}
int main()
{
int p, a;
prime();
while (cin >> p >> a)
{
if (!vis[p])
cout << "no\n";
else
{
if (pow_mod(a, p, p) == a)
cout << "yes\n";
else
cout << "no\n";
}
}
delete[] vis;
return ;
}
AC代码:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll pow_mod(ll a, ll n, ll m)
{
if (n == )
return ;
ll x = pow_mod(a, n / , m);
ll ans = x * x % m;
if (n % == )
ans = ans * a % m;
return ans;
}
bool prime(ll a)
{
if (a == )
return ;
if (a == )
return ;
for (int i = ; i * i <= a; i++)
if (a % i == )
return ;
return ;
}
int main()
{
ll a, p;
while (cin >> p >> a)
{
if (a == && p == )
break;
else
{
if (prime(p))
{
cout << "no\n";
continue;
}
if (pow_mod(a, p, p) == a)
cout << "yes\n";
else
cout << "no\n";
}
}
}
POJ3641 Pseudoprime numbers (幂取模板子)的更多相关文章
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- 【快速幂】POJ3641 - Pseudoprime numbers
输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!
题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...
- POJ3641-Pseudoprime numbers(快速幂取模)
题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
随机推荐
- Nginx负载均衡之TCP/UDP流
负载均衡是指在多个后端服务器之间有效地分配网络流量. 从NGINX Plus R5[1] 版本开始可以代理和负载均衡传输控制协议(Transmission Control Protocol,TCP)通 ...
- Kali开启SSH服务
1. 一.配置SSH参数 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将NO修 ...
- Android深度探索-卷1第四章心得体会
这一章的和三章的git用法有联系,so,吧上一章的git基本用法搞好了再来,具体的方法就是看书上网查,这里就不做详细步骤介绍了.这章就有点意思了,是源码的下载和编译,有能看的,能自己鼓捣的,本章介绍的 ...
- Tomcat服务的配置
首先到Apache官网,下载tomcat,在官网有两种tomcat,一种是安装版,一种是压缩版,对于安装版的一台机器只能安装一个tomcat,而对于压缩版的tomcat一台机器可以安装多个tomcat ...
- 10个Eclipse珍藏插件推荐
1.Open Explorer 打开资源管理器插件,这是一个从Eclipse里面可以直接定位打开windows资源管理器文件的插件,这个版本的插件在最新的Eclipse版本中都能使用. 下载地址:ht ...
- ELK+filebeat+redis 日志分析平台
一.简介 ELK Stack是软件集合Elasticsearch.Logstash.Kibana的简称,由这三个软件及其相关的组件可以打造大规模日志实时处理系统. 其中,Elasticsearch 是 ...
- 三、python之文件的处理
1.文件的读取 1.1 读取整个文件 假设我们有一个叫做“hello.txt”的文件,文件内容如下: helloWorld helloPython helloJava 在该文件中,有三行字符串,接下来 ...
- Codeforces 492D Vanya and Computer Game
D. Vanya and Computer Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Jquery的$.ajax、$.get、$.post发送、接收JSON数据及回调函数用法
平时研究代码时,经常会遇到AJAX的相关用法,做项目时才真正体会到Ajax的强大之处(与服务器数据交互如此之便捷,更新DOM节点而不用刷新整个页面),以及运用的频繁程度.今天整理了一下自己之前没搞清楚 ...
- Juqery插件编写 基础说明
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...