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. unity3d 角色头顶信息3D&2D遮挡解决方案(二)

    在阅读本文之前请先阅读上一篇文章:http://www.cnblogs.com/shenggege/p/4179012.html 本来一篇文章就可以说完了,但是上次只是实现了已知的一些功能 后来在实际 ...

  2. 「LeetCode」0003-Add Two Numbers(Typescript)

    分析 代码 /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers=fu ...

  3. Linux命令应用大词典-第22章 GRUB

    22.1 grub-md5-crypt:使用MD5格式加密口令 22.2 grub-install:在设备上安装GRUB 22.3 grub:进入GRUB命令shell 22.4 grub-crypt ...

  4. 微信小程序之注释出现的问题(.json不能注释)

    js的注释一般是双斜杠// 或者是/**/这样的快注释 .json是配置文件,其内容必须符合json格式内部不允许有注释. JSON有两种数据结构: 名称/值对的集合:key : value样式: 值 ...

  5. 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列

    头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...

  6. 深入理解eos账户体系 active和action

    在eos中,账户是一个非常重要的概念. 账户分为两部分组成 一种是active 一种是action. 智能合约本质上来讲就是一个action加上一个回馈脚本程序.任何智能合约都有这俩个部分组成. 那么 ...

  7. POJ 1113 Wall(计算几何の凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  8. 【转】Expressions versus statements in JavaScript

    原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...

  9. Cow Contest(最短路floyed传递闭包)

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  10. matlab 直方图均衡化(含rgb)

    步骤: 统计原图像素每个像素的个数 统计原图像<每个灰度级的像素的累积个数 家里灰度级得映射规则 将原图每个像素点的灰度映射到新图 代码: clear all I=imread('1.jpg') ...