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. webstorm keymap

    http://www.jetbrains.com/webstorm/documentation/WebStorm_ReferenceCard.pdf

  2. jmeter 中使用ServerAgen链接超时可能出错的原因之一ip不对

    因为我要压测的服务器是需要使用跳板机转发链接的,所以我开始用的是跳板机的IP+ServerAgen端口,发现连不通,实际上应该使用ServerAgen所在服务器的IP,如果:

  3. 自定义android ProgressDialog

    Android系统自己提供的UI的都是比较难看的,开发中经常用到自定义对话框,下面分享个最近项目中使用的加载框.   下面是源代码,主要的原理就是准备几个图片,然后循环播放. MainActivity ...

  4. SVN图形客户端上传静态库.a文件失败

    1.原因客户端未添加静态库 2.解决办法 到项目静态库所在目录用命令行添加静态库文件 svn add ****.a 3.可能存在问题 Mac OS的自带SVN版本过低不能添加,报错如下: svn: E ...

  5. 原生ajax封装,包含post、method方式

    原生ajax封装,包含post.method方式 function ajax(method, url, data, success) { var xhr = null; try { xhr = new ...

  6. Android Touch事件派发流程源码分析

    分native侧事件派发到java侧和Framework派发事件到UI,流程看源码即可,此处不赘叙, Native侧派发事件的干活类图如下:

  7. ANR触发原理(what triggers ANR?)

    Ref: http://developer.android.com/training/articles/perf-anr.html http://stackoverflow.com/questions ...

  8. Chapter 3 Phenomenon——9

    "You were over there," I suddenly remembered, and his chuckle stopped short. “你之前不在这里”我突然记 ...

  9. ActiveMQ HelloWorld入门

    在P2P的消息模型中,双方通过队列交流,一个队列只有一个生产者和一个消费者.a.消息生产者 package com.ljq.durian.test.activemq; import javax.jms ...

  10. k折交叉验证

    原理:将原始数据集划分为k个子集,将其中一个子集作为验证集,其余k-1个子集作为训练集,如此训练和验证一轮称为一次交叉验证.交叉验证重复k次,每个子集都做一次验证集,得到k个模型,加权平均k个模型的结 ...