1,[POI2007]ZAP-Queries

~~~题面~~~
题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$
    $$[gcd(i, j) == d] = [gcd(\lfloor{\frac{i}{d}}\rfloor,\lfloor{\frac{j}{d}}\rfloor) == 1]$$
    所以原式 $$\Rightarrow \quad \sum_{i = 1}^{\lfloor{\frac{n}{d}}\rfloor}\sum_{j = 1}^{\lfloor{\frac{m}{d}}\rfloor}[gcd(i, j)==1]$$
    $$\Rightarrow \quad \sum_{i = 1}^{n}\sum_{j = 1}^{m}\sum_{d|gcd(i, j)}\mu(d)$$
    因为$\mu(d)$会被统计到当且仅当$d | i \quad and \quad d | j$,即$d | gcd(i, j)$
    那么考虑将满足条件的i和j两两搭配,组成的方案数就是$\mu(d)$会被统计到的次数,
    也就是$\mu(d)$会被统计到$\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$次
    $$\Rightarrow \quad ans=\sum_{i=1}^{min(n,m)}{\mu(d)\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor}$$
    然后观察到$\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$中有很多小段$\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$乘积是固定的,也就是$\lfloor{\frac{n}{d}}\rfloor$和$\lfloor{\frac{m}{d}}\rfloor$同时为一个固定的值,因此我们可以用整数分块优化

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 55000
int t, n, m, d;
int prime[AC], mu[AC], sum[AC], tot;
bool z[AC]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void getprime()
{
int now;
mu[] = ;
for(R i = ; i <= ; i++)
{
if(!z[i]) prime[++tot] = i, mu[i] = -;
for(R j = ; j <= tot; j++)
{
now = prime[j];
if(now * i > ) break;
// printf("!!!%d %d\n", now, i);
z[now * i] = true;
if(!(i % now)) break;
mu[i * now] = -mu[i];
}
}
for(R i = ; i <= ; i++)
sum[i] = mu[i] + sum[i - ];
} int ans; void work()
{
int pos;
t = read();
for(R i = ; i <= t; i++)
{
n = read(), m = read(), d = read();
n /= d, m /= d;
int b = min(n, m);
ans = ;
for(R j = ; j <= b; j = pos + )
{
pos = min(n / (n / j), m / (m / j));
ans += (sum[pos] - sum[j-]) * (n / j) * (m / j);
}
printf("%d\n", ans);
}
} int main()
{
// freopen("in.in", "r", stdin);
getprime();
work();
// fclose(stdin);
return ;
}

2,[HAOI2011]Problem b

~~~题面~~~
题解: 这题相比上题多出了两个限制条件,同时对上下限都有限制,那么此时可以用一个容斥来求。
    设$cal(n,m)$表示满足$x \le n$和$y \le m$且$gcd(x, y) = d$的点对个数,
    那么$$ans = cal(b, d) - cal(a - 1, d) - cal(b, c - 1) + cal(a - 1, c - 1);$$

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 55000
#define LL long long
int a, b, c, d, tot, k, t, ans;
int prime[AC], mu[AC], sum[AC];
bool z[AC]; inline int read()
{
int x = ; char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
int now;
mu[] = ;
for(R i = ; i <= ; i++)
{
if(!z[i]) prime[++tot] = i, mu[i] = -;//质数的mu值肯定都是-1
for(R j = ; j <= tot; j++)
{
now = prime[j];
if(now * i > ) break;
z[now * i] = true;
if(!(i % now)) break;
mu[i * now] = -mu[i];//error这里的mu是由mu[i]得来的啊!!!
}
}
for(R i = ; i <= ; i++) sum[i] = sum[i - ] + mu[i];
} int cal(int n, int m)
{
n /= k, m /= k;
int b = min(n, m), pos, rnt = ;
for(R i = ; i <= b; i = pos + )
{
pos = min(n / (n / i), m / (m / i));
rnt += (sum[pos] - sum[i-]) * (n / i) * (m / i);
}
return rnt;
}
//ans = cal(b, d) - cal(a - 1, d) - cal(b, c - 1) + cal(a - 1 , c - 1),相当于容斥 void work()
{
t = read();
for(R i = ; i <= t; i++)
{
a = read(), b = read(), c = read(), d = read(), k = read();
ans = cal(b, d) - cal(a - , d) - cal(b, c - ) + cal(a - , c - );
printf("%d\n", ans);
}
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
// fclose(stdin);
return ;
}

[POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演的更多相关文章

  1. [BZOJ1101&BZOJ2301][POI2007]Zap [HAOI2011]Problem b|莫比乌斯反演

    对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 我们可以令F[n]=使得n|(x,y)的数对(x,y)个数 这个很容易得到,只需要让x, ...

  2. P2522 [HAOI2011]Problem b (莫比乌斯反演)

    题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...

  3. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  4. Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...

  5. BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 1007  Solved: 415[Submit][ ...

  6. BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演

    分析:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 然后对于求这样单个的gcd(x,y)=k的, ...

  7. BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)

    [Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...

  8. BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演

    分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...

  9. 洛谷P2522 [HAOI2011]Problem b(莫比乌斯反演)

    题目描述 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 输入输出格式 输入格式: 第一行一个整数 ...

随机推荐

  1. fiddler不经意的功能

    捕获指定客户端的请求,直接食用 窗口分离,直接食用 Hide this column  隐藏此列Ensure all columns are visible   显示默认所有列Customize co ...

  2. 第一阶段·Linux运维基础 第3章·文件属性、正则表达式、文件权限

    01-文件属性内容介绍 02- inodeyublock讲解 03-访问oldboyfile的寻宝过程 04-inode与block小结 05-磁盘空间不足案例详解 06-Linux文件类型及拓展名 ...

  3. ConfigHelpers

    --默认值可以不传 local ConfigHelpers = {} --设置物体高亮 target:设置对象 isLigth:是否高亮 seeThrough:是否穿透(默认为true,穿透) sta ...

  4. Siki_Unity_3-13_编程内功修炼-算法

    Unity 3-13 编程内功修炼 -- 算法 任务1&2:课程介绍 主要算法: 分治法 堆排序 二叉树 动态规划 贪心算法 图 任务3:分治算法 -- Divide and Conquer ...

  5. Visual Studio Code——PHP Debug扩展

    最近在使用PHP开发,使用了很多IDE,发现都不是很顺手,之前一直都在使用Sublime Text,但是作为一个爱折腾的人,当我发现VS Code以后觉得很是很适合自己的编程需要的.配置过程中遇到了一 ...

  6. MySQL数据库怎么截取字符串?

    函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...

  7. ionic 获取input的值

    1.参数传递法 例子:获取input框内容 这里有个独特的地方,直接在input处使用 #定义参数的name值,注意在ts中参数的类型 在html页面中 <ion-input type=&quo ...

  8. python常用命令—windows终端查看安装包信息

    1, pip list 会将 Python 的所有安装包全部显示出来, 左边是包名, 右边是包的版本号. 2, pip show 包的名字 会将这个包的名字,版本号,包的功能说明,按装这个包的路径显示 ...

  9. opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测

    opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测 这章讲了 sobel算子 scharr算子 Laplacion拉普拉斯算子 图像深度问题 Canny检测 图像梯度 sobel算子 ...

  10. [leetcode-676-Implement Magic Dictionary]

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...