【CodeForces】CodeForces Round #400 (Div. 1 + Div. 2) 题解

本文提供了CodeForces Round #400 (Div. 1 + Div. 2) 比赛的详细题解,包括A到G七道题目。每道题目的解题思路包括时间复杂度分析,并附有代码实现。例如,A题维护两个字符串模拟,B题根据质数和非质数涂色,C题枚举序列和与合法左端点,D题使用2-SAT解决门与开关问题,E题通过欧拉函数计算,F题利用树分治策略,G题采用数位DP解决加密数据问题。
摘要由CSDN通过智能技术生成

【比赛链接】

【题解链接】

**【A】**A Serial Killer

【思路要点】

  • 维护两个字符串模拟。
  • 时间复杂度 O ( N ) O(N) O(N)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T> void chkmax(T &x, T y) {
    x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {
    x = min(x, y); } 
template <typename T> void read(T &x) {
    
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
    
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
    
	write(x);
	puts("");
}
string a, b, c;
int main() {
    
	cin >> a >> b;
	int n; cin >> n;
	cout << a << ' ' << b << endl;
	for (int i = 1; i <= n; i++) {
    
		cin >> c;
		if (a == c) cin >> a;
		else cin >> b;
		cout << a << ' ' << b << endl;
	}
	return 0;
}

**【B】**Sherlock and his girlfriend

【思路要点】

  • N ≤ 2 N≤2 N2,将所有数涂为一种颜色。
  • 否则,将质数涂为一种颜色,非质数涂为一种颜色。
  • 时间复杂度 O ( N N ) O(N\sqrt{N}) O(NN )

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T> void chkmax(T &x, T y) {
    x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {
    x = min(x, y); } 
template <typename T> void read(T &x) {
    
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
    
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
    
	write(x);
	puts("");
}
bool prime(int x) {
    
	for (int i = 2; i * i <= x; i++)
		if (x % i == 0) return false;
	return true;
}
int main() {
    
	int n; read(n);
	if (n <= 2) {
    
		printf("%d\n", 1);
		for (int i = 1; i <= n; i++)
			printf("%d ", 1);
		return 0;
	}
	printf("%d\n", 2);
	for (int i = 1; i <= n; i++)
		printf("%d ", 1 + prime(i + 1));
	return 0;
}

**【C】**Molly’s Chemicals

【思路要点】

  • 枚举可能的序列和(至多 O ( L o g K ) O(LogK) O(LogK)种)。
  • 枚举右端点,维护一个以前缀和为下标的左端点map,查询对应合法的左端点的个数即可。
  • 时间复杂度 O ( N L o g N L o g V ) O(NLogNLogV) O(NLogNLogV)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T> void chkmax(T &x, T y) {
    x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {
    x = min(x, y); } 
template <typename T> void read(T &x) {
    
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
    
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
    
	write(x);
	puts("");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值