GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7529    Accepted Submission(s): 2773

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




    题意:输入五个整数a,b,c,d,k。要求从区间[a,b]取出一个x,从区间[c,d]取出一个y,使得GCD(x,y) == k求出有多少种情况,只是注意的是GCD(5,7)与GCD(7,5)是一种。



    思路:将x,y同一时候除以k。就转变成求x,y互质,就能用容斥定理做了。







#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map> #define N 101000 using namespace std; vector<int>q[N];
int num[N];
int a,b,c,d,k; void init(){
for(int i=0;i<=N;i++){
q[i].clear();
}
for(int i=1;i<=100000;i++){
int p = i;
int pi = sqrt(p);
for(int j=2;j<=pi;j++){
if(p%j == 0){
q[i].push_back(j);
while(p%j == 0){
p = p/j;
}
}
}
if(p!=1){
q[i].push_back(p);
}
}
} __int64 IEP(int ii,int pn){
int pt = 0;
__int64 s = 0;
num[pt++] = -1;
for(int i=0;i<q[ii].size();i++){
int l = pt;
for(int j=0;j<l;j++){
num[pt++] = num[j]*q[ii][i]*(-1);
}
}
for(int i=1;i<pt;i++){
s += pn/num[i];
}
return s;
} int main(){
int T;
init();
int kk = 0;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(b>d){
int e = b;
b = d;
d = e;
}
if(k == 0){
printf("Case %d: 0\n",++kk);
continue;
}
b = b/k;
c = b+1;
d = d/k;
__int64 sum = 0;
for(int i=1;i<=b;i++){
sum += b - IEP(i,b);
}
sum = (sum+1)/2;
for(int i=1;i<=b;i++){
sum += d - c + 1 - IEP(i,d) + IEP(i,c-1);
}
printf("Case %d: %I64d\n",++kk,sum);
}
return 0;
}

 

HDU 1695 GCD(容斥定理)的更多相关文章

  1. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  2. hdu 1695 GCD 容斥+欧拉函数

    题目链接 求 $ x\in[1, a] , y \in [1, b] $ 内 \(gcd(x, y) = k\)的(x, y)的对数. 问题等价于$ x\in[1, a/k] , y \in [1, ...

  3. HDU - 1695 GCD (容斥+枚举)

    题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数.(i,j)与(j,i) 算一个. 分析:gcd(i,j)=k可以转化为 ...

  4. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  5. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  6. 【hdu-2588】GCD(容斥定理+欧拉函数+GCD()原理)

    GCD Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  9. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. Virtual Treeview 安装以及入门

    Virtual Treeview是一套Delphi下优秀的VCL控件,代码质量高,使用灵活.功能强大.性能非常好,可以用于表达Treeview和表格类数据.它的代码现在托管在google code上. ...

  2. asp动态数组

    本文所说的 ASP 数组是指在 ASP 中以默认语言 VBScript 为语言的数组. 样例: Dim   MyArray()        for   i   =   0   to   10    ...

  3. Linux下查找命令(收集整理)

    原文:http://blog.csdn.net/sunstars2009918/article/details/8510878 一.Linux查找文件的相关命令 常 用 命 令 简要中文说明 程序所在 ...

  4. Java垃圾回收精粹 — Part2

    Java垃圾回收精粹分4个部分,本篇是第2部分.在第2部分里介绍了Hotspot中的堆结构.对象分配以及次要回收. Hotspot中的堆结构 理解不同的收集器的工作方式,是探讨Java堆结构如何支持分 ...

  5. Neo4j 使用cypher语言进行查询

    Neo4j是一个Java开发的图数据库,它将结构化数据存储在网络(从数学角度叫做图)上而不是表中.相对于关系数据库来说,图数据库善于处理大量复杂.互连接.低结构化的数据,这些数据变化迅速,需要频繁的查 ...

  6. jQuery 回调函数

    jQuery(回调函数) 此函数的作用将callback参数以函数的定义形式,在页面onload的时候进行调用.相当于$(document).ready(callback). <script t ...

  7. nib 加载过程分析以及对File’s Owner的理解

    nib loading的过程,这个是app文档里面有说到资源编程指南 1.  It loads the contents of the nib file and any referenced reso ...

  8. C#使用DirectShow播放视频文件 [转]

    原文网址:http://blog.csdn.net/openzpc/article/details/48442751 最近在开发一个视频播放软件,主要要求就是循环播放多个视频文件,并且要求两个视频文件 ...

  9. JS中字符串的相关操作

    一.字符串的创建 创建一个字符串有几种方法.最简单的是用引号将一组字符包含起来,可以将其赋值给一个字符串变量. var myStr = "Hello, String!"; 可以用双 ...

  10. mysql数据库维护(备份和还原)和性能提高

    为了有效防止数据丢失,并将损失降到最低,应对数据库服务器做维护.数据库维护,包括数据备份,还原,导出和导入操作. 1. MySQL数据库备份 所谓数据库维护,主要包含备份数据,还原数据和数据库迁移,对 ...