质数的判定 Miller_Rabin
2024-09-04 06:37:42
----------- 10^18
#include <bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
typedef long long ll;
inline int read() {
int f=1,sum=0;
char x=getchar();
for(;(x<'0'||x>'9');x=getchar()) if(x=='-') f=-1;
for(;x>='0'&&x<='9';x=getchar()) sum=sum*10+x-'0';
return f*sum;
} int x_[10]={3,5,7,11,13,17,19,23}; inline ll mul(ll x,ll y,ll mod) {
ll tmp=x*y-((ll)((long double)x/mod*y+0.5))*mod;
return tmp<0?tmp+mod:tmp;
} inline ll qmul(ll x,ll ci,ll mod) {
ll sum=1;
for(;ci;ci>>=1,x=mul(x,x,mod))
if(ci&1) sum=mul(sum,x,mod);
return sum;
} inline bool Miller_Rabin(ll n) {
if(n==1) return 0;
if(n==2) return 1;
if(!(n&1)) return 0;
ll t=n-1;
int now=0;
while (!(t&1)) t>>=1,++now; for(int i = 0; i <= 7; i++){
if(x_[i]==n) return 1;
ll x=qmul(x_[i],t,n),y=x;
for(int j = 1; j <= now; j++) {
x=mul(x,x,n);
if(x==1&&!(y==1||y==n-1)) return 0;
y=x;
}
if(x!=1) return 0;
}
return 1;
} int main () {
//freopen("a.in","r",stdin);
ll x;
while (scanf("%lld",&x)==1) {
if(Miller_Rabin(x)) puts("Y");
else puts("N");
}
}
最新文章
- 错误";ORA-04091: table is mutating, trigger/function may not see it";的原因以及解决办法
- Android_SQLite版本升级,降级 管理
- atoi()函数
- jquery查找父元素、子元素(个人经验总结)
- DisableExplicitGC和Direct ByteBuffer
- css:中文词不断开,整体换行
- linux的学习系列 4---文件权限和访问模式
- MUI开发记录——我的考勤
- Java SE 8 流库(二)
- 2.python中self详解(程序适用于python3版本)
- NOIP 2018 大翻车记
- 【NLP】Conditional Language Models
- Python中的Numpy入门教程
- lightswitch binding custom control
- individual reading task ---12061183 叶露婷
- array_multisort
- adb常用命令及详解
- AngularJs 1.x和AngularJs2的区别
- Python: 字典列表: 通过某个字段将记录分组
- 求Read Depth
热门文章
- H5 存储数据sessionStorage
- 2018-2-13-WPF-拖动时出现-Invalid-FORMATETC-structure
- UVa 10603 Fill [暴力枚举、路径搜索]
- P1066 汪老师玩卡片
- H3C根桥的选举
- AutoHotKey 用打码的快捷键
- Linux 内核kobject 层次, kset, 和子系统
- es6笔记 day4---模块化
- LightOJ - 1265 Island of Survival (概率dp)
- How to fix nuget Unrecognized license type MIT when pack