GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 17212    Accepted Submission(s): 6637

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).

题解:

题意是求满足1<=x<=b和1<=y<=d的gcd(x,y)=k的(x,y)有多少对,可以转化为求1<=x<=b/k和1<=y<=d/k的gcd(x,y)=1的(x,y)有多少对,可以用到莫比乌斯反演解决,证明链接https://blog.csdn.net/outer_form/article/details/50588307

这里运用到第二个式子:

为满足的对数

为满足的对数

那么,一个数对x,y要满足它们的gcd是i的倍数,则x和y中都必须包含i这个因子,所以F(i)=⌊N/i⌋·⌊M/i⌋。所以反演后得到

所以得到  ,因为要去重,所以后面再减去(1,b)区间的(x,y)对数的一半;

#include<iostream>
#include<string.h>
#define ll long long
using namespace std;
ll mu[100007],prime[100007];
bool mark[100007];
void getmu()
{
mu[1]=1;
ll cnt=0;
for(ll i=2;i<100007;i++){
if(!mark[i]){
prime[cnt++]=(ll)i;
mu[i]=-1;
}
for(ll j=0;j<cnt&&i*prime[j]<100007;j++){
mark[i*prime[j]]=1;
if(i%prime[j]){
mu[i*prime[j]]=-mu[i];
}else{
mu[i*prime[j]]=0;
break;
}
}
}
}
int main()
{
int T;
ll a,b,c,d,k,ans1,ans2;
getmu();
scanf("%d",&T);
for(int ca=1;ca<=T;ca++){
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
printf("Case %d: ",ca);
if(!k){printf("0\n");continue;}
b/=k,d/=k;
if(b>d) swap(b,d);
ans1=ans2=0;
for(ll i=1;i<=b;i++)
ans1+=mu[i]*(b/i)*(d/i);
for(ll i=1;i<=b;i++)
ans2+=mu[i]*(b/i)*(b/i);
printf("%lld\n",ans1-ans2/2);
}
return 0;
}

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 Submiss ...

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

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

  8. ●HDU 1695 GCD

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

  9. hdu1695(莫比乌斯反演模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 对于 a, b, c, d, k . 有 x 属于 [a, b],  y 属于 [c, ...

随机推荐

  1. OpenCV 入门

    1.入门攻略[安装用] https://www.cnblogs.com/linshuhe/p/5764394.html 2.VS2017配置opencv教程(超详细!!!) https://blog. ...

  2. HDOJ 1004 Let the Balloon Rise (字符串+stl)

    题目: Problem Description Contest time again! How excited it is to see balloons floating around. But t ...

  3. [转]python3之模块psutil系统性能信息

    转自:https://www.cnblogs.com/zhangxinqi/p/9106265.html 阅读目录 1.psutil模块安装 2.获取CPU信息 3.内存信息 4.磁盘信息 5.网络信 ...

  4. 【转】MySQL-Select语句高级应用

    [转]MySQL-Select语句高级应用 1.1 SELECT高级应用 1.1.1 前期准备工作 本次测试使用的是world数据库,由mysql官方提供下载地址: https://dev.mysql ...

  5. GeoDesc: Learning Local Descriptors by Integrating Geometry Constraints

    这篇论文提出了一种新的局部描述子学习方法,有一些点值得学习,记录下来以供参考.文章中涉及了一些3D reconstruction.structure from 的知识,不是很了解,所以理解可能有偏颇. ...

  6. keepalived 的某台vip连接不通【原创】

    keepalived 的某台vip连接不通,vip可以漂移到这台服务器,但是ping vip不通,telnet vip 3306服务也不通,但是telnet 服务器真实物理IP 3306是通的. 切换 ...

  7. vsftp、ftps 搭建

    今天公司某个产品预上线,该产品需要向政府某部门提供一些数据. 该部门提交数据需要使用ftps,苦逼的我只能是测试环境搭建一套,用来测试提交数据. 先自行科普下ftps. 一.搭建vsftp 安装vsf ...

  8. mongdb中的_id

    MongoDB中数据的基本单元称为文档(Document).文档是MongoDB的核心概念,多个键极其关联的值有序的放置在一起便是文档. 在一个特定集合内部,需要唯一的标识文档.因此MongoDB中存 ...

  9. 带你十分钟快速构建好 SpringBoot + SSM 框架

    目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐.而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了 ...

  10. codeforces 416div.2

        A CodeForces 811A Vladik and Courtesy   B CodeForces 811B Vladik and Complicated Book   C CodeFo ...