给你两个数字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 (幂取模板子)的更多相关文章

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

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

  2. 【快速幂】POJ3641 - Pseudoprime numbers

    输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include ...

  3. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

  4. 【POJ - 3641】Pseudoprime numbers (快速幂)

    Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以  ...

  5. 洛谷 P1226 【模板】快速幂||取余运算

    题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...

  6. HDU 3641 Pseudoprime numbers(快速幂)

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

  7. 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) ...

  8. POJ3641-Pseudoprime numbers(快速幂取模)

    题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...

  9. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

随机推荐

  1. Oralce常用系统函数

    dual:Oracle系统内部提供的一个用于实现临时数据计算的特殊表,它只有一个列DUMMY (VARCHAR2(1)) 字符类函数: concat--连接字符串 initcap--每个单词首字母大写 ...

  2. SPRING CLOUD微服务DEMO-下篇

    目录 1 Hystix 1.1 简介 1.2 配置并测试 2. Feign 2.1 简介 2.2 使用Feign 2.3 负载均衡 2.4 Hystrix支持 2.5.请求压缩 3. Zuul网关 3 ...

  3. webbrowser 防止读取 缓存

    http://bbs.csdn.net/topics/240011502 引用 3 楼 kelei0017 的回复: Delphi(Pascal) codeprocedure TInformation ...

  4. mybatis Invalid bound statement (not found)错误解决办法

    由于新版的IntelliJ IDEA不再编译source folder下的xml文件,而我们平时使用mybatis时,习惯于将*Mapper.xml文件放在与dao层.service层平级的src目录 ...

  5. mysql 用户及权限管理 允许远程连接

    mysq,功能强大的关系型数据库,它的用户管理在开发过程中当然也尤其重要,接下来就看看mysql的用户管理 1.登录数据库 mysql -uroot -p 回车 输入密码... 回车 2.登录成功后, ...

  6. Apache Shiro 认证+授权(一)

    1.核心依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-co ...

  7. sqlalchemy防sql注入

    银行对安全性要求高,其中包括基本的mysql防注入,因此,记录下相关使用方法: 注意:sqlalchemy自带sql防注入,但是在 execute执行 手写sql时 需要考虑此安全问题 对于 wher ...

  8. Docker基础(下)

    Docker基础(下) 链接:https://pan.baidu.com/s/1u8Tg5qB4ZZHEK6GqCJkjwg 提取码:u8hb 复制这段内容后打开百度网盘手机App,操作更方便哦 5. ...

  9. mysql的几种锁

    由于对于mysql的锁机制了解的并不深入,所以翻阅了资料,整理一下自己所理解的锁.以mysql数据库的InnoDB引擎为例,因为InnoDB支持事务.行锁.表锁:且现在大部分公司使用的都是InnoDB ...

  10. Repeater的使用

    1.页面代码 如果要分页,那么页面开头必须写(<%@ Register Src="~/Controls/Page.ascx" TagName="Page" ...