隐藏页面特效

hdu 4746Mophues[莫比乌斯反演]

1|0 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; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6748126.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(354)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示