GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4141    Accepted Submission(s): 1441

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
 /*
题意:区间x属于[1,A] , y属于区间[1,B]
求最大公约数是K,即gcd(x,y)=K。
并且[1,3]和[3,1]属于同一种情况。 思路:HDU 4135 Co-prime 的思路在这一题有用。
它的题意:区间[A,B],与整数N的互素的个数
对于这一到题目:gcd(x,y)=k.
要满足最大公约数是K,可以转化为
[1,A],[1,B]==>[1,A/K],[1,B/K] 求互素的个数。
好像有点难以想到。????
{{
借鉴一下别人是说法。会更明白
gcd(x, y) == k 说明x,y都能被k整除,但是能被k整除的未必gcd=k ,
必须还要满足互质关系.
问题就转化为了求1~a/k 和 1~b/k间互质对数的问题
}}
这样的话,如何处理呢?
题意要求[1,3]和[3,1]不能重复。
对于区间[1,A/K],[1,B/K] 看成==>[1,a],[1,b] 有几种情况
1____________a
1____________________b 1____________a
1________b 1____________a
1____________b 这三种情况。我们来个判断,总是让a<=b,用b做更大的值。就会变成 1—————————a
1—————————————————b
在求取的过程中也是采取这样的规则。
[?,b1];确定后一位数。表示在[1,a]中与b1互质的个数。
那么就很好的避免了[1,3],[3,1]的情况了。
求取总和sum=sum1+sum2;
sum1=欧拉函数值[1,a]; 想想为什么?
sum2={枚举a+1--->b,与区间[1,a]互质的个数};
sum2就和以前的一题有关系了,要用欧拉函数+容斥定理处理。
具体的参考:http://www.cnblogs.com/tom987690183/p/3246197.html */ #include<stdio.h>
#include<string.h>
#include<stdlib.h> int prime[],len;
bool s[];
int opl[];
int Que[];
int f[],flen; void make_prime() //素数打表
{
int i,j;
len=;
for(i=;i<=;i++)//刚开始写错,i*i<=100000;⊙﹏⊙b汗
if(s[i]==false)
{
prime[++len]=i;
for(j=i*;j<=;j=j+i)
s[j]=true;
}
} void make_Euler() //欧拉函数打表。
{
int i,j;
make_prime();
for(i=;i<=;i++)
opl[i]=i;
opl[]=;
for(i=;i<=len;i++)
for(j=prime[i];j<=;j=j+prime[i])
opl[j]=opl[j]/prime[i]*(prime[i]-);
} void make_dEuler(int n) //单点欧拉的素因子。
{
int i;
flen=;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
while(n%i==)
n=n/i;
f[++flen]=i;
}
}
if(n!=)
f[++flen]=n;
} int Capacity(int m)
{
int i,j,t=,sum=,k;
Que[t++]=-;
for(i=;i<=flen;i++)
{
k=t;
for(j=;j<k;j++)
Que[t++]=-*Que[j]*f[i];
}
for(i=;i<t;i++)
sum=sum+m/Que[i];
return sum;
} void sc()//输出函数,测试用的。
{
int i;
for(i=;i<=;i++)
printf("%d ",opl[i]);
printf("\n");
} __int64 make_ini(int b,int c,int k)
{
int i,x,y,tmp;
__int64 sum=;
x=b/k;y=c/k;//加特判的用处。不能除0
if(x>y)
{
tmp=x;
x=y;
y=tmp;
}
for(i=;i<=x;i++)
sum=sum+opl[i];//第一步
for(i=x+;i<=y;i++)//第二步,枚举
{
make_dEuler(i);
sum=sum+(x-Capacity(x));
}
//sc();
return sum; } int main()
{
int T,a,b,c,d,k,i;
__int64 sum;
make_Euler();
while(scanf("%d",&T)>)
{
for(i=;i<=T;i++)
{
sum=;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==) //特判,否则会Runtime Error (INTEGER_DIVIDE_BY_ZERO)
{
sum=;
}
else sum=make_ini(b,d,k);
printf("Case %d: %I64d\n",i,sum); }
}
return ;
}
下面再介绍一种方法。莫比乌斯反演
GCD(a,b) = d;
可以转化为
GCD(a/d,b/d) = 1;
设f(d)为(a,b) = d的种类数
   F(d)为(a,b) = d 的倍数 的种类数。
例如
F(2) = (a/2)*(b/2);
F(3) = (a/3)*(b/3);
mu[i]可以打表求出。
关于一个优化在于a/i = a/(i+k) && b/i = b/(i+k);
此时我们能节省时间来求。详见代码部分
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL; const int maxn = 1e5+;
bool s[maxn];
int prime[maxn],len = ;
int mu[maxn];
int sum1[maxn];
void init()
{
memset(s,true,sizeof(s));
mu[] = ;
for(int i=;i<maxn;i++)
{
if(s[i] == true)
{
prime[++len] = i;
mu[i] = -;
}
for(int j=;j<=len && (long long)prime[j]*i<maxn;j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=)
mu[i*prime[j]] = -mu[i];
else
{
mu[i*prime[j]] = ;
break;
}
}
}
for(int i=;i<maxn;i++)
sum1[i] = sum1[i-]+mu[i];
}
LL solve(int a,int b)
{
LL sum = ;
for(int i=,la = ;i<=a;i++,i = la+)
{
la = min(a/(a/i),b/(b/i)); //优化部分
sum = sum + ((LL)(a/i))*(b/i)*(sum1[la]-sum1[i-]);
}
return sum;
}
int main()
{
int T,l,a,b,d;
init();
scanf("%d",&T);
for(int t=;t<=T;t++)
{
scanf("%d%d%d%d%d",&l,&a,&l,&b,&d);
LL sum = ;
if(d==) ;
else{
if(a>b) swap(a,b);
sum = solve(a/d,b/d);
sum = sum - solve(a/d,a/d)/;
}
printf("Case %d: %I64d\n",t,sum);
}
return ;
}
 

HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演的更多相关文章

  1. 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 和 ...

  2. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  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 (欧拉函数+容斥原理)

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

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

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

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

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

  7. hdu 1695 GCD(欧拉函数+容斥)

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

  8. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  9. HDU 2588 GCD (欧拉函数)

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

随机推荐

  1. CentOS7 - 给VMwear Workstation 15安装VMwear tools

    操作系统:CentOS 7 VMwear Workstation :15 Pro 最简单方法,打开shell,输入下面命令: yum install open-vm-tools -y 参考: http ...

  2. Python3.5 学习八

    #动态导入 官方建议import importlibimport_str="lib.aa"lib=importlib.import_module(import_str)obj=li ...

  3. 670. Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  4. Codeforces Round #452 (Div. 2) C. Dividing the numbers(水)

    C. Dividing the numbers Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in t ...

  5. Spring常用知识点总结

    1. Spring有哪些优点? 轻量级:Spring在大小和透明性方面绝对属于轻量级的,基础版本的Spring框架大约只有2MB. 控制反转(IOC):Spring使用控制反转技术实现了松耦合.依赖被 ...

  6. day 31 html(二) 和css入门

    前情提要: 本次主要是继续昨天学的简单的html 补充以及 css的简单入门 一:表单标签 >1:get请求 <!DOCTYPE html> <html lang=" ...

  7. selenium+java iframe定位

      关于 driver.switchTo().frame(参数).这中间的参数表达有以下几种方式. driver.switchTo().frame(0):用<iframe>标签的位置数量来 ...

  8. Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现

    转载请注明出处:http://www.cnblogs.com/bethansy/p/6953625.html LPA算法的思路: 首先每个节点有一个自己特有的标签,节点会选择自己邻居中出现次数最多的标 ...

  9. Ubuntu 连接手机 不识别设备 -- 解决办法

    1.usb线连接手机,输入命令 $ lsusb Bus 004 Device 002: ID 8087:8000 Intel Corp. Bus 004 Device 001: ID 1d6b:000 ...

  10. (转)Python数据分析之numpy学习

    原文:https://www.cnblogs.com/nxld/p/6058572.html https://morvanzhou.github.io/tutorials/data-manipulat ...