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. day 93 Restframwork

    苑昊博客: http://www.cnblogs.com/yuanchenqi/articles/7570003.html  一.queryset 特性 from django.db import m ...

  2. Mysql 练习题一

    库操作: 1. 创建 数据库  create database db1; 2. 使用数据库 use db1 3. 查看表  show tables; 4. 删除  drop database db1  ...

  3. 《Python黑帽子:黑客与渗透测试编程之道》 Windows下木马的常用功能

    有趣的键盘记录: 安装pyHook: http://nchc.dl.sourceforge.net/project/pyhook/pyhook/1.5.1/pyHook-1.5.1.win32-py2 ...

  4. Minimax-486. Predict the Winner

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  5. ng的点滴记录

    1,directive http://damoqiongqiu.iteye.com/blog/1917971/ 2,constructor  https://segmentfault.com/q/10 ...

  6. Myeclipse中java项目转成Web项目

    在eclipse导入一个myeclipse建的web项目后,在Eclipse中显示的还是java项目,按下面的步骤可以将其转换成web项目. 1.找到项目目录下的.project文件 2.编辑.pro ...

  7. servlet中request和response

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  8. 把本地git仓库的项目上传到远程仓库

    之前在学校实验室服务器上建了一个git远程仓库,存放我写的express项目代码.后来由于出去实习,就无法访问那个远程仓库了,因为它在校园网内. 还好我的笔记本中有这个项目完整的本地仓库,于是我就试着 ...

  9. mysql层的内存分配

    参考 http://www.cnblogs.com/justfortaste/p/3198406.html http://m.blog.csdn.net/blog/IT_PCode/17007833 ...

  10. Maven 上传本地包到仓库 (来源于同事(gagahjt)的笔记本)

    1:将本地jar包导入到自己的Maven仓库 mvn install:install-file -Dfile=D:\\kaptcha-2.3.2.jar -DgroupId=com.google -D ...