santisify Site

Back

AtCoder Beginner Contest 425Blur image

A Sigma Cubes#

题目描述#

输入NN,计算i=1N(1)i×i3\sum_{i=1}^N (-1)^i \times i^3

参考代码#

#include<bits/stdc++.h>

#define int long long
#define pii std::pair<int,int>

void solve() {
	int n;
	std::cin >> n;
	int res = 0;
	for(int i = 1; i <= n; i ++) {
		if(i & 1) res -= i * i * i;
		else res += i * i * i;
	}

	std::cout << res << std::endl;
}

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr), std::cout.tie(nullptr);
  solve();
}
cpp

B Find Permutation 2#

题目描述#

给出数组 AA, 判断是否存在一个排列 PP 使得满足:

  • PP是一个排列。
  • Pi=Ai(Ai1)P_{i}=A_{i}(A_{i} \neq -1), Pi=R(Ai=1)P{i} = R (A_{i} = -1)


若存在这样的排列,输出 PP

解题思路#

首先记录下 AA 中非 1-1 的其他数的个数记为 ctct,若cti2ct_{i} \ge 2 则不可能有合法的 PP
ctict_{i}00 的依次填入Ai=1A_{i}=-1 处即可获得排列 PP.

参考代码#

#include<bits/stdc++.h>

#define int long long
#define pii std::pair<int,int>

void solve() {
	int n;
	std::cin >> n;
	std::vector<int> a(n + 1, 0), b(n + 1, 0);
	for(int i = 1; i <= n; i ++) {
		std::cin >> b[i];
		if(b[i] != -1) {
			a[b[i]] ++;
		}
	}

	for(int i = 1; i <= n; i ++) {
		if(a[i] > 1) {
			std::cout << "No" << std::endl;
			return;
		}
	}
	std::cout << "Yes" << std::endl;
	std::queue<int> q;
	for(int i = 1; i <= n; i ++) {
		if(!a[i]) {
			q.push(i);
		}
	}
	for(int i = 1; i <= n; i ++) {
		if(b[i] == -1) {
			std::cout << q.front() << ' ';
			q.pop();
		}else {
			std::cout << b[i] << ' ';
		}
	}
}

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr), std::cout.tie(nullptr);
  solve();
}
cpp

C Rotate and Sum Query#

题目描述#

给出数组 AAqq次查询,每次查询如下:

  • 11   cc: 循环 cc 次,每次循环都将 A1A_{1} 放在数组末尾.
  • 22   ll   rr: 查询区间[l,r][l, r]的总和

数据范围:

  • 1N2×1051\le N\le 2\times 10^5
  • 1Q2×1051\le Q\le 2\times 10^5
  • 1Ai1091\le A_i \le 10^9
  • 1cN1\le c\le N
  • 1lrN1\le l\le r \le N

解题思路#

可以发现数据范围较大,无法使用rotate去模拟,但是可以发现每次循环次数 ctct 到达 nn 后就会回到初始数组,区间总和也只与ct%nct \% n有关.
我们可以使用变化后的区间 [l,r][l, r]ctct 去找变化前的区间 [x,y][x, y],可以发现需要预处理前缀和 ss.
但是变化前的区间可能会出现 x>yx > y,这个区间[y,x][y, x]的值则是区间外的总和.

参考代码#

#include<bits/stdc++.h>

#define int long long
#define pii std::pair<int,int>

void solve() {
	int n, q;
	std::cin >> n >> q;
	std::vector<int> s(n + 1, 0), a(n + 1, 0);
	for (int i = 0; i < n; i++) {
		std::cin >> a[i];
		s[i] = a[i];
		if (i) s[i] = s[i - 1] + a[i];
	}
	int t, x, y, ct = 0;
	while (q--) {
		std::cin >> t >> x;
		if (t == 1) {
			ct += x;
		} else {
			std::cin >> y;
			x = (x - 1 + ct) % n;
			y = (y - 1 + ct) % n;
			if (x > y) {
				std::cout << s[y] + s[n - 1] - (x ? s[x - 1] : 0) << std::endl;
			} else {
				std::cout << s[y] - (x ? s[x - 1] : 0) << std::endl;
			}
		}
	}
}

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr), std::cout.tie(nullptr);
	solve();
}
cpp
AtCoder Beginner Contest 425
https://santisify.top/blog/atcoder/abc425
Author santisify
Published at October 14, 2025
Comment seems to stuck. Try to refresh?✨