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. vim分屏功能总结

    vim的分屏功能 总结起来,基本都是ctrl+w然后加上某一个按键字母,触发一个功能.(1)在shell里打开几个文件并且分屏: vim -On file1 file2 ... vim -on fil ...

  2. libevent学习八(evbuffer)

    1.evbuffer以队列的形式管理字节,从尾部添加,从头部取出(FIFO) 2.evbuffer内部存储形式是多个独立的连续内存       接口 //创建和删除 struct evbuffer * ...

  3. html div内第二行文字显示不下的时候才用省略号代替 css实现

    有时候文字太多,但为了美观想要在第二行的时候才显示省略号,而不是第一行超出马上就出现省略号 下面是css代码: overflow:hidden;text-overflow: ellipsis;//显示 ...

  4. 通过 Python_Faker 生成测试数据

    通过 Python_Faker 生成测试数据 一.介绍 在软件需求.开发.测试过程中,有时候需要使用一些测试数据,针对这种情况,我们一般要么使用已有的系统数据,你不可能通过手工来生成(最傻的方法)可能 ...

  5. javac 编译过程

    javac 编译过程     一.解析与填充符号表: 1.  语法.此法分析:          a) 语法分析:将源代码字符流转换为标记(Token:编译过程最小元素)集合.          b) ...

  6. lintcode12 带最小值操作的栈

    实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 建一个栈helpStack,用来存放从 ...

  7. 【上传-下载】-jmeter工具

    上 传 ================================================================================================ ...

  8. 微信小程序navigator跳转失效

    在编写小程序时遇到一个问题:使用 <navigator url='/pages/lists/index'>...</navigator>进行跳转没有反应.控制台也没有报错,ap ...

  9. js经典试题之数据类型

    js经典试题之数据类型 1:输出"B" + "a" + + "B" + "a"的值: 答案:BaNaNa. 分析:因为+ ...

  10. http://www.cnblogs.com/120626fj/p/7545958.html

    1.本周PSP 2.本周进度条: 代码行,博文字数,用到的知识点 3.累计进度图 3.1累计代码折线图 3.2累计博文字数折线图 4.本周PSP饼状图