GCD

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

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

Hint

For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).

 
Source
 
Recommend
wangye
 

题意: 在1~a, 1~b中挑出(x,y)满足gcd(x,y) = k , 求(x,y) 的对数 , a,b<=10^5

思路: gcd(x, y) == k 说明x,y都能被k整除, 但是能被k整除的未必gcd=k  , 必须还要满足

互质关系. 问题就转化为了求1~a/k 和 1~b/k间互质对数的问题

可以把a设置为小的那个数, 那么以y>x来保持唯一性(题目要求, 比如[1,3] = [3,1] )

接下来份两种情况:

1. y <= a , 那么对数就是 1~a的欧拉函数的累计和(容易想到)

2. y >= a , 这个时候欧拉函数不能用了,怎么做?  可以用容斥原理,把y与1~a互质对数问题转换为

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/19 22:08:43
File Name :F:\2013ACM练习\专题学习\数学\HDU\HDU1695GCD.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i%prime[j] == )break;
}
}
}
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][] = ;
if(tmp%prime[i] == )
{
factor[fatCnt][] = prime[i];
while(tmp%prime[i] == )
{
factor[fatCnt][]++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
return fatCnt;
}
int euler[];
void getEuler()
{
memset(euler,,sizeof(euler));
euler[] = ;
for(int i = ;i <= ;i++)
if(!euler[i])
for(int j = i; j <= ;j += i)
{
if(!euler[j])
euler[j] = j;
euler[j] = euler[j]/i*(i-);
}
}
int calc(int n,int m)//n < m,求1-n内和m互质的数的个数
{
getFactors(m);
int ans = ;
for(int i = ;i < (<<fatCnt);i++)
{
int cnt = ;
int tmp = ;
for(int j = ;j < fatCnt;j++)
if(i&(<<j))
{
cnt++;
tmp *= factor[j][];
}
if(cnt&)ans += n/tmp;
else ans -= n/tmp;
}
return n - ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
getPrime();
int a,b,c,d;
int T;
int k;
scanf("%d",&T);
int iCase = ;
getEuler();
while(T--)
{
iCase++;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k == || k > b || k > d)
{
printf("Case %d: 0\n",iCase);
continue;
}
if(b > d)swap(b,d);
b /= k;
d /= k;
long long ans = ;
for(int i = ;i <= b;i++)
ans += euler[i];
for(int i = b+;i <= d;i++)
ans += calc(b,i);
printf("Case %d: %I64d\n",iCase,ans);
} return ;
}

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

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

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

  5. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

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

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

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

  7. HDU 2588 GCD (欧拉函数)

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

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

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

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

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

随机推荐

  1. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

  2. ActiveReports 报表应用教程 (3)---图表报表

    ActiveReports 的图表控件支持绝大多数常用的二维和三维图表类型,包括XY图表和财务图表.通过使用图表控件的定制功能,如修改坐标轴.图注.图例等,用户可以创建任何其所需要的图表效果.用户还可 ...

  3. Titanium开发环境搭建第二个坑

    1. build时总提示  --key-password <keypass> 参数没传,不填又说密码不对,填对了又说没传,应该是ide的问题,暂时不知怎样去设置该命令参数: 2. 继续去T ...

  4. KMP--Cyclic Nacklace

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/D Description CC always be ...

  5. PHP遍历目录四种方法

    学习SPL的时候,遇到了DirectoryIterator这个目录类,谢了一下遍历目录的方法.于是总结一下遍历目录的四种写法 如下: <?php /* * 方法一:利用SPL的目录类,这个很简单 ...

  6. sublimeCodeIntel 的配置

    在项目的根目录目录下建立.codeintel/config 但是在windows 需要进入dos 环境下建立.以点开头的文件夹和文件.资源管理器不允许创建点开头的文件或文件夹,但在命令提示符下是可以的 ...

  7. SQL Server的各种表

    以下表格简便易懂 请认真仔细斟酌! 字符串函数: 字符串函数用于对字符串数据进行处理,并返回一个字符串或者数字. 函数名 描述 例子 CHARINDEX 用来寻找一个指定的字符串在另一个字符串中的起始 ...

  8. ASP.NET MVC自定义AuthorizeAttribute篇知识点讲解—登录限制

    1.前言 a.微软对ASP.NET的开发从WebForm到MVC的转变,已经正式过去5,6个年头,现在WebForm和MVC也都越来越完善,小小算来我也已经工作了将近三年,从大学的时候学习ASP.NE ...

  9. python模块基础之json,requeste,xml,configparser,logging,subprocess,shutil。

    1.json模块 json     用于[字符串]和 [python基本数据类型] 间进行转换(可用于不同语言之前转换),json.loads,将字符串转成python的基本数据类型,json.dum ...

  10. HTML 5 <mark> 标签

    一,定义和用法 <mark> 标签定义带有记号的文本.请在需要突出显示文本时使用 <m> 标签. 二,实例 突出显示部分文本: <!DOCTYPE HTML> &l ...