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

Description(CHN)

求 \(\sum_{i=1}^b\sum_{j=1}^d[gcd(i,j)=k]\)

Solution

水一道题,随便莫反一下就好了

设\(f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=d]\)

设\(F(d)=\sum_{i=1}^n\sum_{j=1}^m[d|gcd(i,j)]=\lfloor \frac{n}{d}\rfloor \lfloor \frac{m}{d} \rfloor=\sum_{d|n}f(n)\)

所以 \(f(d)=\sum_{d|p}\mu(\frac{p}{d})F(p)=\sum_{t=1}^{\frac{min(n,m)}{d}}\mu(t)F(dt)=\sum_{t=1}^{\frac{min(n,m)}{d}}\mu(t)\lfloor \frac{n}{dt} \rfloor \lfloor \frac{m}{dt} \rfloor\)

最后, \(ans=f(1)=\sum_{t=1}^{min(n,m)}\mu(t)\lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor\)

整除分块算(似乎都不需要整除分块?)

哦,对于无序对的处理,发现无序对的重复只存在于 \(i<=min(n,m),j<=min(n,m)\) 的情况下,否则一方将达不到另一方的数

那么算出总的,减去这个 \(min\) 的答案除二的商,就是最后的结果了(体会体会)

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100000+10;
int T,vis[MAXN],prime[MAXN],cnt,mu[MAXN],s[MAXN],a,b,c,d,k;
ll ans;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
memset(vis,1,sizeof(vis));
vis[0]=vis[1]=0;
mu[1]=1;
for(register int i=2;i<MAXN;++i)
{
if(vis[i])
{
prime[++cnt]=i;
mu[i]=-1;
}
for(register int j=1;j<=cnt&&i*prime[j]<MAXN;++j)
{
vis[i*prime[j]]=0;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else break;
}
}
for(register int i=1;i<MAXN;++i)s[i]=s[i-1]+mu[i];
}
inline ll solve(int n,int m)
{
ll res=0;
for(register int i=1;;)
{
if(i>min(n,m))break;
int j=min(n/(n/i),m/(m/i));
res+=1ll*(n/i)*(m/i)*(s[j]-s[i-1]);
i=j+1;
}
return res;
}
int main()
{
init();
read(T);
for(register int i=1;i<=T;++i)
{
printf("Case %d: ",i);
read(a);read(b);read(c);read(d);read(k);
if(!k)
{
puts("0");
continue;
}
b/=k;d/=k;
write(solve(b,d)-(solve(min(b,d),min(b,d))>>1),'\n');
}
return 0;
}

【刷题】HDU 1695 GCD的更多相关文章

  1. ●HDU 1695 GCD

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

  2. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  3. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  4. HDU 1695 GCD#容斥原理

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 翻译题目:给五个数a,b,c,d,k,其中恒a=c=1,x∈[a,b],y∈[c,d],求有多少组(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 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  7. HDU 1695 GCD (莫比乌斯反演)

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

  8. HDU 1695 GCD (欧拉函数+容斥原理)

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

  9. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

随机推荐

  1. SaltStack入门篇(四)之深入理解SaltStack远程执行

    1.目标 2.执行模块 3.返回 salt ‘*’ cmd.run ‘uptime’ 命令 目标 执行模块 执行模块参数 1.SlatStack远程执行–目标 执行目标:https://docs.sa ...

  2. python-前方高能-面向对象-进阶3

    面向对象 你写代码的时候 什么时候用面向对象 代码量大,功能多的时候 处理比较复杂的角色之间的关系 qq 好友 陌生人 群 组 复杂的电商程序 公司/学校的人事管理/功能的系统 我的代码的清晰度更高了 ...

  3. 做程序开发的你如果经常用Redis,这些问题肯定会遇到

    分布式缓存Redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等多种场景.Redis使用ANSI C语言编写,提供字符串(String).哈希(Hash ...

  4. LeetCode-124.二叉树中的最大路径和

    给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 ...

  5. eBay:美国各州最受欢迎的产品品类

    雨果网从美国媒体<商业内幕>8月26日的报道中获悉,电商巨头eBay近日发布了美国各州最受欢迎的产品品类.包括:加州人青睐女性高端配件,而新泽西 州的男人喜欢古龙香水.相比这些华丽配饰而言 ...

  6. Linux虚拟机centos系统安装

    linux 其他知识目录 安装完后如果虚拟机网络不通请参考:虚拟机网络不通故障解决 1.centos6.9安装 后面安装出了点问题,ip没有并且eth0网卡找不到,不过重新配置ifcfg-eth0后重 ...

  7. netty初认识

    Netty是什么? 本质:JBoss做的一个Jar包 目的:快速开发高性能.高可靠性的网络服务器和客户端程序 优点:提供异步的.事件驱动的网络应用程序框架和工具 通俗的说:一个好使的处理Socket的 ...

  8. BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树

    题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...

  9. Thunder团队第七周 - Scrum会议2

    Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:王航 工作照片: 参会成员: 王航(Master):http://www.cnblogs.com/wangh ...

  10. Requests库与HTTP协议

    了解HTTP协议 请求与响应模式的协议: 用户提出对URL(用来定位网络中的资源位置)地址数据的操作请求,服务器给予相应. 无状态的应用层协议:两次请求之间不会互相影响. HTTP协议支持的请求种类: ...