【BZOJ】【P2301】【HAOI2011】【Problem b】【题解】【莫比乌斯反演】
2014-10-20 17:30
253 查看
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2301
事实证明刷完一个zap就可以水很多题了……
Code:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
typedef long long LL;
const int maxn=50005;
int T,n,m;
bool p[maxn];
int u[maxn],prime[maxn];
int getint(){
int res=0;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))res=res*10+c-'0',c=getchar();
return res;
}
void init(){
u[1]=1;
for(int i=2;i<maxn;i++){
if(!p[i]){
prime[++prime[0]]=i;u[i]=-1;
}for(int j=1;i*prime[j]<maxn&&j<=prime[0];j++){
int K=i*prime[j];
p[K]=1;
if(i%prime[j]==0){
u[K]=0;break;
}else u[K]=-u[i];
}
}for(int i=2;i<maxn;i++)u[i]+=u[i-1];
}
LL calc(int n,int m,int k){
n/=k;m/=k;
if(n>m)swap(n,m);
LL ans=0;
for(int i=1,last;i<=n;i=last+1){
last=min(n/(n/i),m/(m/i));
ans+=(LL)(u[last]-u[i-1])*(n/i)*(m/i);
}return ans;
}
int main(){
T=getint();
init();
while(T--){
int a=getint(),b=getint(),c=getint(),d=getint(),k=getint();
LL ans=calc(b,d,k)-calc(a-1,d,k)-calc(b,c-1,k)+calc(a-1,c-1,k);
printf("%lld\n",ans);
}
return 0;
}
事实证明刷完一个zap就可以水很多题了……
Code:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
typedef long long LL;
const int maxn=50005;
int T,n,m;
bool p[maxn];
int u[maxn],prime[maxn];
int getint(){
int res=0;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))res=res*10+c-'0',c=getchar();
return res;
}
void init(){
u[1]=1;
for(int i=2;i<maxn;i++){
if(!p[i]){
prime[++prime[0]]=i;u[i]=-1;
}for(int j=1;i*prime[j]<maxn&&j<=prime[0];j++){
int K=i*prime[j];
p[K]=1;
if(i%prime[j]==0){
u[K]=0;break;
}else u[K]=-u[i];
}
}for(int i=2;i<maxn;i++)u[i]+=u[i-1];
}
LL calc(int n,int m,int k){
n/=k;m/=k;
if(n>m)swap(n,m);
LL ans=0;
for(int i=1,last;i<=n;i=last+1){
last=min(n/(n/i),m/(m/i));
ans+=(LL)(u[last]-u[i-1])*(n/i)*(m/i);
}return ans;
}
int main(){
T=getint();
init();
while(T--){
int a=getint(),b=getint(),c=getint(),d=getint(),k=getint();
LL ans=calc(b,d,k)-calc(a-1,d,k)-calc(b,c-1,k)+calc(a-1,c-1,k);
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- BZOJ 2301 [HAOI2011]Problem b 莫比乌斯反演
- Bzoj2301 [HAOI2011]Problem b 莫比乌斯反演
- BZOJ 2301: [HAOI2011]Problem b - 莫比乌斯反演
- 【BZOJ2301】【HAOI2011】Problem b [莫比乌斯反演]
- bzoj 2301: [HAOI2011]Problem b 莫比乌斯反演
- BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演
- bzoj 2301: [HAOI2011]Problem b 【莫比乌斯反演】
- 【BZOJ2301】【HAOI2011】Problem b 莫比乌斯反演+分块+前缀和
- BZOJ 2301: [HAOI2011]Problem b【莫比乌斯反演
- 【BZOJ】【P2440】【中山市选2011】【完全平方数】【题解】【莫比乌斯反演】
- BZOJ 2301 [HAOI2011]Problem b (容斥+莫比乌斯反演+分块优化 详解)
- [BZOJ 2301] HAOI 2011 Problem b · 莫比乌斯
- 【BZOJ】【P2298】【HAOI2011】【problem a】【题解】【dp+二分】
- bzoj2301: [HAOI2011]Problem b 莫比乌斯反演
- [BZOJ2301]HAOI2011 ProblemB|莫比乌斯
- BZOJ 2301([HAOI2011]Problem b-mobius反演)
- Bzoj-2301 [HAOI2011]Problem b 容斥原理,Mobius反演,分块
- bzoj 2301: [HAOI2011]Problem b (反演)
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
- [莫比乌斯反演] BZOJ 2301 [HAOI2011]Problem b