传送门
A. Sakurako’s Exam
分析
- 由于出现的数中只有 1 1 1 和 2 2 2,所以 1 1 1 只能由 1 1 1 消除,因此如果有奇数个 1 1 1 的话,那么肯定无法 = 0 =0 =0,然后再对 2 2 2 的个数进行奇偶判断即可
/*******************************
| Author: xiaojianhua1110
| Problem: A. Sakurako's Exam
| Contest: Codeforces Round 970 (Div. 3)
| URL: https://codeforces.com/contest/2008/problem/A
| When: 2024-09-01 22:35:20
|
| Memory: 256 MB
| Time: 1000 ms
*******************************/
#include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using vi32 = std::vector<int>;
using pii32 = std::pair<int, int>;
using vii32 = std::vector<pii32>;
using vi64 = std::vector<i64>;
using pii64 = std::pair<i64, i64>;
using vii64 = std::vector<pii64>;
void solve() {
int a, b;
std::cin >> a >> b;
if(a == 0) {
if(b % 2 == 0) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
} else if(b == 0) {
if(a % 2 == 0) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
} else {
if(a % 2 == 0 && b % 2 == 0) {
std::cout << "YES\n";
} else if(a % 2 == 1 && b % 2 == 1) {
std::cout << "NO\n";
} else if(a % 2 == 1 && b % 2 == 0) {
std::cout << "NO\n";
} else {
std::cout << "YES\n";
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
std::cin >> t;
while(t--) {
solve();
}
return 0;
}
B. Square or Not
分析
- 要注意到题目说明的是 s q u a r e square square,也就是说最终得到的 m a t r i x matrix matrix一定是个正方形,那么我们可以先得到边长,然后再判断 0 0 0的个数是否满足要求即可(The string is always the result of writing out the strings of a beautiful matrix.)
/*******************************
| Author: xiaojianhua1110
| Problem: B. Square or Not
| Contest: Codeforces Round 970 (Div. 3)
| URL: https://codeforces.com/contest/2008/problem/B
| When: 2024-09-01 22:40:31
|
| Memory: 256 MB
| Time: 2000 ms
*******************************/
#include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using vi32 = std::vector<int>;
using pii32 = std::pair<int, int>;
using vii32 = std::vector<pii32>;
using vi64 = std::vector<i64>;
using pii64 = std::pair<i64, i64>;
using vii64 = std::vector<pii64>;
void solve() {
int n;
std::cin >> n;
std::string s;
std::cin >> s;
int cnt = std::count(all(s), '1');
int ok = 0;
int c = sqrt(n);
int sum = c * 4 - 4;
if(sum == cnt) {
ok = 1;
}
if(ok) {
std::cout << "Yes\n";
} else {
std::cout << "No\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
std::cin >> t;
while(t--) {
solve();
}
return 0;
}
C. Longest Good Array
分析
- 这题可以用贪心的做法,对于 a 1 a_{1} a1,为了满足要求,应该让 a 2 − a 1 a_{2} - a_{1} a2−a1尽量小,同时让 a 3 − a 2 a_{3} - a_{2} a3−a2也尽量小并且满足题给条件,结合测试样例来看,以 1 , 2 , 3... 1, 2, 3... 1,2,3...的幅度为增加最是最优的,因此只需要注意判断一下边界条件即可
/*******************************
| Author: xiaojianhua1110
| Problem: C. Longest Good Array
| Contest: Codeforces Round 970 (Div. 3)
| URL: https://codeforces.com/contest/2008/problem/C
| When: 2024-09-01 22:46:35
|
| Memory: 256 MB
| Time: 2000 ms
*******************************/
#include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using vi32 = std::vector<int>;
using pii32 = std::pair<int, int>;
using vii32 = std::vector<pii32>;
using vi64 = std::vector<i64>;
using pii64 = std::pair<i64, i64>;
using vii64 = std::vector<pii64>;
void solve() {
int l, r;
std::cin >> l >> r;
int gap = 1;
int cur = l;
int ans = 0;
while(cur < r) {
if(cur + gap <= r) {
cur += gap;
gap++;
ans++;
} else {
break;
}
}
std::cout << ans + 1 << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
std::cin >> t;
while(t--) {
solve();
}
return 0;
}
D. Sakurako’s Hobby
分析
- 由于题给的是一个排列,每个数只会出现一次,因此任意数在有限次内都会回到自身,并且该过程所经过的数也同样会经过同样的数,只不过是顺序不一,因此可以用 D S U DSU DSU将属于同一组的数并起来,最后只需要查找 s i z siz siz即可
/*******************************
| Author: xiaojianhua1110
| Problem: D. Sakurako's Hobby
| Contest: Codeforces Round 970 (Div. 3)
| URL: https://codeforces.com/contest/2008/problem/D
| When: 2024-09-01 22:50:59
|
| Memory: 256 MB
| Time: 2000 ms
*******************************/
#include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using vi32 = std::vector<int>;
using pii32 = std::pair<int, int>;
using vii32 = std::vector<pii32>;
using vi64 = std::vector<i64>;
using pii64 = std::pair<i64, i64>;
using vii64 = std::vector<pii64>;
struct DSU
{
std::vector<int> f,siz;
DSU() {}
DSU(int n){
init(n);
}
void init(int n){
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, -1);
}
int find(int x){
while(x!=f[x]){
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x,int y){
return find(x) == find(y);
}
bool merge(int x,int y){
x = find(x);
y = find(y);
if(x == y){
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x){
return siz[find(x)];
}
};
void solve() {
int n;
std::cin >> n;
vi32 a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::string s;
std::cin >> s;
DSU dsu(n);
for(int i = 0; i < n; i++) {
if(s[i] == '0') {
dsu.siz[i] = 1;
} else {
dsu.siz[i] = 0;
}
}
for(int i = 0; i < n; i++) {
dsu.merge(i, a[i] - 1);
}
for(int i = 0; i < n; i++) {
std::cout << dsu.siz[dsu.find(i)] << " ";
}
std::cout << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
std::cin >> t;
while(t--) {
solve();
}
return 0;
}
F. Sakurako’s Box
分析
- 题目意思很明确,让我们求所有数两两相乘然后除以个数 % 1 0 9 + 7 10^9 + 7 109+7即可,对于求两两相乘,可以用前缀和进行优化,个数就是个等差数列的公式即可
/*******************************
| Author: xiaojianhua1110
| Problem: F. Sakurako's Box
| Contest: Codeforces Round 970 (Div. 3)
| URL: https://codeforces.com/contest/2008/problem/F
| When: 2024-09-01 22:51:01
|
| Memory: 256 MB
| Time: 2000 ms
*******************************/
#include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using vi32 = std::vector<int>;
using pii32 = std::pair<int, int>;
using vii32 = std::vector<pii32>;
using vi64 = std::vector<i64>;
using pii64 = std::pair<i64, i64>;
using vii64 = std::vector<pii64>;
template<class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr i64 mul(i64 a, i64 b, i64 p) {
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 998244353;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 1000000007;
using Z = MInt<P>;
void solve() {
int n;
std::cin >> n;
std::vector<Z> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::vector<Z> b(n);
b[0] = a[0];
for(int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
}
Z ans = 0;
for(int i = 1; i < n; i++) {
ans += a[i] * b[i - 1];
}
Z cnt = Z(n) * Z(n - 1) / Z(2);
std::cout << ans / cnt << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
std::cin >> t;
while(t--) {
solve();
}
return 0;
}