跳到正文

个人记录

10.21提高组模拟赛

__DragonFly约 7 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

[https://share.note.youdao.com/s/ZfivG2hb] (icebin的tj)

01 串 (binary)

有个规律:

2 ^ 0 mod 3 = 1

2 ^ 1 mod 3 = 2

2 ^ 2 mod 3 = 1

2 ^ 3 mod 3 = 2 …

发现2的次幕mod3是1,2,1,2交替,那么在二进制中相邻的两个1mod3=0,按照此规律构造即可。

正确代码

C++
#include <bits/stdc++.h>
using namespace std;

string mul(string s, int num) {
	string ans = "";
	for (int i = 1; i <= num; ++i) ans += s;
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		int n, m;
		cin >> n >> m;
		string mx, mn;
		if (n <= 1 || (n % 2 == 1 && m <= 1)) {
			cout << "-1\n-1\n";
			continue;
		}
		if (n % 2 == 0) {
			mx = mul("1", n) + mul("0", m);
			mn = "1" + mul("0", m / 2 * 2) + "1" + mul("0", m % 2) + mul("1", n - 2);
		}
		else if (n % 2 != 0) {
			mx = mul("1", n - 2) + "0101" + mul("0", m - 2);
			mn = "1" + mul("0", m / 2 * 2 - 1) + "101" + mul("0", m % 2) + mul("1", n - 3);
		}
		else {
			cout << -1 << "\n" << -1 << "\n";
			continue;
		}
		cout << mx << "\n" << mn << "\n";
	}
	return 0;
}

树 (tree)

依题意得,树上距离计算:dist[u] + dist[v] - 2 × dist[lca(u,v)];步数计算:ceil(总距离 / c) = (距离 + c - 1) / c

正确代码

C++
#include <bits/stdc++.h>
using namespace std;
struct node {
	int v, w;
}; 
vector <node> v[10005];
int to[10005], nx[10005][30];
int wa[10005];
int n, m;

void dfs(int x, int fa) {
	to[x] = to[fa] + 1;
	nx[x][0] = fa;
	for (int i = 1; i < 30; ++i) {
		nx[x][i] = nx[nx[x][i - 1]][i - 1];
	}
	for (int i = 0; i < v[x].size(); ++i) {
		int xx = v[x][i].v, w = v[x][i].w;
		if (xx == fa) continue;
		wa[xx] = wa[x] + w;
		dfs(xx, x);
	}
	return ;
}

int up(int x, int y) {
	if (to[x] < to[y]) swap(x, y);
	int going = 29;
	while (to[x] > to[y] && going >= 0) {
		if (to[nx[x][going]] >= to[y]) {
			x = nx[x][going];
		}
		going--;
	}
	going = 29;
	while (x != y && going >= 0) {
		if (nx[x][going] != nx[y][going]) {
			x = nx[x][going], y = nx[y][going];
		}
		going--;
	}
	if (x != y) x = nx[x][0], y = nx[y][0];
	return x;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n - 1; ++i) {
		int x, y, z;
		cin >> x >> y >> z;
		v[x].push_back(node{y, z});
		v[y].push_back(node{x, z});
	}
	dfs(1, 0);
	cin >> m;
	for (int i = 1; i <= m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		int going = up(a, b);
		int sum = wa[a] + wa[b] - wa[going] * 2;
		cout << ceil(sum * 1.0 / c) << "\n";
	}
	return 0;
}

T682365 括号序列(bracket)

用栈记录左括号位置,每次与右括号配对,记录配对的左括号的下标与右括号下标,用a数组和b数组分记录以i结尾的数量,以i开头的数量,最后统计答案,因为前面的括号配对或后面的括号配对i都是这些的子串。

正确代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5, mod = 1e9 + 7;
int ans[N], stk[N], up[N], m[N], n;
int a[N], b[N], top; 
char s[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> s + 1;
	n = strlen(s + 1); 
	for (int i = 1; i <= n; ++i) {
		if (s[i] == '(') {
			up[i] = stk[top];
			stk[++top] = i;
		}
		else if (top) {
			m[stk[top]] = i;
			m[i] = stk[top];
			top--;
		}
	}
	for (int i = 1; i <= n; ++i) {
		if (!m[i] || s[i] == '(') continue;
		b[i] = b[m[i] - 1] + 1;
	}
	for (int i = n; i >= 1; --i) {
		if (!m[i] || s[i] == ')') continue;
		a[i] = a[m[i] + 1] + 1;
	}
	for (int i = 1; i <= n; ++i) {
		if (!m[i] || s[i] == ')') continue;
		ans[i] = 1ll * a[i] * b[m[i]] % mod;
		if (up[i]) {
			ans[i] = (ans[i] + ans[up[i]]) % mod;
		} 
		ans[m[i]] = ans[i];
	}
	long long res = 0;
	for (int i = 1; i <= n; ++i) {
		res += 1ll * ans[i] * i % mod;
	}
	cout << res;
    return 0;
}
原文