【刷题】HDU 1695 GCD
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的更多相关文章
- ●HDU 1695 GCD
题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- 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 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD 欧拉函数 + 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 1695 GCD (莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
随机推荐
- 修改Qt源码遇到的问题
1.修改源码后用新的Qt版本调试Qt工程,程序直接崩溃:
- unittest,requests——接口测试脚本及报告
用unittest管理两个利用requests模块,做百度搜索的简单接口测试用例,之后自动输出报告 # encoding=utf-8import requests,unittest,HTMLTestR ...
- 四、Django之模型与管理后台-Part 2
一.数据库安装 打开mysite/settings.py配置文件,这是整个Django项目的设置中心.Django默认使用SQLite数据库,因为Python源生支持SQLite数据库,所以你无须安装 ...
- 《Node.js核心技术教程》读书笔记---思维导图版
书薄,挺经看!
- javascript 强制转换规则 boolean 布尔值类型
摘自 <你不知道的Javascript(中卷)> p55 一句话简述, 假值表以外的值均可以认为是真值,部分浏览器可能自定义了假值表以外的假值,并不符合W3C规范,需要特殊对待. 首先也是 ...
- JAVA学习笔记--初始化与清理
编写程序时,常会由于变量没有初始化而产生各种错误:用完一个元素,如果不将其占用的内存资源释放,则会导致资源耗尽,这也很严重,为此,C++引入了构造器的概念,这是一个在创建对象时被自动调用的特殊方法,以 ...
- 十大经典排序算法总结 (Python)
作业部落:https://www.zybuluo.com/listenviolet/note/1399285 以上链接是自己在作业部落编辑的排序算法总结- Github: https://github ...
- windows8和windows server2012不联网安装.net 3.5(包括2.0和3.0)
安装完win8后 发现系统默认没有安装.net3.5 如果使用在线更新的话需要很久才能完成,特别是当前的网速以及微软的服务器.速度很忙,其实我们利用win8的安装盘就可以不需要联网更新,而且几分钟就搞 ...
- 一个小时搭建一个全栈 Web 应用框架
把想法变为现实的能力是空想家与实干家的区别.不管你是在一家跨国公司工作,还是正在为自己的创业公司而努力,那些有能力将创意转化为真正产品的人,都具有宝贵的技能并拥有明显的实力.如果你能在不到一个小时的时 ...
- ubuntu关闭系统自动检测错误
sudo gedit /etc/default/apport 将enabled=1 改成 enabled=0