CodeForces 548E Mike and Foam (容斥+数论)
来源:互联网 发布:淘宝突然没有流量了 编辑:程序博客网 时间:2024/05/22 15:56
E. Mike and Foam
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mike is a bartender at Rico’s bar. At Rico’s, they put beer glasses in a special shelf. There are n kinds of beer at Rico’s numbered from 1 to n. i-th kind of beer has ai milliliters of foam on it.
Maxim is Mike’s boss. Today he told Mike to perform q queries. Initially the shelf is empty. In each request, Maxim gives him a number x. If beer number x 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 that i < j and where is the greatest common divisor of numbers a and b.
Mike is tired. So he asked you to help him in performing these requests.
Input
The first line of input contains numbers n and q (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 integer x (1 ≤ x ≤ n), the index of a beer that should be added or removed from the shelf.
Output
For each query, print the answer for that query in one line.
Examples
input
5 6
1 2 3 4 6
1
2
3
4
5
1
output
0
1
3
5
6
2
先预处理出一个数的素因子,和已经存在的数的质因子做容斥·
#include "cstring"#include "cstdio"#include "iostream"#include "string.h"#include "vector"#include "cmath"#define LL long longusing namespace std;int a[200005];int vis[200005];int cnt[500005];long long total,ans,len;//long long s[500005];vector <long long> list[500005];void makeList(long long i){ long long old=i; for(long long j=2;j<=sqrt(i);j++) { if(i%j==0) { list[old].push_back(j); while(i%j==0) i/=j; } } if(i!=1) list[old].push_back(i);}long long add(int x){ vector <long long> now=list[x]; long long len=list[x].size(); long long sum=0; for(int i=1;i<(1<<len);i++) { long long id=1; long long flag=0; for(int j=0;j<len;j++) { if(i&(1<<j)) { id*=now[j]; //id*=s[j]; flag++; } } if(flag%2==0) sum-=cnt[id]; else sum+=cnt[id]; cnt[id]++; } ans+=(total-sum); total++; return ans;}long long sub(int x){ vector <long long> now=list[x]; long long len=list[x].size(); long long sum=0; for(int i=1;i<(1<<len);i++) { long long id=1; long long flag=0; for(int j=0;j<len;j++) { if(i&(1<<j)) { id*=now[j]; flag++; } } cnt[id]--; if(flag%2==0) sum-=cnt[id]; else sum+=cnt[id]; } total--; ans-=(total-sum); return ans;}int main(){ int n,q; int mark[500005]; memset(mark,0,sizeof(mark)); while(~scanf("%d%d",&n,&q)) { memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); total=0; ans=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); while(q--) { int now; scanf("%d",&now); if(mark[a[now]]==0) { makeList(a[now]); mark[a[now]]=1; } //_set(a[now]); if(vis[now]==0) { vis[now]=1; printf("%lld\n",add(a[now])); } else { vis[now]=0; printf("%lld\n",sub(a[now])); } } }}
- CodeForces 548E Mike and Foam (容斥+数论)
- Codeforces 548E Mike and Foam(容斥)
- Codeforces #305 div2 E. Mike and Foam 数论 容斥原理
- Codeforces 548E Mike and Foam (容斥+莫比乌斯反演)
- Codeforces Round #305 (Div. 2) 548E. Mike and Foam 容斥
- E. Mike and Foam(容斥原理)
- 【codeforces 548E】Mike and Foam
- Codeforces Round #305 (Div. 2) E. Mike and Foam 容斥原理
- CF 305 div2 E. Mike and Foam (容斥原理)
- Mike and Foam - CodeForces #547 C 容斥原理
- Codeforces 547C Mike and Foam 容斥
- codeforces/#305 Div1/547/C Mike and Foam 【容斥】
- 【Codeforces】547C Mike and Foam 容斥
- *Codeforces Round #305 (Div. 1) C. Mike and Foam(容斥原理)
- CF 547 C Mike and Foam(容斥原理)
- codeforces548E Mike and Foam -- 容斥
- Codeforces Round #305 (Div. 1)C. Mike and Foam(素数+容斥)
- codeforces 548 E. Mike and Foam (莫比乌斯反演)
- 垃圾回收机制-----软引用与弱引用
- ListView背景修饰
- The Linux Command Line 11-20章总结
- Redis在游戏开发中的应用
- 继承BaseAdapter实现Filterable的adapter类完整示例
- CodeForces 548E Mike and Foam (容斥+数论)
- android 加载大图的原理和实现
- PagerSildingTab的简单实现
- windows10下安装Microsoft Visual Studio 2013.4 Community enu中文语言包出错,提示“”程序兼容模式已打开.请将其关闭”的解决方法
- LuBan图片压缩
- 51Nod-1320-合法尾数序列
- Android 仿照微信发说说,既能实现拍照,选图库,多图案上传 使用Retrofit2.0技术
- hdu 2089 不要62 (数位DP)
- c#中线程池