Sample Input

6
1 6 2 5 3 4

Sample Output

10

You are given a {1, 2, ..., n}-permutation a[1], a[2], ..., a[n]. How many pairs of integers (i, j) satisfy 1 ≤ i ≤ j ≤ n and gcd(i, j) = gcd(a[i], a[j]) = 1? Here gcd means greatest common divisor.

Input

First line contains an integer n. (1 ≤ n ≤ 200000)

Second line contains n space-separated integers a[1], a[2], ..., a[n] which form a permutation.

Output

One line contains the answer.

题意:给定N的排列a[],问有多少对(i,j),满足gdc(i,j)=gcd(a[i],a[j])=1;

思路:我们知道区间互质对统计可以用莫比乌斯来容斥,对于每个数d,其贡献=mu[d]*C(含d的个数,2);

但是这里有两个条件,可以说是个二维的。 那么,我们枚举第一位的d,然后在第二维里正常的操作。

复杂度:因为每个数在第一维最多被使用log次,第二维也是,所以复杂度不大于N*logN*logN。加上我们有一些减枝,比如mu[i]=0时不操作。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],mu[maxn],pos[maxn],num[maxn],N; vector<int>G[maxn]; ll ans;
void init()
{ rep(i,,N) {
if(i==) mu[]=; G[i].push_back(i);
for(int j=i+i;j<=N;j+=i) mu[j]-=mu[i],G[j].push_back(i);
}
}
ll get(int x)
{
ll res=;
for(int i=x;i<=N;i+=x)
rep(j,,G[a[i]].size()-) num[G[a[i]][j]]++;
for(int i=x;i<=N;i+=x)
rep(j,,G[a[i]].size()-) res+=1LL*(num[G[a[i]][j]]-)*mu[G[a[i]][j]];
for(int i=x;i<=N;i+=x)
rep(j,,G[a[i]].size()-) num[G[a[i]][j]]=;
return res/;
}
int main()
{
scanf("%d",&N); init();
rep(i,,N) scanf("%d",&a[i]),pos[a[i]]=i;
rep(i,,N)
if(mu[i]) ans+=1LL*mu[i]*get(i);
printf("%lld\n",ans+(a[]==));
return ;
}

HihoCoder - 1867: GCD (莫比乌斯容斥)的更多相关文章

  1. CF(439E - Devu and Birthday Celebration)莫比乌斯容斥

    题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...

  2. [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]

    题面 传送门 思路 新姿势get 莫比乌斯容斥 $\sum_{i=1}{n}\mu(i)f(i)$ 这个东西可以把所有没有平方质因子的东西表示出来,还能容斥掉重复的项 证明是根据莫比乌斯函数的定义,显 ...

  3. ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)

    GCD Expectation Time Limit: 4 Seconds     Memory Limit: 262144 KB Edward has a set of n integers {a1 ...

  4. CodeForces - 803F: Coprime Subsequences(莫比乌斯&容斥)

    Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common div ...

  5. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  6. hdu6053(莫比乌斯+容斥+分块)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意: 给出一个含 n 个元素的 a 数组, 求 bi <= ai 且 gcd(b1, ...

  7. BZOJ2440(全然平方数)二分+莫比乌斯容斥

    题意:全然平方数是指含有平方数因子的数.求第ki个非全然平方数. 解法:比較明显的二分,getsum(int middle)求1-middle有多少个非全然平方数,然后二分.求1-middle的非全然 ...

  8. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  9. CodeForces - 1097F:Alex and a TV Show (bitset & 莫比乌斯容斥)

    Alex decided to try his luck in TV shows. He once went to the quiz named "What's That Word?!&qu ...

随机推荐

  1. 3-15 《元编程》第6章 3-16 hook method

    Code That Writes Code 6.1 Coding your way to the weekend 6.2 Kernel#eval, Binding#eval Binding: Obje ...

  2. Spring Boot 系统要求

    Spring Boot 2.1.0.RELEASE 方需要 Java 8 or 9 的支持和 Spring Framework 5.1.2.RELEASE 以上的版本. 明确的构建工具的支持,请参考下 ...

  3. python-day49--前端 html

    一.列表标签 1.有序列表 <ol>       (order list ) 在浏览器中显示包括:padding , 有序排列     <li>:列表中的每一项. 2.无序列表 ...

  4. 51nod-1605-博弈

    1605 棋盘问题  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在 ...

  5. Shell脚本的学习(一)

    Shell脚本的学习(一) 一)代码式shell脚本简介 1.下载 Xshell 5 建一个文件夹 mkdri home/data ; 1)查看一个在data里建一个1.sh 查看是否建立成功. 2) ...

  6. Activiti工作流笔记(2)

    1.Activiti工作数据表 Activiti用来存放流程数据的表共使用23张表,表名都是以"ACT_"开头,底层操作默认使用mybatis操作 工作流Activiti的表是用来 ...

  7. bzoj4001

    题解: 答案就是n*(n+1)/2/(2*n-1) 代码: #include<bits/stdc++.h> double n; int main() { scanf("%lf&q ...

  8. Flask初级(十一)flash与APScheduler 实现定时任务

    from flask import Flask from flask_apscheduler import APScheduler # 引入APScheduler class Config(objec ...

  9. Flash网页mp3播放器代码(3例)

    第一款:可以添加多首歌 代码如下:可以添加多首歌曲中间用 |  间隔符号隔开 <EMBED  height=20 type=application/x-shockwave-flash plugi ...

  10. Java——多线程常见面试题

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...