素数判定的常用 【在线 or 离线】
来源:互联网 发布:linux安装内核开发包 编辑:程序博客网 时间:2024/06/10 12:08
常用的是 素筛打表
代码
离线算法 素筛
#include <bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fconst int MAXN = 1e7; // *** const int MAXM = 1e6+100;const double PI = acos(-1.0);const double eps = 1e-8;inline int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f;}/***********************************************/bool su[MAXN]={1,1,0};void dabiao(){ //埃及筛 for(int i=2;i*i<n;i++) if(!su[i]) for(int j=i*i;j<n;j++) // 从i*i开始就行 su[j]=1;}int main(){}
代码
费马定理 在线算法
代码
#include <bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fconst int maxn = 100000;const int MAXM = 1e6+100;const double PI = acos(-1.0);const double eps = 1e-8;inline int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f;}/*****************************************/int pri[maxn],f[maxn],top;bool vis[maxn];int l,r,mid,n;int G(int n,int m){ if (!m) return n; if (m==1) return n-n/2; if (n<=maxn) { if (f[n]<=m) return 1; if (f[(int)sqrt(n)]<=m) return f[n]-m+1; } return G(n,m-1)-G(n/pri[m],m-1);}bool pan(int x){ int y=sqrt(x); return f[y]+G(x,f[y])-1>=n;}int main(){ for (int i=2;i<=maxn;++i) { if (!vis[i]) pri[++top]=i; for (int j=1;j<=top&&pri[j]*i<=maxn;++j) { vis[i*pri[j]]=1; if (i%pri[j]==0) break; } } for (int i=2;i<=maxn;++i) f[i]=f[i-1]+(!vis[i]); int k = 0; /**************************************/ while(~scanf("%d",&n),n) { // 前第n个素数是什么 l=1,r=300000000; while(l<r) { mid=(l+r)/2; if (pan(mid)) r=mid; else l=mid+1; } printf("Case %d: %d\n",++k,l); }}
阅读全文
0 0
- 素数判定的常用 【在线 or 离线】
- 素数的判定
- 素数的判定
- 素数的判定
- 素数的判定
- 素数的判定二
- 素数的Miller_Rabbin判定算法
- hdu 2012 素数的判定
- 好的 2012 素数判定
- HDU 2012 素数的判定
- 素数的判定(常规)
- POJ2262 素数判定,万能的素数筛选
- FZU 1649 Prime number or not (大素数判定)
- SPOJ D-query 区间不同数的个数 [在线主席树 or 离线树状数组]
- 素数判定
- 素数判定
- 素数判定
- 素数判定
- 350. Intersection of Two Arrays II
- wxPython之解决闪烁
- codeforces #352(div1)-Robin Hood
- [绍棠] SourceTree过期,需要注册导入 SourceTree License 许可证
- 【初看TCP】基本知识与三次握手--使用echo服务抓包
- 素数判定的常用 【在线 or 离线】
- java 中常用的类
- 编写java程序151条建议读书笔记(20)
- Mysql-python环境配置(centos)
- (六)北斗定位和伽利略定位
- 简单工厂模式
- 树莓派、 Arduino 、传统单片机开发板该如何选择?
- PHP Interpreter is not configured(浏览器502 bad gateway)
- Send a Table UVA