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. C#按回车Enter使输入焦点自动跳到下一个TextBox的方法收集

    在录入界面中,用户往往需要按回车键时光标自动跳入下一个文本框,以方便录入操作.在C#中实现该功能有多种方法,以下是小编收集的不使用TAB键,而直接用回车键将光标转到下一个文本框的实现方法. 一.利用W ...

  2. 用Perl编写Apache模块续二 - SVN动态鉴权实现SVNAuth 禅道版

    代码地址:https://code.csdn.net/x3dcn/svnauth 以禅道项目管理系统的数据库结构为标准,实现了可用的svn authz验证功能. 以用户名.密码.项目的acl开发程度o ...

  3. [moka同学笔记]yii2.0缓存

    1.控制器中CacheDemoController.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/06/29 ...

  4. 中国快递包裹总量的预测-基于SARIMA模型

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  5. C#内存占用大量资源的解决办法

    昨天,独立完成了一个项目,一直运行起,起初运行内存为15Mb左右,但是发现内存以每秒2Mb的速度增加,吓了我一跳(注:我实习生,我的工作中第一个项目).从头找寻对象,再来dispose,弄得我晕头转向 ...

  6. eclipse:File->New没有Android Application Project的解决办法

    我的Eclipse版本是:Kepler Service Release 1,截图: 解决步骤: 1.单击Window,选择Customize Perspective,如图: 2.勾选Android A ...

  7. 对抗静态分析——运行时修复dex

    对抗静态分析——运行时修复dex   本文来源:i春秋社区-分享你的技术,为安全加点温度 零.写在前面   这个系列本来题目想写对抗反编译,可是想想对抗反编译的这个范围有点大,总结如下 灵魂作图   ...

  8. SharePoint 2013 手动删除爬网项目

    本文介绍如何手动删除某些搜索项目,其实删除搜索项目并不常用,主要还是在刚刚完成爬网,就删除了某些项目,然后有比较敏感需要马上删除的时候.下面,就跟着图文简单了解下手动删除已爬网的项目吧. 1.配置好搜 ...

  9. 基础学习day06---面向对象二---static,类的初始化和调用顺序、单例模式

    一.static关键字 1.1.static关键字 静态:static用法:是一个修饰符,用于修饰成员(成员变量,成员函数)static 修饰的内容,所有对象共享当成员被静态修饰后,就多了一个调用方式 ...

  10. IOS 网络浅析(一 网络监测~Reachability)

    网络监测应用于各种需要连接网络的app设计,由于现在开发的app几乎都用到网络,因此,网络监测也成为了较为重点的知识,下面我给大家简单讲解一下网络监测的实际应用,依旧会有代码哦. 想要实现网络监测,可 ...