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. SQL Server的thread scheduling(线程调度)

      https://www.zhihu.com/question/53125711/answer/134461670 https://www.zhihu.com/question/53125711   ...

  2. MariaDB Audit Plugin 1.2

    下载地址:https://downloads.mariadb.com/enterprise/bbfz-atd2/mariadb-audit-plugin/server_audit-1.2.0.tar. ...

  3. HTTP和HTTPS的区别,以及各自的优缺点

    转自  https://www.cnblogs.com/wqhwe/p/5407468.html 超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容 ...

  4. Singleton 单例模式(懒汉方式和饿汉方式)

    单例模式的概念: 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 关键点: 1)一个类只有一个实例       这是最基本 ...

  5. Git 学习(二)版本库创建

    Git 版本库创建 什么是版本库(repository)? 可理解为文件仓库.由Git管理每个文件的新增.修改及删除,但这个仓库可以追溯历史.可还原至任意历史节点. 版本库创建 创建一个版本库非常简单 ...

  6. nmap速查表v1.0

    基本语法: #nmap [扫描方式] [命令选项] {目标}   扫描目标格式: IPv4 地址: 192.168.1.1IPv6 地址:AABB:CCDD::FF%eth0主机名:www.targe ...

  7. go语言基础之闭包捕获外部变量特点

    1.闭包捕获外部变量特点 示例: package main //必须 import "fmt" func main() { a := 10 str := "mike&qu ...

  8. VC++多线程--进程间通信

    1.邮槽 邮槽是windows系统提供的一种单向通信的机制,邮槽能传输的数据非常小,一般在400k左右. 创建邮槽 HANDLE CreateMailslot( LPCTSTR lpName, //指 ...

  9. 修改SharePoint 2013中Search Topology时遇到的一些问题以及一些Tips

    这次操作在笔者的场中涉及到5台服务器, CA, APP2, APP3, APP4, APP5. 原本只有CA运行着Search Service Applicaiton, 现在想让APP2-5运行这项服 ...

  10. C#中Split用法~字符串分隔

    1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...