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×10
5. 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 

 

//Sourcehttp://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.
给定两整数 PC, k<=P ( kC的素因子个数),CP的幸运数.
现小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;
}

 

posted @ 2017-04-22 16:12  神犇(shenben)  阅读(346)  评论(0编辑  收藏  举报