Zap

Time Limit: 10 Sec  Memory Limit: 162 MB
[Submit][Status][Discuss]

Description

  对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。

Input

  第一行包含一个正整数n,表示一共有n组询问。接下来n行,每行表示一个询问,每行三个正整数,分别为a,b,d。

Output

  输出一个正整数,表示满足条件的整数对数。

Sample Input

  2
  4 5 2
  6 4 3

Sample Output

  3
  2

HINT

  1<=n<= 50000, 1<=d<=a,b<=50000

Solution

  我们运用莫比乌斯反演,然后推一下式子得到:

  我们依旧对于下界分块求解即可。

Code

  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<cmath>
  8. using namespace std;
  9. typedef long long s64;
  10.  
  11. const int ONE = ;
  12.  
  13. int T;
  14. int n,m,k;
  15. bool isp[ONE];
  16. int prime[ONE],p_num;
  17. int miu[ONE],sum_miu[ONE];
  18. s64 Ans;
  19.  
  20. int get()
  21. {
  22. int res=,Q=; char c;
  23. while( (c=getchar())< || c>)
  24. if(c=='-')Q=-;
  25. if(Q) res=c-;
  26. while((c=getchar())>= && c<=)
  27. res=res*+c-;
  28. return res*Q;
  29. }
  30.  
  31. void Getmiu(int MaxN)
  32. {
  33. miu[] = ;
  34. for(int i=; i<=MaxN; i++)
  35. {
  36. if(!isp[i])
  37. prime[++p_num] = i, miu[i] = -;
  38. for(int j=; j<=p_num, i*prime[j]<=MaxN; j++)
  39. {
  40. isp[i * prime[j]] = ;
  41. if(i%prime[j] == )
  42. {
  43. miu[i * prime[j]] = ;
  44. break;
  45. }
  46. miu[i * prime[j]] = -miu[i];
  47. }
  48. miu[i] += miu[i-];
  49. }
  50. }
  51.  
  52. void Solve()
  53. {
  54. n=get(); m=get(); k=get();
  55. if(n > m) swap(n,m);
  56.  
  57. int N = n/k, M = m/k; Ans = ;
  58. for(int i=,j=; i<=N; i=j+)
  59. {
  60. j = min(N/(N/i), M/(M/i));
  61. Ans += (s64)(N/i) * (M/i) * (miu[j] - miu[i-]);
  62. }
  63.  
  64. printf("%lld\n",Ans);
  65. }
  66.  
  67. int main()
  68. {
  69. Getmiu(ONE-);
  70. T=get();
  71. while(T--)
  72. Solve();
  73. }

【BZOJ1101】Zap [莫比乌斯反演]的更多相关文章

  1. 【题解】Zap(莫比乌斯反演)

    [题解]Zap(莫比乌斯反演) 裸题... 直接化吧 [P3455 POI2007]ZAP-Queries 所有除法默认向下取整 \[ \Sigma_{i=1}^x\Sigma_{j=1}^y[(i, ...

  2. BZOJ1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2951  Solved: 1293[Submit][Status ...

  3. Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...

  4. 1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...

  5. bzoj 1101 Zap —— 莫比乌斯反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 直接莫比乌斯反演. 代码如下: #include<cstdio> #inc ...

  6. BZOJ 1101 Luogu P3455 POI 2007 Zap (莫比乌斯反演+数论分块)

    手动博客搬家: 本文发表于20171216 13:34:20, 原地址https://blog.csdn.net/suncongbo/article/details/78819470 URL: (Lu ...

  7. BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )

    求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...

  8. BZOJ 1101 Zap(莫比乌斯反演)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1101 给定a,b,d,求有多少gcd(x,y)==d(1<=x<=a&& ...

  9. 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)

    先看一道例题:[POI2007]Zap BZOJ 洛谷 题目大意:$T$ 组数据,求 $\sum^n_{i=1}\sum^m_{j=1}[gcd(i,j)=k]$ $1\leq T\leq 50000 ...

随机推荐

  1. c#一些常用的方法集合

    是从一个asp.net mvc的项目里看到的.挺实用的. 通过身份证号码获取出生日期和性别 通过身份证号码获取出生日期和性别 #region 由身份证获得出生日期 public static stri ...

  2. ABP框架设置默认语言

    在Global.asax文件中设置 private readonly IAbpWebLocalizationConfiguration _webLocalizationConfiguration; p ...

  3. 使用IDEA快速搭建Gradle项目

  4. CentOs 版本名字说明

    What images are in this directory CentOS-6.3-x86_64-netinstall.iso This is the network install and r ...

  5. oracle 数据库字段名与实体类字段名称不匹配的处理方法

    之前公司一直都使用sql server 即使数据库字段名称与实体类名称不相同 可以使用诸如: select id as userId from tb_user 这种写法,可换到了oracle 之后坑爹 ...

  6. Leetcode 672.灯泡开关II

    灯泡开关II 现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮.在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态. 假设这 n 只灯泡被编号为 [1, 2, 3 . ...

  7. 文本向量化及词袋模型 - NLP学习(3-1)

    分词(Tokenization) - NLP学习(1) N-grams模型.停顿词(stopwords)和标准化处理 - NLP学习(2)   之前我们都了解了如何对文本进行处理:(1)如用NLTK文 ...

  8. React错误总结解决方案(二)

    1.React native: Cannot add a child that doesn't have a YogaNode or parent node 该错误一般是因为render方法中注释语句 ...

  9. mysql insert into select 语法

    Insert into Table2(field1,field2,...) select value1,value2,... from Table1  这样就对了

  10. Manacher算法——最长回文子串

    一.相关介绍 最长回文子串 s="abcd", 最长回文长度为 1,即a或b或c或d s="ababa", 最长回文长度为 5,即ababa s="a ...