[CF1454] Codeforces Round #686 (Div. 3) solution

标签(空格分隔): 经验 题解

时量 : 2h

概括 :

\[\text{2min t1 }\\ \text{10min t2 (hacked)}\\ \text{30min t3 }\\ \text{over 1h t4(false) }\\ \text{rating down : 30pts}\\ \]

  • t1、t2、t3、t4都是一眼题
  • 最大的收获是在cf不要随意使用memset(t2 hack)
  • t4 错在想当然的把第一个质因子当作最多的了。。。

A

求一个排列\(p_i\),使 \(p_i \ne i\)
\(answer : p_i = i \% n + 1\)

B

找一个数,仅仅出现过一次且最小
$answer : $ 桶子维护即可

C

给出一个序列\(\{a_n\}\)
求一个数\(b\), 两个\(b\)之间删一次,问最小删除次数
\(answer:unique\)一发,求除首尾最小出现个数即可。

D

给一个数\(n\), 求一个序列\(a_i\)
使得\(a_i | a_{i + 1} \&\& \prod\limits_{i = 1} ^ n a_i = n\)
\(answer:\) 找出最多的质因子\(p\)
\(a_{1 \sim n-1}=p,a_n = n / p^{n - 1}\)

E

给定一颗基环树,求树上总简单路径个数(一个路径与它的逆路径只算一次)
\(answer:\)先撸环,对于每个环上点的子树中的路径一定是单一的,其他路径经过环,所以有两个
因此,\(ans=C_n^2 - \sum\limits_{x \in rope} C_{son[x]}^2\)

F

\({a_n}\)分成三段,第一段的最大值等于第二段的最小值等于第三段的最大值
\(answer:\)因为有三段所以有两个分界点。枚举第一个,二分查找第二个。因为最小值和最大值都有单调性,所以可以二分,具体来说就是如果第二段最小值小了就往右,否则往左,相等时再看第三段最大值,大右小左,没有成立的就输出no。


A

/*************************************************
			Copyright: 724033213@qq.com
				  Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {
		if(c == '-') f = -f;
		c = getchar();
	}
	while(isdigit(c)) {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * f;
}
il void write(int x) {
	char c[33] = {0}, tot = 0;
	if(x == 0) {putchar('0'); return;}
	while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
	while(tot) {putchar(c[tot --]);}
	return ;
}
int s, t, n, m;
int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	cin >> t;
	while(t --) {
		n = read();
		for(int i = 1; i <= n; i ++) {
			printf("%d ", i % n + 1);
		}
		printf("\n");
	}
	return 0;
}


B

/*************************************************
			Copyright: 724033213@qq.com
				  Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {
		if(c == '-') f = -f;
		c = getchar();
	}
	while(isdigit(c)) {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * f;
}
il void write(int x) {
	char c[33] = {0}, tot = 0;
	if(x == 0) {putchar('0'); return;}
	while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
	while(tot) {putchar(c[tot --]);}
	return ;
}
const int maxn = 2e5 + 5;
int s, t, n, m;
int a[maxn], vis[maxn];
void work() {
	n = read(); m = 1e9; s = -1;
	for(int i = 1; i <= n; i ++) vis[(a[i] = read())] = 0;
	for(int i = 1; i <= n; i ++) vis[a[i]] ++;
	for(int i = 1; i <= n; i ++) if(vis[a[i]] == 1 && a[i] < m) m = a[i], s = i;
	printf("%d\n", s);
}
int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	t = read();
	while(t --) work();
	return 0;
}


C

/*************************************************
			Copyright: 724033213@qq.com
				  Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {
		if(c == '-') f = -f;
		c = getchar();
	}
	while(isdigit(c)) {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * f;
}
il void write(int x) {
	char c[33] = {0}, tot = 0;
	if(x == 0) {putchar('0'); return;}
	while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
	while(tot) {putchar(c[tot --]);}
	return ;
}
const int maxn = 2e5 + 5;
int s, t, n, m, a[maxn], box[maxn];
void work() {
	int ans = 1e9;
	n = read();
	memset(box, 0, sizeof(box)); 
	for(int i = 1; i <= n; i ++) a[i] = read();
	n = unique(a + 1, a + 1 + n) - (a + 1);
	if(n == 1) {puts("0"); return ;}
	for(int i = 2; i < n; i ++) box[a[i]] ++;
	for(int i = 1; i <= n; i ++) chkmin(ans, box[a[i]] + 1);
	printf("%d\n", ans);
}
int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	t = read();
	while(t --) work();  
	return 0;
}


D

/*************************************************
			Copyright: 724033213@qq.com
				  Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define int long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {
		if(c == '-') f = -f;
		c = getchar();
	}
	while(isdigit(c)) {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * f;
}
il void write(int x) {
	char c[33] = {0}, tot = 0;
	if(x == 0) {putchar('0'); return;}
	while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
	while(tot) {putchar(c[tot --]);}
	return ;
}
const int maxn = 2e5 + 5;
int s, t, n, m;
int p[maxn], c[maxn], ans[maxn], top;
int pr[maxn], vis[maxn], isp[maxn], cnt;
void euler(int n) {
 	for(int i = 2; i <= n; i ++) {
		if(vis[i]) continue;
		pr[++ cnt] = i;
		for(int j = i; j * i <= n; j ++) 
			vis[i * j] = 1;
	}
}
void work() {
	int lb = 0;
	m = n = read(); top = 1; 
	memset(c, 0, sizeof(c));
	for(int i = 1; pr[i] * pr[i] <= n; i ++) {
		if(n % pr[i] == 0) {	
			while(m % pr[i] == 0) {
				c[i] ++;
				if(c[i] > c[lb]) lb = i;
				m /= pr[i];
			} 
		}
	}
	if(lb == 0) {
		printf("1\n%lld\n", m);
		return ;
	}
	cout << c[lb] << "\n";
	for(int i = 1; i < c[lb]; i ++) {
		printf("%lld ", pr[lb]); top *= pr[lb];
	}
	printf("%lld\n", n / top);
	return ;
}
signed main() {
	t = read();
	euler(2e5);
	while(t --) work();
	return 0;
}


E

/*************************************************
			Copyright: 724033213@qq.com
				  Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {
		if(c == '-') f = -f;
		c = getchar();
	}
	while(isdigit(c)) {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * f;
}
il void write(int x) {
	char c[33] = {0}, tot = 0;
	if(x == 0) {putchar('0'); return;}
	while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
	while(tot) {putchar(c[tot --]);}
	return ;
}
int s, t, n, m;
const int maxn = 2e5 + 5;
struct node {
	int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
//int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void add(int x, int y) {
	e[++ tot] = {y, head[x]}; head[x] = tot;
}
int dep[maxn], num[maxn], scc[maxn], cnt;
int ring;
void rope(int B, int E) {	
	if(dep[B] < dep[E]) swap(B, E);
	for( ; dep[B] > dep[E]; ) 
		scc[B] = 1, B = num[B];
	for( ; B ^ E; )
		scc[B] = scc[E] = 1, B = num[B], E = num[E];
	scc[B] = 1;
	ring = 1;
} 
void fnd(int x, int fa) {
//	cerr << x << "sadads asd\n";
	if(ring == 1) return ;
	num[x] = fa; dep[x] = dep[fa] + 1;
	for(int i = head[x]; i ; i = e[i].nxt) {
		int y = e[i].to;
		if(y ^ fa) {
//			cout << x << "+" << y << " " << num[y] << "\n";
			if(dep[y]) {
//				cerr << "sda " << x << " " << y << "\n";
				rope(x, y);
				ring = 1; 
				return ;
			} else {
				fnd(y, x);
			}
		}
	}
}
int son[maxn];
void dfs(int x, int fa) {
	son[x] = 1; 
	for(int i = head[x]; i ; i = e[i].nxt) {
		int y = e[i].to;
		if((y ^ fa) && !scc[y]) {
			dfs(y, x);
			son[x] += son[y]; 
		}
	}
} 
void work() {
	LL ans = 0; tot = 0; ring = 0;
	n = read();
	memset(head, 0, sizeof(head));
	for(int i = 1, x, y; i <= n; i ++) {
		dep[i] = num[i] = scc[i] = 0;
		x = read(), y = read();
		add(x, y); add(y, x);
	}
	ans = 1ll * n * (n - 1);
	fnd(1, 0); 
	for(int i = 1; i <= n; i ++) if(scc[i] == 1) dfs(i, 0), ans -= 1ll * son[i] * (son[i] - 1) / 2;
//	for(int i = 1; i <= n; i ++) cout << scc[i] << " "; cout << "\n";
	printf("%lld\n", ans);
	return ;
}
int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	t = read();
	while(t --) work();
	return 0;
}

F

/*************************************************
			Copyright: 724033213@qq.com
				  Author: Jack
*************************************************/
#include <bits/stdc++.h>
#define LL long long
#define il inline
#define rg register
using namespace std;
il void chkmax(int &a, int b) {a = a > b ? a : b;}
il void chkmin(int &a, int b) {a = a < b ? a : b;}
il int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) {
		if(c == '-') f = -f;
		c = getchar();
	}
	while(isdigit(c)) {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * f;
}
il void write(int x) {
	char c[33] = {0}, tot = 0;
	if(x == 0) {putchar('0'); return;}
	while(x) {c[++ tot] = x % 10 + '0'; x /= 10;}
	while(tot) {putchar(c[tot --]);}
	return ;
}
int s, t, n, m;
const int maxn = 2e5 + 5;
struct node {
	int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
//int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void add(int x, int y) {
	e[++ tot] = {y, head[x]}; head[x] = tot;
}
int dep[maxn], num[maxn], scc[maxn], cnt;
int ring;
void rope(int B, int E) {	
	if(dep[B] < dep[E]) swap(B, E);
	for( ; dep[B] > dep[E]; ) 
		scc[B] = 1, B = num[B];
	for( ; B ^ E; )
		scc[B] = scc[E] = 1, B = num[B], E = num[E];
	scc[B] = 1;
	ring = 1;
} 
void fnd(int x, int fa) {
//	cerr << x << "sadads asd\n";
	if(ring == 1) return ;
	num[x] = fa; dep[x] = dep[fa] + 1;
	for(int i = head[x]; i ; i = e[i].nxt) {
		int y = e[i].to;
		if(y ^ fa) {
//			cout << x << "+" << y << " " << num[y] << "\n";
			if(dep[y]) {
//				cerr << "sda " << x << " " << y << "\n";
				rope(x, y);
				ring = 1; 
				return ;
			} else {
				fnd(y, x);
			}
		}
	}
}
int son[maxn];
void dfs(int x, int fa) {
	son[x] = 1; 
	for(int i = head[x]; i ; i = e[i].nxt) {
		int y = e[i].to;
		if((y ^ fa) && !scc[y]) {
			dfs(y, x);
			son[x] += son[y]; 
		}
	}
} 
void work() {
	LL ans = 0; tot = 0; ring = 0;
	n = read();
	memset(head, 0, sizeof(head));
	for(int i = 1, x, y; i <= n; i ++) {
		dep[i] = num[i] = scc[i] = 0;
		x = read(), y = read();
		add(x, y); add(y, x);
	}
	ans = 1ll * n * (n - 1);
	fnd(1, 0); 
	for(int i = 1; i <= n; i ++) if(scc[i] == 1) dfs(i, 0), ans -= 1ll * son[i] * (son[i] - 1) / 2;
//	for(int i = 1; i <= n; i ++) cout << scc[i] << " "; cout << "\n";
	printf("%lld\n", ans);
	return ;
}
int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	t = read();
	while(t --) work();
	return 0;
}

posted @ 2020-11-26 22:32  永远_少年  阅读(244)  评论(0编辑  收藏  举报