input
stringlengths
29
13k
output
stringlengths
9
73.4k
There is a city that can be represented as a square grid with corner points in (0, 0) and (10^6, 10^6). The city has n vertical and m horizontal streets that goes across the whole city, i. e. the i-th vertical streets goes from (x_i, 0) to (x_i, 10^6) and the j-th horizontal street goes from (0, y_j) to (10^6, y_j). ...
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; const int N = 3e5 + 5; const int M = 20 * N; void Evlos() { int n, m, k; cin >> n >> m >> k; vector<int> x(n), y(m); for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < m; i++) { cin >> y[i]; } long long ans = ...
2^k teams participate in a playoff tournament. The tournament consists of 2^k - 1 games. They are held as follows: first of all, the teams are split into pairs: team 1 plays against team 2, team 3 plays against team 4 (exactly in this order), and so on (so, 2^{k-1} games are played in that phase). When a team loses a g...
import sys def pr(x): sys.stdout.write(str(x)+'\n') def inp(): return sys.stdin.readline() M = 998244353 k, a, h = [int(i) for i in inp().split()] pp = [[],[2],[3,2],[5,3,2],[9,5,3,2],[17,9,5,3,2]] pa = [(a**i)%M for i in pp[k]] rgs = [[], [[1,3]], [[4,7], [1,5]], [[16, 21], [6,13], [1,9]], [[64, 73], [28, 41],...
You are given a simple undirected graph with n vertices, n is even. You are going to write a letter on each vertex. Each letter should be one of the first k letters of the Latin alphabet. A path in the graph is called Hamiltonian if it visits each vertex exactly once. A string is called palindromic if it reads the sam...
#include <bits/stdc++.h> using namespace std; int rev[1 << 20]; int dp[1 << 13][13][13]; int visit[1 << 13][13][13]; bool visit2[1 << 20]; int maps[13], c[13]; int a[13][13]; int n, m, r; long long ans = 0; int stage; vector<pair<int, int> > v[1 << 13][13][13]; vector<int> edges[1 << 20]; long long A(int r, int g) { ...
You are given a sequence a of length n consisting of 0s and 1s. You can perform the following operation on this sequence: * Pick an index i from 1 to n-2 (inclusive). * Change all of a_{i}, a_{i+1}, a_{i+2} to a_{i} βŠ• a_{i+1} βŠ• a_{i+2} simultaneously, where βŠ• denotes the [bitwise XOR operation](https://en.wikip...
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2") using namespace std; const long double PI = 3.14159265358979323846; const long double E = 2.71828182845904523536; const long long LINF = (long long)2e18; const int INF = (1 << 30); const l...
You are given a 1 by n pixel image. The i-th pixel of the image has color a_i. For each color, the number of pixels of that color is at most 20. You can perform the following operation, which works like the bucket tool in paint programs, on this image: * pick a color β€” an integer from 1 to n; * choose a pixel i...
#include <bits/stdc++.h> using namespace std; const int maxn = 3005; int T, n; int a[maxn], dp[maxn][maxn], pos[maxn]; vector<int> vec[maxn]; int calc(int l, int r) { if (l > r) return 0; int &ret = dp[l][r]; if (ret >= 0) return ret; ret = calc(l, r - 1); for (int i = pos[r] - 1; i >= 0; i--) { int p = v...
There are currently n hot topics numbered from 0 to n-1 at your local bridge club and 2^n players numbered from 0 to 2^n-1. Each player holds a different set of views on those n topics, more specifically, the i-th player holds a positive view on the j-th topic if i\ \&\ 2^j > 0, and a negative view otherwise. Here \& d...
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const long long mod = 1e9 + 7; inline long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } inline long long q_pow(long long a, long long x = mod - 2) { long long ans = 1, tmp = a; while (x) { if (x & 1) (ans *= tmp) %=...
You are given a strictly convex polygon with n vertices. You will make k cuts that meet the following conditions: * each cut is a segment that connects two different nonadjacent vertices; * two cuts can intersect only at vertices of the polygon. Your task is to maximize the area of the smallest region that ...
#include <bits/stdc++.h> #pragma GCC optimize(2, 3, "Ofast") #pragma GCC target("avx", "avx2") using namespace std; template <typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } int read() { int x = 0, f = 0; c...
There are n cities in a row numbered from 1 to n. The cities will be building broadcasting stations. The station in the i-th city has height h_i and range w_i. It can broadcast information to city j if the following constraints are met: * i ≀ j ≀ w_i, and * for each k such that i < k ≀ j, the following conditio...
#include <bits/stdc++.h> using namespace std; using LL = long long; const int MAXN = 200010; const int INF = 0x7fffffff; class SegTree { public: SegTree() {} void Build(int t, int l, int r) { if (l == r) { x[t] = sum[t] = ox[l]; return; } int mid = (l + r) >> 1; Build(t + t, l, mid); ...
You are given a digital clock with n digits. Each digit shows an integer from 0 to 9, so the whole clock shows an integer from 0 to 10^n-1. The clock will show leading zeroes if the number is smaller than 10^{n-1}. You want the clock to show 0 with as few operations as possible. In an operation, you can do one of the ...
import java.io.*; import java.util.*; public class Problem { public static void main(final String[] args) { final InputStream inputStream = System.in; final OutputStream outputStream = System.out; final FastScanner in = new FastScanner(inputStream); final FastPrintWriter out = new ...
You are given a book with n chapters. Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list. Currently you don't understand any of the chapters. You are going...
#include <bits/stdc++.h> using namespace std; string yes[2] = {"NO\n", "YES\n"}; const long long mod = long long(1e9) + 7; const long long inf = long long(1e16) + 1; bool testc = 1; const int N = 2e5 + 5; int n, i, to[N], x, j, dp[N], o; vector<int> adj[N]; void dfs(int x) { int j, y; for (j = 0; j < adj[x].size();...
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are r...
#include <bits/stdc++.h> using namespace std; long long const tax = 1e9 + 7; void setIO() {} long long xx; void helper(vector<string> &v, string s, int n, int m) { if (n == 0 && m == 0) { v.push_back(s); return; } if (v.size() > xx) return; if (n > 0) helper(v, s + "(", n - 1, m + 1); if (m > 0) helpe...
You are given four integer values a, b, c and m. Check if there exists a string that contains: * a letters 'A'; * b letters 'B'; * c letters 'C'; * no other letters; * exactly m pairs of adjacent equal letters (exactly m such positions i that the i-th letter is equal to the (i+1)-th one). Input The ...
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner scn = n...
Recently, Petya learned about a new game "Slay the Dragon". As the name suggests, the player will have to fight with dragons. To defeat a dragon, you have to kill it and defend your castle. To do this, the player has a squad of n heroes, the strength of the i-th hero is equal to a_i. According to the rules of the game...
import java.io.*; import java.util.*; public class Main { static MyScanner sc = new MyScanner(); static Output out=new Output(); public static void main(String[] args) { // int t=sc.nextInt(); // for(int i=0;i<t;i++) { solve(); // } out.flush(); } public static void solve() { int n=sc.nextInt(); lon...
Ivan is playing yet another roguelike computer game. He controls a single hero in the game. The hero has n equipment slots. There is a list of c_i items for the i-th slot, the j-th of them increases the hero strength by a_{i,j}. The items for each slot are pairwise distinct and are listed in the increasing order of the...
import java.io.*; import java.util.*; public class D { static int[][] mat; static int compare(int[] x, int[] y) { for (int i = 0; i < x.length; i++) { if (x[i] != y[i]) return x[i] - y[i]; } return 0; } static class node { int sum; int[] arr; public node(int s, int[] a) { sum = s; arr ...
A matrix of size n Γ— m, such that each cell of it contains either 0 or 1, is considered beautiful if the sum in every contiguous submatrix of size 2 Γ— 2 is exactly 2, i. e. every "square" of size 2 Γ— 2 contains exactly two 1's and exactly two 0's. You are given a matrix of size n Γ— m. Initially each cell of this matri...
#include <bits/stdc++.h> using namespace std; inline int read() { int s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + ch - '0', ch = getchar(); return s * f; } const int N = 1e6 + ...
A subarray of array a from index l to the index r is the array [a_l, a_{l+1}, ..., a_{r}]. The number of occurrences of the array b in the array a is the number of subarrays of a such that they are equal to b. You are given n arrays A_1, A_2, ..., A_n; the elements of these arrays are integers from 1 to k. You have to...
#include <bits/stdc++.h> using namespace std; template <long long mod> struct modular { long long value; modular(long long x = 0) { value = x % mod; if (value < 0) value += mod; } modular& operator+=(const modular& other) { if ((value += other.value) >= mod) value -= mod; return *this; } mod...