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. 【PHP内存泄漏案例】PHP对象递归引用造成内存泄漏

    [案例一] 作者:老王 如果PHP对象存在递归引用,就会出现内存泄漏.这个Bug在PHP里已经存在很久很久了,先让我们来重现这个Bug,代码如下: <?php class Foo { funct ...

  2. Visual SVN 企业版代码管理平台的建设

    通常需要完整的SVN的代码管理平台系统的搭建,需要安装三个文件,Visual SVN server  , TortoiseSVN, Visual SVN. Visual SVN server  企业版 ...

  3. dwz关闭当前dialog

    首先,前台代码如下: <form method="post" class="pageForm required-validate" onsubmit=&q ...

  4. threading.local()、多线程里全局变量锁

    这个人的系列文章值得一读:http://blog.51cto.com/suhaozhi/category3.html/p2,不过这个系列总共15偏,Python并发入门,有很多文字描述错误,有些道理也 ...

  5. 树莓派安装 Nginx + PHP7.0 + Pi Dashboard

    之前我们介绍过树莓派搭建LNMP环境的方法,以及给树莓派装一个仪表盘来监控树莓派运行状态.近期有用户反馈树莓派最新版的系统已经无法找到 PHP5 的软件包了,这是因为新版本已经用 PHP7 替代了 P ...

  6. hdu2175之找规律

    汉诺塔IX Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  7. C#转义字符[转]

    C#转义字符: 一种特殊的字符常量 以反斜线"\"开头,后跟一个或几个字符 具有特定的含义,不同于字符原有的意义,故称“转义”字符. 主要用来表示那些用一般字符不便于表示的控制代码 ...

  8. .NET中的六个重要概念

    内容导读 概述 当你声明一个变量背后发生了什么? 堆和栈 值类型和引用类型 哪些是值类型,哪些是引用类型? 装箱和拆箱 装箱和拆箱的性能问题 一.概述 本文会阐述六个重要的概念:堆.栈.值类型.引用类 ...

  9. Javaee项目经验须知

    Java的主要应用领域就是企业级的项目开发!具体要点(09年,那一年我去面试,被拒了几次,想起来还不错!他锻炼了我的心理素质,让我体会到很多,笑一个吧!): 1.掌握项目开发的基本步骤 2.具备极强的 ...

  10. 开源力量:微软竟开源 PowerShell

    导读 曾经有段时间,微软称 Linux 是“癌症”,但是随着时光流逝,现在微软已经认识到了开源世界的不断增长,除了在这个领域加大投入之外别无选择.微软已经启动了几个开源项目,希望能吸引一些 Linux ...