程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Codeforces Round #305 (Div. 1) C(容斥原理)

Codeforces Round #305 (Div. 1) C(容斥原理)

編輯:關於C++

 

C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard 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 1to 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.

Sample test(s) input
5 6
1 2 3 4 6
1
2
3
4
5
1
output
0
1
3
5
6
2

給一個數組,q次操作,每次輸入x,如果x位置上沒有數字,就將a[x]放到x位置上,否則將a[x]從x上移走,輸出每次操作後gcd為1的二元組對數。

 

動態維護,每次只考慮有多少個數和a[x]互質。容斥原理

 

 

#include 
#include 
#include 
#include 
#include 
#define foreach(it,v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
bool check[maxn];
vector v[maxn];
int g[maxn],a[maxn];
void init(int n)
{
	memset(check, 0, sizeof check);
	for(int i = 2; i <= n; i++) {
		if(check[i])continue;
		for(int j = i; j <= n; j += i)
			v[j].push_back(i),check[j] = 1;
	}
}
int Query(int x)
{
	vector &cur = v[x];
	int M,sz = cur.size();
	M = 1<>j)&1){
			now *= cur[j];
			tot++;
		}
		if(tot & 1) ans -= g[now];
		else ans += g[now];
	}
	return ans;
}
void Modify(int x,int d)
{
	vector &cur = v[x];
	int M,sz = cur.size();
	M = 1<>j)&1){
			now *= cur[j];
		}
		g[now] += d;
	}
}
int main(int argc, char const *argv[])
{
	init(maxn-5);
	int n,q;
	while(~scanf("%d%d",&n,&q)) {
		memset(check,0,sizeof (check[0])*(n+2));
		memset(g,0,sizeof g);
		for(int i = 1; i <= n; i++) {
			scanf("%d",a + i);
		}
		ll ans = 0;
		while(q--) {
			int x;scanf("%d",&x);
			if(!check[x]) {
				check[x] = 1;
				ans += Query(a[x]);
				Modify(a[x],1);
			}else {
				check[x] = 0;
				Modify(a[x],-1);
				ans -= Query(a[x]);
			}
			printf("%I64d\n", ans);
		}
	}
	return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved