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 ...
随机推荐
- [转]springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- Python 集合、字典、运算符
先区分一下序列类型和散列类型: 序列类型:list.string.tuple,他们中的元素是有序的. 散列类型:set.dict,他们中的元素无序的. 序列类型有序,可以用索引.而散列类型中的元素是无 ...
- Linux mariadb(Mysql)的主从复制架构
mysql的主从复制架构,需要准备两台机器,并且可以通信,安装好2个mysql,保持版本一致性 mysql -v 查看数据库版本 1.准备主库的配置文件 /etc/my.cnf 写入开启主库的参数[ ...
- Google的Guava之IO升华
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luo201227/article/details/36413279 程序员在开发过程中,使用文件的几 ...
- Python socket server demo
#coding:utf-8 from socket import * #开启ip和端口 ip_port = ("192.168.1.103",8088) print ip_port ...
- Vue-cli创建项目从单页面到多页面4 - 本地开发服务器设置代理
前后端分离开发时,有时候会遇到跨域的情况:只在开发的时候存在跨域,项目上线后,由于配置的域名相同,跨域就会不存在. 这个时候,有两种方案可以比较快的解决: 1.利用h5的特性,使用cors,在ngni ...
- 图片的等比缩放和Logo水印
/** * 等比缩放函数(以保存的方式实现) * @param string $picname 被缩放的处理图片源 * @param int $maxx 缩放后图片的最大宽度 * @param int ...
- Data Structure Linked List: Write a function to reverse a linked list
iterative太简单不写了 http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/ ...
- dev系列之gridview
gridview新增一行就激活编辑,及显示闪动的光标 gridView1.ShowEditor(); 隐藏Gridview表头上面的panel this.gridView1.OptionsView.S ...
- vim 的配置文件(.vimrc)
linux 下面的root的主目录中新建.vimrc配置文件,配置文件注析方式为“,不是#: 如: "显示行号 set number set ruler