GCD

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

Total Submission(s): 9046    Accepted Submission(s): 3351

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~n之间,y在1~m之间的gcd(x,y)=k; 就相当于求x在1~n/k之间,y在1~m/k之间的gcd(x,y)=1;即x,y互质的对数 对于欧拉函数,可以求比n小的和n互质的个数。 而容斥原理可以求1~指定范围,和n互质的个数。 所以我们枚举一个区间的数,然后求这个数在另一个区间的互质的个数。 容斥原理可以解决,但是为了学习熟悉欧拉函数,所以可以分成两段,一段用欧拉函数,另一段用容斥原理。 求解欧拉函数,可以用线性素数晒求解,这样同时打了一个素数表,为容斥原理服务
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <bitset> using namespace std;
typedef long long int LL;
#define MAX 1000000
bool check[MAX+5];
LL fai[MAX+5];
LL prime[MAX+5];
LL sprime[MAX+5];
LL q[MAX+5];
int cnt;
void eular()//线性筛求解欧拉函数
{
memset(check,false,sizeof(check));
fai[1]=1;
int tot=0;
for(int i=2;i<=MAX+5;i++)
{
if(!check[i])
{
prime[tot++]=i;
fai[i]=i-1;
}
for(int j=0;j<tot;j++)
{
if(i*prime[j]>MAX+5) break;
check[i*prime[j]]=true;
if(i%prime[j]==0)
{
fai[i*prime[j]]=fai[i]*prime[j];
break;
}
else
{
fai[i*prime[j]]=fai[i]*(prime[j]-1);
}
}
}
}
void Divide(LL n)//分解质因子
{
cnt=0;
LL t=(LL)sqrt(1.0*n);
for(LL i=0; prime[i]<=t; i++) {
if(n%prime[i]==0) {
sprime[cnt++]=prime[i];
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n>1)
sprime[cnt++]=n;
}
LL Ex(LL n)//容斥原理之队列实现
{ LL sum=0;
LL t=1;
q[0]=-1;
for(LL i=0; i<cnt; i++) {
LL x=t;
for(LL j=0; j<x; j++){
q[t]=q[j]*sprime[i]*(-1);
t++;
}
}
for(LL i=1; i<t; i++)
sum+=n/q[i];
return sum;
}
int main()
{
int t;
scanf("%d",&t);
eular();
int cas=0;
int a,b,c,d,k;
while(t--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==0||k>b||k>d)
{
printf("Case %d: 0\n",++cas);
continue;
}
if(b>d) swap(b,d);
b/=k;d/=k;
LL ans=0;
for(int i=1;i<=b;i++)
ans+=fai[i];
for(int i=b+1;i<=d;i++)
{ Divide(i);ans+=(b-Ex(b));}
printf("Case %d: %lld\n",++cas,ans);
}
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 欧拉函数+容斥定理

    输入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 和 ...

  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 2588 GCD (欧拉函数)

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

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

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

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

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

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

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

随机推荐

  1. C# nosql之redis初体验

    Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是: https://github.com/MS ...

  2. 解决Janusgraph索引状态不变更的问题

    JanusGraph的索引因为要同步不同实例及不同后端的数据,因此不是实时能够完成的,视配置,网络和数据量不同,建立/生效索引通常需要一段时间,这也是为什么创建索引时会创建wait()的原因. 在实践 ...

  3. Oracle Restart能够用来给Oracle GoldenGate 做 High Availability 使用么?

    Oracle Restart能够用来给Oracle GoldenGate  做 High Availability 使用么? 来源于: Can Oracle Restart be used with ...

  4. 多线程-synchronized

    引言 synchronized是Java线程同步中的一个重要的概念,synchronized是独占锁(互斥锁),同时也是可重入锁(可重入锁一定程度上避免了死锁的问题,内部是关联一个计数器,加一次锁计数 ...

  5. Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案

    Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案 1. IE8.0 显示本地图片 img.src=本地图片路径无效的解决方案1 1.1. div来完成  ...

  6. 所需即所获:像 IDE 一样使用 vim

    所需即所获:像 IDE 一样使用 vim 转载 yangyangwithgnu@yeah.net2015-11-08 10:05:53 谢谢 捐赠:支付宝 yangyangwithgnu@yeah.n ...

  7. 阿里大鱼短信接口(Python3版)

    近期由于须要用到短信接口,选型的的结果是用阿里大鱼的短信服务,然而淘宝开放平台(TOP)的SDK已经非常多年没有更新了.不支持python3.自己动手改了半天,还是不太正常,索性不用它,自己写一个算了 ...

  8. Java序列化与反序列化学习(三):序列化机制与原理

    Java序列化算法透析 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的 过程.Java序列化API提供 ...

  9. 清理iOS中的“其他”空间垃圾文件

    关于如何清理 iOS 里的"其他"空间的教程,网上搜索那是一大堆,不过都是对于2010年某坛某篇"技术文"的无数次简单复制粘帖,可行性已经被各路尝试者们踩到了地 ...

  10. android跨进程通信(IPC)——AIDL

    转载请标明出处: http://blog.csdn.net/sinat_15877283/article/details/51026711: 本文出自: [温利东的博客] 近期在看 @任玉刚 大神编写 ...