Min_25筛初级应用:求$[1,n]$内质数个数
2024-09-07 03:59:39
代码
#include <bits/stdc++.h>
#define rin(i,a,b) for(int i=(a);i<=(b);++i)
#define irin(i,a,b) for(int i=(a);i>=(a);--i)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl;
inline LL read(){
LL 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<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
const LL MAXN=10000000005;
const LL MOD=2999999;
LL n,prm[200005],cnt;
LL num[200005],g[200005],tot;
bool vis[200005];
struct Hash_Table{
int head[MOD+5],nxt[200005],siz;
LL to[200005],val[200005];
inline void insert(LL x,LL y){
++siz;
nxt[siz]=head[x%MOD];
to[siz]=y;
val[siz]=x;
head[x%MOD]=siz;
}
inline LL operator [] (LL x){
for(register int i=head[x%MOD];i;i=nxt[i])
if(val[i]==x) return to[i];
}
}mp;
void pre_process(LL n){
rin(i,2,n){
if(!vis[i]) prm[++cnt]=i;
for(register int j=1;j<=cnt&&i*prm[j]<=n;++j){
vis[i*prm[j]]=1;
if(i%prm[j]==0) break;
}
}
}
int main(){
n=read();
pre_process((LL)(sqrt(n)+0.5));
LL nxti;
for(register LL i=1;i<=n;i=nxti){
nxti=n/(n/i)+1;
num[++tot]=n/i;
mp.insert(num[tot],tot);
g[tot]=num[tot]-1;
}
rin(i,1,cnt){
rin(j,1,tot){
if(prm[i]*prm[i]>num[j]) break;
int k=mp[num[j]/prm[i]];
g[j]-=g[k]-(i-1);
}
}
printf("%lld",g[1]);
return 0;
}
最新文章
- C#如何反射出委托的签名,如何使用反射调用委托
- .NET魔法堂:工程构建基石->;MSBuild
- NYOJ题目768移位密码
- Leetcode Sqrt(x)
- javascript图片库
- .cmd文件不小心管理记事本打开的恢复
- ajax读取txt文件
- Pluto - iOS 上一个高性能的排版渲染引擎
- oracle解决导入高版本dmp报错问题:IMP-00058: ORACLE error 12547 encountered
- caffe生成deploy.prototxt文件
- JAVA 和.NET在安全功能的比较
- dll反编译工具总结
- redis清除缓存和连接远程服务器
- 编程调节Win7/Win8系统音量的一种方法
- Qt编写自定义控件12-进度仪表盘
- 公司里面用的iTextSharp(教程)---简介
- 组件化表单解决方案AForm 1.3 发布
- solr5.5.0在CenOS上的安装与配置
- Druid.io系列(八):部署
- 【SPOJ -NSUBSTR】Substrings 【后缀自动机+dp】