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. Count(二维树状数组)

    [bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12   HINT 题解:对于每一个颜色建一 ...

  2. 九度OJ 1334:占座位 (模拟)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:864 解决:202 题目描述: sun所在学校的教室座位每天都是可以预占的. 一个人可以去占多个座位,而且一定是要连续的座位,如果占不到他所 ...

  3. Mac标识物理位置算法 import Levenshtein mac列表特征值

    mac 字符串 与 基准字符串的 Levenshtein   距离,考虑  mac信号强度的时序性,60秒内若干次变化 不引入强度 mac字符串的唯一性 如何排序 基准字符串的选取 同一尺度 都按强度 ...

  4. 模仿jquery框架源码

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. 【leetcode刷题笔记】Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. 第五章 python中的异常处理

    每种编程语言都会有自己的异常处理机制,虽然各有特色,但基本上都差不多,那么python中强大异常处理机制是什么样的呢? 一.异常: python用异常对象来表示异常情况,遇到错误后,会引发异常.如果异 ...

  7. 第二篇、css尺寸和边框

    一.尺寸和标签图 二.尺寸单位 %:百分比 in:英寸 cm:厘米 mm:毫米 pt:磅(点)(1pt等于1/72英寸) px:像素(计算机屏幕上的一个点) em:1em等于当前的字体尺寸,2em等于 ...

  8. linux通过脚本获取内存信息

    1 原理 脚本中通过执行free获取内存信息,然后将文本信息通过“空格”分隔符分割成字符串数组将不同信息提取出来,最后通过bc计算出百分比 2 脚本 #!/bin/shHOSTNAME=`hostna ...

  9. Android 蓝牙实例【转】

    本文转自:http://www.yiibai.com/android/android_bluetooth.html 在很多方面,蓝牙是一种能够发送或接受两个不同的设备之间传输的数据. Android平 ...

  10. 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/6578352 在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行 ...