Codevs 1702 素数判定 2(Fermat定理)
1702 素数判定 2
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
传送门
题目描述 Description
一个数,他是素数么?
设他为P满足(P<=263-1)
输入描述 Input Description
P
输出描述 Output Description
Yes|No
样例输入 Sample Input
2
样例输出 Sample Output
Yes
数据范围及提示 Data Size & Hint
算法导论——数论那一节
注意Carmichael Number
分类标签 Tags
素数判定 数论
#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
LL n;
int s[]={2,3,5,7,11,13,17,19,23,29};
LL slowmi(LL a,LL b)
{
LL tot=0;
for(LL i=a;i;i>>=1)
{
if(i&1) tot=(tot+b)%n;
b=(b+b)%n;
}
return tot%n;
}
LL mi(LL a,LL b)
{
LL tot=1;
for(LL i=a;i;i>>=1)
{
if(i&1) tot=slowmi(tot,b)%n;
b=slowmi(b,b)%n;
}
return tot%n;
}
bool check()
{
if(n==2) return true;
else if(n<2||!(n&1)) return false;
for(int i=0;i<10;i++)//费马小定理10次计算
{
if(n==s[i])return true;
if(mi(n-1,s[i])!=1) return false;
}
return true;
}
int main()
{
cin>>n;
if(check())
printf("Yes");
else printf("No");
return 0;
}
Codevs 1702 素数判定 2(Fermat定理)的更多相关文章
- Miller-Rabin算法 codevs 1702 素数判定 2
转载自:http://www.dxmtb.com/blog/miller-rabbin/ 普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(slog³n)的算法. 定理一:假如p是质数,且 ...
- Miller_Rabin codevs 1702 素数判定2
/* 直接费马小定理 */ #include<iostream> #include<cstdio> #include<cstdlib> #include<ct ...
- codevs 1702素数判定2
Miller-Rabin算法实现,但是一直被判题程序搞,输入9999999999得到的结果分明是正确的但是一直说我错 #include <cstdio> #include <cmat ...
- codevs——1430 素数判定
1430 素数判定 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Description 质数又称素数.指在一个大于1的自然数中, ...
- HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)
Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...
- 【数论】【素数判定】CODEVS 2851 菜菜买气球
素数判定模板. #include<cstdio> #include<map> using namespace std; ],ans=-,l,r,n,sum[]; bool is ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法!
公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法! 先存档再说,以后实验报告还得打印上交. Miller-Rabin大素数判定对于学算法的人来讲不是什么难事,主要了解其原理. 先来灌 ...
- Miler-Rabbin素数判定
前言 素数判定? 小学生都可以打的出来! 直接暴力O(n)O(\sqrt n)O(n)-- 然后就会发现,慢死了-- 于是我们想到了筛法,比如前几天说到的詹欧筛法. 但是线性的时间和空间成了硬伤-- ...
随机推荐
- SNMP: Simple? Network Management Protocol(转)
转自:http://www.rane.com/note161.html An SNMP Overview The Message Format The Actual Bytes Introductio ...
- 框架技术--S2SH框架整合(spring部分)No 3--声明式事务
声明式事务:就是讲事务的处理,通过配置进行配置. 几种传播特性 1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务.如果没有事务则开启(比较常用) 2. PROPA ...
- 运用Date日期来做日历
import java.util.*;import java.text.*;class Two { public static void main(String[] args) { ...
- 分布式搜索Elasticsearch增、删、改、查操作深入详解
引言: 对于刚接触ES的童鞋,经常搞不明白ES的各个概念的含义.尤其对“索引”二字更是与关系型数据库混淆的不行.本文通过对比关系型数据库,将ES中常见的增.删.改.查操作进行图文呈现.能加深你对ES的 ...
- php连mssql中文乱码问题
我在将一个aspx+mssql的系统做成php+mssql的系统时,感觉架构大不一样,aspx多是aspx页面+aspx.cs后台协同开发,多用可视化空间开发,而php我则选用了smarty模板,感觉 ...
- ios的@property属性和@synthesize属性
当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property 和@synthesize属性,@property用在 .h ...
- JVM专题
http://blog.csdn.net/ITer_ZC/article/category/2758863
- 从源码角度深入理解LayoutInflater
关于LayoutInflater,在开发中经常会遇到,特别是在使用ListView的时候,这个几乎是必不可少.今天我们就一起来探讨LayoutInflater的工作原理. 一般情况下,有两种方式获得一 ...
- 下载好一个android软件之后,怎样自动提示安装?
最近在做毕设,里面牵涉到版本更新,当有新版本时可以下载新版本,下载完成之后提示安装.那么怎么实现下载完成之后提示安装呢? 直接上代码吧: File mFile = new File(Environme ...
- 安装完Kali linux 之后 需要配置的一些 常用软件 记录
apt 阿里云开源镜像站 deb http://mirrors.aliyun.com/kali sana main non-free contribdeb http://mirrors.aliyun. ...