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

时间:2022-09-23 00:09:02

题目

P2522 [HAOI2011]Problem b

解析:

具体推导过程同P3455 [POI2007]ZAP-Queries

不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j=c}^dgcd(i,j)=k\)

像二维前缀和一样容斥一下,输出就完了。

根据luogu某大佬的说法

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

开longlong的话会TLE。。

代码

//莫比乌斯反演
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int t, n, m, num, k;
int mu[N], p[N], sum[N];
bool vis[N]; template<class T>inline void read(T &x) {
x = 0; int f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = f ? -x : x;
return;
} void get_mu(int n) {
mu[1] = 1;
for (int i = 2; i <= n; ++i) {
if (!vis[i]) p[++num] = i, mu[i] = -1;
for (int j = 1; j <= num; ++j) {
if (i * p[j] > n) break;
vis[i * p[j]] = 1;
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
} else mu[i * p[j]] = -mu[i];
}
}
} int cal(int x, int y, int k) {
int mx = min(x, y), ans = 0;
for (int l = 1, r; l <= mx; l = r + 1) {
r = min(x / (x / l), y / (y / l));
ans += ((x / (l * k)) * (y / (l * k)) * (sum[r] - sum[l - 1]));
}
return ans;
} int a, b, c, d; signed main() {
get_mu(N);
for (int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + mu[i];
read(t);
while (t --) {
read(a), read(b), read(c), read(d), read(k);
printf("%d\n", cal(b, d, k) - cal(b, c - 1, k) - cal(a - 1, d, k) + cal(a - 1, c - 1, k));
}
return 0;
}

P2522 [HAOI2011]Problem b (莫比乌斯反演)的更多相关文章

  1. 洛谷P2522 &lbrack;HAOI2011&rsqb;Problem b&lpar;莫比乌斯反演&rpar;

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

  2. Luogu P2522 &lbrack;HAOI2011&rsqb;Problem b 莫比乌斯反演

    设$f(d)=\sum_{i=1}^N\sum_{j=1}^M[gcd(i,j)==d],\\F(n)=\sum_{n|d}f(d)=\lfloor \frac{N}{n} \rfloor \lflo ...

  3. BZOJ2301&colon; &lbrack;HAOI2011&rsqb;Problem b&lbrack;莫比乌斯反演 容斥原理&rsqb;【学习笔记】

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

  4. Bzoj 2301&colon; &lbrack;HAOI2011&rsqb;Problem b&lpar;莫比乌斯反演&plus;除法分块&rpar;

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

  5. BZOJ 2301&colon; &lbrack;HAOI2011&rsqb;Problem b 莫比乌斯反演

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

  6. BZOJ2301&colon; &lbrack;HAOI2011&rsqb;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&period;2301&period;&lbrack;HAOI2011&rsqb;Problem B&lpar;莫比乌斯反演 容斥&rpar;

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

  8. &lbrack;POI2007&rsqb;ZAP-Queries &amp&semi;&amp&semi; &lbrack;HAOI2011&rsqb;Problem b 莫比乌斯反演

    1,[POI2007]ZAP-Queries ---题面---题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$ ...

  9. &lbrack;BZOJ1101&amp&semi;BZOJ2301&rsqb;&lbrack;POI2007&rsqb;Zap &lbrack;HAOI2011&rsqb;Problem b&vert;莫比乌斯反演

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

随机推荐

  1. &lbrack;Bug&rsqb;枚举数组,并找到某些元素删除

    lldb报错:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <_ ...

  2. HTML5Viewer中如何运行时绑定多数据源

    很多报表控件提供HTML5Viewer 支持跨设备的报表系统,当然在很多情况下,一个系统可包含多个报表文件,这些报表的数据有可能均为运行时绑定数据源,那么在html5viewer中对一张报表通过重写W ...

  3. IOS开发之——自定义导航控制器

    BasicNavigationViewController:UINavigationViwController /* 隐藏导航底部线条 */ -(void)viewDidLoad{    [super ...

  4. Enterprise Library 中加密数据库连接字符串

    看了SHY520写的关于Data Access Application Block的文章,写得不错,忽略了一点就是如何去加密数据库连接字符串,这儿我简单的介绍一下.我们知道,在Enterprise L ...

  5. IDisposable 接口2

    定义一种释放分配的资源的方法. 命名空间:  System程序集:  mscorlib(在 mscorlib.dll 中) 语法 C# C++ F# VB [ComVisibleAttribute(t ...

  6. 像table一样布局div的CSS属性详解

    .equal {                     display:table;                     border-collapse:separate;margin: aut ...

  7. configure HDFS&lpar;hadoop 分布式文件系统&rpar; high available

    注:来自尚学堂小陈老师上课笔记 1.安装启动zookeeper a)上传解压zookeeper包 b)cp zoo_sample.cfg zoo.cfg修改zoo.cfg文件 c)dataDir=/o ...

  8. 分享一下在aspx页面弹框的设置代码

    public static class MessageBox { /// <summary> /// 显示消息提示对话框 /// </summary> /// <para ...

  9. MySQL-代码自动补全工具

    一.工具名称 mycli :  具有自动完成和语法高亮的功能 二.安装 pip install mycli 三.使用方法: mycli -u root -p password 四.效果图

  10. JS高级总结

    网址:https://www.cnblogs.com/signheart/p/d6c229a5a758ee1dc21ad5ca2042ab8f.html 通常,通过 JavaScript,您需要操作 ...