GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4291    Accepted Submission(s): 1502

Problem Description
Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.

Yoiu can assume that a = c = 1 in all test cases.

 
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
 
Output
For each test case, print the number of choices. Use the format in the example.
 
Sample Input
2
1 3 1 5 1
1 11014 1 14409 9
 
Sample Output
Case 1: 9
Case 2: 736427

Hint

For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).

 
Source
 
Recommend
wangye
 

前几天用容斥原理写过这题:

http://www.cnblogs.com/kuangbin/p/3269182.html

速度比较慢。

用莫比乌斯反演快很多。

莫比乌斯反演资料:

http://wenku.baidu.com/view/542961fdba0d4a7302763ad5.html

http://baike.baidu.com/link?url=1qQ-hkgOwDJAH4xyRcEQVoOTmHbiRCyZZ-hEJxRBQO8G0OurXNr6Rh6pYj9fhySI0MY2RKpcaSPV9X75mQv0hK

这题求[1,n],[1,m]gcd为k的对数。而且没有顺序。

转化之后就是[1,n/k],[1,m/k]之间互质的数的个数。

用莫比乌斯反演就很容易求了。

为了去除重复的,去掉一部分就好了;

这题求的时候还可以分段进行优化的。

具体看我的下一篇博客吧!

  1. /* ***********************************************
  2. Author :kuangbin
  3. Created Time :2013/8/21 19:32:35
  4. File Name :F:\2013ACM练习\专题学习\数学\莫比乌斯反演\HDU1695GCD.cpp
  5. ************************************************ */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. using namespace std;
  20. const int MAXN = ;
  21. bool check[MAXN+];
  22. int prime[MAXN+];
  23. int mu[MAXN+];
  24. void Moblus()
  25. {
  26. memset(check,false,sizeof(check));
  27. mu[] = ;
  28. int tot = ;
  29. for(int i = ; i <= MAXN; i++)
  30. {
  31. if( !check[i] )
  32. {
  33. prime[tot++] = i;
  34. mu[i] = -;
  35. }
  36. for(int j = ; j < tot; j++)
  37. {
  38. if(i * prime[j] > MAXN) break;
  39. check[i * prime[j]] = true;
  40. if( i % prime[j] == )
  41. {
  42. mu[i * prime[j]] = ;
  43. break;
  44. }
  45. else
  46. {
  47. mu[i * prime[j]] = -mu[i];
  48. }
  49. }
  50. }
  51. }
  52. int main()
  53. {
  54. //freopen("in.txt","r",stdin);
  55. //freopen("out.txt","w",stdout);
  56. int T;
  57. int a,b,c,d,k;
  58. Moblus();
  59. scanf("%d",&T);
  60. int iCase = ;
  61. while(T--)
  62. {
  63. iCase++;
  64. scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
  65. if(k == )
  66. {
  67. printf("Case %d: 0\n",iCase);
  68. continue;
  69. }
  70. b /= k;
  71. d /= k;
  72. if(b > d)swap(b,d);
  73. long long ans1 = ;
  74. for(int i = ; i <= b;i++)
  75. ans1 += (long long)mu[i]*(b/i)*(d/i);
  76. long long ans2 = ;
  77. for(int i = ;i <= b;i++)
  78. ans2 += (long long)mu[i]*(b/i)*(b/i);
  79. ans1 -= ans2/;
  80. printf("Case %d: %I64d\n",iCase,ans1);
  81. }
  82. return ;
  83. }

HDU 1695 GCD (莫比乌斯反演)的更多相关文章

  1. hdu 1695 GCD 莫比乌斯反演入门

    GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...

  2. HDU 1695 GCD 莫比乌斯反演

    分析:简单的莫比乌斯反演 f[i]为k=i时的答案数 然后就很简单了 #include<iostream> #include<algorithm> #include<se ...

  3. hdu 1695 GCD 莫比乌斯

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  5. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. HDU 1695 GCD (莫比乌斯反演模板)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. hdu 1695: GCD 【莫比乌斯反演】

    题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...

  8. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  9. ●HDU 1695 GCD

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...

随机推荐

  1. VS "15" 预览 5 中 VB 15 新增的功能

    VS "15" 预览 5 给 VB 带来了更新.这次的更新内容有3个: * 值元组 ValueTuple这个功能能把一组计算结果成组返回.为了使用这个功能,我们要安装 System ...

  2. C#十五子游戏

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 三通短信每月发送量导入Sqlserver随笔

    创建表sql CREATE TABLE SmsSentLog2014101625( Phone NVARCHAR(MAX), MessageContent NVARCHAR(MAX), Message ...

  4. C#实现判断字符是否为中文

    C#实现判断字符是否为中文 (2012-08-14 14:25:28) 标签: gb2312 big5编码 gbk编码 判断 汉字 杂谈 分类: 技术 protected bool IsChinese ...

  5. JavaWebSession

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  6. Python科学计算——前期准备

    1.开发环境搭建 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公 ...

  7. 【GOF23设计模式】责任链模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_责任链模式.公文审批.供应链系统的采购审批.异常链.过滤器和拦截器调用过程 package com.test.chainO ...

  8. 【GOF23设计模式】组合模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...

  9. 解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01

    操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasource导入Acces ...

  10. ASP.NET MVC 微信公共平台开发之验证消息的真实性

    ASP.NET MVC 微信公共平台开发 验证消息的真实性 在MVC Controller所在项目中添加过滤器,在过滤器中重写 public override void OnActionExecuti ...