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. TypeScript 映射类型

    typescript支持定义类型加入推导式后产生新的类型 属性不变 但会改变对象的使用方式 这个是类型Person中加入ReadOnly推导出的新类型 他的属性全部是只读的 这个是推导出部分属性 这是 ...

  2. python-创建一个本地txt文本

    def text_create(name, msg): desktop_path = '/Users/Hou/Desktop/' full_path = desktop_path + name + ' ...

  3. printf函数对参数的计算顺序

    没想到啊,没想到: printf函数对参数的计算顺序是从右往左的! 我不禁想问一句,这么坑爹的事情,书里居然没有写过.还是我看书不仔细,没有找到?(回头,在自己翻翻那本c语言编程) 于是下面的程序结果 ...

  4. matplotlib之设置极坐标的方向

    #!/usr/bin/env python3 #-*- coding:utf-8 -*- ############################ #File Name: polar.py #Auth ...

  5. 日期在苹果手机上显示NaN的处理方法

    注意两点即可: 1.苹果只认识 yyyy/mmmm/dddd/  这类格式的日期 2.如果输出后还要进行处理日期对比,苹果默认会带中文字,如:年月日,需要转成上面1当中的日期格式在转时间戳进行比较 G ...

  6. 微信中调起qq

    http://wpa.qq.com/msgrd?uin={$qq}&menu=yes

  7. 您的位置:首页 » IOS » iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch

    原文地址:http://blog.5ibc.net/p/86562.html 前提:当时看到别人写过这个类似AssistiveTouch的demo,但是有问题,第一改变不了位置.第二切换页面后无法使用 ...

  8. maven+nexus setting.xml配置(收藏)

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  9. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  10. hdu2255 奔小康赚大钱 km算法解决最优匹配(最大权完美匹配)

    /** 题目:hdu2255 奔小康赚大钱 km算法 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 题意:lv 思路:最优匹配(最大权完美匹配) ...