Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

AtCoder Regular Contest Problem Selection

Tech Apr 18 10

Introduction

This article presents a curated selection of problems from various AtCoder Regular Contest (ARC) editions. Each problem is analyzed and rewritten to preserve the core concepts while reducing similarity to the original content. The focus is on problem-solving techniques, algorithmic approaches, and code optimization strategies.

Problem E: Pairing Wizards (ARC142)

This problem involves graph theory and network flow techniques. The key idea is to model the problem as a bipartite graph and apply minimum cut techniques to determine the optimal pairing strategy. The solution leverages the properties of graph nodes and edges to efficiently compute the required constraints.

Key Concepts:

  • Bipartite Graph
  • Minimum Cut
  • Flow Networks

Code Example:


#include <cstdio>
#include <algorithm>
#include <atcoder/maxflow.hpp>

using namespace std;
int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int N = 103;
int n, m;
int a[N], b[N];
vector<int> vec[N];
bool sp[N];
int id[N], cnt;

int main() {
   n = read();
   for (int i = 1; i <= n; ++i) {
       a[i] = read();
       b[i] = read();
   }
   m = read();
   for (int i = 1; i <= m; ++i) {
       int u = read(), v = read();
       vec[u].emplace_back(v);
       vec[v].emplace_back(u);
   }

   int res = 0;
   int s = cnt++, t = cnt++;

   // Problem-specific logic and flow computation
   // ...

   printf("%d\n", res + mf.flow(s, t));
   return 0;
}
   

Problem D: Deterministic Placing (ARC142)

This problem revolves around tree decomposition and dynamic programming. The solution involves transforming the tree into a set of chains while ensuring specific constraints on chain endpionts. Tree DP is used to count valid configurations efficiently.

Key Concepts:

  • Tree Decomposition
  • Dynamic Programming
  • Chain Constraints

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int N = 200003, P = 998244353;
int hd[N], ver[N << 1], nxt[N << 1], tot;
inline void add(int u, int v) {
   nxt[++tot] = hd[u];
   hd[u] = tot;
   ver[tot] = v;
}

int n;
int f[N], g[N], h[N], s[N];

void dfs(int u, int fa) {
   h[u] = 1;
   int prod = 1, tmp = 0;
   for (int i = hd[u]; i; i = nxt[i]) {
       int v = ver[i];
       if (v == fa) continue;
       dfs(v, u);
       f[u] = ((ll)h[u] * (g[v] + h[v]) + (ll)f[u] * f[v]) % P;
       g[u] = ((ll)prod * (g[v] + h[v]) + (ll)g[u] * s[v]) % P;
       s[u] = ((ll)tmp * (g[v] + h[v]) + (ll)s[u] * s[v]) % P;
       tmp = ((ll)prod * (g[v] + h[v]) + (ll)tmp * s[v]) % P;
       prod = (ll)prod * s[v] % P;
       h[u] = (ll)h[u] * f[v] % P;
   }
   s[u] <<= 1;
   if (s[u] >= P) s[u] -= P;
}

int main() {
   n = read();
   for (int i = 1; i < n; ++i) {
       int u = read(), v = read();
       add(u, v);
       add(v, u);
   }
   dfs(1, 0);
   int res = f[1];
   res <<= 1;
   if (res >= P) res -= P;
   res += s[1];
   if (res >= P) res -= P;
   printf("%d\n", res);
   return 0;
}
   

Problem F: Well-defined Abbreviation (ARC141)

This problem focuses on string manipulation and automata theory. The goal is to determine whether a given set of strings can be reduced to an empty string using a set of rules. The solution involves building an automaton and checking for reducibility.

Key Concepts:

  • String Reduction
  • Automata Theory
  • Prefix Matching

Code Example:


#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>

using namespace std;
const int N = 1000003, M = 2000003;
__gnu_pbds::gp_hash_table<int, int> mp[M];
int n, len[N];
char *s[N], ss[M];
int stk[M], tp;

int main() {
   scanf("%d", &n);
   for (int i = 1; i <= n; ++i) {
       scanf("%s", ss);
       len[i] = strlen(ss);
       s[i] = new char[len[i]];
       memcpy(s[i], ss, len[i]);
       int p = 1;
       for (int j = 0; j < len[i]; ++j) {
           int c = ss[j] - 'A';
           if (!tr[p][c]) de[tr[p][c] = ++cnt] = j + 1;
           p = tr[p][c];
       }
       ed[pos[i] = p] = 1;
   }

   // Further logic for automaton traversal and reduction
   // ...

   return 0;
}
   

Problem E: Sliding Edge on Torus (ARC141)

This problem involves graph theory and number theory. The key idea is to model the problem as a graph with modular arithmetic properties and use a union-find data structure with weights to maintain connectivity and cycle constraints.

Key Concepts:

  • Graph Theory
  • Union-Find with Weights
  • Modular Arithmetic

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int N = 200003;
int n, q;
int f[N], g[N], w[N];

int gcd(int a, int b) {
   if (!b) return a;
   return gcd(b, a % b);
}

inline void link(int u) {
   if (f[u] == u) return;
   link(f[u]);
   w[u] = (w[u] + w[f[u]]) % n;
   f[u] = f[f[u]];
}

int main() {
   n = read();
   q = read();
   ll res = 0;
   for (int i = 0; i < n; ++i) {
       f[i] = i;
       g[i] = n;
       w[i] = 0;
       res += n;
   }

   for (int i = 0; i < q; ++i) {
       int a = read(), b = read(), c = read(), d = read();
       int x = (b - a + n) % n;
       int y = (d - c + n) % n;
       int z = (d - b + n) % n;

       // Logic for union-find and cycle constraint handling
       // ...
   }

   printf("%lld\n", res);
   return 0;
}
   

Problem D: Non-divisible Set (ARC141)

This problem involves combinatorics and partial order theory. The goal is to construct a set of integers that avoids divisibility constraints. The solution uses topological sorting and dynamic programming to determine valid configurations.

Key Concepts:

  • Partial Order
  • Topological Sorting
  • Dynamic Programming

Code Example:


#include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 1000003, INF = 0x3f3f3f3f;
int hd[N], ver[N << 1], nxt[N << 1], tot;
inline void add(int u, int v) {
   nxt[++tot] = hd[u];
   hd[u] = tot;
   ver[tot] = v;
}

int n, m;
int a[N], b[N];
vector<int> vec[N];
vector<int> F[N], G[N];
int df[N], dg[N];
int f[N], g[N];

int main() {
   n = read();
   m = read();
   for (int i = 0; i < m; ++i) vec[i].emplace_back(-INF);
   for (int i = 1; i <= n; ++i) {
       a[i] = read();
       while (a[i] % 2 == 0) a[i] >>= 1, ++b[i];
       vec[a[i] >>= 1].emplace_back(b[i]);
   }

   // Logic for constructing and validating the set
   // ...

   return 0;
}
   

Problem F: ABS Permutation (ARC140)

This problem involves permutations and generating functions. The solution uses exponential generating functions (EGF) and combinatorial techniques to count valid permutations.

Key Concepts:

  • Permutations
  • Generating Functions
  • Combinatorics

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

// Placeholder for permutation and generating function logic
int main() {
   // Implementation details
   return 0;
}
   

Problem E: Not Equal Rectangle (ARC140)

This problem involves matrix construction and combinatorial design. The solution uses a number-theoretic approach to construct a valid matrix where no four corners of a submatrix have the same value.

Key Concepts:

  • Matrix Construction
  • Combinatorial Design
  • Number Theory

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int B = 23, N = 529;
int n, m;
int f[N][N];

int main() {
   for (int i = 0; i < B; ++i)
       for (int j = 0; j < B; ++j)
           for (int k = 0; k < B; ++k) {
               int x = (i + k) % B;
               for (int t = 0; t < B; ++t) {
                   f[i * B + j][t * B + x] = k;
                   x = (x + j) % B;
               }
           }

   n = read();
   m = read();
   for (int i = 0; i < n; ++i) {
       for (int j = 0; j < m; ++j) printf("%d ", f[i][j] + 1);
       putchar('\n');
   }
   return 0;
}
   

Problem D: One to One (ARC140)

This problem involves graph theory and counting connected components. The solution uses union-find to track cycles and dynamic programming to count valid configurations of chains.

Key Concepts:

  • Graph Theory
  • Union-Find
  • Dynamic Programming

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int N = 2003;
int f[N];

int rt(int x) {
   if (f[x] == x) return x;
   return f[x] = rt(f[x]);
}

int main() {
   n = read();
   for (int i = 1; i <= n; ++i) f[i] = i;
   for (int i = 1; i <= n; ++i) {
       int x = read();
       if (~x) merge(x, i);
   }

   // Additional logic for component tracking
   // ...

   return 0;
}
   

Problem E: Decreasing Subsequence (ARC138)

This problem involves combinatorics and sequence analysis. The solution uses graph theory to model sequences as chains and applies combinatorial counting to determine valid subsequences.

Key Concepts:

  • Graph Theory
  • Combinatorics
  • Sequence Analysis

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int N = 5003;
int n, k;
int s[N][N], c[N][N], b[N];

int main() {
   n = read() + 1;
   k = read();
   c[0][0] = s[0][0] = b[0] = 1;
   for (int i = 1; i <= n; ++i) {
       c[i][0] = 1;
       for (int j = 1; j <= i; ++j) {
           s[i][j] = (s[i - 1][j - 1] + (ll)s[i - 1][j] * j) % P;
           c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
           b[i] += s[i][j];
           if (b[i] >= P) b[i] -= P;
       }
   }

   // Additional logic for subsequence counting
   // ...

   return 0;
}
   

Problem E: Wazir (ARC139)

This problem involves grid coloring and combinatorial counting. The solution uses dynamic programming and matrix exponentiation to count valid configurations of a grid under specific constraints.

Key Concepts:

  • Grid Coloring
  • Dynamic Programming
  • Matrix Exponentiation

Code Example:


#include <cstdio>
#include <algorithm>

using namespace std;

const int P = 998244353;
typedef long long ll;

ll n, m;
typedef long long ll;

int main() {
   scanf("%lld%lld", &n, &m);
   if ((n & 1) && (m & 1)) {
       if (n > m) swap(n, m);
       // Implementation for odd dimensions
       // ...
   }

   return 0;
}
   

Problem D: Priority Queue 2 (ARC139)

This problem involves probability and dynamic programming. The solution uses prefix sums and combinatorial calculations to determine the expected value of a priority queue operation.

Key Concepts:

  • Probability
  • Dynamic Programming
  • Combinatorics

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

const int N = 2003;
int n, m, k, s, res;
int a[N], pw[N];
int c[N][N];

int main() {
   n = read();
   m = read();
   k = read();
   s = n + 1 - read();
   for (int i = 1; i <= n; ++i) a[i] = read();
   // Logic for probability calculation
   // ...

   return 0;
}
   

Problem F: Overlaps (ARC137)

This problem involves probability and combinatorics. The solution uses generating functions and inclusion-exclusion principles to count valid configurations of overlapping intervals.

Key Concepts:

  • Generating Functions
  • Inclusion-Exclusion Principle
  • Combinatorics

Code Example:


#include <array>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

// Implementation details for generating functions
// ...

int main() {
   n = read();
   k = read();
   // Logic for generating function manipulation
   // ...

   return 0;
}
   

Problem E: Bakery (ARC137)

This problem involves flow networks and minimum cost flow. The solution uses a minimum cost flow algorithm to model and solve the bakery distribution problem.

Key Concepts:

  • Flow Networks
  • Minimum Cost Flow
  • Graph Modeling

Code Example:


#include <cstdio>
#include <atcoder/mincostflow.hpp>

using namespace std;
using namespace atcoder;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

int main() {
   int n = read();
   int m = read();
   int d = read();
   // Implementation of minimum cost flow
   // ...

   return 0;
}
   

Problem F: Flip Cells (ARC136)

This problem involves linear algebra and generating functions. The solution uses generating functions and matrix operations to model and solve the flipping process.

Key Concepts:

  • Generating Functions
  • Linear Algebra
  • Matrix Operations

Code Example:


#include <cstdio>

using namespace std;

int read() {
   char c = getchar();
   int x = 0;
   while (c < 48 || c > 57) c = getchar();
   do x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
   while (c >= 48 && c <= 57);
   return x;
}

// Implementation for flipping process
// ...

int main() {
   // Logic for flipping process
   // ...

   return 0;
}
   

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.