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. asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法

    MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交

  2. PHP常规模板引擎中与CSS/JSON冲突的解决

    主要针对对象:Smarty/Dwoo 参考:http://developer.51cto.com/art/201009/224929.htm 其实以前都不怎么关注模板引擎,觉得没必要使用.但随着年龄的 ...

  3. Python学习之Python简介

    Python简介 Python的由来 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象.解释型计算机程序设计语言,它是吉多·范罗苏姆(Guide van ...

  4. 利用php实现:当获取的网址不是特定网址时候跳转到指定地址

    这个问题是在百度知道看到的问答,我不懂做,特定去百度了下.然后结合别人获取域名和跳转的知识,综合做了这个功能,以下是实现代码: <?php //获取当前的域名: echo "获取到的域 ...

  5. YII 的基本CURL操作

    Order::model()->updateAll(array('merchant_id'=>'bbb'), "id in ('140868169311','1408690584 ...

  6. ASP.NET Core1.0 带来的新特性

    1.采用新的文件系统,不再通过工程文件(.sln和.csproj)来定义项目文件清单. 解决方案文件还是*.sln,但项目文件变成*.xproj了.在项目文件夹下新增的文件会被自动添加到项目中,不用再 ...

  7. 常用Keytool 命令

    常用Keytool 命令Keytool 是一个JAVA环境下的安全钥匙与证书的管理工具.它管理一个存储了私有钥匙和验证相应公共钥匙的与它们相关联的X.509 证书链的keystore(相当一个数据库, ...

  8. 我的一个javascript项目的重构历程

    一个月前,组内的一个内部使用的浏览器比价插件的前端部分交给我来维护,作为一个老司机我是拒绝的,自己的代码都是坑,还要去给别人填坑,搞笑地说. 呵呵,能拒绝么.... 好好享受吧,骚年...... 第一 ...

  9. SharePoint 更新文档库文档标题(Title)字段

    前言:记录下写代码中遇到的小问题,帮同事写一个批量更新文档库标题字段的小程序,本来以为就Update一下就可以了,10分钟可以搞定.结果10分钟过去了,代码写好了,执行起来不报错,调试也没问题,只是要 ...

  10. Level 4 A10: 飞张?

    看来庄家的红桃2个输张没法解决,只能寄希望于飞K了. 但如果将牌2-2分布,还有更稳的打法.在下面这种东家3张黑桃的情况时,庄家只需垫到红桃2就行了. 如果东家有4张黑桃,那就只有飞红桃K这一条路了.