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 ...
随机推荐
- python 类和对象上
面向对象 Object Oriented 面向对象的学习: 面向对象的语法(简单,记忆就可以搞定) 面向对象的思想(稍难,需要一定的理解) 面向过程和面向对象的区别 面向过程开发,以函数作为基本结构使 ...
- Springboot03-异常处理
springboot默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并然后返回一个固定的错误页面 统一异常处理 创建全局异常处理类 @ ...
- StarUml3.10 Mac 注册key 破解
/Applications/StarUML.app/Contents/Resources StarUML是用nodejs写的.确切的说是用Electron前端框架写的.新版本中所有的starUML源代 ...
- leetcode.双指针.680验证回文字符串-Java
1. 具体题目 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...
- redis 入门之集合
sadd 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略.假如 key 不存在,则创建一个只包含 member 元素作成员的集合.当 key 不 ...
- [伯努利数] poj 1707 Sum of powers
题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS Memory Lim ...
- C#冒泡排序法学习
一,冒泡排序法理解:就是将一个集合里的数据当前位置和后一位比较,然当前位置大于后一位,则两个位置替换,直到排序完成 using System; using System.Collections.Gen ...
- Logback配置,error和普通日志分离
<?xml version="1.0" encoding="utf-8"?> <configuration> <springPro ...
- columns样式属性使用
columns样式属性使用 columns:用于设置元素的列宽和列数.它是column-width和column-count的简写属性. 语法: columns: <'column-width' ...
- 点击td对同行的其他td进行操作
假设有这样的一段tr: <tr> <td><input type="checkbox" name="item" class=&qu ...