DIVCNT2 - Counting Divisors (square)
DIVCNT3 - Counting Divisors (cube)
杜教筛
(其实不算是杜教筛,类似杜教筛的复杂度分析而已)
你要大力推式子:
把约数个数代换了
把2^质因子个数 代换了
构造出卷积,然后大于n^(2/3)还要搞出约数个数的式子和无完全平方数的个数的容斥。。。
。。。。
然后恭喜你,spoj上过不去。。。
bzoj能过:
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define ul unsigned long long
#define numb (ch^'0')
using namespace std;
typedef long long ll;
il void rd(int &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
namespace Miracle{
const int N=;
ll n;
ul miu[N],sig[N],sq[N];
bool vis[N];
int divcnt[N],pri[N+],tot;
ll a[];
ll up;
void sieve(ll n){
miu[]=;sig[]=;
for(reg i=;i<=n;++i){
if(!vis[i]){
pri[++tot]=i;
miu[i]=-;
sig[i]=;
divcnt[i]=;
}
for(reg j=;j<=tot;++j){
if(pri[j]*i>n) break;
vis[pri[j]*i]=;
if(i%pri[j]==){
divcnt[i*pri[j]]=divcnt[i]+;
miu[i*pri[j]]=;
sig[i*pri[j]]=sig[i]/(divcnt[i]+)*(divcnt[i]+);
break;
}
divcnt[i*pri[j]]=;
miu[i*pri[j]]=-miu[i];
sig[i*pri[j]]=sig[i]*sig[pri[j]];
}
}
sq[]=;
for(reg i=;i<=n;++i) {
sq[i]=miu[i]*miu[i];
sq[i]+=sq[i-]; sig[i]+=sig[i-];
}
}
ul M(ll n){
if(n<=up) return sq[n];
ul ret=;
for(reg i=;(ll)i*i<=n;++i){
ret=ret+miu[i]*(n/(i*i));
}
//cout<<" M "<<ret<<endl;
return ret; }
ul S(ll n){
if(n<=up) return sig[n];
ul ret=;
for(ll i=,x=;i<=n;i=x+){
x=(n/(n/i));
ret=ret+(x-i+)*(n/i);
}
// cout<<" S "<<ret<<endl;
return ret; }
ul solve(ll n){
ul ret=;
for(ll i=,x=;i<=n;i=x+){
x=(n/(n/i));
ret=ret+(M(x)-M(i-))*S(n/i);
// cout<<"["<<i<<","<<x<<"] : "<<ret<<endl;
}
return ret;
}
int main(){
int t;
rd(t);
ll mx=;
for(reg i=;i<=t;++i) scanf("%lld",&a[i]),mx=max(mx,a[i]);
if(mx<=N-){
up=mx;
sieve(up);
}else{
up=N-;
sieve(up);
}
for(reg i=;i<=t;++i){
printf("%llu\n",solve(a[i]));
}
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
Date: 2019/3/6 21:18:05
*/
Min_25筛
sigma(i^3)是积性函数!
没了。
#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define ul unsigned long long
#define mk(a,b) make_pair(a,b)
#define int long long
#define numb (ch^'0')
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
template<class T>il void ot(T x){x/?ot(x/):putchar(x%+'');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) printf("%lld ",a[i]);putchar('\n');} namespace Miracle{
const int N=2e6+;
const int U=2e6+;
const int K=;
int vis[N],pri[N],tot;
int cnt;
ll n;
il void sieve(int n){
for(reg i=;i<=n;++i){
if(!vis[i]){
pri[++tot]=i;
}
for(reg j=;j<=tot;++j){
if(i*pri[j]>n) break;
vis[i*pri[j]]=;
if(i%pri[j]==) break;
}
}
}
ul f[N];
ll id1[N+],id2[N+];
ll val[*N],num;
il ul G(ll M,int j){
// cout<<" G "<<M<<" "<<j<<endl;
if(M<=||M<pri[j]) return ;
int id=(M<=U)?id1[M]:id2[n/M];
ul ret=f[id]-(ul)(j-)*(K+);
for(reg t=j;t<=cnt&&(ll)pri[t]*pri[t]<=M;++t){
ul now=pri[t];
// cout<<" mindiv "<<t<<" : "<<now<<endl;
for(reg e=;now*pri[t]<=M;++e,now*=pri[t]){
// cout<<" ee "<<e<<endl;
ret=ret+(ul)(K*e+)*G(M/now,t+)+(ul)(K*e+K+);
}
}
// cout<<" ret "<<M<<" "<<j<<" : "<<ret<<endl;
return ret;
}
void clear(){
num=;cnt=;
}
int main(){
int T;
rd(T);
sieve(N-);
while(T--){
rd(n);
if(n==){
puts("");
continue;
}
int ban=sqrt(n);
cnt=lower_bound(pri+,pri+tot+,ban)-pri;
// cout<<" cnt "<<cnt<<endl;
for(ll i=,x=;i<=n;i=x+){
x=(n/(n/i));
val[++num]=n/i;
if(n/i<=U) id1[n/i]=num;
else id2[x]=num;
}
// cout<<" num "<<num<<endl;
for(reg i=;i<=num;++i){
f[i]=(ul)(K+)*(val[i]-);
}
for(reg j=;j<=cnt;++j){
// cout<<" j ------------- "<<j<<endl;
for(reg i=;i<=num;++i){
if((ll)pri[j]*pri[j]>val[i]) break;
int fr=val[i]/pri[j]<=U?id1[val[i]/pri[j]]:id2[n/(val[i]/pri[j])];
//cout<<" fr "<<fr<<endl;
f[i]=f[i]-(f[fr]-(ul)(K+)*(j-));
}
}
// for(reg i=1;i<=num;++i){
// cout<<i<<" : "<<f[i]<<endl;
// }
printf("%llu\n",(ul)G(n,)+);
clear();
}
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
Date: 2019/3/9 16:39:18
*/
测试发现
Min_25在n<=1e12时候基本都是比杜教筛快。
在N<=1e9时候更是秒出
但是数据组数多了以后,杜教筛记忆化的优势就体现明显了。