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(容斥原理 || 莫比乌斯)的更多相关文章

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

  2. ACM学习历程—HDU4746 Mophues(莫比乌斯)

    Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of some ...

  3. ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)

    Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...

  4. ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...

  5. ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)

    Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal ...

  6. ACM学习历程—HDU 5072 Coprime(容斥原理)

    Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...

  7. ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)

      时间限制:6000ms 单点时限:1000ms 内存限制:256MB   描述 你在赌场里玩梭哈,已经被发了4张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...

  8. ACM学习历程—HDU1717 小数化分数2(gcd)

    Description Ray 在数学课上听老师说,任何小数都能表示成分数的形式,他开始了化了起来,很快他就完成了,但他又想到一个问题,如何把一个循环小数化成分数呢? 请你写一个程序不但可以将普通小数 ...

  9. hdu1695 GCD(莫比乌斯入门题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 给出n.m.k ,求出1<=x<=n, 1<=y<=m 且gcd ...

随机推荐

  1. Eureka集群搭建

    服务注册.发现是微服务架构的关键原理之一,由于微服务架构是由一系列职责单一的细粒度服务构成的网状结构,服务之间通过轻量机制进行通信,这就必然引入一个服务注册发现的问题,也就是说服务提供方要注册报告服务 ...

  2. IIS架构介绍

    IIS7及以上版本提供的请求-处理架构包括以下内容: Windows Process Activation Service(WAS)可以让站点支持更多协议,不仅仅是HTTP和HTTPS 可以通过增加或 ...

  3. ubuntu 17 编译BTCoin

    一. 安装开发环境 sudo apt-get update sudo apt-get install build-essential libtool autotools-dev autoconf pk ...

  4. Full卷积 Same卷积 Vaild卷积

  5. 转载一篇将C/C++ 与lua混合使用入门讲的比较好的文章

    转自 http://www.open-open.com/home/space-6246-do-blog-id-1426.html Lua是一个嵌入式的脚本语言,它不仅可以单独使用还能与其它语言混合调用 ...

  6. spring struts2整合

    把struts2的action交给spring管理 一.导入相应jar包 导入与spring有关的基本jar包,和与struts2有关的基本jar包 还需要导入 struts2-spring整合jar ...

  7. Linux系统监控的几个命令

    uptime 系统时间.运行时间.连接数(没一个终端算一个连接).在1,5,15分钟内系统负载 uname -a    查看系统所有相关信息 -r     查看系统内核版本 -s    查看系统内核名 ...

  8. iOS 展示 gif

    gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 .   有的是均匀时间更换下一张  有的 则不是均匀时间变化 1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自 ...

  9. hd acm1013

    Problem Description(数根) The digital root of a positive integer is found by summing the digits of the ...

  10. nginx 启动/停止/重启

    启动: -c filename   : set configuration file (default: conf/nginx.conf) [root@LinuxServer sbin]# /usr/ ...