其他分享
首页 > 其他分享> > CSP-S开小灶2

CSP-S开小灶2

作者:互联网

A. 元素周期表

发现当同一列有多个点,他们所在行可以合并

同一行有多个点,他们所在列可以合并

一个方案可行,当且仅当能够合并成一个格子

用并查集处理,合并行列后得到的矩形,他的每一行每一列至多有一个元素

那么\(ans = n +m - 1 - cnt\)

code
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 200005;

inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9')c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int n, m, q;
struct node{int x, y;}a[maxn];
bool vis[maxn];
struct SET{
	int f[maxn], cnt;
	void pre(int x){for(int i = 1; i <= x; ++i)f[i] = i;cnt = x;}
	int fa(int x){return x == f[x] ? x : f[x] = fa(f[x]);}
	void merge(int x, int y){x = fa(x); y = fa(y); if(x == y)return; f[y] = x; --cnt;}
}pn, pm;
int rem[maxn], rm[maxn];
int work(){
	pn.pre(n); pm.pre(m);
	for(int i = 1; i <= q; ++i){
		if(rem[a[i].x])pm.merge(a[i].y, rem[a[i].x]);
		else rem[a[i].x] = a[i].y;
		if(rm[a[i].y])pn.merge(a[i].x, rm[a[i].y]);
		else rm[a[i].y] = a[i].x;
	}
	int ans = pn.cnt + pm.cnt - 1;
	for(int i = 1; i <= q; ++i)if(!vis[pn.fa(a[i].x)]){vis[pn.fa(a[i].x)] = 1; --ans;}
	return ans;
}
int main(){
	n = read(), m = read(), q = read();
	for(int i = 1; i <= q; ++i)a[i].x = read(), a[i].y = read();
	printf("%d\n",work());
	return 0;
}

B. gcd

简单莫反, 式子是考场推出来的,题目是考后切的。。。。

不难发现所求为 \(\displaystyle \sum_{i = 1}^{mx}\sum_{j = 1}^{mx}[gcd(i,j) == 1]cnt_i cnt_j\)

套路莫反

\(\displaystyle \sum_{d = 1}^{mx}\mu(d)\sum_{i = 1}^{mx/d}\sum_{j = 1}^{mx/d}cnt_i cnt_j\)

设 \(f(d) = \sum_{i = 1}^{mx/d}cnt_i\)

答案为 \(1/2 \times \sum_{i = 1}^{mx}f(d)(f(d) - 1)\)

减一是为了去掉该数和自己配对的非法方案,其实特判一下\(1\)也可以

code
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 500005;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9')c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int n, m, a[maxn], mx;
bool vis[maxn];
int prime[maxn], cnt, mu[maxn], f[maxn];
bool flag[maxn];
vector<int>v[maxn];
int main(){
	n = read(), m = read();
	for(int i = 1; i <= n; ++i)a[i] = read();
	for(int i = 1; i <= n; ++i)mx = max(mx, a[i]);
	for(int i = 2; i <= mx; ++i){
		if(!flag[i])prime[++cnt] = i, mu[i] = -1;
		for(int j = 1; j <= cnt && prime[j] * i <= mx; ++j){
			flag[i * prime[j]] = 1;
			if(i % prime[j] == 0)break;
			mu[i * prime[j]] = -mu[i];
		}
	}
	mu[1] = 1;
	for(int i = 1; i <= mx; ++i)if(mu[i])for(int j = i; j <= mx; j += i)v[j].push_back(i);
	ll sum = 0;
	for(int i = 1; i <= m; ++i){
		int x = read(); 
		if(vis[x]){
			for(int p : v[a[x]]){
				sum = sum - 1ll * f[p] * (f[p] - 1) * mu[p];
				--f[p];
				sum = sum + 1ll * f[p] * (f[p] - 1) * mu[p];
			}
		}else{
			for(int p : v[a[x]]){
				sum = sum - 1ll * f[p] * (f[p] - 1) * mu[p];
				++f[p];
				sum = sum + 1ll * f[p] * (f[p] - 1) * mu[p];
			}
		}
		vis[x] = vis[x] ^ 1;
		printf("%lld\n",sum / 2);
	}
	
	return 0;
}

标签:cnt,int,long,mx,maxn,开小灶,include,CSP
来源: https://www.cnblogs.com/Chencgy/p/16674034.html