GCD

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

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
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define esp 0.00000000001
  5. #define pi 4*atan(1)
  6. const int N=1e5+,M=1e7+,inf=1e9+,mod=1e9+;
  7. int mu[N], p[N], np[N], cnt, sum[N];
  8. void init() {
  9. mu[]=;
  10. for(int i=; i<N; ++i) {
  11. if(!np[i]) p[++cnt]=i, mu[i]=-;
  12. for(int j=; j<=cnt && i*p[j]<N; ++j) {
  13. int t=i*p[j];
  14. np[t]=;
  15. if(i%p[j]==) { mu[t]=; break; }
  16. mu[t]=-mu[i];
  17. }
  18. }
  19. }
  20. int main()
  21. {
  22. int T,cas=;
  23. init();
  24. scanf("%d",&T);
  25. while(T--)
  26. {
  27. int a,b,c,d,k;
  28. scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
  29. if(k==)
  30. {
  31. printf("Case %d: 0\n",cas++);
  32. continue;
  33. }
  34. b/=k,d/=k;
  35. if(b>=d)swap(b,d);
  36. ll ans=;
  37. for(int i=;i<=b;i++)
  38. {
  39. ans+=(ll)mu[i]*(b/i)*(d/i);
  40. }
  41. ll ans2=;
  42. for(int i=;i<=b;i++)
  43. {
  44. ans2+=(ll)mu[i]*(b/i)*(b/i);
  45. }
  46. printf("Case %d: %lld\n",cas++,ans-ans2/);
  47. }
  48. return ;
  49. }

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. D - GCD HDU - 1695 -模板-莫比乌斯容斥

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

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

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

  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 【莫比乌斯函数】

    题目大意:给你 a , b , c , d , k 五个值 (题目说明了 你可以认为 a=c=1)  x 属于 [1,b] ,y属于[1,d]  让你求有多少对这样的 (x,y)满足gcd(x,y)= ...

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

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

  9. hdu 1695 GCD(莫比乌斯反演)

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

随机推荐

  1. <转> 堆和栈的区别

    一.预备知识—程序的内存分配    一个由C/C++编译的程序占用的内存分为以下几个部分    1.栈区(stack)—   由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数 ...

  2. 《挑战程序设计竞赛》2.1 深度优先搜索 POJ2386 POJ1979 AOJ0118 AOJ0033 POJ3009

    POJ2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25366   Accepted: ...

  3. Markov Process

    w Markov Process -- from Wolfram MathWorld  http://mathworld.wolfram.com/MarkovProcess.html 谷歌背后的数学_ ...

  4. 我的Android进阶之旅------>Android的ListView数据更新后,如何使最新的条目可以自动滚动到可视范围内?

    在ListView的layout配置中添加 android:transcriptMode="alwaysScroll" <ListView android:id=" ...

  5. linux安装jdk_1.8

    转载自http://blog.csdn.net/ldl22847/article/details/7605650 1.下载jdk的rpm安装包,这里以jdk-8u141-linux-x64.rpm为例 ...

  6. 018-Spring Boot Starter开发

    自建spring-boot-starter artifactId命名 Spring 官方 Starter通常命名为spring-boot-starter-{name}如 spring-boot-sta ...

  7. django的模板系统过滤器笔记

    -------------------django内建的过滤器-------------------1.add 使用形式为:{{ value | add: "2"}}意义:将val ...

  8. Amazon2014在线笔试 第三题

    问题描述: 算法分析: s1:层数对齐:分别求两个数所在的层(l1,l2),把层数大的(假设l2>l1)先往上找父节点,从而对齐到l1层: s2:两个数同时往上找, 直到找到公共的父节点(一定能 ...

  9. python——单例模式

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在. 当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 比如, ...

  10. Python 7 多线程及进程

    进程与线程: 进程的概念: 1.程序的执行实例称为进程. 2.每个进程都提供执行程序所需的资源.一个进程有一个虚拟地址空间.可执行代码.对系统对象的开放句柄.一个安全上下文.一个独特的进程标识符.环境 ...