刚开始看题,想了一会想到了一种容斥的做法。复杂度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. Linux目录处理命令

    1 创建命令 mkdir  -p  目录名 其中 -p表示递归创建,英文为make directories td@td-Lenovo-IdeaPad-Y410P:~$ mkdir Test 上述命令在 ...

  2. 2017.7.10 Redis报错:DENIED Redis is running in protected mode

    参考来自: java 客户端链接不上redis解决方案 DENIED Redis is running in protected mode 完整错误信息: Caused by: redis.clien ...

  3. 怎么windows10下设置始终以管理员身份运行

    怎么windows10下设置始终以管理员身份运行 学习了:https://jingyan.baidu.com/article/e2284b2b6e6df8e2e7118d7a.html 可以对快捷方式 ...

  4. ListView控件绑定DataSet

    DataSet数据集,数据缓存在客户端内存中,支持断开式连接.   在对DataSet做操作的时候,首先一定要修改其行的状态,然后执行SqlDataAdapter的Update方法,Update方法根 ...

  5. 页面加载后累加,自加1&&判断数字是否为两位数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. python 类特殊成员

    class Foo: def __init__(self,age): self.age=age print('init') def __call__(self): print('call') def ...

  7. SQLite升级数据库:

    SQLiteOpenHelper子类关键代码: SQLite升级数据库: SQLiteOpenHelper子类关键代码: public class MyDataHelper extends SQLit ...

  8. URL相对路径和URL绝对路径

    经常在页面中引用图片,html页面等,自己常常弄错相对路径和绝对路径,今天写下此文总结一下.    直接举例说明吧. 在 D:\例子\html下有这么几个文件和文件夹     1.若引用的资源和本身在 ...

  9. 【Android开发-6】了解内情,我们须要一些调试和測试手段

    前言:人生不可能十全十美,总会有些遗憾存在,经历过遗憾,我们才懂的什么是生活. 程序也一样.追求完美,就必定会有经历bug存在的时候. 经历过不断的bug磨练.我们技术才会不断的成长.对于调试bug, ...

  10. Linux 查看某个程序所在端口的 PID

    lsof -i:8085 加入显示的是2001 kill 2001 就可以杀死程序了