当前位置: 首页 > news >正文

[POI2007] ZAP-Queries (莫比乌斯反演)

[POI2007] ZAP-Queries

题目描述

Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He has alreadyfound out that whilst deciphering a message he will have to answer multiple queries of the form"for givenintegers aa, bb and dd, find the number of integer pairs (x,y)(x,y) satisfying the following conditions:

1\le x\le a1≤x≤a,1\le y\le b1≤y≤b,gcd(x,y)=dgcd(x,y)=d, where gcd(x,y)gcd(x,y) is the greatest common divisor of xx and yy".

Byteasar would like to automate his work, so he has asked for your help.

TaskWrite a programme which:

reads from the standard input a list of queries, which the Byteasar has to give answer to, calculates answers to the queries, writes the outcome to the standard output.

FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。

输入输出格式

输入格式:

The first line of the standard input contains one integer nn (1\le n\le 50 0001≤n≤50 000),denoting the number of queries.

The following nn lines contain three integers each: aa, bb and dd(1\le d\le a,b\le 50 0001≤d≤a,b≤50 000), separated by single spaces.

Each triplet denotes a single query.

输出格式:

Your programme should write nn lines to the standard output. The ii'th line should contain a single integer: theanswer to the ii'th query from the standard input.

输入输出样例

输入样例#1:

2
4 5 2
6 4 3

输出样例#1:

3
2

Solution

预备知识:莫比乌斯反演,整除分块

不会的看这位dalao的博客莫比乌斯反演

本蒟蒻的整除分块

根据题意
\[ans=\sum_{i=1}^a \sum_{j=1}^b [{gcd(i,j)=d}]\]
\[ans=\sum_{i=1}^{a/d} \sum_{j=1}^{b/d}[gcd(i,j)=1]\]

下面就是反演

\[ans=\sum_{i=1}^{a/d} \sum_{j=1}^{b/d} \sum_{p|gcd(i,j)}\mu(p)\]

但是这样枚举还是\(O(n^2)\),所以我们换一个变量枚举,把最后一个求和提到前面,因为p既是i的因子又是j的因子,所以枚举范围就是\(min(a/d,b/d)\),那么继续推公式

\[ans=\sum_{p=1}^{min(a/d,b/d)}{\mu(p)} \sum_{i=1}^{a/d} \sum_{j=1}^{b/d} \lfloor\frac{a}{p\times d} \rfloor \lfloor\frac{b}{p\times d}\rfloor\]

如果对于后面的式子不理解,可以这么看,令\(x=a/d,y=b/d\)
\(p\)\(x,y\)的一个因子,在\(x\)的范围内有\(\lfloor\frac{x}{p}\rfloor\)\(p\)的倍数,对于\(y\)同理,所以每个因子\(p\)都有\(\lfloor\frac{x}{p}\rfloor\lfloor\frac{y}{p}\rfloor\)的贡献

而对于后面的两个求和我们是可以用前缀和预处理出来的,这个时候是可以做到\(O(n)\)了,但是由于多组数据,所以我们发现,对于一段连续的p,因为a和b的值是确定的,所以\(\lfloor\frac{a}{p\times d}\rfloor\lfloor\frac{b}{p\times d}\rfloor\)的值也是确定的,这中间有许多重复的值,那么我们就可以使用整除分块优化到\(O(\sqrt n)\)

(有错误欢迎指出)

Code

#include<bits/stdc++.h>
#define lol long long
#define il inline
#define rg register
#define Min(a,b) (a)<(b)?(a):(b)
#define Max(a,b) (a)>(b)?(a):(b)

using namespace std;

const int N=5e4+10;

void in(int &ans)
{
    ans=0; int f=1; char i=getchar();
    while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
    while(i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+i-'0', i=getchar();
    ans*=f;
}

int n,m,d,tot,ans,T;
int mu[N],sum[N],prime[N];
bool vis[N];

il void get_mu() {
    mu[1]=1;
    for(int i=2;i<=N-10;i++) {
        if(!vis[i]) prime[++tot]=i,mu[i]=-1;
        for(int j=1;j<=tot && prime[j]*i<=N-10;j++) {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
            else mu[i*prime[j]]=-mu[i];
        }
    }
    for(int i=1;i<=N-10;i++) sum[i]=sum[i-1]+mu[i];
}

int main()
{
    in(T); get_mu();
    while(T--) {
        in(n),in(m),in(d); int nn=n/=d,mm=m/=d,ans=0;
        for(rg int i=1,pos,p=Min(n,m);i<=p;i=pos+1) {
            pos=Min(n/(n/i),m/(m/i));
            ans+=(sum[pos]-sum[i-1])*(nn/i)*(mm/i);
        }
        printf("%d\n",ans);
    }
    return 0;
}

博主蒟蒻,随意转载.但必须附上原文链接

http://www.cnblogs.com/real-l/

转载于:https://www.cnblogs.com/real-l/p/9628232.html

相关文章:

  • re:从零开始的数位dp
  • I/O多路复用
  • Nginx配置HTTPS
  • 正则表达式 整理
  • 分布式版本控制系统Git的安装与使用
  • 【BZOJ 4551】【TJOI2016】【HEOI2016】树
  • oracle多表查询-自连接
  • swiper 点击切换,拖动切换后继续自动轮播
  • Python 之 文件操作
  • Java 8 方法引用
  • Docker-基本命令
  • jenkins发送html测试报告
  • 项目配置 xml文件时 报错提示(The reference to entity useSSL must end with the ';' delimiter.)...
  • Golang操作结构体、Map转化为JSON
  • 犯得错误QAQ
  • 分享的文章《人生如棋》
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • canvas 五子棋游戏
  • HashMap ConcurrentHashMap
  • JS+CSS实现数字滚动
  • node-glob通配符
  • vue-router的history模式发布配置
  • Web设计流程优化:网页效果图设计新思路
  • XML已死 ?
  • 安装python包到指定虚拟环境
  • 百度小程序遇到的问题
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 排序算法学习笔记
  • 入口文件开始,分析Vue源码实现
  • 深度学习入门:10门免费线上课程推荐
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 应用生命周期终极 DevOps 工具包
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • 我们雇佣了一只大猴子...
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • #AngularJS#$sce.trustAsResourceUrl
  • (2020)Java后端开发----(面试题和笔试题)
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)计算机毕业设计SSM智慧停车系统
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (论文阅读11/100)Fast R-CNN
  • (论文阅读40-45)图像描述1
  • (没学懂,待填坑)【动态规划】数位动态规划
  • (四) 虚拟摄像头vivi体验
  • (四)JPA - JQPL 实现增删改查
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)人的集合论——移山之道
  • ***测试-HTTP方法
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法