跳到正文

个人记录

2022 CSP-J 总结

zengyukai2012约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年7月23日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 10 分钟

版本

  1. v1当前

T1 乘方

代码

C++
#include <bits/stdc++.h>
using namespace std;
int a, b;
long long t = 1;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> a >> b;
	// a = 1 时答案总是 1
	if (a == 1)
	{
		cout << "1\n";
		return 0;
	}
	// 暴力模拟
	for (int i = 1; i <= b; ++i)
	{
		t *= a;
		// 判断范围是否超限
		if (t > (long long)1e9)
		{
			cout << "-1\n";
			return 0;
		}
	}
	cout << t << '\n';
	return 0;
}

思路

由于 11 的任意次幂均为 11,所以需要特判。而 2log⁡2109=1092^{\log_2{{10}^9}}={10}^9,29≤log⁡2109≤3029\leq\log_2{{10}^9}\leq30,所以暴力枚举一定不会超时。时间复杂度上界为 O(log⁡ab)O(\log_a{b})。

T2 解密

代码

C++
#include <bits/stdc++.h>
using namespace std;
int k;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> k;
	for (int K = 1; K <= k; ++K)
	{
		long long n, e, d;
		cin >> n >> e >> d;
		// n = p * q
		// ed = p * q - p - q + 2
		// ed = n - p - q + 2
		long long j = n, h = n + 2 - e * d;
		long long sqr_c = h * h - j * 4;
		if (sqr_c >= 0)
		{
			long double c_d = sqrtl(sqr_c);
			if (c_d == floor(c_d))
			{
				long long c = c_d;
				long long p = (h - c) / 2, q = (h + c) / 2;
				if (p > 0 && q > 0)
					cout << p << ' ' << q << '\n';
				else
					cout << "NO\n";
			}
			else
				cout << "NO\n";
		}
		else
			cout << "NO\n";
	}
}

思路

根据题目条件推导:

             {ni=pi×qi,ei×di=(pi−1)(qi−1)+1         ⇒{ni=pi×qi,ei×di=pi×qi−pi−qi+2 ⇒{ni=pi×qi,ei×di=ni−pi−qi+2\ \ \ \ \ \ \ \ \ \ \ \ \ \left\{ \begin{array}{lr} n_i = p_i \times q_i, & \\ e_i \times d_i = (p_i-1)(q_i-1)+1 & \\ \end{array} \right. \newline \ \ \ \ \ \ \ \ \ \Rightarrow \left\{ \begin{array}{lr} n_i = p_i \times q_i, & \\ e_i \times d_i = p_i\times q_i-p_i-q_i+2 & \\ \end{array} \right. \newline \ \Rightarrow \left\{ \begin{array}{lr} n_i = p_i \times q_i, & \\ e_i \times d_i = n_i-p_i-q_i+2 & \\ \end{array} \right.

得出 pi×qip_i \times q_i 和 pi+qip_i+q_i 后,就可以算出 pi−qip_i-q_i,因为:

(pi+qi)2=pi2+2×pi×qi+qi2(qi−pi)2=qi2−2×qi×pi+pi2                 =pi2−2×pi×qi+qi2(p_i+q_i)^2=p_i^2+2\times p_i \times q_i + q_i^2 \newline (q_i-p_i)^2=q_i^2-2\times q_i \times p_i + p_i^2 \newline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =p_i^2-2\times p_i \times q_i + q_i^2

进而通过解二元一次方程组得出答案。

T3 逻辑表达式

代码

C++
#include <bits/stdc++.h>
using namespace std;
/// @brief 带计算的逻辑表达式
string s;
/// @brief andd 表示形如 a&b 的“短路”,orr 表示形如 a|b 的“短路”
int andd, orr;
/// @brief and_dep[i] 表示在深度 i 下的最后一个“&”的位置,or_dep[i] 表示在深度 i 下的最后一个“|”的位置
int and_dep[1000001], or_dep[1000001];
/// @brief and_ch[i] 表示在 s[i] 之前最后一个“&”的位置,or_ch[i] 表示在 s[i] 之前最后一个“|”的位置
int and_ch[1000001], or_ch[1000001];
/// @brief 当前深度
int depth;
/// @brief 计算函数
/// @param l 要计算的字符串的起始位置
/// @param r 要计算的字符串结束位置
/// @return 字符串 s 中 [l,r] 部分的计算结果
bool calc(int l, int r)
{
	// 先寻找最后一个或运算
	if (or_ch[r] > l /*确保在当前范围内*/ && s[or_ch[r]] == '|' /*确保不是默认值 0*/)
	{
		// 计算左半部分
		int l_val = calc(l, or_ch[r] - 1);
		// “短路”处理
		if (l_val == true)
		{
			++orr;
			return true;
		}
		// 计算右半部分
		int r_val = calc(or_ch[r] + 1, r);
		// 直接计算
		return l_val || r_val;
	}
	// 再寻找最后一个与运算
	if (and_ch[r] > l /*确保在当前范围内*/ && s[and_ch[r]] == '&' /*确保不是默认值 0*/)
	{
		// 计算左半部分
		int l_val = calc(l, and_ch[r] - 1);
		// “短路”处理
		if (l_val == false)
		{
			++andd;
			return false;
		}
		// 计算右半部分
		int r_val = calc(and_ch[r] + 1, r);
		// 直接计算
		return l_val && r_val;
	}
	// 注:因为与运算优先级更高,而递归是倒序处理的(回溯后再计算),所以应该先或运算
	// 去除外层括号
	if (s[l] == '(' && s[r] == ')')
		return calc(l + 1, r - 1);
	// 注:最后去除外层括号是因为外层括号不一定是一组,如:
	// (1|0)&(0&1)
	// 如果最先去除就会变成:
	// 1|0)&(0&1
	// 单字符处理
	if (l == r)
		return (s[(l + r) / 2] == '1');
	// 这行应该不会执行
	return false;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	// 输入,没有空格,无需 getline
	cin >> s;
	for (size_t i = 0; i < s.size(); ++i)
	{
		switch (s[i])
		{
		// 左括号增加深度
		case '(':
			++depth;
			break;
		// 右括号减小深度
		case ')':
			--depth;
			break;
		// 更新与运算当前深度的最后位置
		case '&':
			and_dep[depth] = i;
			break;
		// 更新或运算当前深度的最后位置
		case '|':
			or_dep[depth] = i;
			break;
		default:
			break;
		}
		// 更新与运算当前字符的最后位置
		and_ch[i] = and_dep[depth];
		// 更新或运算当前字符的最后位置
		or_ch[i] = or_dep[depth];
	}
	// 输出
	cout << calc(0, s.size() - 1) << '\n'
		 << andd << ' ' << orr << '\n';
}

思路

先 O(∣s∣)O(|s|) 预处理,求出每个字符前最后一个 & 字符和 | 字符的位置,然后递归处理。递归时优先处理 | 运算,因为进一步的处理在回溯之后,而回溯的顺序和调用的顺序相反。括号在最后剥离,因为如字符串 (1|0)&(0&1) 的最外层两个括号不是同一组,直接去除得到 1|0)&(0&1,不合法。

T4 上升点列

代码

C++
#include <bits/stdc++.h>
using namespace std;
int n, k;
struct node
{
	int x, y;
	bool operator<(const node &other)
	{
		return x < other.x || (x == other.x && y < other.y);
	}
} a[501];
int mx;
void dfs(int d, int s)
{
	mx = max(mx, s);
	for (int i = d + 1; i <= n; ++i)
		if ((a[i].x - a[d].x == 1 && a[i].y - a[d].y == 0) || (a[i].x - a[d].x == 0 && a[i].y - a[d].y == 1))
			dfs(i, s + 1);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i)
		cin >> a[i].x >> a[i].y;
	sort(a + 1, a + 1 + n);
	if (k == 0)
	{
		dfs(1, 1);
		cout << mx;
	}
	else
	{
		srand(time(0));
		srand(rand() + clock());
		cout << rand() % (n + k);
	}
	return 0;
}

思路

只实现了 k=0k = 0 的情况。使用 dfs 解决。因为欧几里得距离为 11 的两个点只能是上下两个或者左右两个,dfs 函数时间复杂度 O(2n×n)O(2^n \times n)。

原文