【快速幂】POJ3641 - Pseudoprime numbers
输入a和p。如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no。最简单的快速幂,啥也不说了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
ll p,a; int whether(int p)
{
int f=;
for (int i=;i*i<=p;i++)
if (p%i==)
{
f=;
break;
}
return f;
} int submain()
{
ll res=,n=p,x=a;
while (n>)
{
if (n&) res=res * x % p;
/*如果n最后一位是一,那么乘上x*/
x=x*x % p;
n>>=;
/*右移以为,即除以二*/
}
return (res==a);
} int main()
{
while (scanf("%lld%lld",&p,&a))
{
if (p==a && a==) break;
if (!whether(p))
{
if (submain()) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
else
cout<<"no"<<endl;
}
return ;
}
【快速幂】POJ3641 - Pseudoprime numbers的更多相关文章
- GCD&&素筛&&快速幂 --A - Pseudoprime numbers
Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). Th ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
- POJ3641 Pseudoprime numbers (幂取模板子)
给你两个数字p,a.如果p是素数,并且ap mod p = a,输出“yes”,否则输出“no”. 很简单的板子题.核心算法是幂取模(算法详见<算法竞赛入门经典>315页). 幂取模板子: ...
- 【POJ - 3641】Pseudoprime numbers (快速幂)
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- pojPseudoprime numbers (快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
- HDU 2817 A sequence of numbers 整数快速幂
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- Angular2.0 基础:双向数据绑定 [(ngModel)]
在属性绑定中,值从模型到屏幕上的目标属性 (property). 通过把属性名括在方括号中来标记出目标属性,[]. 这是从模型到视图的单向数据绑定. 而在事件绑定中,值是从屏幕上的目标属性 到 mod ...
- Python标准库笔记(1) — string模块
String模块包含大量实用常量和类,以及一些过时的遗留功能,并还可用作字符串操作. 1. 常用方法 常用方法 描述 str.capitalize() 把字符串的首字母大写 str.center(wi ...
- chromedriver版本 支持的Chrome版本
在使用selenium测试时,如果选择chrome浏览器,需要将chrome driver的exe文件放在项目下 错误的driver版本,会导致无法正常打开本机的浏览器 以下为对应关系 来自网络 ch ...
- GNU Readline 库及编程简介【转】
转自:https://www.cnblogs.com/hazir/p/instruction_to_readline.html 用过 Bash 命令行的一定知道,Bash 有几个特性: TAB 键可以 ...
- vs 2015 插件 supercharger 破解方式
亲测有效:效果如图 方法如下: 1.打开Supercharger的options; 2.点击Pricing & Registration 3.复制 license 然后再按Paste &am ...
- [路由] -- Yii2 url地址美化与重写
转载:http://blog.csdn.net/lmjy102/article/details/53857520
- jdbc连接远程数据库进行操作
链接远程数据库的时候,要把获得链接的url进行修改 1 package com.test; import java.sql.Connection; import java.sql.DriverMana ...
- lnmp的安装--mysql
1.前期准备 创建组:groupadd mysql 创建用户:useradd -r -g mysql mysql 创建mysql文件夹于数据存放文件夹data mkdir -p /usr/www/my ...
- Fraction to Recurring Decimal——数值处理&&哈希表
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- python2下向文件写入unicode编码的内容,codecs包
python内置库中的open方法只能读写ascii码,如果想写入Unicode字符,需要使用codecs包. # -*- coding: utf-8 -*- import codecs import ...