51nod1675 序列变换
题意:
给定长为n的序列a,b,下标从1开始,问有多少对x,y满足gcd(x,y)=1且$a_{b_x}=b_{a_y}$?
$n\leq 10^5.$
题解:
$a_{b_x}$和$b_{a_y}$是个幌子,定义成$A_x$和$B_y$就好了,没有什么影响。
考虑倍数反演:记f(i)表示i=gcd(x,y)的方案数;g(i)表示i|gcd(x,y)的方案数。g(i)可以枚举倍数得到。那么有:
$$\begin{equation}g(n)=\sum_{n|d}f(d)\Longrightarrow f(n)=\sum_{n|d}\mu(\frac{d}{n})g(d)\end{equation}$$
这里只需要$f(1)=\sum_{i=1}^{n}\mu(i)\times g(i)$。
复杂度$\mathcal{O}(n\log n)$。
code:
- #include<bits/stdc++.h>
- #define rep(i,x,y) for (int i=(x);i<=(y);i++)
- #define ll long long
- #define inf 1000000001
- #define y1 y1___
- using namespace std;
- char gc(){
- static char buf[],*p1=buf,*p2=buf;
- return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
- }
- #define gc getchar
- ll read(){
- char ch=gc();ll x=;int op=;
- for (;!isdigit(ch);ch=gc()) if (ch=='-') op=-;
- for (;isdigit(ch);ch=gc()) x=(x<<)+(x<<)+ch-'';
- return x*op;
- }
- #define N 100005
- int n,a[N],b[N],t[N],mu[N],p[N],cnt;ll ans,g[N];bool vis[N];
- void init(int n){
- mu[]=;
- rep (i,,n){
- if (!vis[i]) p[++cnt]=i,mu[i]=-;
- for (int j=;j<=cnt&&i*p[j]<=n;j++){
- vis[i*p[j]]=;
- if (i%p[j]==) break;
- mu[i*p[j]]=-mu[i];
- }
- }
- }
- int main(){
- n=read();init(n);
- rep (i,,n) a[i]=read();
- rep (i,,n) b[i]=read();
- rep (d,,n){
- for (int i=d;i<=n;i+=d) t[b[a[i]]]++;
- for (int i=d;i<=n;i+=d) g[d]+=t[a[b[i]]];
- for (int i=d;i<=n;i+=d) t[b[a[i]]]--;
- }
- rep (i,,n) ans+=mu[i]*g[i];
- printf("%lld\n",ans);
- return ;
- }
51nod1675 序列变换的更多相关文章
- 51Nod1675 序列变换 数论 莫比乌斯反演
原文http://www.cnblogs.com/zhouzhendong/p/8665675.html 题目传送门 - 51Nod1675 题意 给定序列$a,b$,让你求满足$\gcd(x,y)= ...
- 2015年百度之星初赛(1) --- C 序列变换
序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 序列变换(hdu5248)
序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 序列变换(Lis变形)
序列变换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- hdu 5256 序列变换 (LIS变形)
序列变换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 51Nod 欢乐手速场1 B 序列变换[容斥原理 莫比乌斯函数]
序列变换 alpq654321 (命题人) 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 lyk有两序列a和b. lyk想知道存在多少对x,y,满足以下两个条件. 1:gcd( ...
- LIS 2015百度之星初赛2 HDOJ 5256 序列变换
题目传送门 题意:中文题面 分析:LIS(非严格):首先我想到了LIS,然而总觉得有点不对:每个数先减去它的下标,防止下面的情况发生:(转载)加入序列是1,2,2,2,3,这样求上升子序列是3,也就是 ...
- 二分搜索 2015百度之星初赛1 HDOJ 5248 序列变换
题目传送门 /* 二分搜索:在0-1e6的范围找到最小的max (ai - bi),也就是使得p + 1 <= a[i] + c or a[i] - c 比赛时以为是贪心,榨干智商也想不出来:( ...
- luogu P3411 序列变换
链接 P3411 序列变换 如果要最小化答案,那么就最大化不移动的数. 那么不移动的子序列一定是最后答案的一段连续区间,而移动的数我们是一定有办法把他们还原的. 设\(f_{i}\)表示\(i\)点的 ...
随机推荐
- 使用Skyworking 作全链路api调用监控,Integration of Skyworking, auditing the whole chain circuit.
Applicable scenario: Structure Map ~ Skywalking uses elasticsearch to store data, don't mistake elas ...
- 在Windows下安装MongoDB
概述 读者可以通过本文来学习在Windows操作系统上安装MongoDB. 从2.2版本开始,Mongo DB不在支持Windows XP.请使用最近的windows来安装最近发布的MongoDB.本 ...
- 5、Linux操作系统介绍
1操作系统的作用·是现代计算机系统中最基本和最重要的系统软件·是配置在计算机硬件上的第一层软件,是对硬件系统的首次扩展·主要作用是管理好硬件设备,并为用户和应用程序提供一个简单的接口,以便于使用·而其 ...
- CMDB概述(二)
运维自动化路线: cmdb的开发需要包含三部分功能: ·采集硬件数据 ·API ·页面管理 执行流程:服务器的客户端采集硬件数据,然后将硬件信息发送到API,API负责将获取到的数据保存到数据库中, ...
- thinkphp 5.0 代码执行漏洞
https://github.com/vulhub/vulhub/blob/master/thinkphp/5-rce docker-compose -f /home/root/compose.yml ...
- MongoDB之安装和基本使用(一)
环境 ubuntu16.04 mongodb基本特点 MongoDB 是一个基于分布式 文件存储的NoSQL数据库;可以把MongoDB想象成一个大py字典. 模式自由 :可以把不同结构的文档存储在同 ...
- think php模板的使用
{include file="../application/public/header.html"}<!-- Jumbotron --><div class=&q ...
- puppet practice
目标 试验环境有两台主机(VM)构成,一台是master,一台是agent,完成以下工作: 新建用户newuser; 安装 ubuntu-cloud-keyring package,更改文件/etc/ ...
- vsts 自动部署到Azure
如果要部署到中国区的Azure ,请先阅读 http://www.cnblogs.com/cnryb/p/7867275.html 前置条件,我把代码托管在vsts(放在GitHub上也没问题,这里不 ...
- angular项目文件概览
在Mac上打开终端,输入ng new b-app 如下: 然后在webstorm中打开 src文件夹 你的应用代码位于src文件夹中. 所有的Angular组件.模板.样式.图片以及你的应用所需的任 ...