Mophues

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

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
 
http://blog.csdn.net/wh2124335/article/details/11846661 转载自此
 
记录一下自己的思路
未简化过的代码核心应该是这样的
 
   for(int i=;i<=n;++i)//枚举每个因子
if(d[i]<=k)//如果因子的素数质因子小于等于k
for(int j=i;j<=n;j+=i) ans+=u(j/i)*(n/i)*(m/i)//枚举F(i);

利用的是第二个,然后可以发现,对于每个数字i,他的倍数j的系数都要加上u[j/i],可以与处理出来U(N),其中U(i)就是u[i/第一个因子]+u[i/第二个因子]+....(这里的U先不考虑素因子个数限制)

那么上述式子就可以化简成为

for(int i=;i<=n;++i) ans+=U(i)*(n/i)*(m/i);//直接枚举

然后U(i)考虑素因子个数限制的话,那么显然预处理也是可以搞出来的,详细见代码,代码里的cnt[N][19]就是U考虑限制的。

然后就是普通的分块操作,为了简化时间,因为W=(n/i)*(m/i),i倘若在一定范围内,这个W是不变的,所以可以加速。

所以最后就是这样了

for(int i=,last=i;i<=n;i=last+){
last=min(n/(n/i),m/(m/i));
ans+=(ll)(cnt[last][k]-cnt[i-][k])*(n/i)*(m/i);
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = ;
typedef long long ll;
int mu[maxn],sum[maxn],num[maxn];
ll cnt[maxn][];
bool flag[maxn];
vector<int>prime;
void init(){
mu[]=;
for(int i=;i<maxn;i++){
if(!flag[i]){
prime.push_back(i);
mu[i]=-;
num[i]=;
}
for(int j=;j<prime.size()&&i*prime[j]<maxn;j++){
flag[i*prime[j]]=true;
num[i*prime[j]]=num[i]+;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else {mu[i*prime[j]]=;break;}
}
}
for(int i=;i<maxn;i++){
for(int j=i;j<maxn;j+=i){
cnt[j][num[i]]+=mu[j/i];
}
}
for(int i=;i<maxn;i++){
for(int j=;j<;j++){
cnt[i][j]+=cnt[i][j-];
}
}
for(int i=;i<maxn;i++){
for(int j=;j<;j++){
cnt[i][j]+=cnt[i-][j];
}
}
}
int main(){
init();
int q;
scanf("%d",&q);
while(q--){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
k=min(k,);
ll ans=;
if(n>m)swap(n,m);
for(int i=,last=i;i<=n;i=last+){
last=min(n/(n/i),m/(m/i));
ans+=(ll)(cnt[last][k]-cnt[i-][k])*(n/i)*(m/i);
}
//printf("%lld\n",ans);
printf("%I64d\n",ans);
}
}
 

hdu4746莫比乌斯反演进阶题的更多相关文章

  1. SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)

    Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...

  2. 莫比乌斯反演进阶-洛谷P2257/HDU5663

    学了莫比乌斯反演之后对初阶问题没有任何问题了,除法分块也码到飞起,但是稍微变形我就跪了.用瞪眼观察法观察别人题解观察到主要内容除了柿子变形之外,主要就是对于miu函数的操作求前缀和.进而了解miu函数 ...

  3. hdu4746莫比乌斯反演+分块

    http://blog.csdn.net/mowayao/article/details/38875021 题意: 5000组样例. 问你[1,n] 和 [1,m]中有多少对数的GCD的素因子个数小于 ...

  4. BZOJ1011 莫比乌斯反演(基础题

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1101 [题目大意] 求[1,n][1,m]内gcd=k的情况 [题解] 考虑求[1,n ...

  5. hdu1695莫比乌斯反演模板题

    hdu1695 求1<=i<=n&&1<=j<=m,gcd(i,j)=k的(i,j)的对数 最后的结果f(k)=Σ(1<=x<=n/k)mu[x]* ...

  6. BZOJ 2440 完全平方数 莫比乌斯反演模板题

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2440 题目大意: 求第k个无平方因子的数 思路: 二分答案x,求1-x中有多少个平方因 ...

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

    这道题看巨巨的题解看了好久,好久.. 本文转自hdu4746(莫比乌斯反演) 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<= ...

  8. 【BZOJ2820】YY的GCD(莫比乌斯反演)

    [BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...

  9. 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)

    [UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...

随机推荐

  1. Spring Boot中使用@JsonComponent

    文章目录 序列化 反序列化 在同一个class中序列化和反序列化 Spring Boot中使用@JsonComponent @JsonComponent 是Spring boot的核心注解,使用@Js ...

  2. js 实现淘宝无缝轮播图效果,可更改配置参数 带完整版解析代码[slider.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS写淘宝无缝轮播图效果 需求分析: ...

  3. QML-AES加解密小工具

    Intro 为了解码网课视频做的小工具,QML初学者可以参考一下. 项目地址 Todo 在插入新条目时,ListView不会自动根据section进行重排,因此出现同一个文件夹重复多次的现象.目测强行 ...

  4. 打造更好用的 EF 自动审计

    打造更好用的 EF 自动审计 Intro 上次基于 EF Core 实现了一个自动审计的功能,详细可以参考 https://www.cnblogs.com/weihanli/p/auto-audit- ...

  5. webpack前端构建angular1.0!!!

    webpack前端构建angular1.0 Webpack最近很热,用webapcak构建react,vue,angular2.0的文章很多,但是webpack构建angualr1.0的文章找来找去也 ...

  6. Nginx重写请求后将url?后的参数去除

    2019独角兽企业重金招聘Python工程师标准>>> 使用?结尾     注意,关键点就在于"?"这个尾缀.重定向的目标地址结尾处如果加了?号,则不会再转发传递 ...

  7. Xftp的下载安装,以及如何使用XFtp连接虚拟主机/服务器

    1.下载ftp软件  下载地址: 点我立即下载 2.下载后双击安装  下一步  选择Free for Home/School   然后其他的默认下一步即可 3.打开之前领取的免费一年虚拟主机的网址,登 ...

  8. 一只简单的网络爬虫(基于linux C/C++)————socket相关及HTTP

    socket相关 建立连接 网络通信中少不了socket,该爬虫没有使用现成的一些库,而是自己封装了socket的相关操作,因为爬虫属于客户端,建立套接字和发起连接都封装在build_connect中 ...

  9. 第3章:关系数据库标准语言 SQL

    目录 第3章:关系数据库标准语言 SQL 3.1.SQL概述 3.1.1.历史 3.3.2.SQL语言的功能 3.3.3.SQL的特点 3.3.4.基本概念 3.2.学生-课程数据库 3.3.数据定义 ...

  10. jQuery中操作属性的方法attr与prop的区别

    attr 与 prop 都可以对某个属性进行获取和设置的操作,二者的用法相同: <script src = 'jQuery.js'></script> <script&g ...