hdu6607 min25筛+杜教筛+伯努利数求k次方前缀和
Posted acjiumeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu6607 min25筛+杜教筛+伯努利数求k次方前缀和相关的知识,希望对你有一定的参考价值。
推导过程类似https://www.cnblogs.com/acjiumeng/p/9742073.html
前面部分min25筛,后面部分杜教筛,预处理min25筛需要伯努利数
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
//#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b)return b?gcd(b,a%b):a;
inline void sub(ll &a,ll b)a-=b;if(a<0)a+=mod;
inline void add(ll &a,ll b)a+=b;if(a>=mod)a-=mod;
template<typename T>inline T const& MAX(T const &a,T const &b)return a>b?a:b;
template<typename T>inline T const& MIN(T const &a,T const &b)return a<b?a:b;
inline ll mul(ll a,ll b,ll c)return (a*b-(ll)((ld)a*b/c)*c+c)%c;
inline ll qp(ll a,ll b)ll ans=1;while(b)if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;return ans;
inline ll qp(ll a,ll b,ll c)ll ans=1;while(b)if(b&1)ans=mul(ans,a,c);a=mul(a,a,c),b>>=1;return ans;
using namespace std;
//using namespace __gnu_pbds;
const ld pi=acos(-1);
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=10000000+10,inf=0x3f3f3f3f;
bool mark[maxn];
int prime[maxn],cnt,K,phi[maxn],cnt1,f[maxn];
ll g[N],id[2][N],val[N],sum[N],up,n;
ll inv2=qp(2,mod-2),inv6=qp(6,mod-2),inv[111],c[111][111],b[111];
map<ll,ll>phii;
ll getb(int n)
if(b[n]!=-1)return b[n];
if(n==0)return b[0]=1;
ll ans=0;
for(int i=0;i<n;i++)
add(ans,c[n+1][i]*getb(i)%mod);
ans=-ans*inv[n+1]%mod;
ans=(ans%mod+mod)%mod;
return b[n]=ans;
ll get(ll n,ll k)
ll ans=0;
for(int i=1;i<=k+1;i++)
add(ans,c[k+1][i]*b[k+1-i]%mod*qp((n+1)%mod,i)%mod);
return ans*inv[k+1]%mod;
void pre()
phi[1]=1;
for(int i=2;i<maxn;i++)
if(!mark[i])prime[++cnt1]=i,phi[i]=i-1;
for(int j=1;j<=cnt1&&i*prime[j]<maxn;j++)
mark[i*prime[j]]=1;
if(i%prime[j]==0)
phi[i*prime[j]]=phi[i]*prime[j];
break;
phi[i*prime[j]]=phi[i]*(prime[j]-1);
for(int i=1;i<maxn;i++)
f[i]=1ll*i*i%mod*phi[i]%mod;
f[i]+=f[i-1];
if(f[i]>=mod)f[i]-=mod;
inv[1]=1;
for(ll i=2;i<111;i++)
inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
for(int i=0;i<111;i++)
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
memset(b,-1,sizeof b);
getb(110);
void init()
up=sqrt(n);
for(int i=1;prime[i]<=up;i++)sum[i]=(sum[i-1]+qp(prime[i],K+1))%mod,cnt=i;
int m=0;
for(ll i=1,j;i<=n;i=j+1)
j=n/(n/i);
val[++m]=n/i;
g[m]=get(n/i,K+1);//insert val
sub(g[m],1ll);
if(n/i<=up)id[0][n/i]=m;
else id[1][i]=m;
for(int j=1;j<=cnt;j++)for(int i=1;i<=m&&1ll*prime[j]*prime[j]<=val[i];i++)
ll te=val[i]/prime[j];
int k=(te<=up)?id[0][te]:id[1][n/te];
sub(g[i],1ll*(sum[j]-sum[j-1]+mod)*(g[k]-sum[j-1]+mod)%mod);
ll getf(ll n)
if(n<maxn)return f[n];
if(phii.find(n)!=phii.end())return phii[n];
ll ans=n%mod*((n+1)%mod)%mod*inv2%mod;ans=ans*ans%mod;
for(ll i=2,j;i<=n;i=j+1)
j=n/(n/i);
ll tj=j%mod,ti=i%mod;
ll te=tj*(tj+1)%mod*(2ll*tj+1)%mod*inv6%mod-(ti-1)*ti%mod*(2ll*ti-1)%mod*inv6%mod;
te=(te%mod+mod)%mod;
sub(ans,te*getf(n/i)%mod);
return phii[n]=ans;
int main()
pre();
int T;scanf("%d",&T);
while(T--)
scanf("%lld%d",&n,&K);
init();
ll ans=0;
for(ll i=1,j;i<=n;i=j+1)
j=n/(n/i);
int k1=(j<=up)?id[0][j]:id[1][n/j];
int k2=(i-1<=up)?id[0][i-1]:id[1][n/(i-1)];
// printf("%lld %lld\\n",(g[k1]-g[k2]+mod)%mod,getf(n/i));
add(ans,getf(n/i)*(g[k1]-g[k2]+mod)%mod);
printf("%lld\\n",ans);
return 0;
/********************
********************/
以上是关于hdu6607 min25筛+杜教筛+伯努利数求k次方前缀和的主要内容,如果未能解决你的问题,请参考以下文章