SPOJDIVCNT2: Counting Divisors(莫比乌斯反演)
http://acm.tzc.edu.cn/acmhome/vProblemList.do?method=problemdetail&oj=SPOJ&pid=DIVCNT2
给出n求
其中
是除数函数,0代表0次方.





#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define ll long long
ll n,sig[],N,a[];
int p[];
bool mark[];
char mul[];
int smu[],L;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
ll Read(){
ll t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void Init(int n){
sig[]=;smu[]=;mul[]=;
for (int i=;i<=n;i++){
if (!mark[i]){
p[++p[]]=i;
mul[i]=-;
smu[i]=;
sig[i]=;
}
for (int j=;j<=p[]&&p[j]*i<=n;j++){
mark[p[j]*i]=;
if (i%p[j]==){
sig[p[j]*i]=sig[i]/(smu[i]+)*(smu[i]+);
smu[p[j]*i]=smu[i]+;
mul[p[j]*i]=;
break;
}
sig[p[j]*i]=sig[i]*;
smu[p[j]*i]=;
mul[p[j]*i]=-mul[i];
}
}
for (int i=;i<=n;i++) sig[i]+=sig[i-],smu[i]=smu[i-]+std::abs((int)mul[i]);
} ll R(ll x){
if (x<=L) return sig[x];
ll res=;
for (register ll i=,j;i<=x;i=j+){
j=(x/(x/i));
res+=(j-i+)*(x/i);
}
return res;
}
ll Smu(ll x){
if (x<=L) return smu[x];
ll res=;
for (register ll i=;i*i<=x;i++)
if (mul[i]) res+=mul[i]*(x/(i*i));
return res;
}
void solve(ll n){
int m=sqrt(n);
ll ans=;
ll pre=smu[m],tt;
for (int i=;i<=m;i++) if (mul[i]) ans+=R(n/i);
for (ll i=m+,j;i<=n;i=j+){
j=(n/(n/i));
tt=Smu(j);
ans+=(tt-pre)*(R(n/i));
pre=tt;
}
printf("%lld\n",ans);
}
int main(){
int T=read();
ll mx=;
N=;
for (int i=;i<=T;i++)
a[i]=Read(),mx=std::max(mx,a[i]);
if (mx<=) L=;else L=pow(N,2.0/3.0);
Init(L);
for (int i=;i<=T;i++){
solve(a[i]);
}
}
SPOJDIVCNT2: Counting Divisors(莫比乌斯反演)的更多相关文章
- 【Project Euler】530 GCD of Divisors 莫比乌斯反演
[题目]GCD of Divisors [题意]给定f(n)=Σd|n gcd(d,n/d)的前缀和F(n),n=10^15. [算法]莫比乌斯反演 [题解]参考:任之洲数论函数.pdf 这个范围显然 ...
- [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)
题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0(n) be the number of positive diviso ...
- hdu1695 GCD(莫比乌斯反演)
题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...
- BZOJ 2154: Crash的数字表格 [莫比乌斯反演]
2154: Crash的数字表格 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2924 Solved: 1091[Submit][Status][ ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- 莫比乌斯函数筛法 & 莫比乌斯反演
模板: int p[MAXN],pcnt=0,mu[MAXN]; bool notp[MAXN]; void shai(int n){ mu[1]=1; for(int i=2;i<=n;++i ...
- 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2371 Solved: 1143[Submit][Sta ...
- POI2007_zap 莫比乌斯反演
题意:http://hzwer.com/4205.html 同hdu1695 #include <iostream> #include <cstring> #include & ...
随机推荐
- ArcGIS API for Silverlight学习笔记
ArcGIS API for Silverlight学习笔记(一):为什么要用Silverlight API(转) 你用上3G手机了吗?你可能会说,我就是喜欢用nokia1100,ABCDEFG跟我都 ...
- JAVA字符串转日期或日期转字符串
文章中,用的API是SimpleDateFormat,它是属于java.text.SimpleDateFormat,所以请记得import进 来! 用法: SimpleDateFormat sdf = ...
- 运行jar应用程序引用其他jar包的四种方法(转)
Runnable JAR RunnableJAR(1)在Eclipse中操作 上面的截图中eclipse的版本是: 方案二:安装Eclipse打包插件Fat Jar 方案一对于含有较多第三方jar文 ...
- NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 和american_america.AL32UTF8
oadb01:/home/oracle> echo $NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 oadb01:/home/oracle> sql ...
- 怎样合并排序数组(How to merge 2 sorted arrays?)
Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...
- Hive 2、Hive 的安装配置(本地MySql模式)
一.前提条件 安装了Zookeeper.Hadoop HDFS HA 安装方法: http://www.cnblogs.com/raphael5200/p/5154325.html 二.安装Mysq ...
- C语言漫谈(二) 图像显示 Windows和Linux
关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库. 了解最原始的方式,对 ...
- 免费自学Cocos2d-x3.0final2014原创视频教程(56集)(适用于Cocos2d-x3.1 Cocos2d-x3.2版本号全)
Cocos2d-x3.0final(适用于Cocos2d-x3.1 Cocos2d-x3.2版本号全) 视频播放地址:http://yun.itxdl.cn/course/62 视频下载地址:http ...
- Windows - 程序猿应该熟记的CMD常用命令
notepad 计事本 mspaint 画图 iisreset 重启IIS appwiz.cpl 控制面板 inetmgr IIS管理器 eventvwr 事件查看器 mstsc 远程桌面 net s ...
- cookie丢失、登陆自动退出问题解决
cookie保存在客户端或者内存中,不易丢失.但是在某些情况下会被忽略.在项目过程中遇到过跨域丢失的情况.在VS里面运行的程序,产生的cookie默认是没有domain值的,但是给它设定domain值 ...