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)-- 然后就会发现,慢死了-- 于是我们想到了筛法,比如前几天说到的詹欧筛法. 但是线性的时间和空间成了硬伤-- ...
随机推荐
- IT的工作是这样?
天若有情天亦老,人干IT死得早; 谁知盘中餐,IT老加班; 锦瑟无端五十弦, 我做PM净贴钱; 庄生晓梦迷蝴蝶, 领导客户是大爷; 沧海月明珠有泪, 吃亏受气也开会; 蓝田日暖玉生烟, 可怜人生已跑偏 ...
- zoj 3460 Missile【经典建图&&二分】
Missile Time Limit: 2 Seconds Memory Limit: 65536 KB You control N missile launching towers. Ev ...
- Asp.net Mvc 自定义Session (二)
在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...
- .net如何自定义config配置文件节点
本文转载:http://www.cnblogs.com/lori/archive/2013/04/03/2997617.html 对于小型项目来说,配置信息可以通过appSettings进行配置,而如 ...
- ubuntu压缩解压
ubuntu安装解压rar 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤就可 ...
- J2EE之ServletContext读取资源文件
ServletContext读取资源文件内容的方式有两种: 方法1. public void doGet(HttpServletRequest request, HttpServletResponse ...
- wechat server的配置
一 服务器地址 https://github.com/donal-tong/wechat4server 包括源代码及数据库脚本放在dump文件夹里 1.根据sql脚本创建需要的数据库hcapi,然后命 ...
- Eclipse启动时报需要安装"Java SE 6 Runtime"致无法启动解决方案
今天心血来潮,把MBP升级到了osx mavericks,然后启动了闲置好久的eclipse,启动时居然报错了: 若要打开Eclipse.app,您需要Java SE 6 runtime,您想现在安装 ...
- TCPDUMP Command Examples
tcpdump command is also called as packet analyzer. tcpdump command will work on most flavors of unix ...
- 快速安装VIM开发环境
*Mac上当前用户的.vim目录打包*:附件地址:http://pan.baidu.com/s/1sj5FjZJ 1. 备份好系统的原来的vim配置文件,以备恢复使用: mv ~/.v ...