ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)
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).
题目大意就是求x在[a, b],y在[c, d]区间内的满足gcd(x, y) == k的(x, y)对有多少,而且(3, 4) == (4, 3)这种。
可以使用容斥原理来做。
也可以使用莫比乌斯反演。
设F(d)表示满足区间的(x, y),且d | gcd(x, y)的个数。
f(d)表示满足区间的(x, y), 且d == gcd(x, y)的个数。
那么F(d)
= sum(f(n)) (d|n)
然后根据莫比乌斯反演:
F(d) =
sum(f(n)) (d|n) => f(d) = sum(u(n/d)*F(n)) (d|n)
然后目标就是求F(n)了。
首先不计重复对的话,总共有(b/n)*(d/n)对。
然后需要排除重复的,重复的不外乎是(a, b) == (b, a)这种情况。而且这种情况都且出现两次。
而且需要减掉(a, a)的情况(强行让b <= d),于是就是:(b/n)*(b/n) – (b/n)
然后除二就是多加的,于是:
F(n) = (b/n)*(d/n)-((b/n)*(b/n)-(b/n))/2
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ;
int u[maxN], prime[maxN];
bool vis[maxN];
int a, b, c, d, k; //莫比乌斯反演
//F(n)和f(n)是定义在非负整数集合上的两个函数
//并且满足条件F(n) = sum(f(d)) (d|n)
//那么f(n) = sum(u(d)*F(n/d)) (d|n)
//case 1: if d = 1, u(d) = 1
//case 2: if d = p1*p2...pk, (pi均为互异素数), u(d) = (-1)^k
//case 3: else, u(d) = 0;
//性质1: sum(u(d)) (d|n) = 1 (n == 1) or 0 (n > 1)
//性质2: sum(u(d)/d) (d|n) = phi(n)/n
//另一种形式:F(d) = sum(f(n)) (d|n) => f(d) = sum(u(n/d)*F(n)) (d|n)
//线性筛选求莫比乌斯反演函数代码
void mobius()
{
memset(vis, false,sizeof(vis));
u[] = ;
int cnt = ;
for(int i = ; i < maxN; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
u[i] = -;
}
for(int j = ; j < cnt && i*prime[j] < maxN; j++)
{
vis[i*prime[j]] = true;
if(i%prime[j])
u[i*prime[j]] = -u[i];
else
{
u[i*prime[j]] = ;
break;
}
}
}
} void work()
{
if (k == )
{
printf("0\n");
return;
}
LL ans = ;
if (b > d)
swap(b, d);
for (LL i = k; i <= b; i += k)
ans += ((b/i)*(d/i)-((b/i)*(b/i)-(b/i))/)*u[i/k];
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
mobius();
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case %d: ", times);
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
work();
}
return ;
}
ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)的更多相关文章
- ACM学习历程—HDU4675 GCD of Sequence(莫比乌斯)
Description Alice is playing a game with Bob. Alice shows N integers a 1, a 2, …, a N, and M, K. She ...
- ACM学习历程—HDU4746 Mophues(莫比乌斯)
Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of some ...
- ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)
Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...
- ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
- ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)
Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal ...
- ACM学习历程—HDU 5072 Coprime(容斥原理)
Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...
- ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)
时间限制:6000ms 单点时限:1000ms 内存限制:256MB 描述 你在赌场里玩梭哈,已经被发了4张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...
- ACM学习历程—HDU1717 小数化分数2(gcd)
Description Ray 在数学课上听老师说,任何小数都能表示成分数的形式,他开始了化了起来,很快他就完成了,但他又想到一个问题,如何把一个循环小数化成分数呢? 请你写一个程序不但可以将普通小数 ...
- hdu1695 GCD(莫比乌斯入门题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 给出n.m.k ,求出1<=x<=n, 1<=y<=m 且gcd ...
随机推荐
- MySQL复制经常使用拓扑结构具体解释
复制的体系结构有下面一些基本原则: (1) 每一个slave仅仅能有一个master: (2) 每一个slave仅仅能有一个唯一的serverID: (3) 每一个master能够有 ...
- html5+css3实现手机下拉和下拉刷新
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 识别不了socket未知的名称或服务
个人博客地址:https://blog.sharedata.info/ socket 链接导致java.net.UnknownHostException: R1-T1-N6: R1-T1-N6: 未知 ...
- Python一些基础练习题
可变的数据类型:list, dict, set(可修改其中的元素) 不可变的数据类型:str, tuple 重点:str, list, dict (1).推导式练习 # 利用列表推导式: 找出100以 ...
- 实现asp.net mvc页面二级缓存,提高访问性能
实现的mvc二级缓存的类 //Asp.Net MVC视图页面二级缓存 public class TwoLevelViewCache : IViewLocationCache { private rea ...
- docker安装并配置加速
安装 旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本: sudo apt-get remove docker \ docker-engine \ ...
- 创建第一个SpringBoot的demo程序
在这里,我只介绍手动创建的其中一种方式. 默认,你已经安装了IntelliJ IDEA和JDK1.8,如果没有,请先安装. 第一步:选择新建一个项目 File-->New-->Proj ...
- python3 生成随即激活码
import string import random #激活码中的字符和数字 field = string.ascii_letters + string.digits #获得四个字母和数字的随即组合 ...
- Linux服务器上的tomcat中部署web项目
首先了解一下下面几个概念,讲得不太准确:1.JVMJVM是class以及jar(实际上就是很多个class压缩在一起)的运行环境,特征就是java和javaw命令,通过这两个命令,你可以执行class ...
- NFS指定端口,NFS缓存
nfs服务端: #编辑/etc/nfsmount.conf,在末尾添加: #RQUOTAD_PORT=30001#LOCKD_TCPPORT=30002#LOCKD_UDPPORT=30002#MOU ...