link

题意:

给定长为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 序列变换的更多相关文章

  1. 51Nod1675 序列变换 数论 莫比乌斯反演

    原文http://www.cnblogs.com/zhouzhendong/p/8665675.html 题目传送门 - 51Nod1675 题意 给定序列$a,b$,让你求满足$\gcd(x,y)= ...

  2. 2015年百度之星初赛(1) --- C 序列变换

    序列变换 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. 序列变换(hdu5248)

    序列变换 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. 序列变换(Lis变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. hdu 5256 序列变换 (LIS变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. 51Nod 欢乐手速场1 B 序列变换[容斥原理 莫比乌斯函数]

    序列变换 alpq654321 (命题人)   基准时间限制:1 秒 空间限制:131072 KB 分值: 40 lyk有两序列a和b. lyk想知道存在多少对x,y,满足以下两个条件. 1:gcd( ...

  7. LIS 2015百度之星初赛2 HDOJ 5256 序列变换

    题目传送门 题意:中文题面 分析:LIS(非严格):首先我想到了LIS,然而总觉得有点不对:每个数先减去它的下标,防止下面的情况发生:(转载)加入序列是1,2,2,2,3,这样求上升子序列是3,也就是 ...

  8. 二分搜索 2015百度之星初赛1 HDOJ 5248 序列变换

    题目传送门 /* 二分搜索:在0-1e6的范围找到最小的max (ai - bi),也就是使得p + 1 <= a[i] + c or a[i] - c 比赛时以为是贪心,榨干智商也想不出来:( ...

  9. luogu P3411 序列变换

    链接 P3411 序列变换 如果要最小化答案,那么就最大化不移动的数. 那么不移动的子序列一定是最后答案的一段连续区间,而移动的数我们是一定有办法把他们还原的. 设\(f_{i}\)表示\(i\)点的 ...

随机推荐

  1. java检验银行卡号

    /* 校验过程: 1.从卡号最后一位数字开始,逆向将奇数位(1.3.5等等)相加. 2.从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和 ...

  2. easyui datagrid 去掉 全选checkbox

    在加载 表格的时候添加事件:onLoadSuccess 在事件中写入下面句,用空代替原有HTML 达到取消效果. $(".datagrid-header-check").html( ...

  3. Codeforces Round #502

    Codeforces Round #502 C. The Phone Number 题目描述:求一个\(n\)排列,满足\(LIS+LDS\)最小 solution 枚举\(LIS\),可证明\(LD ...

  4. 2017 CERC

    2017 CERC Problem A:Assignment Algorithm 题目描述:按照规则安排在飞机上的座位. solution 模拟. 时间复杂度:\(O(nm)\) Problem B: ...

  5. Lempel-Ziv algorithm realization

    Lempel-Ziv 复杂度程序 随着人们对非线性方法的分析越加深入,他们发现,虽然关联维度和最大李雅谱诺夫指数在分析脑电时具有一定的帮助,但是它们对数据的依赖性太强,对干扰和噪 声太敏感,而且要得到 ...

  6. 半小时分组统计个数sql

    group by 最后一个时间是多少按多少分组 select count(1), trunc(a.refund_insert_time, 'hh24') + case when to_char(ref ...

  7. POJ2528 Mayor's posters(线段树染色问题+离散化)

    题目大意:有t组数据,每组数据给你n张海报(1<=n<=10000),下面n组数据分别给出每张海报的左右范围(1 <= l <= r <= 10000000),下一张海报 ...

  8. 洛谷 P2043质因子分解 题解

    题目传送门 N的范围很小,所以使用O(n2)的算法就能过啦! #include<bits/stdc++.h> using namespace std; ],n; int main(){ c ...

  9. 洛谷 P2788数学1(math1)- 加减算式 题解

    题目传送门 这道题目可以使用C++的神奇功能: #include<bits/stdc++.h> using namespace std; int ans,t; int main(){ wh ...

  10. 一个简单的AboutMe页面

    Web2.0程序设计的小练习. Firefox下的效果 Chrome下的效果,套上了Helvetica,不知道为什么FF没有 (其实好像应该再优先加个Helvetica Neue的……呃……) 代码和 ...