2301: [HAOI2011]Problem b

Time Limit: 50 Sec  Memory Limit: 256 MB
Submit: 6519  Solved: 3026
[Submit][Status][Discuss]

Description

对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。

Input

第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k

Output

共n行,每行一个整数表示满足要求的数对(x,y)的个数

Sample Input

2

2 5 1 5 1

1 5 1 5 2

Sample Output

14

3

HINT

100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000

Source

析:首先很容易看出来是莫比乌斯反演,但是直接用会TLE,因为有数据数组,总时间复杂度就是O(n^2),肯定会超时,所以要进行优化,因为在求答案的时候,对于每个G函数都要一个个来求,而G函数就是(m/i)*(n/i),m,n表示现最大的两个边界,在一段值内,它们的值是相等的,所以可以先求莫比乌斯函数的前缀和,进行优化,时间复杂度就大降低了。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #include <numeric>
  21. #define debug() puts("++++")
  22. #define gcd(a, b) __gcd(a, b)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. #define fi first
  26. #define se second
  27. #define pb push_back
  28. #define sqr(x) ((x)*(x))
  29. #define ms(a,b) memset(a, b, sizeof a)
  30. #define sz size()
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. //#define all 1,n,1
  35. #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
  36. #define freopenr freopen("in.txt", "r", stdin)
  37. #define freopenw freopen("out.txt", "w", stdout)
  38. using namespace std;
  39.  
  40. typedef long long LL;
  41. typedef unsigned long long ULL;
  42. typedef pair<int, int> P;
  43. const int INF = 0x3f3f3f3f;
  44. const LL LNF = 1e17;
  45. const double inf = 1e20;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-8;
  48. const int maxn = 5e4 + 10;
  49. const int maxm = 3e5 + 10;
  50. const int mod = 1e9 + 7;
  51. const int dr[] = {-1, 0, 1, 0};
  52. const int dc[] = {0, -1, 0, 1};
  53. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  54. int n, m;
  55. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  56. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  57. inline bool is_in(int r, int c) {
  58. return r >= 0 && r < n && c >= 0 && c < m;
  59. }
  60.  
  61. bool vis[maxn];
  62. int prime[maxn];
  63. int mu[maxn];
  64.  
  65. void Moblus(){
  66. mu[1] = 1;
  67. int tot = 0;
  68. for(int i = 2; i < maxn; ++i){
  69. if(!vis[i]) prime[tot++] = i, mu[i] = -1;
  70. for(int j = 0; j < tot; ++j){
  71. if(i * prime[j] >= maxn) break;
  72. vis[i*prime[j]] = 1;
  73. if(i % prime[j] == 0){
  74. mu[i*prime[j]] = 0;
  75. break;
  76. }
  77. else mu[i*prime[j]] = -mu[i];
  78. }
  79. }
  80. }
  81.  
  82. int sum[maxn];
  83. LL solve(int n, int m){
  84. if(n > m) swap(n, m);
  85. LL ans = 0;
  86. for(int i = 1, det = 1; i <= n; i = det+1){
  87. det = min(n/(n/i), m/(m/i));
  88. ans += (LL)(sum[det] - sum[i-1]) * (n/i) * (m/i);
  89. }
  90. return ans;
  91. }
  92.  
  93. int main(){
  94. Moblus();
  95. for(int i = 1; i < maxn; ++i) sum[i] = sum[i-1] + mu[i];
  96. int T; cin >> T;
  97. while(T--){
  98. int a, b, c, d, k;
  99. scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
  100. LL ans = solve(b/k, d/k) - solve((a-1)/k, d/k) - solve((c-1)/k, b/k) + solve((a-1)/k, (c-1)/k);
  101. printf("%lld\n", ans);
  102. }
  103. return 0;
  104. }

  

BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)的更多相关文章

  1. BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 436  Solved: 187[Submit][S ...

  2. 2301: [HAOI2011]Problem b ( 分块+莫比乌斯反演+容斥)

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

  3. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)

    题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...

  4. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演)

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

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

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

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

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

  7. bzoj 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...

  8. BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  9. 【BZOJ】2301: [HAOI2011]Problem b(莫比乌斯+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 和这题不是差不多的嘛--[BZOJ]1101: [POI2007]Zap(莫比乌斯+分块) 唯 ...

随机推荐

  1. crm作业知识点集合[二]

    知识点1 前面我们实现了这个功能,就是在models中如果有了choice选项,我们可以实现在页面显示这个chocice的value值,而不是key值,我们这个知识点就是在优化一下这个点 首先如果表中 ...

  2. mybatis插入数据并获取主键值

    有时候我们的主键是自增的,但是我们想要在插入一条数据以后获取这条数据的主键值,而我们知道,mybatis执行完插入操作以后返回的是生效的记录数.那如何才能获取这个主键值呢. 1.在配置文件mapper ...

  3. ubuntu下tomcat的安装及注册成系统服务

    在ubuntu下tomcat的安装有两种方式,第一种是下载二进制文件,解压安装:第二种则是使用apt-get自动下载.这里不推荐第二种方法安装,因为这种方法安装会像天女散花一样把安装的文件散落在系统的 ...

  4. StringBuffer 和 StringBuilder 类

    当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类. 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够 ...

  5. [Z]scp example

    https://www.cnblogs.com/autumnvivi/articles/3447964.html

  6. localstorage和vue结合使用

    父组件 <template> <div class="hello"> <p>Original message:"{{message}} ...

  7. idea spring-boot gradle mybatis 搭建开发环境

    使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot.mybatis 1.新建项目 说明:1.src为源码路径,开发主要在src下 2.src/main/jav ...

  8. PAT 1085 PAT单位排行(25)(映射、集合训练)

    1085 PAT单位排行(25 分) 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随 ...

  9. GTK图形控件中的rc文件使用心得

    转载自: 1.http://blog.csdn.net/saintwinona/article/details/6972754 2. (1).GTK 主题指南 1.Widgets         GT ...

  10. python httplib2应用get post

    import httplib2,time #装饰器方法,用于记录方法消耗时间 #推荐将print 改成log def timer(func):     def _warpper(self,*argv) ...