GCD

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

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
 

前几天用容斥原理写过这题:

http://www.cnblogs.com/kuangbin/p/3269182.html

速度比较慢。

用莫比乌斯反演快很多。

莫比乌斯反演资料:

http://wenku.baidu.com/view/542961fdba0d4a7302763ad5.html

http://baike.baidu.com/link?url=1qQ-hkgOwDJAH4xyRcEQVoOTmHbiRCyZZ-hEJxRBQO8G0OurXNr6Rh6pYj9fhySI0MY2RKpcaSPV9X75mQv0hK

这题求[1,n],[1,m]gcd为k的对数。而且没有顺序。

转化之后就是[1,n/k],[1,m/k]之间互质的数的个数。

用莫比乌斯反演就很容易求了。

为了去除重复的,去掉一部分就好了;

这题求的时候还可以分段进行优化的。

具体看我的下一篇博客吧!

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/21 19:32:35
File Name :F:\2013ACM练习\专题学习\数学\莫比乌斯反演\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 = ;
bool check[MAXN+];
int prime[MAXN+];
int mu[MAXN+];
void Moblus()
{
memset(check,false,sizeof(check));
mu[] = ;
int tot = ;
for(int i = ; i <= MAXN; i++)
{
if( !check[i] )
{
prime[tot++] = i;
mu[i] = -;
}
for(int j = ; j < tot; j++)
{
if(i * prime[j] > MAXN) break;
check[i * prime[j]] = true;
if( i % prime[j] == )
{
mu[i * prime[j]] = ;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int a,b,c,d,k;
Moblus();
scanf("%d",&T);
int iCase = ;
while(T--)
{
iCase++;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k == )
{
printf("Case %d: 0\n",iCase);
continue;
}
b /= k;
d /= k;
if(b > d)swap(b,d);
long long ans1 = ;
for(int i = ; i <= b;i++)
ans1 += (long long)mu[i]*(b/i)*(d/i);
long long ans2 = ;
for(int i = ;i <= b;i++)
ans2 += (long long)mu[i]*(b/i)*(b/i);
ans1 -= ans2/;
printf("Case %d: %I64d\n",iCase,ans1);
}
return ;
}

HDU 1695 GCD (莫比乌斯反演)的更多相关文章

  1. hdu 1695 GCD 莫比乌斯反演入门

    GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...

  2. HDU 1695 GCD 莫比乌斯反演

    分析:简单的莫比乌斯反演 f[i]为k=i时的答案数 然后就很简单了 #include<iostream> #include<algorithm> #include<se ...

  3. hdu 1695 GCD 莫比乌斯

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

  4. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  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 (莫比乌斯反演模板)

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

  7. hdu 1695: GCD 【莫比乌斯反演】

    题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...

  8. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  9. ●HDU 1695 GCD

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...

随机推荐

  1. 配置了<mvc:resources> 导致以前的controller 无法访问。

    解决方案: <mvc:annotation-driven/>

  2. 你还记的那一年你我学习的-->>用表组织数据*(数据表)

    不知不觉,踏上IT之路,光阴似箭,日月如梭.虽好像回到从前,回到那个无忧无虑的童年,回到那个花样少年的青春;回到那个年少幼稚的小学;回到那个整天幻想的初中;回到那个顽强不屈,誓死不弃的高中;回到那个整 ...

  3. centos下完全卸载mysql

    版权声明:本文为博主原创文章,未经博主允许不得转载. yum方式安装的MySQL 1.yum remove mysql mysql-server mysql-libs compat-mysql51 2 ...

  4. sessionId在fragment里无法保存的问题

    fragment页面需要验证用户是否登录.若没登陆调用登录页面,后返回fragment. 这个问题解决 sessionId = SharePreferenceUtils.getSessionId(); ...

  5. 一句命令快速合并 JS、CSS

    在项目开发环境下,我们会把 JS 代码尽可能模块化,方便管理和修改,这就避免不了会出现一个项目自身 JS 文件数量达到 10 个或者更多. 而项目上线后,会要求将所有 JS 文件合并为 1 个或者几个 ...

  6. oracle断电重启之ORA-00600[4194]

    1.问题描述 Oracle服务器断电重启以后无法数据库无法正常连接,使用sqlplus envision/envision连接报错.常见的错误有以下这些: ORA-12518: TNS:listene ...

  7. 错误:StrictMode $ AndroidBlockGuardPolicy.onNetwork

    you have to insert 2 lines "StrictMode" on MainActivity Class, example's below: 在onCreate( ...

  8. 【原】PSD图标素材的全自动切图方法,适用于IOS、安卓、web前端等领域

    屌丝个人开发者经常遇到的尴尬问题是,自己不会设计UI素材又请不起专业的美工.最好的方式是去网上下载符合自己需求的素材修修改改直接用上.但是,在这个过程中会发现很多下载下来的素材是PSD格式的,很多图标 ...

  9. CollectionView 简用

    创建一个CollectionView 分为几个步骤 1.先创建布局FlowLayout 设置布局格式 2.创建CollectionView 并使用布局Flowlayout  -initWithFram ...

  10. iOS开发之NSTimer使用初探

    创建一个定时器(NSTimer) - (void)viewDidLoad { [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval: ...