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);
}
}
相關文章
- ZOJ 3868 GCD Expectation (容斥+莫比烏斯反演)GC
- HDU 1695 GCD (容斥 + 莫比烏斯反演)GC
- POJ 3904 Sky Code (容斥+莫比烏斯反演)
- HDU 5212 Code (容斥 莫比烏斯反演基礎題)
- lg容斥與反演
- CF547C Mike and Foam
- 莫比烏斯反演
- Codeforces 235E Number Challenge (神定理+莫比烏斯反演)
- BZOJ 2301 [HAOI2011]Problem b (容斥+莫比烏斯反演+分塊優化 詳解)優化
- Hackerrank GCD Product(莫比烏斯反演)GC
- 莫比烏斯反演學習筆記筆記
- 比較典的莫比烏斯反演
- 有標號DAG計數 [容斥原理 子集反演 組合數學 fft]FFT
- 容斥
- 狄利克雷卷積 & 莫比烏斯反演卷積
- HDU 4746 Mophues (莫比烏斯反演應用)
- CodeForces571A. Lengthening Sticks(組合數學-容斥)
- 反射容斥反射
- 容斥原理
- 狄利克雷卷積與莫比烏斯反演卷積
- Codeforces Gym 100548F Color (組合數+容斥)
- BZOJ 2818 Gcd (莫比烏斯反演 或 尤拉函式)GC函式
- 演算法隨筆——數論之莫比烏斯反演演算法
- 容斥 + 組合數學 ---Codeforces Round #317 A. Lengthening Sticks
- 【模板】容斥原理
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 容斥原理講解
- Min-Max 容斥
- CSU 1325 A very hard problem (莫比烏斯反演+分塊求和優化)優化
- SPOJ VLATTICE Visible Lattice Points (莫比烏斯反演基礎題)
- ZOJ 3435 Ideal Puzzle Bobble (莫比烏斯反演基礎題)Idea
- 遊戲裡面的容斥原理遊戲
- SPOJ PGCD - Primes in GCD Table (好題! 莫比烏斯反演+分塊求和優化)GC優化
- 容斥原理學習筆記筆記
- 容斥原理——數學知識
- #19. 計數(容斥原理)
- HDU 4059 The Boss on Mars ( 容斥原理)
- BZOJ 3309 DZY Loves Math (莫比烏斯反演的應用 好題)