博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速判定是否为素数
阅读量:4537 次
发布时间:2019-06-08

本文共 2210 字,大约阅读时间需要 7 分钟。

快速判断是否为素数

#include
#include
#include
#include
typedef unsigned long long ll;using namespace std;ll add_mod(ll a,ll b,ll mod){ ll ans=0; while(b){ if(b&1) ans=(ans+a)%mod; a=a*2%mod; b>>=1; } return ans;}ll pow_mod(ll a,ll n,ll mod){ if(n>1){ ll tmp=pow_mod(a,n>>1,mod)%mod; tmp=add_mod(tmp,tmp,mod); if(n&1) tmp=add_mod(tmp,a,mod); return tmp; } return a;}bool Miller_Rabbin(ll n,ll a){ ll d=n-1,s=0,i; while(!(d&1)){ d>>=1; s++; } ll t=pow_mod(a,d,n); if(t==1 || t==-1) return 1; for(i=0;i
tab[i] && !Miller_Rabbin(n,tab[i])) return 0; } return 1;}int main(){///快速判断是否为素数 ll n; scanf("%lld",&n); if(is_prime(n)) printf("yes\n"); else printf("no\n"); return 0;}

颠倒的素数,正序和逆序都是素数

ac:

#include
#include
#include
#include
typedef unsigned long long ll;using namespace std;ll add_mod(ll a,ll b,ll mod){ ll ans=0; while(b){ if(b&1) ans=(ans+a)%mod; a=a*2%mod; b>>=1; } return ans;}ll pow_mod(ll a,ll n,ll mod){ if(n>1){ ll tmp=pow_mod(a,n>>1,mod)%mod; tmp=add_mod(tmp,tmp,mod); if(n&1) tmp=add_mod(tmp,a,mod); return tmp; } return a;}bool Miller_Rabbin(ll n,ll a){ ll d=n-1,s=0,i; while(!(d&1)){ d>>=1; s++; } ll t=pow_mod(a,d,n); if(t==1 || t==-1) return 1; for(i=0;i
tab[i] && !Miller_Rabbin(n,tab[i])) return 0; } return 1;}int main(){ ll n; while(cin>>n) { ll kk=n; ll temp=0; int flag=1; while(n) { ll t=n%10; if(t==3||t==4||t==7) { flag=0; break; } if(t==6) t=9; else if(t==9) t=6; temp=temp*10+t; n/=10; } if(!flag) { cout<<"no"<

 

 

 

转载于:https://www.cnblogs.com/wangtao971115/p/10358385.html

你可能感兴趣的文章
自动化测试框架selenium+java+TestNG——配置篇
查看>>
测量标准体重
查看>>
(转)关于Android中为什么主线程不会因为Looper.loop()里的死循环卡死?引发的思考,事实可能不是一个 epoll 那么 简单。...
查看>>
SQL*Plus 系统变量之32 - NEWP[AGE]
查看>>
Spring配置文件总结
查看>>
4.三角形面积
查看>>
基础-事务
查看>>
MAC下安装与配置MySQL [转]
查看>>
ERROR: ld.so: object '/usr/lib64/libtcmalloc.so.4' from LD_PRELOAD cannot be preloaded: ignored
查看>>
爬虫入门【10】Pyspider框架简介及安装说明
查看>>
android面试(4)---文件存储
查看>>
(转载) 标准C中的字符串操作函数
查看>>
如何提高android串口kernel log等级
查看>>
Docker快速配置指南
查看>>
Python基础---OS模块 (二)
查看>>
【JS点滴】substring和substr以及slice和splice的用法和区别。
查看>>
awk多模式匹配
查看>>
线段树
查看>>
a span等行内元素加margin属性后无效果解决方案
查看>>
傻瓜式硬盘重装win7系统图文加视频教程
查看>>