题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4746

题意:

1≤x,y≤n , 求gcd(x,y)分解后质因数个数小于等k的(x,y)的对数。

分析:

莫比乌斯反演。

还是一个套路,我们设

f(d):满足gcd(x,y)=d且x,y均在给定范围内的(x,y)的对数。

F(d):满足d|gcd(x,y)且x,y均在给定范围内的(x,y)的对数。

显然F(x)=[n/x]∗[m/x],反演后我们得到

f(x)=∑x|dμ(d/x)[n/d]∗[m/d]

最直接的方法,枚举质数p,那么

ans=∑pmin(n,m)(∑dmin(n/p,m/p)μ(d)∗[n/(p∗d)]∗[m/(p∗d)])

这样肯定会超时。

我们令a=p∗d,那么

ans=∑a=1min(n,m)[n/a]∗[m/a]∗∑p|aμ(a/p)

我们希望快速获得每个a对应的∑p|aμ(a/p),由于题目规定了最大的质因子数目,所以我们增加一维,设f[i][j]表示质因子数目小于等于j时 前i项和,根据公式计算即可。

最后我们再取个前缀和就好了。注意这里仍然使用了分段优化。

代码:

/*
-- Hdu 4746
-- Created by jiangyuzhu
-- 2016/5/30
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stack>
using namespace std;
typedef long long ll;
#define sa(n) scanf("%d", &(n))
#define sal(n) scanf("%I64d", &(n))
#define pl(x) cout << #x << " " << x << endl
#define mdzz cout<<"mdzz"<<endl;
const int maxn = 5e5 + 5 ;
int tot = 0;
int miu[maxn], prime[maxn], f[maxn][20 + 5];
int cnt[maxn];
bool flag[maxn];
void mobius()
{
miu[1] = 1;
tot = 0;
for(int i = 2; i < maxn; i++){
if(!flag[i]){
prime[tot++] = i;
miu[i] = -1;
cnt[i] = 1;
}
for(int j = 0; j < tot && i * prime[j] < maxn; j++){
flag[i * prime[j]] = true;
cnt[i * prime[j]] = cnt[i] + 1;
if(i % prime[j]){
miu[i * prime[j]] = -miu[i];
}
else{
miu[i * prime[j]] = 0;
break;
}
}
}
for(int i = 1; i < maxn; i++){
for(int j = i; j < maxn; j += i){
f[j][cnt[i]] += miu[j / i];
}
}
for(int i = 1; i < maxn; i++){
for(int j = 1; j < 20; j++){
f[i][j] += f[i][j - 1] ;
}
}
//前缀和
for(int i = 1; i < maxn; i++){
for(int j = 0; j < 20; j++){
f[i][j] += f[i - 1][j];
}
}
}
int main (void)
{
mobius();
int T;sa(T);
int n, m, k;
for(int kas = 1; kas <= T; kas++){
scanf("%d%d%d", &n, &m, &k);
ll ans = 0;
k = min(k, 19);
int j;
if(n > m) swap(n, m);
for(int i = 1; i <= n; i = j + 1){
j = min(n /(n / i), m / (m / i ));
ans += (n / j) * 1ll * (m / j) * (f[j][k] - f[i - 1][k]);
}
printf("%lld\n", ans);
}
return 0;
}

HDU 4746 Mophues【莫比乌斯反演】的更多相关文章

  1. HDU 4746 Mophues (莫比乌斯反演应用)

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total ...

  2. hdu 4746 Mophues 莫比乌斯反演+前缀和优化

    Mophues 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 有Q组数据:(n, m, ...

  3. HDU 4746 Mophues 莫比乌斯反演

    分析: http://blog.csdn.net/acdreamers/article/details/12871643 分析参见这一篇 http://wenku.baidu.com/view/fbe ...

  4. Mophues HDU - 4746 (莫比乌斯反演)

    Mophues \[ Time Limit: 10000 ms\quad Memory Limit: 262144 kB \] 题意 求出满足 \(gcd\left(a,b\right) = k\), ...

  5. HDU - 4746预处理莫比乌斯反演

    链接 求[1,n] 和 [1,m]中有多少对数的GCD的素因子个数小于等于p 直接暴力做特定超时,所以我们想办法预处理,对于p大于18(1到5e5的最大素数因子个数)的情况,每一对都满足条件,O(1) ...

  6. HDU 4746 Mophues(莫比乌斯反演)题解

    题意: \(Q\leq5000\)次询问,每次问你有多少对\((x,y)\)满足\(x\in[1,n],y\in[1,m]\)且\(gcd(x,y)\)的质因数分解个数小于等于\(p\).\(n,m, ...

  7. hdu.5212.Code(莫比乌斯反演 && 埃氏筛)

    Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

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

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

  9. HDU 1695 GCD 莫比乌斯反演

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

随机推荐

  1. LeetCode(143) Reorder List

    题目 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do ...

  2. poj-1700 crossing river(贪心题)

    题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...

  3. poj 3669 火星撞地球问题 bfs算法

    题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间 思路:bfs+预处理 这题的数据量比较大,所以需要进行预处理 对每个位置设上时间(被撞的最早时间) 未被撞的设为-1 for (int j = ...

  4. luogu2893 [USACO08FEB]修路Making the Grade

    ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...

  5. Mysql - 安装及初始化设置

    1. 下载mysql-5.7.13-tar.gz http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x8 ...

  6. jqery实现一个图标上下滑动效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  7. STP

    生成树协议  spanning-tree protocol     网络中额外添加的链路连接着路由器和交换机 会引起流量的环路   当一个交换机的连接丢失时 另一条链路能快速地取代失败的链路  并且不 ...

  8. [uiautomator篇][exist 存在,但click错误]

    uiautomator定位页面元素是,定位存在的;但是click的时候,发现点的位置不对,(不知道是android系统的问题还是uiautomator的问题,初步怀疑是系统的问题)

  9. Educational Codeforces Round 36 (Rated for Div. 2)

    A. Garden time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  10. 2018"百度之星"程序设计大赛 - 资格赛

    调查问卷  Accepts: 1546  Submissions: 6596  Time Limit: 6500/6000 MS (Java/Others)  Memory Limit: 262144 ...