GCD

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

Total Submission(s): 7357    Accepted Submission(s): 2698

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]区间里面gcd(x,y)=k的数的对数。

思路:既然是求gcd为k的数的对数,最好还是先将b和d都除以k,这样问题就转化为[1,n]和[1,m]区间里面gcd(x,y)为1 的数的对数。由于题目里已经说明a和c 能够觉得是1,这样就更简单了。

对于一个[1,n]的区间。我们能够用欧拉函数算出总对数。

那么问题就能够分解成2个:
1、在[1,n]上用欧拉函数算出总对数。

2、在[n+1,m]上。计算在[1,n]里面的总对数,能够用容斥原理。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#define min(a,b) a<b?a:b
#define max(a,b) a>b? a:b
#define Max 100005
#define LL __int64
using namespace std;
LL sum[Max],tot;
int p[Max][20];
int num[Max];
void init()
{
sum[1]=1;
for(int i=2;i<Max;i++)
sum[i]=i;
for(int i=2;i<Max;i++)
if(sum[i]==i)
for(int j=i;j<Max;j+=i)
sum[j]=sum[j]/i*(i-1); }
void init2()
{
LL x,k,i,j;
for( i=1;i<=Max;i++)
{
x=i;k=0;
for(j=2;j<=sqrt(i);j++)
{
if(x%j==0){
while(x%j==0)x=x/j;
// p[i].push_back(j);
p[i][num[i]++]=j;
}
}
if(x>1)p[i][num[i]++]=x;
}
}
LL dfs(int n,int b,int x,int k)
{
LL ans=0;
for(int i=x;i<k;i++)
{
ans+=b/p[n][i]-dfs(n,b/p[n][i],i+1,k);
}
return ans;
}
int main()
{
LL T,a,b,c,d,k;
int i,j,t;
init();
init2();
// printf("%I64d %I64d\n",sum[2],sum[3]);
scanf("%I64d",&T);
t=0;
while(T--)
{
tot=0;
t++;
scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&k);
printf("Case %d: ",t);
if(k==0){printf("0\n");continue;}
b=b/k;
d=d/k;
int m;
m=min(b,d);
d=max(b,d);
b=m;
for(i=1;i<=b;i++)
tot=tot+sum[i];
for(i=b+1;i<=d;i++)
{
// printf("%d\n",p[i].size());
tot+=b-dfs(i,b,0,num[i]);
}
printf("%I64d\n",tot);
}
return 0;
}


hdu 1695 GCD (欧拉函数、容斥原理)的更多相关文章

  1. hdu 1695 GCD (欧拉函数+容斥原理)

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

  2. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  3. HDU 1695 GCD (欧拉函数,容斥原理)

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

  4. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

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

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  7. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

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

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

  9. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  10. HDU 1695 GCD (欧拉函数+容斥原理)

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

随机推荐

  1. axios的坑

    1.axios默认发送application/json 格式 https://www.cnblogs.com/qdcnbj/p/8143155.html 资料: https://www.npmjs.c ...

  2. 【BZOJ4487】【JSOI2015】染色问题

    题意: 棋盘是一个n×m的矩形,分成n行m列共n*m个小方格.现在萌萌和南南有C种不同颜色的颜料,他们希望把棋盘用这些颜料染色,并满足以下规定:  1.  棋盘的每一个小方格既可以染色(染成C种颜色中 ...

  3. LightOJ-1138 Trailing Zeroes (III) 唯一分解定理 算n!的某个因数个数

    题目链接:https://cn.vjudge.net/problem/ 题意 找一个最小的正整数n 使得n!有a个零 思路 就是有几个因数10呗 考虑到10==2*5,也就是说找n!因数5有几个 数据 ...

  4. JVM内存管理简单剖析

    Java是一个跨平台语言,屏蔽操作系统的差异,无需关心复杂内存管理,做到编写一次到处运行.其强大的能力源于Java Virtual Machine (虚拟机)默默的付出.代码运行在虚拟机之上,虚拟机运 ...

  5. oracle和mysql的分页

    如果我们是通过JDBC的方式访问数据库,那么就有必要根据数据库类型采取不同的SQL分页语句,对于MySql数据库,我们可以采用limit语句进行分页,对于Oracle数据库,我们可以采用rownum的 ...

  6. 作为一名Android APP开发者的自我总结

    每当接近年尾,最痛苦的工作无疑是写年终总结,写总结的同时不禁感叹这一年过得不容易阿.突然想起这一年也是自己开发Android APP的第一年,于是觉得应该给自己的APP来一个年终总结. 一.开发方面严 ...

  7. [Test] Easy automated testing in NodeJS with TestCafe

    Quickly get up and running with sensible automated testing scenarios written in ES6. Installing and ...

  8. libevent的使用(socket)

    这篇文章介绍下libevent在socket异步编程中的应用.在一些对性能要求较高的网络应用程序中,为了防止程序堵塞在socket I/O操作上造成程序性能的下降,须要使用异步编程,即程序准备好读写的 ...

  9. 改动Android设备信息,如改动手机型号为iPhone7黄金土豪版!

    首先你的手机必需要有ROOT权限,误操作有风险需慎重 请先开启手机的USB调试,防止手机改动后无法启动时导致的无法修复 1.假设你是在手机上改动,直接使用RE文件管理器,编辑/system/build ...

  10. jsp出现错误can not find the tag directory /web-inf/tags

    百度google了一大圈没找到中文答案,无奈之下硬着头皮看了一个英文答案http://stackoverflow.com/questions/11502703/eclipse-can-not-find ...