zoukankan      html  css  js  c++  java
  • luogu P5325 【模板】Min_25筛

    代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    typedef long long LL;
    const int M=1000000007,N=1000009;
    int a[N],cnt,inv6,ind1[N],ind2[N],tot;
    LL sp1[N],sp2[N],Sqr,n,p[N],g2[N],g1[N],w[N];
    
    void prework(int fuck)
    {
    	for (LL i=2;i<=fuck;i++)
    	{
    		if(!a[i])
    			a[i]=i,p[++cnt]=i,sp1[cnt]=(sp1[cnt-1]+i)%M,sp2[cnt]=(sp2[cnt-1]+i*i%M)%M;
    		for (int j=1;j<=cnt;j++)
    		{
    			if(p[j]>a[i]||p[j]>fuck/i)
    				break;
    			a[p[j]*i]=p[j];
    		}
    	}
    }
    
    int ksm(int a,int b)
    {
    	int res=1;
    	while(b)
    	{
    		if(b&1)
    			res=1ll*res*a%M;
    		b>>=1,a=1ll*a*a%M;
    	}
    	return res;
    }
    
    void init()
    {
    	scanf("%lld",&n);
    	inv6=ksm(6,M-2);
    	Sqr=(LL)sqrt(n);
    	prework(Sqr);
    }
    
    LL S(LL x,int y)
    {
    	if(p[y]>=x) return 0;
    	int k=x<=Sqr?ind1[x]:ind2[n/x];
    	LL res=(g2[k]-g1[k]-sp2[y]+sp1[y])%M;
    	for (int i=y+1;i<=cnt&&p[i]*p[i]<=x;i++)
    	{
    		LL pe=p[i];
    		for (int e=1;pe<=x;e++,pe*=p[i])
    		{
    			LL xx=pe%M;
    			res=(res+xx*(xx-1)%M*(S(x/pe,i)+(e!=1))%M)%M;
    		}
    	}
    	return res;
    }
    
    void work()
    {
    	for (LL i=1;i<=n;i++)
    	{
    		LL j=n/(n/i);
    		w[++tot]=n/i;
    		g1[tot]=w[tot]%M;
    		g2[tot]=(g1[tot]*(g1[tot]+1)%M*(g1[tot]+g1[tot]+1)%M*inv6%M-1)%M;
    		g1[tot]=(g1[tot]+1)*g1[tot]/2%M-1;
    		if(n/i<=Sqr) ind1[n/i]=tot;
    		else ind2[n/(n/i)]=tot;
    		i=j;
    	}
    	for (int i=1;i<=cnt;i++)
    		for (int j=1;j<=tot&&p[i]*p[i]<=w[j];j++)
    		{
    			int k=w[j]/p[i]<=Sqr?ind1[w[j]/p[i]]:ind2[n/(w[j]/p[i])];
    			g1[j]=(g1[j]-p[i]*(g1[k]-sp1[i-1])%M)%M;
    			g2[j]=(g2[j]-p[i]*p[i]%M*(g2[k]-sp2[i-1])%M)%M;
    		}
    	printf("%lld
    ",(S(n,0)+1+M)%M);
    }
    
    int main()
    {
    	init();
    	work();
    	return 0;
    }
    
    由于博主比较菜,所以有很多东西待学习,大部分文章会持续更新,另外如果有出错或者不周之处,欢迎大家在评论中指出!
  • 相关阅读:
    Java程序的设计环境配置
    Java总结之Java简介
    Python Panda
    Python with MYSQL
    python string与list互转
    Filter/replace
    django-pagination分页
    python 链接MS SQL
    Django 文件下载功能
    Sharepoint list webpart
  • 原文地址:https://www.cnblogs.com/With-penguin/p/13251966.html
Copyright © 2011-2022 走看看