给你两个数字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. datastudion 资源导入python包,编写模块

    学习文档,不懂再问. https://help.aliyun.com/document_detail/74423.html?spm=a2c4g.11186623.6.688.72635debHqgkV ...

  2. linux远程管理器 - xshell和xftp使用教程(zhuan)

    准备好连接linux服务器的工具,推荐用xshell和xftp. xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET ...

  3. [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being

    [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...

  4. (转)微信调用扫码和支付功能是都报错 the permission value is offline verifying

    原文地址:https://blog.csdn.net/qq_34794885/article/details/98504970

  5. MYSQL 使用存储过程批量更新表数据

    功能:实现将表result_good_city_dzl中的字段lat更新为表 result_good_city_lh中的lat,条件是两个表中的id一样 即: update result_good_c ...

  6. Codeforces 500D New Year Santa Network(树 + 计数)

    D. New Year Santa Network time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. 简单认识php

    1.输出语句: echo 'hello world'; 2.声明变量用 $ 符号 $uname = 'andy'; 3. php 拼接字符串用 点(.) //设置中文编码 header("c ...

  8. docker 安装Filebeat

    1.查询镜像 docker search filebeat 2.拉取镜像 我此处选择的是prima/filebeat docker pull prima/filebeat 3.创建配置文件 fileb ...

  9. day01 html介绍 文档声明头 head标签 body标签

    day01 html   初识html <!--文档的声明--> <!doctype html>   <html lang="en">    # ...

  10. 42th-2

    '''   1, 元祖(2,3)'''def summ2(self, *args):    '''这是一个求一系列数平方和的函数'''    s = 0    for i in args:  #历遍元 ...