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. Spring cookie 实战(山东数漫江湖)

    Cookie是什么 简单来说,cookie就是浏览器储存在用户电脑上的一小段文本文件.cookie 是纯文本格式,不包含任何可执行的代码.一个web页面或服务器告知浏览器按照一定规范来储存这些信息,并 ...

  2. python keras YOLOv3实现目标检测

    1.连接 https://www.jianshu.com/p/3943be47fe84

  3. Chrome 浏览器 autocomplete off无效

    在表单填写时突然发现autocomplete 失效了 网上搜索后得出大概意思是在某些情况下确实无效[捂脸] 解决方案 大致原因是浏览器默认为type为password的input标签自动填充密码 这样 ...

  4. hdu 2059 龟兔赛跑(动态规划DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    M ...

  5. adb操作指令大全

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试android程序,说白了就是debug工具.a ...

  6. 在linux程序里面,知道一个函数地址,改函数是属于某个动态库的,怎么样得到这个动态库的全【转】

    转自:http://www.360doc.com/content/17/1012/11/48326749_694292472.shtml 另外dl_iterate_phdr可以查到当前进程所装在的所有 ...

  7. perl_nc.pl

    #!/usr/bin/perl use strict; use IO::Socket; use IO::Select; use Getopt::Std; my %option;getopts('lp: ...

  8. 安装lszrz,用于上传文件

    wget http://down1.chinaunix.net/distfiles/lrzsz-0.12.20.tar.gztar zxvf lrzsz-0.12.20.tar.gzcd lrzsz- ...

  9. java中this的用法如:this.name=name

    package com.chensi; /** * 这个是为了搞懂那个 this.name = name的. * @author ZHL * */ public class ThisTestZhl { ...

  10. ZOJ-3314

    CAPTCHA Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class ...