hdu 4746Mophues[莫比乌斯反演]

Mophues

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)
Total Submission(s): 1669    Accepted Submission(s): 675
 

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

Source

2013 ACM/ICPC Asia Regional Hangzhou Online

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018

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


Description(题意):

任何整数C
( C >= 2 )都可以写成素数之积
C = p1×p2×
p3×
... × pk
其中, p1, p2 ... pk 是素数。如
C = 24, 则
24 = 2 ×
2 × 2
× 3,
其中, p1 = p2 = p3 = 2, p4 = 3, k = 4.
给定两整数 P和 C,
若 k<=P ( k是
C的素因子个数),称
C是P的幸运数.
现小X需计算的点对 (a,
b)的个数,其中1<=a<=n
, 1<=b<=m, gcd(a,b)是 P的幸运数
( “gcd”是最大公因数).
注意:因为1无素因子,定义1为任何非负数的幸运数.

Input

首行有一个整数
T,表示有 T 组测试数据.接下来有T行,每行是一种测试数据,含3个非负整数n,
m 与P (n, m, P <= 5×105.
T <=5000).

Output

对每种测试数据,输出对
(a, b)的个数,其中 1<=a<=n , 1<=b<=m,
且 gcd(a,b)
是 P的幸运数.

Sample
Input

2

10 10 0

10 10 1

Sample
Output

63

93

//num[j]记录j的因子数。
//g[j][num[i]]用于计算具有相同个数的素因子的i的?(j/i)之和,
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int M=5e5+5,N=19;
int n,m,p,T,g[M][N],num[M];
int tot,prime[M/3],mu[M];bool check[M];
int calc(int y,int x){
    int res=0;
    while(!(y%x)) y/=x,res++;
    return res;
}
void sieve(){
    n=5e5;mu[1]=1;
    for(int i=2;i<=n;i++){
        if(!check[i]) prime[++tot]=i,mu[i]=-1;
        for(int j=1;j<=tot&&i*prime[j]<=n;j++){
            check[i*prime[j]]=1;
            if(!(i%prime[j])){mu[i*prime[j]]=0;break;}
            else mu[i*prime[j]]=-mu[i];
        }
    }
    for(int i=2;i<=n;i++) if(!num[i]) for(int j=i;j<=n;j+=i) num[j]+=calc(j,i);
    for(int i=1;i<=n;i++) for(int j=i;j<=n;j+=i) g[j][num[i]]+=mu[j/i];
    for(int i=1;i<=n;i++) for(int j=1;j<19;j++) g[i][j]+=g[i][j-1];
    for(int i=1;i<=n;i++) for(int j=0;j<19;j++) g[i][j]+=g[i-1][j];
}
ll solve(int n,int m,int p){
    if(p>=19) return 1LL*n*m;
    if(n>m) swap(n,m);
    ll ans=0;
    for(int i=1,pos=0;i<=n;i=pos+1){
        pos=min(n/(n/i),m/(m/i));
        ans+=1LL*(n/i)*(m/i)*(g[pos][p]-g[i-1][p]);
    }
    return ans;
}
int main(){
    sieve();
    for(scanf("%d",&T);T--;){
        scanf("%d%d%d",&n,&m,&p),
        printf("%I64d\n",solve(n,m,p));
    }
    return 0;
}
时间: 2024-07-29 19:24:16

hdu 4746Mophues[莫比乌斯反演]的相关文章

hdu 1695 莫比乌斯反演

hdu 1695 莫比乌斯反演 题意: 给出a,b,c,d,k, 求满足a <= x <= b && c <= y <= d && gcd(x,y)=k 的数对(x,y)的对数. 限制: a=c=1; 0 < b,c <= 1e5; (n1,n2) 和 (n2,n1) 算为同种情况 思路: 其实是求满足1 <= x <= b/k && 1 <= y <= d/k && gcd(x,y

HDU 1695 (莫比乌斯反演) GCD

题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是不知道怎么应用. 这里有关于莫比乌斯反演的知识,而且最后的例题中就有这道题并给出了公式的推导. 1 #include <cstdio> 2 #include <algorithm> 3 typedef long long LL; 4 5 const int maxn = 1000000

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)

HDU 5382 莫比乌斯反演

题目大意: 求S(n)的值 n<=1000000 这是官方题解给出的推导过程,orz,按这上面说的来写,就不难了 这里需要思考的就是G(n)这个如何利用积性函数的性质线性筛出来 作为一个质数,那么肯定G(i) = 2 1. 那么一个数 i 乘上了一个未出现的素数prime,那么就相当于,在当前符合的因子上面都乘了prime之后依旧符合,而原来 i 对应的数也符合,那么说明翻了两倍 也就是 g(i*prime) = 2*g(i) = g(prime) * g(i) 2. 如果这个乘上的素数prim

hdu1569 莫比乌斯反演

hdu 1695 莫比乌斯反演 给出a,b,c,d,k, 求满足a <= x <= b && c <= y <= d && gcd(x,y)=k 的数对(x,y)的对数. a=c=1; 0 < b,c <= 1e5; (n1,n2) 和 (n2,n1) 算为同种情况 其实是求满足1 <= x <= b/k && 1 <= y <= d/k && gcd(x,y)=1 的 数对(x,y

hdu 1695 容斥原理或莫比乌斯反演

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5310    Accepted Submission(s): 1907 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)

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

E - GuGuFishtion HDU - 6390(欧拉函数 / 莫比乌斯反演)

GuGuFishtion (HDU - 6390) 题意: 定义\(G_u (a,b)=\frac{\phi(ab)}{\phi(a)\phi(b)}\). 求\((\sum\limits_{a=1}^m\sum\limits_{b=1}^nG_u (a,b))\pmod p\). 题解: 考虑\(\phi(x) = x*(1-\frac{1}{p_1})*(1-\frac{1}{p_2})...*(1-\frac{1}{p_n})\). 将\(G_u (a,b)\)的分子与分母按上述分解.约分

HDU - 6715 - 算术 = 莫比乌斯反演

http://acm.hdu.edu.cn/showproblem.php?pid=6715 题意: 求:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\mu(lcm(i,j))\),其中n,m是1e6范围内,10组. 不会,想了很久,也不知道假在哪里.大概是一开始方向就错了. 正解: 所求:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\mu(lcm(i,j))\) 即:\(\sum\limits_{i=1}^