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. 【洛谷 P1390】 公约数的和 (欧拉函数)

    题目链接 做过\(n\)遍这种题了... 答案就是\(\sum_{i=1}^{n}\sum_{j=1}^{n/i}[\varphi(j)*i]\) 线筛欧拉函数求前缀和直接算就行. #include ...

  2. Part2-HttpClient官方教程-Chapter3-HTTP状态管理

    ps:近日忙于课设与一个赛事的准备....时间真紧啊~~ 最初,HTTP被设计为一种无状态的,面向请求/响应的协议,它并没有为跨越多个逻辑相关的请求/响应交换的有状态会话做出特殊规定.随着HTTP协议 ...

  3. Python自动化运维 - Django(三)CSRF - Cookie&Session

    CSRF跨站请求伪造 CSRF跨站点请求伪造(Cross—Site Request Forgery),跟XSS攻击一样,存在巨大的危害性,你可以这样来理解:攻击者盗用了你的身份,以你的名义发送恶意请求 ...

  4. 禁用 Cortana 的解决办法

    1. GPedit.msc 2. 然后在本地组策略编辑器中,点击“用户配置”中的“管理模版”,接着双击右侧的“Windows 组件”. 3. 下拉滚动条,并找到“文件资源管理器”,双击进入. 找到“在 ...

  5. python实战===图片转换为字符的源码(转)

    #cmd执行命令为>>> python xx.py pic.png#-*- coding:utf-8 -*- from PIL import Image import argpars ...

  6. 己动手创建最精简的Linux

    己动手创建最精简的Linux http://blog.sina.com.cn/s/blog_71c87c170101e7ru.html 首次 LFS 搭建全过程 http://zmyxn.blog.5 ...

  7. python windows下安装celery调度任务时出错

    由于celery 4.0不支持windows系统.所以用命令pip install Celery安装的celery是最新版4.0的不能在windows下运行. 在windows命令窗口运行: cele ...

  8. 【POJ3254】coinfield

    状压dp初步. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

  9. C#多线程编程之:集合类中Synchronized方法与SyncRoot属性原理分析

    我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot属性,他们之间有联系吗?我怎么才能用好他们呢? 以Hashtab ...

  10. Leetcode 之Longest Common Prefix(33)

    在一组字符串中找到最长的子串.采用纵向匹配,遇到第一个不匹配的停止. string longestComPrefix(vector<string> &strs) { if (str ...