题目大意
有$n$个正整数$x_1\ldots x_n$,初始时状态均为未选。
有$m$个操作,每个操作给定一个编号$i$,将$x_i$的选取状态取反。每次操作后,你需要求出选取的数中有多少个互质的无序数对。
题目分析
考虑题目要求动态维护:
用莫比乌斯函数替代bool表达式得到:
因为只是单点修改$a$,故每次变化的值为:
变化一下变成:
对于前面部分$\sum_{k|a}\mu(k)$,每次暴力枚举约数即可,对于后面部分$\sum_{k|x_b}$每次修改的时候维护一下其约数出现的次数即可。
时间复杂度$O(n\sqrt{n})$,预处理出每个数的约数可以快一些。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=200005,maxw=500005; int n,m,a[maxn],u[maxw],sum[maxw]; bool Hash[maxw],bj[maxn]; vector<int>vec[maxw]; long long ans=0; void Solve(int n) { u[1]=1; for(int i=1; i<=n; i++) { if(Hash[i])vec[i].push_back(i); for(int j=2*i; j<=n; j+=i) { u[j]-=u[i]; if(Hash[j])vec[j].push_back(i); } } } void add(int x) { for(auto& i:vec[x]) { ans+=u[i]*sum[i]; sum[i]++; } } void del(int x) { for(auto& i:vec[x]) { sum[i]--; ans-=u[i]*sum[i]; } } int main() { n=Get_Int(); m=Get_Int(); for(int i=1; i<=n; i++)a[i]=Get_Int(),Hash[a[i]]=1; Solve(500000); for(int i=1; i<=m; i++) { int x=Get_Int(); if(!bj[x])add(a[x]); else del(a[x]); bj[x]^=1; printf("%lld\n",ans); } return 0; }
|