刚开始看题,想了一会想到了一种容斥的做法。复杂度O( n(3/2) )但是因为题目上说有3000组测试数据,然后吓尿。完全不敢写。 然后想别的方法。

唉,最近精神有点问题,昨天从打完bc开始想到1点多,没想到什么好的方法,然后躺床上睡不着,迷迷糊糊又好像挺清醒的,大概想到了用莫比乌斯反演的一种解法,初略的证明了一下发现应该是对的,然后才逐渐有困意,大概也快天亮了。。。 这种事发生了好几次了。上次在证明莫比乌斯反演的时候也是想到快5点才想出来。 感觉整个人都不好了。。

题目: 求在区间[1,b]和[1,d]中各选一个数,使得这两个数的gcd为k,问有多少种选法。

稍微推理下问题可以变为:在区间[1,b/k]和[1,d/k]中选两个gcd为1的数。

设b1=b/k,d1=d/k,假设b1<d1 (b1>b1时swap一下就好了)

F(x) 表示从区间[1,b1/x]和区间[1,d1/x]中任意选两个数,有多少选数的方法,其实就是(b1/x)*(d1/x)了。

f(y)  表示从区间[1,b1]和区间[1,d1]中选两个数,使得这两个数的gcd为y的所有种选法。

那么就可以得到:

F(1)=f(1)+f(2)+...+f(b1)

F(2)=f(2)+f(4)+...+f( (b1/2)*2 )

F(3)=f(3)+f(6)+...+f( (b1/3)*3 )

...

F(b1)=f(b1)

然后莫比乌斯函数miu(n)为最经典的莫比乌斯函数。

if n== 1

  miu(n)=1

else

if n只由不重复的素数构成

{

  if(不重复的素数个数为偶数) miu(n)=1;

  else miu(n)=-1;

}

else

  miu(n)=0

//其实这个只要懂了莫比乌斯反演的原理,还是很好理解的。

有了整个主题思维后,

f(1)=miu(1)*F(1)+miu(2)*F(2)+...+miu(b1)*F(b1)

因为F(x)是显而易见的,我当时一直在以往的因子和里面纠结着,以为莫比乌斯只能应用于求因子的积性函数中。其实莫比乌斯的应用远不如此。要用莫比乌斯的关键是如何找到一个很容易得到F(X)。

得到了f(1)之后还需要去重复,这个就好弄多了。

得到1-b1中所有数的欧拉函数之和sum,f(1)-sum+1即为最后的答案。

详细的见代码:

//
// main.cpp
// hdu1695
//
// Created by 陈加寿 on 15/12/13.
// Copyright (c) 2015年 陈加寿. All rights reserved.
// #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 100100 int miu[N];
long long sum[N]; int phi[N];
void getphis(int maxn)
{
phi[]=;
phi[]=;
for(int i=;i<=maxn;i++) phi[i]=i;
for(int i=;i<=maxn;i+=) phi[i]/=;
for(int i=;i<=maxn;i+=)
{
if(phi[i]==i)//为素数
{
for(int j=i;j<=maxn;j+=i)
{
phi[j]=phi[j]-phi[j]/i; }
}
}
} int main() {
miu[]=;
for(int i=;i<N;i++)
{
int ti=i;
int tcnt=;
for(int j=;j*j<=ti;j++)
{
if(ti%j==)
{
ti/=j;
tcnt++;
if(ti%j==)
{
tcnt=-;
miu[ i ]=;
break;
}
}
}
if(tcnt!=-)
{
if(ti>)
{
tcnt++;
}
miu[i] = tcnt%==?:-;
}
}
getphis(N-);
sum[]=;
for(int i=;i<N;i++)
sum[i] += sum[i-]+phi[i]; int tt=;
int T;
cin>>T;
while(T--)
{
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("Case %d: ",tt++);
if(k==)
{
//这个是什么鬼。
printf("0\n");
continue;
} b/=k;
d/=k; if(b== || d==)
{
printf("0\n");
continue;
}
if(b>d) swap(b,d);
long long ans=;
for(int i=;i<=b;i++)
{
ans += miu[i]*( (long long)(b/i)*(d/i) );
}
ans -= sum[b];
cout<<ans+<<endl;
}
return ;
}

GCD

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

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
 

hdu1695(容斥 or 莫比乌斯反演)的更多相关文章

  1. BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)

    2005: [Noi2010]能量采集 Time Limit: 10 Sec  Memory Limit: 552 MBSubmit: 4493  Solved: 2695[Submit][Statu ...

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

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

  3. 【容斥原理,莫比乌斯反演】用容斥替代莫比乌斯反演第二种形式解决gcd统计问题

    名字虽然很长.但是其实很简单,对于这一类问题基本上就是看你能不能把统计的公式搞出来(这时候需要一个会推公式的队友) 来源于某次cf的一道题,盼望上紫的我让潘学姐帮我代打一道题,她看了看跟我说了题解,用 ...

  4. 【CF900D】Unusual Sequences 容斥(莫比乌斯反演)

    [CF900D]Unusual Sequences 题意:定义正整数序列$a_1,a_2...a_n$是合法的,当且仅当$gcd(a_1,a_2...a_n)=x$且$a_1+a_2+...+a_n= ...

  5. 洛谷P4318 完全平方数(容斥,莫比乌斯反演)

    传送门 求第$k$个没有完全平方数因数的数 一开始是想筛一波莫比乌斯函数,然后发现时间复杂度要炸 于是老老实实看了题解 一个数的排名$k=x-\sum_{i=1}^{x}{(1-|\mu(i)|)}$ ...

  6. ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)

    GCD Expectation Time Limit: 4 Seconds     Memory Limit: 262144 KB Edward has a set of n integers {a1 ...

  7. BZoj 2301 Problem b(容斥定理+莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 7732  Solved: 3750 [Submi ...

  8. 【HDU1695】GCD(莫比乌斯反演)

    [HDU1695]GCD(莫比乌斯反演) 题面 题目大意 求\(a<=x<=b,c<=y<=d\) 且\(gcd(x,y)=k\)的无序数对的个数 其中,你可以假定\(a=c= ...

  9. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

随机推荐

  1. 6、Python模块

    最常用的两个模块: os    #可以允许python调用执行系统命令,如shell sys    #处理与python程序本身的事情   Python自带200多个常用模块 Python官网收集了2 ...

  2. django验证码django-simple-captha

    搭建网站很经常要用到验证码,django中就有这样的中间件django-simple-captha githup地址https://github.com/mbi/django-simple-captc ...

  3. 关于各浏览器的cookie上限

    IE6~IE6以下,每个域名最多20个cookie IE7及以上,每个域名最多50个cookie Firefox,每个域名最多50个cookie Opera,每个域名最多30个cookie Safar ...

  4. 遍历Enumeration

    版权声明:http://blog.csdn.net/qq924862077/ Enumeration(枚举)接口的作用和Iterator类似,只提供了遍历Vector和HashTable类型集合元素的 ...

  5. Hadoop部署记录

    1.准备Linux环境 1.0先将虚拟机的网络模式选为NAT 1.1修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=node1 ### ...

  6. ITIL,是否已是昨日黄花

    首先声明自己不是ITIL方面的专家,特别是具体的规范细节,后面论述如有不当,请指正.但我为什么会提起它?主要是因为它和运维(IT服务管理)相关性太大了.早起的运维完全就是以ITIL来蓝本构建的,在当时 ...

  7. Java基础学习过程

    转载:http://blog.csdn.net/scythe666/article/details/51699954JVM 1. 内存模型( 内存分为几部分? 堆溢出.栈溢出原因及实例?线上如何排查? ...

  8. Linux Java开发环境

    一.旧版本JDK卸载 1.卸载系统自带JDK版本 #rpm -qa|grep gcj 查看到如下信息,如图所示:   进行卸载默认安装JDK: #rpm -e --nodeps java-1.4.2- ...

  9. Laravel之加密解密/日志/异常处理及自定义错误

    一.加密解密 1.加密Crypt::encrypt($request->secret) 2.解密try { $decrypted = Crypt::decrypt($encryptedValue ...

  10. lodash 数组裁剪 drop

    _.drop(array, [n=1]) 裁剪数组中的前 N 个数组,返回剩余的部分. <!DOCTYPE html> <html lang="zh"> & ...