Codeforces 548E Mike and Foam (容斥+莫比烏斯反演)
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There aren kinds of beer at Rico's numbered from1 to n. i-th kind of beer hasai milliliters of foam on it.
Maxim is Mike's boss. Today he told Mike to performq queries. Initially the shelf is empty. In each request, Maxim gives him a numberx. If beer numberx is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf.
After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs(i, j) of glasses in the shelf such thati < j and where is the greatest common divisor of numbersa andb.
Mike is tired. So he asked you to help him in performing these requests.
The first line of input contains numbers n andq (1 ≤ n, q ≤ 2 × 105), the number of different kinds of beer and number of queries.
The next line contains n space separated integers,a1, a2, ... , an (1 ≤ ai ≤ 5 × 105), the height of foam in top of each kind of beer.
The next q lines contain the queries. Each query consists of a single integer integerx (1 ≤ x ≤ n), the index of a beer that should be added or removed from the shelf.
For each query, print the answer for that query in one line.
5 6
1 2 3 4 6
1
2
3
4
5
1
0
1
3
5
6
2
題目連結:http://codeforces.com/contest/548/problem/E
題目大意:n個數,q次詢問,每次詢問給出一個下標,如果詢問位置未出現,則將數字加入容器,否則從容器中刪除對應位置的元素,查詢的是容器中gcd(ai, aj) = 1的對數(i < j)
題目分析:典型的容斥題,預處理出每個數字的約數,設F(x)為gcd(ai, aj)=x的倍數時的個數,G(x)為gcd(ai, aj) = x時的倍數
則有F(x) = ΣG(kx) (k >= 1),莫比烏斯反演得
G(x) = ΣF(kx)u(k) => G(1) = ΣF(k)u(k)
每次操作時維護一個cnt陣列,cnt[i]表示i的倍數的個數,這樣就不用計算F(k)了,因為1個數的時候是0,加的時候先計算當前的再加,減的時候先減再計算,注意每次計算的是ans的累計量
比如樣例:
1:cnt[1] = 0
2:cnt[1] = 1,cnt[2] = 0
3:cnt[1] = 2,cnt[3] = 0
4:cnt[1] = 3,cnt[2] = 1,cnt[4] = 0
5:cnt[1] = 4,cnt[2] = 2,cnt[3] = 1,cnt[6] = 0 可以發現Σcnt[1] = 10 = C(4,2)
1:cnt[1] = 4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
int const MAX = 5e5 + 5;
int p[MAX], mob[MAX], a[MAX], cnt[MAX];
bool noprime[MAX], vis[MAX], has[MAX];
vector <int> vt[MAX];
void Mobius()
{
int pnum = 0;
mob[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(!noprime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
}
}
int main()
{
Mobius();
int n, q, idx, ma = 0;
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
has[a[i]] = true;
ma = max(ma, a[i]);
}
for(int i = 1; i <= ma; i++)
for(int j = i; j <= ma; j += i)
if(has[j])
vt[j].push_back(i);
ll ans = 0;
while(q--)
{
scanf("%d", &idx);
if(!vis[idx])
{
vis[idx] = true;
for(int i = 0; i < (int) vt[a[idx]].size(); i++)
{
int num = vt[a[idx]][i];
ans += (ll) mob[num] * cnt[num];
cnt[num] ++;
}
}
else
{
vis[idx] = false;
for(int i = 0; i < (int) vt[a[idx]].size(); i++)
{
int num = vt[a[idx]][i];
cnt[num] --;
ans -= (ll) mob[num] * cnt[num];
}
}
printf("%I64d\n", ans);
}
}
相關文章
- cf900D. Unusual Sequences(容斥 莫比烏斯反演)
- 莫比烏斯反演
- 比較典的莫比烏斯反演
- Hackerrank GCD Product(莫比烏斯反演)GC
- 莫比烏斯反演學習筆記筆記
- Problem H. Curious (莫比烏斯反演)
- 狄利克雷卷積 & 莫比烏斯反演卷積
- 狄利克雷卷積與莫比烏斯反演卷積
- 莫比烏斯
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 演算法隨筆——數論之莫比烏斯反演演算法
- SDOI2018 反迴文串(莫比烏斯反演+Pollard-Rho)
- 莫比烏斯函式函式
- SDOI2018 舊試題(莫比烏斯反演+三元環計數)
- lg容斥與反演
- 莫比烏斯函式 - 學習筆記函式筆記
- CF547C Mike and Foam
- CodeForces571A. Lengthening Sticks(組合數學-容斥)
- 容斥
- 反射容斥反射
- 容斥原理
- 【模板】容斥原理
- Min-Max 容斥
- [Codeforces 1111E] Tree(虛樹+二項式反演)
- 容斥原理學習筆記筆記
- 容斥定理 AtCoder——FizzBuzz Sum Hard
- 容斥原理——數學知識
- P4178 Tree——點分治 容斥
- P1447 [NOI2010] 容斥原理
- Min-Max 容斥學習筆記筆記
- codeforces比賽日記
- 2018世界盃烏拉圭vs俄羅斯誰會贏 烏拉圭vs俄羅斯比分預測
- HDU 4135 Co-prime(容斥原理+分解質因數)
- 薩繆爾·莫爾斯:電報之父的傳奇人生
- cf449D. Jzzhu and Numbers(容斥原理 高維字首和)
- 機率期望進階 + Min-Max容斥 練習題
- 「數學」助力每一個不知死活的容斥夢
- 《漫威蜘蛛俠:邁爾斯·莫拉萊斯》IGN評測:擺盪到新高度
- Ignatius and the Princess IV——伊格納提烏斯和四公主