HDU4746 Mophues(莫比乌斯反演)

Mophues

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

Total Submission(s): 647    Accepted Submission(s): 263

Problem Description

As we know, any positive integer C ( C >= 2 ) can be written as the multiply of some prime numbers:

C = p1×p2× p3× ... × pk

which p1, p2 ... pk are all prime numbers.For example, if C = 24, then:

24 = 2 × 2 × 2 × 3

here, p1 = p2 = p3 = 2, p4 = 3, k = 4

Given two integers P and C. if k<=P( k is the number of C‘s prime factors), we call C a lucky number of P.

Now, XXX needs to count the number of pairs (a, b), which 1<=a<=n , 1<=b<=m, and gcd(a,b) is a lucky number of a given P ( "gcd" means "greatest common divisor").

Please note that we define 1 as lucky number of any non-negative integers because 1 has no prime factor.

Input

The first line of input is an integer Q meaning that there are Q test cases.

Then Q lines follow, each line is a test case and each test case contains three non-negative numbers: n, m and P (n, m, P <= 5×105. Q <=5000).

Output

For each test case, print the number of pairs (a, b), which 1<=a<=n , 1<=b<=m, and gcd(a,b) is a lucky number of P.

Sample Input

2
10 10 0
10 10 1

Sample Output

63
93

题意: 5000组样例。 问你[1,n] 和 [1,m]中有多少对数的GCD的素因子个数小于p。

思路:

首先考虑一个相对简单的版本: [1,a] 和 [1,b] 有多少对的数  满足GCD <= d

首先定义两个函数:A(a,b,d) 表示 GCD(a,b) = d的对数,B(a,b,d)表示GCD(a,b) 是d的倍数的对数 易得 B(a,b,d) = (a/d)*(b/d) 根据容斥原理:

A(a,b,d) =   a*b + (-1)*B(a,b,2)+ (-1) *B(a,b,3) +(0)*B(a,b,4)+(-1)*B(a,b,5)+(1)*B(a,b,6)...........

B(a,b,i) 前面的系数正是莫比乌斯函数的值。

那么公式可以写成:

A(a,b,1) =  u(1)*B(a,b,1) + u(2)*B(a,b,2) + u(3) *B(a,b,3) + u(4)*B(a,b,4) + u(5)*B(a,b,5) + u(6)*B(a,b,6)...........

A(a,b,2) =  u(1)*B(a,b,2) + u(2)*B(a,b,4) + u(3) *B(a,b,6) + u(4)*B(a,b,8) + u(5)*B(a,b,10) + u(6)*B(a,b,12)...........

A(a,b,3) =  u(1)*B(a,b,3) + u(2)*B(a,b,6) + u(3) *B(a,b,9) + u(4)*B(a,b,12) + u(5)*B(a,b,15) + u(6)*B(a,b,18)...........

A(a,b,4) =  u(1)*B(a,b,4) + u(2)*B(a,b,8) + u(3) *B(a,b,12) + u(4)*B(a,b,16) + u(5)*B(a,b,20) + u(6)*B(a,b,24)...........

答案就是

A(a,b,1)+A(a,b,2)+A(a,b,3)+......A(a,b,d) =   u(1)*B(a,b,1)+(u(1)+u(2))*B(a,b,2) + ....... (u(1)+u(2)+u(3)+u(6))*B(a,b,6)........

可见A(a,b,d) 前的系数为  sigma(u(i)) (i为d的约数) =  C(a,b,d)

然后,这一题还有一个限制条件,就是要使素因子的个数小于等于p,那么我们定义这个函数D(a,b,d,p) 表示B(a,b,d) 前的系数,那么我们只要从C(a,b,d)中选出一些满足条件的系数即可。 用一个数组F[d][cnt] (cnt为素因子个数)记录,数组表示的是d的因子的素因子个数为cnt的影响因子大小。先计算完单个,再计算前缀和(接下来有用)。

接着,我们发现对于某个d,会满足B(a,b,d) = (B,a,b,d+x),而且  这个 x = min(a/(a/d),b/(b/d)) ,那么整个式子的计算会呈现块状,因此计算这个区间的时候可以用前缀和。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 5e5+10;
int mobi[maxn];
int preMobi[maxn][20];
int pricnt[maxn];
bool isPrime[maxn];
vector<int> prime;

void getMobi(){
    memset(isPrime,1,sizeof isPrime);
    memset(pricnt,0,sizeof pricnt);
    mobi[1] = 1;
    for(int i = 2; i < maxn; i++){
        if(isPrime[i]){
            mobi[i] = -1;
            pricnt[i] = 1;
            prime.push_back(i);
        }
        for(int j = 0; j < prime.size() && i*prime[j] < maxn; j++){
            pricnt[i*prime[j]] = pricnt[i]+1;
            isPrime[i*prime[j]] = false;
            if(i%prime[j]==0){
                mobi[i*prime[j]] = 0;
                break;
            }else{
                mobi[i*prime[j]] = -mobi[i];
            }
        }
    }
}
void getpreMobi(){
    memset(preMobi,0,sizeof preMobi);
    for(int i = 1; i < maxn; i++){
        for(int j = i; j < maxn; j += i){
            preMobi[j][pricnt[i]] += mobi[j/i];
        }
    }
    for(int i = 1; i < maxn; i++){
        for(int j = 0; j < 20; j++){
            preMobi[i][j] += preMobi[i-1][j];
        }
    }
    for(int i = 0; i < maxn; i++){
        for(int j = 1; j < 20; j++){
            preMobi[i][j] += preMobi[i][j-1];
        }
    }
}
int n,m,p;
ll ans;
void solve(){
    ans = 0;
    for(int i = 1; i <= n; i++){
        int ed = min(n/(n/i),m/(m/i));
        ans += ll(preMobi[ed][p]-preMobi[i-1][p])*(n/i)*(m/i);
        i = ed;
    }
    cout<<ans<<endl;

}
void init(){
    getMobi();
    getpreMobi();

}
int main(){
    init();
    int ncase;
    cin >> ncase;
    while(ncase--){
        scanf("%d%d%d",&n,&m,&p);
        if(p>=19){
            cout<<(ll)n*m<<endl;
            continue;
        }
        if(n > m) swap(n,m);
        solve();
    }
    return 0;
}
时间: 2024-08-25 12:06:16

HDU4746 Mophues(莫比乌斯反演)的相关文章

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

Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 376 Problem Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of

hdu4746 Mophues 莫比乌斯

/** 题目:hdu4746 Mophues 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4746 题意:求x,y在给定范围内gcd(x,y)分解素因子的个数<=p的对数. (n, m, P <= 5×105. Q <=5000). 思路: f(n)表示给定范围内gcd(x,y)==n的对数. g(n)表示给定范围内gcd(x,y)为n的倍数的对数. f(n) = sigma[n|d]mu[d/n]*g(d) = sigma[n|d]mu[d

HDU 4746 Mophues 莫比乌斯反演

分析: http://blog.csdn.net/acdreamers/article/details/12871643 分析参见这一篇 http://wenku.baidu.com/view/fbe263d384254b35eefd34eb.html 分块看这一篇 #include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #i

HDU 4746 (莫比乌斯反演) Mophues

这道题看巨巨的题解看了好久,好久.. 本文转自hdu4746(莫比乌斯反演) 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 分析:设A(d):gcd(a, b)=d的有多少种      设B(j): gcd(a, b)是j的倍数的有多少种,易知B(j) = (n/j)*(m/j)      则由容斥原理得:(注:不同行的μ是不相同的,μ为莫比乌斯函数)      A(1) = μ(1)*B(1)

hud 4746 莫比乌斯反演

Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)Total Submission(s): 579    Accepted Submission(s): 235 Problem Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of s

bzoj 2820 / SPOJ PGCD 莫比乌斯反演

那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j=1}^{m}[(i,j)=p]&=&\sum_{p=2}^{min(n,m)}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}[i⊥j]\newline&=&\sum_{p=

hdu1695(莫比乌斯反演)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 对于 a, b, c, d, k . 有 x 属于 [a, b],  y 属于 [c, d], 求 gcd(x, y) = k 的 x, y 的对数 . 其中 a = b = 1 . 注意: (x, y), (y, x) 算一种情况 . 思路: 莫比乌斯反演 可以参考一下: http://blog.csdn.net/lixuepeng_001/article/details/5057

算法学习——莫比乌斯反演(1)

.. 省选GG了,我果然还是太菜了.. 突然想讲莫比乌斯反演了 那就讲吧! 首先我们看一个等式-- (d|n表示d是n的约束) 然后呢,转换一下 于是,我们就发现! 没错!F的系数是有规律的! 规律is here! 公式: 这个有什么卵用呢? 假如说有一道题 F(n)可以很simple的求出来而求f(n)就比较difficult了,该怎么办呢? 然后就可以用上面的式子了 是莫比乌斯函数,十分有趣 定义如下: 若d=1,则=1 若d=p1*p2*p3...*pk,且pi为互异素数,则=(-1)^k

bzoj2301 [HAOI2011]Problem b【莫比乌斯反演 分块】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 很好的一道题.首先把每个询问转化为4个子询问,最后的结果就是这四个子询问的记过加加减减,类似二维前缀和.那么问题转化为在1 <= x <= lmtx, 1 <= y <= lmty时gcd(x, y) == k的对数,这个问题在转化一下,转化成1 <= x <= lmtx / k,1 <= y <= lmty / k时x与y互质的对数.莫比乌斯反