BZOJ 1101 [POI2007]Zap ——Dirichlet积
【题目分析】
Dirichlet积+莫比乌斯函数。

对于莫比乌斯函数直接筛出处理前缀和。
对于后面向下取整的部分,可以分成sqrt(n)+sqrt(m)部分分别计算
学习了一下线性筛法。
积性函数可以在O(n)的时间内算出。
【代码】
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 50005
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("wa.txt","w",stdout);
// freopen("ac.txt","w",stdout);
#endif
}
int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int pmu[maxn],mu[maxn],vis[maxn],pri[maxn],top=0;
void init()
{
mu[1]=1;vis[1]=1;
F(i,2,maxn-1)
{
if (!vis[i]) {vis[i]=1;pri[++top]=i;mu[i]=-1;}
for (int j=1;j<=top&&i*pri[j]<maxn;++j)
{
vis[i*pri[j]]=1;
if (i%pri[j]==0) {break;}
else mu[pri[j]*i]=-mu[i];
}
}
F(i,1,maxn-1) pmu[i]=pmu[i-1]+mu[i];
}
int t;
int main()
{
init();
Finout();
t=Getint();
while (t--)
{
int ans=0;
int a=Getint(),b=Getint(),d=Getint();
a/=d; b/=d;
int la,lb,nowa,nowb,l,r=a;
while (r)
{
// cout<<"r is "<<r<<endl;
nowa=a/r;nowb=b/r;
la=a/(nowa+1)+1;lb=b/(nowb+1)+1;
// cout<<"la is "<<la<<" lb is "<<lb<<endl;
l=max(la,lb);
ans+=nowa*nowb*(pmu[r]-pmu[l-1]);
r=l-1;
}
printf("%d\n",ans);
}
}
BZOJ 1101 [POI2007]Zap ——Dirichlet积的更多相关文章
- BZOJ 1101: [POI2007]Zap
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2262 Solved: 895[Submit][Status] ...
- BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )
求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...
- BZOJ 1101 [POI2007]Zap(莫比乌斯反演)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1101 [题目大意] 求[1,n][1,m]内gcd=k的情况 [题解] 考虑求[1,n ...
- bzoj 1101 [POI2007]Zap——反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 #include<cstdio> #include<cstring& ...
- BZOJ 1101 [POI2007]Zap | 第一道莫比乌斯反(繁)演(衍)
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...
- 1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...
- 【BZOJ】1101: [POI2007]Zap(莫比乌斯+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=1101 无限膜拜数论和分块orz 首先莫比乌斯函数的一些性质可以看<初等数论>或<具 ...
- 【BZOJ】1101 [POI2007]Zap(莫比乌斯反演)
题目 传送门:QWQ 分析 莫比乌斯反演. 还不是很熟练qwq 代码 //bzoj1101 //给出a,b,d,询问有多少对二元组(x,y)满足gcd(x,y)=d.x<=a,y<=b # ...
- 1101: [POI2007]Zap
Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a ,y<=b,并且gcd(x,y)=d.作为FGD的同 ...
随机推荐
- Android开发 R cannot be resolved to a variable问题的分析
R文件是系统自动生成的,如果没出现的话,你的XML文件是不是有错误?是否之前修改过res文件夹下面.xml文件 R文件没有生成的情况有几种: 1.项目没有自动编译:这种时候只需要简单的编译一下工程就会 ...
- 学习笔记——适配器模式Adapter
适配器模式适用于将不一致的接口转换为一致的接口. 比如,去香港玩儿,带上了自己的笔记本电脑,结果晚上插电时就抓瞎了,电源插孔与插座不一致.WTF…… 插座是酒店装好的,不可能拆了换一个,电源是自己的, ...
- 学习笔记——工厂模式Factory
Product是我们所需要获得的对象,一般的使用中,我们直接通过new获得新对象. 当需要大量获得对象时,每一次都new会很麻烦. <真菌世界>游戏中,一个星球上有很多树,一棵树会不断生成 ...
- 转:selenium webdriver 执行javascript代码
在用selenium webdriver 编写web页面的自动化测试代码时,可能需要执行一些javascript代码,selenium本身就支持执行js,我们在代码中import org.openqa ...
- HDU 3346 Lucky Number
水题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...
- 为什么有时候必须添加sys.setdefaultencoding('utf-8')
今天在尝试Python的CGI模块时遇到中文字符不能正确显示的问题,很郁闷.在网上仔细找了找,终于解决了这个问题,现在将解决方法陈述如下,以防下次失误. 页面源代码如下 #-*- coding: ut ...
- Web应用的组件化(二)——管控平台 #7
Web应用的组件化(二) 管控平台 在上一篇中我们提到了组件化的大致思路,这一篇主要讲述在这么做之后,我们需要哪些外围手段去管控整个开发过程.从各种角度看,面对较大规模前端开发团队,都有必要建立这么一 ...
- mysql deadlock
http://database.51cto.com/art/201108/286325.htm 这篇文章说的很清楚,记下来. 原因分析: 当“update tab_test set state=106 ...
- java.lang.RuntimeException: java.lang.NoSuchMethodException:
[java] 15/12/19 14:09:46 INFO mapred.JobClient: Task Id : attempt_201512182036_0017_m_000000_0, Stat ...
- DDS视图&Button控件
<Button android:id="@+id/btn1" android:layout_width="wrap_content" //包裹文字 ...