GCD
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): Problem Description
Given 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=, y=) and (x=, y=) are considered to be the same. Yoiu can assume that a = c = 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 , cases.
Each case contains five integers: a, b, c, d, k, < a <= b <= ,, < c <= d <= ,, <= k <= ,, as described above. Output
For each test case, print the number of choices. Use the format in the example. Sample Input Sample Output Case :
Case : Hint
For the first sample input, all the pairs of numbers are (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ). /**
题目:hdu1695 GCD2
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题意:求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
思路:
gcd(x,y)=k => gcd(x/k,y/k) = 1; 则求x/k与y/k互质对数。 即求:[1,b/k]与[1,d/k]之间互质的对数 设x属于[1,b/k], y属于[1,d/k];
枚举x,求x与y互质的对数。所以要预处理所有x的质因子。然后容斥处理。由于(x,y)=>(5,7),(7,5)是同一组。
所以:答案为ans += (d/k) - x在d/k中不互质的数 - (x-1);
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+;
vector<int> prime[maxn];
int flag[maxn];
void init()
{
memset(flag, , sizeof flag);
for(ll i = ; i < maxn; i++){
if(flag[i]==){
prime[i].push_back(i);
for(ll j = *i; j < maxn; j+=i){
prime[j].push_back(i);
flag[j] = ;
}
}
}
}
ll rc(int pos,int n)
{
ll sum = ;
ll mult, ones;
ll len = prime[pos].size();
ll m = <<len;
for(int i = ; i < m; i++){
ones = ;
mult = ;
for(int j = ; j < len; j++){
if(i&(<<j)){
ones++;
mult = mult*prime[pos][j];
if(mult>n) break;
}
}
if(ones%==){
sum -= n/mult-(pos-)/mult;
}else
{
sum += n/mult-(pos-)/mult;
}
}
return n-(pos-)-sum;
}
int main()
{
init();
int T;
int cas = ;
int a, b, c, d, k;
cin>>T;
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==){
printf("Case %d: 0\n",cas++);continue;
}
if(b>d) swap(b,d);///for b<=d;
b = b/k;
d = d/k;
ll ans = ;
if(b>=){
ans += d;
}
for(int i = ; i <= b; i++){
ans += rc(i,d);
}
printf("Case %d: %lld\n", cas++,ans);
}
return ;
}

hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。的更多相关文章

  1. GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.

    /** 题目:GCD 链接:https://vjudge.net/contest/178455#problem/E 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对( ...

  2. hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数

    GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...

  3. 容斥原理 求M以内有多少个跟N是互质的

    开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理!   本题是求[a,b]中与n ...

  4. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

  5. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

  6. ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)

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

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对

    /** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b ...

  9. BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )

    一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) *  2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...

随机推荐

  1. IOS学习笔记39--NSString各种语法的使用

    今天就NSString的各种语法学习学习,以后慢慢补充: 1.字符串的遍历 NSString *string = @"CHENGWULI"; //字符串的长度 int count ...

  2. fl2440 platform总线led字符设备驱动

    首先需要知道的是,设备跟驱动是分开的.设备通过struct device来定义,也可以自己将结构体封装到自己定义的device结构体中: 例如:struct platform_device: 在inc ...

  3. python 推导式(Comprehensions)

    一.介绍 列表推导(list comprehensions) 这是一种将for循环.if表达式以及赋值语句放到单一语句中的一种方法.换句话说,你能够通过一个表达式对一个列表做映射或过滤操作. 一个列表 ...

  4. nginx linux 安装

    nginx linux 安装 进入http://nginx.org/en/download.html 下载 n  gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gc ...

  5. 开源知识库管理系统选型 centos6.4 搭建knowlededgeroot-1.0.4知识库平台

    开源知识库管理系统选型,除了使用wiki外,还有下面可选: http://www.knowledgebase-script.com/ https://github.com/lordlamer/know ...

  6. C语言中的union使用方法

    union共用声明和共用一变量定义: "联合"是一种特殊的类,也是一种构造类型的数据结构.在一个"联合"内能够定义多种不同的数据类型. 一个被说明为该" ...

  7. 入侵者已经拿到了主机的管理员权限,请你列举几种留后门的方法:(windows和LINUX系统均可)

    Webshell后门 XSS后门 远控后门&rootit(windows&LINUX) SSH后门 SHIFT终端服务器后门 系统用户账号克隆 SQL数据库扩展存储型后门 SQL数据库 ...

  8. Django——WEB三层架构与MVC

    而我发此文的目的有二:一者,让初学者能够听到一家之言,是为解惑:二者,更希望抛砖引玉,得到专家的批判. 许多学生经常问我,MVC到底和WEB三层架构有啥关系? 开始时,我也只能给他们一些模糊的回答.时 ...

  9. T420s成功加装固态硬盘(SSD)

    目的 为了提升系统和经常使用工具的启动速度,ThinkPad T420s光驱位加一块固态硬盘. 操作步骤 购买:没做太多对照了解,初步计划是安装在光驱位,直接JD上买了SanDisk的128G和推荐的 ...

  10. 【Python3 爬虫】12_代理IP的使用

    我们在爬取页面的时候,如果长时间使用一个网址去爬取某个网站,就会受爬去限制,此时,我们引用了代理IP,IP随时在变化,也就不会被限制了 一下是国内提供免费代理IP的地址:http://www.xici ...