GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9942    Accepted Submission(s): 3732

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

 
Source
 
Recommend
wangye   |   We have carefully selected several similar problems for you:  1689 1690 1693 1691 1698 

此题算是莫比乌斯反演的经典题。
虽然上面说了把a和c当作1 ,但我们还是把它当做任意数来做。
对于求(a,b) (c,d)上对应的最大公约数为k这类题,先用容斥原理分为四种:(1,b)与(1,d);(1,c-1)与(1,b);(1,a-1)与(1,d);
对于每种情况,假设是(1,n)与(1,m)这两个区间(n<m)。
  那这两个区间gcd(x,y)>=k的有(n/k)*(m/k)个。
  若要求最大公约数为k,那么求得便是(n/k)与(m/k)中互质的数的个数。
  接下来就用莫比乌斯函数求这些互质的数的个数了。
  其中我还加入了分段优化。
     最后一步就是去重了,题目说想 G(x,y)==G(y,x),所以要把(a,b) (c,d)里重叠的部分多余的去掉。
  具体【传送门

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#define clr(x) memset(x,0,sizeof(x))
#define LL long long
using namespace std;
int prime[],inf[],mu[],sum[];
long long solve(int n,int m);
void mobius();
int main()
{
int T;
scanf("%d",&T);
int a,b,c,d,k,minx,maxx;
LL ans;
mobius();
for(int ii=;ii<=T;ii++)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k == )
{
printf("Case %d: 0\n",ii);
continue;
}
ans=solve(b/k,d/k) - solve((a-)/k,d/k) - solve(b/k,(c-)/k) + solve((a-)/k,(c-)/k);
if((maxx=max(a,c))<(minx=min(b,d)))
ans=ans-(solve(minx/k,minx/k)-solve((maxx-)/k,minx/k)*+solve((maxx-)/k,(maxx-)/k))/;
printf("Case %d: %lld\n",ii,ans);
}
return ;
}
void mobius()
{
clr(inf);
clr(prime);
clr(sum);
clr(mu);
mu[] = ;
inf[]=inf[]=;
int tot = ;
for(int i = ; i <= ; i++)
{
if( !inf[i] )
{
prime[tot++] = i;
mu[i] = -;
}
for(int j = ; j < tot; j ++)
{
if( i * prime[j] > ) break;
inf[i * prime[j]] = true;
if( i % prime[j] == )
{
mu[i * prime[j]] = ;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
}
for(int i = ;i <= ;i++)
sum[i] = sum[i-] + mu[i];
}
LL solve(int n,int m)
{
LL ans = ;
if(n > m)swap(n,m);
for(int i = , la = ; i <= n; i = la+)
{
la = min(n/(n/i),m/(m/i));
ans += (LL)(sum[la] - sum[i-])*(n/i)*(m/i);
}
return ans;
}

hdu 1965 (莫比乌斯函数 莫比乌斯反演)的更多相关文章

  1. 莫比乌斯函数&莫比乌斯反演

    莫比乌斯函数:http://wenku.baidu.com/view/fbec9c63ba1aa8114431d9ac.html Orz  PoPoQQQ

  2. BZOJ 2440 莫比乌斯函数+容斥+二分

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5473  Solved: 2679[Submit][Sta ...

  3. 51nod 1240 莫比乌斯函数

    题目链接:51nod 1240 莫比乌斯函数 莫比乌斯函数学习参考博客:http://www.cnblogs.com/Milkor/p/4464515.html #include<cstdio& ...

  4. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  5. UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)

    UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...

  6. HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法

    题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...

  7. hdu 1695 GCD 【莫比乌斯函数】

    题目大意:给你 a , b , c , d , k 五个值 (题目说明了 你可以认为 a=c=1)  x 属于 [1,b] ,y属于[1,d]  让你求有多少对这样的 (x,y)满足gcd(x,y)= ...

  8. 2017 ACM暑期多校联合训练 - Team 3 1008 HDU 6063 RXD and math (莫比乌斯函数)

    题目链接 Problem Description RXD is a good mathematician. One day he wants to calculate: ∑i=1nkμ2(i)×⌊nk ...

  9. HDU 6053 TrickGCD (莫比乌斯函数)

    题意:给一个序列A,要求构造序列B,使得 Bi <= Ai, gcd(Bi) > 1, 1 <= i <= n, 输出构造的方法数. 析:首先这个题直接暴力是不可能解决的,可以 ...

随机推荐

  1. Centos 6.5下安装vsftpd服务器

    1.查看是否安装vsftp  [root@localhost ~]#rpm -qa|grep vsftpd 如果出现 vsftpd-2.2.2-13.el6_6.1.x86_64  则说明已经安装了v ...

  2. POJ 3276 Face The Right Way (尺取法)

    题目链接 Description Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are f ...

  3. VMware 12安装虚拟机Mac OS X 10.10使用小技巧(虚拟机Mac OS X 10.10时间设置,虚拟机Mac OS X 10.10通过代理上网,Mac OS X 10.10虚拟机优化,VMware虚拟机相互复制)

    1:修改Mac OS 系统时间 2:Mac OS系统 通过代理上网 VMware 12安装Mac OS X 10.10虚拟机优化心得 虚拟显卡硬伤,所以必须要优化下才能用,优化的原则就是能精简的精简, ...

  4. C# 文件操作常用方法总结

    需引用 System.IO Path为绝对路径 检测指定目录是否存在 Directory.Exists(Path) 创建目录 Directory.CreateDirectory(Path) 删除目录 ...

  5. javascript工厂模式、单例模式

    //工厂模式 function createObject(name,age){ var obj = new Object(); obj.name = name; obj.age = age; obj. ...

  6. Msfvenom学习总结-MSF反弹webshell

    1.    –p (- -payload-options) 添加载荷payload. 载荷这个东西比较多,这个软件就是根据对应的载荷payload生成对应平台下的后门,所以只有选对payload,再填 ...

  7. Linux 入门记录:十七、Linux 命令行文本/文件处理工具

    一.文件浏览 cat 查看文件内容 more 以翻页形式查看文件内容(只能向下翻页) less 以翻页形式查看文件内容(可以上下翻页) head 查看文件的头几行(默认10行) tail 查看文件的尾 ...

  8. springboot在不同环境下进行不同的配置

    原文链接:http://www.cnblogs.com/java-zhao/p/5469183.html 不同的环境设置一个配置文件,例如:dev(开发)环境下的配置设置在application-de ...

  9. 亚马逊EC2根硬盘空间扩容

    买的系统盘为32G,结果发现只使用了8G,剩下的都未分配 lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda : 32G disk └─xvda1 : ...

  10. [New Learn]被嫌弃的app的一生

    1.简介 为什么叫被嫌弃的app的一生?致敬电影<被嫌弃的松子的一生>. 自学IOS东一锄西一镐的总感觉没有一个总的概念,还是多看看官网吧,先看一下一个app的整个生命周期,本文主要是翻译 ...