contest_id
stringclasses
33 values
problem_id
stringclasses
14 values
statement
stringclasses
181 values
tags
listlengths
1
8
code
stringlengths
21
64.5k
language
stringclasses
3 values
1311
E
E. Construct the Binary Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and dd. You need to construct a rooted binary tree consisting of nn vertices with a root at the vertex 11 and the sum of depths of all vertices equals to dd.A t...
[ "brute force", "constructive algorithms", "trees" ]
// just for fun #include <bits/stdc++.h> #define rep(i, x, y) for (int i = (x); i <= (y); i+=1) #define epr(i, x) for (int i = head[x]; i; i = nxt[i]) #define per(i, x, y) for (int i = (x); i >= (y); i-=1) #define DC int T = gi <int> (); while (T--) #define eb emplace_back #define ep emplace #define pb push_back #defi...
cpp
1303
D
D. Fill The Bagtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a bag of size nn. Also you have mm boxes. The size of ii-th box is aiai, where each aiai is an integer non-negative power of two.You can divide boxes into two parts of equal size. Your goal is t...
[ "bitmasks", "greedy" ]
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long m; cin >> m; int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long sum = ac...
cpp
1311
C
C. Perform the Combotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons ...
[ "brute force" ]
//#include<bits/stdc++.h> #include<algorithm> #include<cstdio> #include<sstream> #include<cstdlib> #include<cctype> #include<cmath> #include<set> #include<queue> #include<stack> #include<list> #include<iostream> #include<fstream> #include<numeric> #include<string> #include<vector> #include<cstring> #in...
cpp
1301
D
D. Time to Runtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going...
[ "constructive algorithms", "graphs", "implementation" ]
#include <bits/stdc++.h> #define int long long using namespace std; int n, m, k, i, j; vector<pair<int, string>> ans; signed main() { cin.tie(0)->sync_with_stdio(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif cin >> n >> m >> k; if (k > 4 * n * m - 2 * n - 2 * m) { cout << "NO"; retu...
cpp
1312
A
A. Two Regular Polygonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm (m<nm<n). Consider a convex regular polygon of nn vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) an...
[ "geometry", "greedy", "math", "number theory" ]
#include<bits/stdc++.h> #define ll long long using namespace std; double pi=3.1415926535897932384626433832795; int fuc(int n){ int sum=0; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; ...
cpp
1312
D
D. Count the Arraystime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYour task is to calculate the number of arrays such that: each array contains nn elements; each element is an integer from 11 to mm; for each array, there is exactly one pair of equal elements; f...
[ "combinatorics", "math" ]
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // everything related to pbds // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // typedef int node; // change type here // typedef tree<node, null_type, less<node>, // rb_tree_tag, tree_...
cpp
1141
E
E. Superhero Battletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA superhero fights with a monster. The battle consists of rounds; each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.E...
[ "math" ]
#include <bits/stdc++.h> using namespace std; #define E 1e-9 #define PI 3.141592653589793238462 #define F first #define S second #define PB push_back #define EB emplace_back #define MP make_pair #define INF 1e18 #define MOD 1000000007 #define SZ(a) int((a).size()) #define setbits(a) (__builtin_popcountll(a)...
cpp
1303
G
G. Sum of Prefix Sumstime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWe define the sum of prefix sums of an array [s1,s2,…,sk][s1,s2,…,sk] as s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk)s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk).You are given a tree consisting of nn vertices. Eac...
[ "data structures", "divide and conquer", "geometry", "trees" ]
#include "bits/stdc++.h" signed main() { using namespace std; std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #ifdef DEBUG freopen("Input.txt", "r", stdin); #endif static int n; std::cin >> n; static std::vector<std::vector<int>> g(n); for (int i = 0; i < n - 1; i++) { ...
cpp
1307
A
A. Cow and Haybalestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of nn haybale piles on the farm. The ii-th pile contains aiai haybales. However, Farmer John has just left for vac...
[ "greedy", "implementation" ]
#include "bits/stdc++.h" #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; #ifdef LOCAL #include "debug.h" #endif con...
cpp
1295
C
C. Obtain The Stringtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two strings ss and tt consisting of lowercase Latin letters. Also you have a string zz which is initially empty. You want string zz to be equal to string tt. You can perform the followi...
[ "dp", "greedy", "strings" ]
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(0); cin.tie(0); typedef long long int ll; const int N = 1e5+9; void solve() { string s1, s2; cin >> s1 >> s2; map<char, vector<int>> mp; for(int i=0;i<s1.size();i++) { mp[s1[i]].push_back(i+1); } i...
cpp
1299
A
A. Anu Has a Functiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9f(11,6)=(11|6)−6=15−6=9. It can be proved that for an...
[ "brute force", "greedy", "math" ]
#include <bits/stdc++.h> using namespace std; #define pb push_back //typedef __int128 lll; #define ll long long #define all(x) x.begin(),x.end() #define repp(n) for(int i=0;i<n;i++) const ll MOD=998244353; const ll mod = 1e9 +7; map<ll,ll> mp; void f(ll x){ ll bit = 0; while(x>0){ ...
cpp
1295
D
D. Same GCDstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers aa and mm. Calculate the number of integers xx such that 0≤x<m0≤x<m and gcd(a,m)=gcd(a+x,m)gcd(a,m)=gcd(a+x,m).Note: gcd(a,b)gcd(a,b) is the greatest common divisor of aa and bb.I...
[ "math", "number theory" ]
# include<bits/stdc++.h> using namespace std; typedef long long int ll; int main () { ll t; cin>>t; while(t--) { ll a,n; cin>>a>>n; n/=__gcd(a,n); ll ans=n; for(ll i=2;i*i<=n;i++) { if(n%i==0) { ...
cpp
1284
D
D. New Year and Conferencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputFilled with optimism; Hyunuk will host a conference about how great this new year will be!The conference will have nn lectures. Hyunuk has two candidate venues aa and bb. For each of the nn l...
[ "binary search", "data structures", "hashing", "sortings" ]
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; using ull=unsigned long long; ull S[N],val[N]; const ull h1=1237123,h2=19260817; ull H(ull x) {return x*x*x*h1+h2;}ull f(ull x){return H(x&((1<<31)-1))+H(x>>31);} void solve() { int n;scanf("%d",&n); vector<array<int,3>>a1,a2; for(i...
cpp
1315
C
C. Restoring Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence b1,b2,…,bnb1,b2,…,bn. Find the lexicographically minimal permutation a1,a2,…,a2na1,a2,…,a2n such that bi=min(a2i−1,a2i)bi=min(a2i−1,a2i), or determine that it is impossib...
[ "greedy" ]
#define RAGHAV_PATEL ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); #include <bits/stdc++.h> #include <algorithm> using namespace std; #define int long long int void solve() { int n; cin >> n; vector<int> v(n); vector<int> fv((2 * n) + 1); for (int i = 0; i < n...
cpp
1320
B
B. Navigation Systemtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe map of Bertown can be represented as a set of nn intersections, numbered from 11 to nn and connected by mm one-way roads. It is possible to move along the roads from any intersection to any othe...
[ "dfs and similar", "graphs", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; #define MAX (int) 2e5 #define INF INT_MAX int distanceTo[MAX]; map<int, vector<int>> bertown1; map<int, vector<int>> bertown2; void BFS(int source) { int parent; set<int> visited; queue<int> toVisit; toVisit.push(source); visited.insert(source); while(!toVi...
cpp
1301
D
D. Time to Runtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going...
[ "constructive algorithms", "graphs", "implementation" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <random> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define AboT...
cpp
1295
F
F. Good Contesttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn online contest will soon be held on ForceCoders; a large competitive programming platform. The authors have prepared nn problems; and since the platform is very popular, 998244351998244351 coder from ...
[ "combinatorics", "dp", "probabilities" ]
#include <bits/stdc++.h> using namespace std; typedef long long int ll; # define mod 998244353 ll a[1000],b[1000],c[1000]; ll dp[1010][1010],g[1010],inv[1010]; ll qp(ll base, ll pow) { ll ans=1; while(pow) { if(pow&1) ans=ans*base%mod; pow>>=1; base=base*bas...
cpp
1312
C
C. Adding Powerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSuppose you are performing the following algorithm. There is an array v1,v2,…,vnv1,v2,…,vn filled with zeroes at start. The following operation is applied to the array several times — at ii-th step (00-...
[ "bitmasks", "greedy", "implementation", "math", "number theory", "ternary search" ]
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define vl vector<ll> #define pl pair<ll, ll> #define vi vector<int> #define pi pair<int, int> #define ff first #define ss second #define pb push_back #define ppb pop_back #define mp make_pair #define all(x) x.begin()...
cpp
1284
F
F. New Year and Social Networktime limit per test4 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputDonghyun's new social network service (SNS) contains nn users numbered 1,2,…,n1,2,…,n. Internally, their network is a tree graph, so there are n−1n−1 direct connections between each user....
[ "data structures", "graph matchings", "graphs", "math", "trees" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define N 250100 ll n; ll du[N]; queue<ll>q; ll v[N<<1],fir[N],nxt[N<<1],cnt=0; inline void add(ll x,ll y){ v[++cnt]=y;nxt[cnt]=fir[x];fir[x]=cnt; return ; } bool vis[N]; ll fa[N]; inline ll getf(ll x){ if(fa[x]==x)return x; return ...
cpp
1324
A
A. Yet Another Tetris Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given some Tetris field consisting of nn columns. The initial height of the ii-th column of the field is aiai blocks. On top of these columns you can place only figures of size 2×12...
[ "implementation", "number theory" ]
#include <iostream> #include <ranges> #include <algorithm> #include <numeric> #include <vector> #include <array> #include <set> #include <map> #include <bit> #include <sstream> #include <list> #include <stack> #include <queue> typedef long long integerType; typedef integerType z; typedef std::vector<in...
cpp
1299
C
C. Water Balancetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn water tanks in a row, ii-th of them contains aiai liters of water. The tanks are numbered from 11 to nn from left to right.You can perform the following operation: choose some subsegment [l...
[ "data structures", "geometry", "greedy" ]
#include<bits/stdc++.h> using namespace std; const int o=1e6+3; int n,b[o],q; double a[o]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n; for (int i=1,y,z;i<=n;i++){ double x; cin>>z,x=z,y=1; while (q&&a[q]/b[q]>x/y) x+=a[q],y+=b[q],q--; a[++q]=x,b[q]=y; } for (int i=1;i<=q;i++) for (int j=1;j<=b[i];j++) ...
cpp
1296
D
D. Fight with Monsterstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal...
[ "greedy", "sortings" ]
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define S second #define F first #define pb push_back using namespace std; typedef long long ll; const int N = 2e5 + 123; ll n, a, b, k; ll c[N]; int main() { cin >> n >> a >> b >> k; for(int i = 1; i <= n; i++) { cin >> c[i]; ...
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type...
[ "data structures", "dfs and similar", "dp" ]
#include <bits/stdc++.h> using namespace std; int ch[1000005][26],dfnow,id[1000005],ans[1000005],dep[1000005]; multiset<int> s; void dfs(int u) { int nw=dep[u]-dfnow; if(id[u]) { ans[id[u]]=min(dep[u],*s.begin()+dfnow+1); dep[u]=min(dep[u],ans[id[u]]); nw=dep[u]-dfnow; ++dfnow; } s.insert(nw)...
cpp
1285
B
B. Just Eat It!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Yasser and Adel are at the shop buying cupcakes. There are nn cupcake types, arranged from 11 to nn on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type ii i...
[ "dp", "greedy", "implementation" ]
#include<bits/stdc++.h> #define ll long long #define el '\n' using namespace std; void solve(){ int n; cin >> n; vector<ll>a(n + 1), pre(n + 1); for (int i = 1; i <= n; i++){ cin >> a[i]; } for (int i = 1; i <= n; i++){ pre[i] += pre[i - 1] + a[i]; } ll j = ...
cpp
1311
A
A. Add Odd or Subtract Eventime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers aa and bb.In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0) and replace aa with a+xa+x; choose any positiv...
[ "greedy", "implementation", "math" ]
#include <bits/stdc++.h> #define hello ios_base::sync_with_stdio(false); #define world cin.tie(NULL); using namespace std; int main(){ hello world int n; cin>>n; int A[n]; for (int i = 0; i < n; i++) { int a,b; cin>>a>>b; if (a==b) { A[i]=0; } else if (a>b) ...
cpp
1303
C
C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arrange...
[ "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define line cout << "\n"; const int N = 2e5 + 2; set<int> adj[N]; int vis[N]; void dfs(int u, int p) { char c = u + 'a'; vis[u] = 1; cout << c; for (auto v : adj[u]) { if (v == p) continue; if (vis[v] == 1) ...
cpp
1311
A
A. Add Odd or Subtract Eventime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers aa and bb.In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0) and replace aa with a+xa+x; choose any positiv...
[ "greedy", "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; int main(){ int t,a,b; cin >> t; while (t--){ cin >> a >> b; if (a==b)cout << 0; else if (a>b && (a-b)%2==0)cout << 1; else if (a>b && (a-b)%2==1)cout << 2; else if (a<b && (b-a)%2==0)cout << 2; else if ...
cpp
1307
E
E. Cow and Treatstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a successful year of milk production; Farmer John is rewarding his cows with their favorite treat: tasty grass!On the field, there is a row of nn units of grass, each with a sweetness sisi. Farmer...
[ "binary search", "combinatorics", "dp", "greedy", "implementation", "math" ]
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <...
cpp
1320
F
F. Blocks and Sensorstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputPolycarp plays a well-known computer game (we won't mention its name). Every object in this game consists of three-dimensional blocks — axis-aligned cubes of size 1×1×11×1×1. These blocks are unaff...
[ "brute force" ]
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; const int dx[]={1,-1,0,0,0,0},dy[]={0,0,1,-1,0,0},dz[]={0,0,0,0,1,-1}; int n,m,k,col[N];vector<int>d[N]; inline int id(int x,int y,int z){return (x-1)*m*k+(y-1)*k+z;} void ins(int x,int y,int z,int c,int tp) { if(x<1||x>n||y<1||y>m||z<1||z>k) { ...
cpp
1292
A
A. NEKO's Maze Gametime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#ΦωΦ has just got a new maze game on her PC!The game's main puzzle is a maze; in the forms of a 2×n2×n rectangle grid. NEKO...
[ "data structures", "dsu", "implementation" ]
#include<bits/stdc++.h> #include<iostream> #include<iomanip> #include<vector> #include<string> #include<math.h> #include<map> #include<algorithm> #include<set> #include<unordered_map> #include <unordered_set> #define fast ios_base::sync_with_stdio(false),cin.tie(NULL) #define popcount __builtin_popcount us...
cpp
1324
E
E. Sleeping Scheduletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVova had a pretty weird sleeping schedule. There are hh hours in a day. Vova will sleep exactly nn times. The ii-th time he will sleep exactly after aiai hours from the time he woke up. You can assu...
[ "dp", "implementation" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; template <typename T> bool chkmax(T &x, T y) { if (y > x) {x = y; return true;} return false;} template <typename T> bool chkmin(T &x, T y) { if (y < x) {x = y; return true;} return false;} const ll INF = 0x3f3...
cpp
1304
B
B. Longest Palindrometime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputReturning back to problem solving; Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "...
[ "brute force", "constructive algorithms", "greedy", "implementation", "strings" ]
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define all(v) ((v).begin()), ((v).end()) #define MOD ll(1e9+7) #define int long long typedef int ll; typedef vector<ll> vi; typedef tree< ll, null_type, le...
cpp
1284
E
E. New Year and Castle Constructiontime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputKiwon's favorite video game is now holding a new year event to motivate the users! The game is about building and defending a castle; which led Kiwon to think about the following puz...
[ "combinatorics", "geometry", "math", "sortings" ]
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <...
cpp
1325
C
C. Ehab and Path-etic MEXstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn nodes. You want to write some labels on the tree's edges such that the following conditions hold: Every label is an integer between 00 and n−2n−2 inclusiv...
[ "constructive algorithms", "dfs and similar", "greedy", "trees" ]
#include<bits/stdc++.h> #define endl '\n' #define iloveds std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); #define all(a) a.begin(),a.end() using namespace std; typedef long long ll ; const int N = 1e5 + 100; vector<int> g[N]; int now = 1; int ans[N]; int main(){ iloveds; int n; cin >> n...
cpp
1285
D
D. Dr. Evil Underscorestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2,…,an and challenged him to choose an integer XX such that the value max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X) is minimum possible, whe...
[ "bitmasks", "brute force", "dfs and similar", "divide and conquer", "dp", "greedy", "strings", "trees" ]
#pragma optimize("Bismillahirrahmanirrahim") //█▀█─█──█──█▀█─█─█ //█▄█─█──█──█▄█─█■█ //█─█─█▄─█▄─█─█─█─█ //Allahuekber //FatihSultanMehmedHan //YavuzSultanSelimHan //AbdulhamidHan //Sani buyuk Osman Pasa Plevneden cikmam diyor. //ahmet23 orz... #define author tolbi #include <bits/stdc++.h> #define int long ...
cpp
1303
B
B. National Projecttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYour company was appointed to lay new asphalt on the highway of length nn. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip...
[ "math" ]
/*********** MK-1311 ***********/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define nl '\n' #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif /* */ #define int long long void solve() { int n, g, b; cin >> n >> g >> b; int need =...
cpp
1310
E
E. Strange Functiontime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLet's define the function ff of multiset aa as the multiset of number of occurences of every number, that is present in aa.E.g., f({5,5,1,2,5,2,3,3,9,5})={1,1,2,2,4}f({5,5,1,2,5,2,3,3,9,5})={1,1,2,2,...
[ "dp" ]
#include<bits/stdc++.h> #define LL long long #define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout); using namespace std; const int N=2e3+20,mod=998244353; int tot,n,k,ans; vector<int>a; inline void slove1() { static int f[N+5],g[N+5]; for(int i=1;;i++) { if(i*(3*i-1)/2<=N) g[++tot]=i*(3...
cpp
1292
D
D. Chaotic V.time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputÆsir - CHAOS Æsir - V."Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.The time right now...... 00:01:12......It's time."The emotion samples are now suffici...
[ "dp", "graphs", "greedy", "math", "number theory", "trees" ]
#include <bits/stdc++.h> #define mod 998244353 #define int long long using namespace std; vector<int> p[5005]; int lst[5005],cnt[5005],w[5005]; vector<int> s[5005]; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i=2;i<=5000;i++) { if(p[i].empty()) { for(i...
cpp
1296
A
A. Array with Odd Sumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.In one move, you can choose two indices 1≤i,j≤n1≤i,j≤n such that i≠ji≠j and set ai:=ajai:=aj. You can perform such moves any number of times (poss...
[ "math" ]
#include <bits/stdc++.h> using namespace std; #define print(a) for(auto x : a) {cout << x << " ";} cout << endl; typedef long long lli; typedef vector<int> vi; typedef vector<lli> vl; int main(){ int t; cin >> t; while (t --){ int n; cin >> n; int o = 0, e = 0; for (int i = 0; i < n; i+...
cpp
1288
D
D. Minimax Problemtime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given nn arrays a1a1, a2a2, ..., anan; each array consists of exactly mm integers. We denote the yy-th element of the xx-th array as ax,yax,y.You have to choose two arrays aiai and ajaj (1≤i,j...
[ "binary search", "bitmasks", "dp" ]
#include<bits/stdc++.h> //#pragma GCC optimize(2) //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(l,r)(rng) using namespace std; typedef long long ll; typedef unsigned long long ull; #define endl '\n' #define INF 0x7FFFFFFF #define ch(a) (int(a-'a')+1) const...
cpp
1325
A
A. EhAb AnD gCdtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a positive integer xx. Find any such 22 positive integers aa and bb such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x.As a reminder, GCD(a,b)GCD(a,b) is the greatest integer that divides both...
[ "constructive algorithms", "greedy", "number theory" ]
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll t; cin>>t; while(t--){ ll x; cin>>x; cout<<x - 1<<" "<<1<<endl; } return 0; }
cpp
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit...
[ "greedy" ]
#include <iostream> using namespace std; int main() { int n,x; cin >> n; for (int i=0; i <n; i++){ cin >> x; if (x%2==0){ while (x>0){ x-=2; cout <<1; } cout << endl; } else{ whil...
cpp
1316
B
B. String Modificationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has a string ss of length nn. He decides to make the following modification to the string: Pick an integer kk, (1≤k≤n1≤k≤n). For ii from 11 to n−k+1n−k+1, reverse the substring s[i:i+k−1]s...
[ "brute force", "constructive algorithms", "implementation", "sortings", "strings" ]
#include<bits/stdc++.h> #include<stdio.h> #define T(n) for(int i=0; i<n; i++) #define T2(k) for(int j=0; j<k; j++) #define fi first #define se second #define os ios_base::sync_with_stdio(false); #define INFI 0x3f3f3f3f using namespace std; typedef long long ll; int arr[129]; int arr2[2000006]; int m...
cpp
1303
F
F. Number of Componentstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a matrix n×mn×m, initially filled with zeroes. We define ai,jai,j as the element in the ii-th row and the jj-th column of the matrix.Two cells of the matrix are connected if they sh...
[ "dsu", "implementation" ]
// LUOGU_RID: 96148485 #define FASTIO #include<bits/stdc++.h> //#include<ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/hash_policy.hpp> //#include<ext/pb_ds/priority_queue.hpp> using namespace std; //using namespace __gnu_pbds; #define fi first #define se second #define fill0(a) memset(a,0,sizeof(a)) #d...
cpp
1324
C
C. Frog Jumpstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It...
[ "binary search", "data structures", "dfs and similar", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define fst first #define sec second #define mod (ll)(1e9+7) void solve(){ string s;cin >> s; ll ans=0; ll ct=0; for(ll i=0;i<s.size();i++){ if(s[i]=='L') ct++; else{ an...
cpp
1316
E
E. Team Buildingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice; the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of pp players playing in pp different positions. She also recognizes the importance of ...
[ "bitmasks", "dp", "greedy", "sortings" ]
#include<bits/stdc++.h> #include<chrono> #include<random> #include<assert.h> #include<stdexcept> #include<iostream> #include<cmath> #include<algorithm> #include<bitset> #include<iomanip> #include<map> #include<vector> #define num1bit(x) __builtin_popcount(x) #define C continue #define B break #define R r...
cpp
1325
D
D. Ehab the Xorcisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.InputThe only line contains 2 integers uu and vv (0≤u,v≤1018)(0≤u,v≤1...
[ "bitmasks", "constructive algorithms", "greedy", "number theory" ]
#include<iostream> #include<vector> #include<string> #include<cmath> #include<algorithm> #include<map> #include<unordered_map> #include<set> #include<stack> #include<queue> #include<iomanip> #include<bitset> #include<climits> #include<cstring> #include<list> using namespace std; typedef long long ll; t...
cpp
1285
E
E. Delete a Segmenttime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn segments on a OxOx axis [l1,r1][l1,r1], [l2,r2][l2,r2], ..., [ln,rn][ln,rn]. Segment [l,r][l,r] covers all points from ll to rr inclusive, so all xx such that l≤x≤rl≤x≤r.Segments can be ...
[ "brute force", "constructive algorithms", "data structures", "dp", "graphs", "sortings", "trees", "two pointers" ]
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") /* ╭╮╱╱╱╱╱╱╱╱╭━╮ ┃┃╱╱╱╱╱╱╱╱┃╭╯ ┃╰━┳━━┳━━┳╯╰╮ ┃╭╮┃╭╮┃╭╮┣╮╭╯ ┃┃┃┃╰╯┃╰╯┃┃┃ ╰╯╰┻━╮┣━╮┃╰╯ ╱╱╱╭━╯┣━╯┃ ╱╱╱╰━━┻━━╯ */ #include<bits/stdc++.h> using namespace s...
cpp
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit...
[ "greedy" ]
#include<bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> pi; #define ll long long #define pb push_back #define lb(s,x) lower_bound(s.begin(),s.end(),x) #define ub(s,x) upper_bound(s.begin(),s.end(),x) #define asort(p) sort(p.begin(),p.end()) #define dsort(p) sort(p.begin...
cpp
1286
D
D. LCCtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn infinitely long Line Chillland Collider (LCC) was built in Chillland. There are nn pipes with coordinates xixi that are connected to LCC. When the experiment starts at time 0, ii-th proton flies from the ii-th...
[ "data structures", "math", "matrices", "probabilities" ]
// LUOGU_RID: 101728632 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 100001 #define yu (998244353) inline ll ksm(ll x,ll y){ ll an=1; for(;y;y>>=1){ if(y&1)an=an*x%yu; x=x*x%yu; }return an; }inline ll ni(ll x){return ksm(x,yu-2);} ll n; ll x[N],v[N],p[N]; ll xu[N]; ...
cpp
1294
A
A. Collecting Coinstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp has three sisters: Alice; Barbara, and Cerene. They're collecting coins. Currently, Alice has aa coins, Barbara has bb coins and Cerene has cc coins. Recently Polycarp has returned from the ...
[ "math" ]
#include <bits/stdc++.h> using namespace std; int main (){ int t;cin >> t; while(t--){ int a,b,c,n;cin >> a>>b>>c>>n; int ans = (a+b+c+n)/3; if((ans < a || ans < b || ans < c) || ((a+b+c+n) % 3 !=0)) cout << "NO"<<endl; else cout << "YES" << endl; } system("pause"); return 0; }
cpp
1303
B
B. National Projecttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYour company was appointed to lay new asphalt on the highway of length nn. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip...
[ "math" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int t; cin >> t; while (t--) { ll n, g, b; cin >> n >> g >> b; auto h = (n >> 1LL) + (n & 1LL); auto t = g + b; cout << max(n, ((h / g) * t) + (h % g == 0LL ? -b : h % g)) << '\n'; ...
cpp
1303
D
D. Fill The Bagtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a bag of size nn. Also you have mm boxes. The size of ii-th box is aiai, where each aiai is an integer non-negative power of two.You can divide boxes into two parts of equal size. Your goal is t...
[ "bitmasks", "greedy" ]
#include<bits/stdc++.h> #define endl '\n' #define int long long using namespace std; const int N=1e5+3; int a[N]; void solve() { int n,m; cin>>n>>m; int sum=0; for(int i=0;i<=100;i++) a[i]=0; for(int i=1; i<=m; i++) { int x,cnt=0; cin>>x; sum+=x; while(x)cnt++,x/=2; a[cnt-1]++; } if(s...
cpp
1307
B
B. Cow and Friendtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically; he wants to get from (0,0)(0,0) to (x,0)(x,0) by...
[ "geometry", "greedy", "math" ]
#include<bits/stdc++.h> using namespace std; int main() { int T; cin>>T; while(T--) { long long n,x,arr[100005],best=INT_MAX; cin>>n>>x; for(int i=0;i<n;i++) { cin>>arr[i]; best=min(best,x/arr[i]+(x%arr[i]?1:0)+(arr[i]>x?1:0)); } cout<<best<<endl; } }
cpp
1303
D
D. Fill The Bagtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a bag of size nn. Also you have mm boxes. The size of ii-th box is aiai, where each aiai is an integer non-negative power of two.You can divide boxes into two parts of equal size. Your goal is t...
[ "bitmasks", "greedy" ]
// LUOGU_RID: 94264985 #include<bits/stdc++.h> #define int long long using namespace std; main(){ ios::sync_with_stdio(false); int t; cin>>t; while (t--){ int n,m,s=0,c=0; cin>>n>>m; multiset<int,greater<int>> f; while(m--){ int x; cin>>x; s+=x; f.insert(x); } if(s<n){cout<<"-1\n"; continue;} while(n){ m=*f.begin(); f...
cpp
1311
C
C. Perform the Combotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons ...
[ "brute force" ]
#include <bits/stdc++.h> #define ll long long #define pb push_back #define lp(loop,a,b) for(loop=a;loop<b;loop++) #define vi(a) vector<int>a; #define vl(a) vector<ll>a; #define nl cout<<"\n"; #define cc(a) cout<<(a?"YES":"NO")<<endl; #defin...
cpp
1316
B
B. String Modificationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has a string ss of length nn. He decides to make the following modification to the string: Pick an integer kk, (1≤k≤n1≤k≤n). For ii from 11 to n−k+1n−k+1, reverse the substring s[i:i+k−1]s...
[ "brute force", "constructive algorithms", "implementation", "sortings", "strings" ]
#include<bits/stdc++.h> #include<iostream> #include<iomanip> #include<vector> #include<string> #include<math.h> #include<map> #include<algorithm> #include<set> #include<unordered_map> #include <unordered_set> #define fast ios_base::sync_with_stdio(false),cin.tie(NULL) #define popcount __builtin_popcount us...
cpp
1312
B
B. Bogosorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,…,ana1,a2,…,an. Array is good if for each pair of indexes i<ji<j the condition j−aj≠i−aij−aj≠i−ai holds. Can you shuffle this array so that it becomes good? To shuffle an array m...
[ "constructive algorithms", "sortings" ]
/* ======Buddha*Dhormo*Songho====== _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// ...
cpp
1310
D
D. Tourismtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMasha lives in a country with nn cities numbered from 11 to nn. She lives in the city number 11. There is a direct train route between each pair of distinct cities ii and jj, where i≠ji≠j. In total there are ...
[ "dp", "graphs", "probabilities" ]
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector d(n, vector<int>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int x; cin >> x; d[i][j] = x; } mt19937 rng(chrono::steady_clock().now().time_since_epoch().count...
cpp
1296
D
D. Fight with Monsterstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal...
[ "greedy", "sortings" ]
#include<iostream> #include<cstring> #include<vector> #include<map> #include<queue> #include<unordered_map> #include<cmath> #include<cstdio> #include<algorithm> #include<set> #include<cstdlib> #include<stack> #include<ctime> #define forin(i,a,n) for(int i=a;i<=n;i++) #define forni(i,n,a) for(int i=n;i>=a;i--) #define f...
cpp
1322
E
E. Median Mountain Rangetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBerland — is a huge country with diverse geography. One of the most famous natural attractions of Berland is the "Median mountain range". This mountain range is nn mountain peaks, located on one...
[ "data structures" ]
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/trie_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_cxx; // using namespace __gnu_pbds; void Hollwo_Pelw(); signed main(){ #ifndef hollwo_pelw_local if (fopen(".inp", "r")) a...
cpp
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit...
[ "greedy" ]
#include<bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t>0) { int n; cin >> n; if(n % 2 == 1){ cout << 7; n -= 3; } while (n > 1) { cout << 1; n -= 2; }...
cpp
1320
C
C. World of Darkraft: Battle for Azathothtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputRoma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.Roma has a choice to buy exactly one of nn diff...
[ "brute force", "data structures", "sortings" ]
/* ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡞⠉⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⣀⡠⠖⠒⠓⣦⢀⡞⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⣴⣪⠟⢙⣶⣴⣿⣿⣿⠟⠀⢲⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⢻⣏⣠⣿⣿⣿⢿⣿⠋⠀⢀⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠋⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⢹⣹⣿⣿⡷⢖⢿⠭⠄⡾⠀...
cpp
1292
C
C. Xenon's Attack on the Gangstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputINSPION FullBand Master - INSPION INSPION - IOLITE-SUNSTONEOn another floor of the A.R.C. Markland-N; the young man Simon "Xenon" Jackson, takes a break after finishing his project early (...
[ "combinatorics", "dfs and similar", "dp", "greedy", "trees" ]
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll MOD=998244353; const ll INF=1e18; #define F first #define S second typedef long double ld; typedef complex<long double> pt; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long l...
cpp
1301
F
F. Super Jabertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJaber is a superhero in a large country that can be described as a grid with nn rows and mm columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specifi...
[ "dfs and similar", "graphs", "implementation", "shortest paths" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <random> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define AboT...
cpp
1311
E
E. Construct the Binary Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and dd. You need to construct a rooted binary tree consisting of nn vertices with a root at the vertex 11 and the sum of depths of all vertices equals to dd.A t...
[ "brute force", "constructive algorithms", "trees" ]
#include <bits/stdc++.h> using namespace std; struct Node{ set<int> child; int par = -1; int depth = 0; }; vector<int> smallest; int check(int n, int d, int n1){ int ln1 = n1*(n1-1); ln1 /= 2; int l1 = d-(n-1)-ln1; int r1 = d-(n-1)-smallest[n1]; int l2 = smallest[n-1-n...
cpp
1322
C
C. Instant Noodlestime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWu got hungry after an intense training session; and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.You are given...
[ "graphs", "hashing", "math", "number theory" ]
#include <bits/stdc++.h> #define ll long long #define ld long double #define pii pair<int, int> #define pll pair<ll int, ll int> #define ff first #define ss second #define pb push_back #pragma GCC optimize("O2") using namespace std; // debug template #ifdef POTATO #include "debug.h" #define debug(...) cerr...
cpp
1287
B
B. Hypersettime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shape...
[ "brute force", "data structures", "implementation" ]
#include<bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; vector<string> vec(n); unordered_map<string, int> mpp; for (int i = 0; i < n; i++) { cin >> vec[i]; mpp[vec[i]]++; } int count = 0; for (int i = 0; i < n; i++) { ...
cpp
1284
B
B. New Year and Ascent Sequencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA sequence a=[a1,a2,…,al]a=[a1,a2,…,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,...
[ "binary search", "combinatorics", "data structures", "dp", "implementation", "sortings" ]
//Made By Phuong Nam PROPTIT <3// #pragma GCC Optimize("O3") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> #define f(i,a,b) for(int i=a;i<=b;i++) #define f1(i,n) for(int i=1;i<=n;i++) #define f0(i,n) for(int i=0;i<n;i++) #define ff(i,b,a) for(int i=b;i>=a;i--) #define el ...
cpp
1324
C
C. Frog Jumpstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It...
[ "binary search", "data structures", "dfs and similar", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while (t--){ string map = "R", str; cin >> str; map += str + "R"; vector<int>arr; for(int i=0; i<map.size(); i++)if(map[i] == 'R')arr.push_back(i); int res = 0; for(int i=1; i<arr....
cpp
1286
A
A. Garlandtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVadim loves decorating the Christmas tree; so he got a beautiful garland as a present. It consists of nn light bulbs in a single row. Each bulb has a number from 11 to nn (in arbitrary order), such that all th...
[ "dp", "greedy", "sortings" ]
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define dbg(...) #define setIO(...) #endif using ll = long long; #define pb push_back #define all(x) (x).begin(), (x).end() int MOD = (int)1e9 + 7; ll INF = 1e18 + 7; int dp[101][2][101][101]; int res = MOD; int ...
cpp
1310
C
C. Au Pont Rougetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK just opened its second HQ in St. Petersburg! Side of its office building has a huge string ss written on its side. This part of the office is supposed to be split into mm meeting rooms in such way th...
[ "binary search", "dp", "strings" ]
// LUOGU_RID: 90343523 #include<cstdio> #include<cstdlib> #include<utility> #include<algorithm> int n,m,lcp[1005][1005]; long long dp[1005][1005],sf[1005][1005],cnt,k; std::pair<int,int> s[1000005]; char st[1005]; long long adjust(long long x){return std::min(x,2000000000000000000ll);} int cmp(std::pair<int,in...
cpp
1288
A
A. Deadlinetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAdilbek was assigned to a special project. For Adilbek it means that he has nn days to run a special program and provide its results. But there is a problem: the program needs to run for dd days to calculate...
[ "binary search", "brute force", "math", "ternary search" ]
#include <bits/stdc++.h> using namespace std; typedef long long int ll; # define mod 998244353 int main() { int t; cin>>t; while(t--) { ll n,d; cin>>n>>d; if((n-1)*(n-1)+4ll*(n-d)>=0) { cout<<"YES"<<endl; } else { cout<<"NO"<<...
cpp
1320
E
E. Treeland and Virusestime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are nn cities in Treeland connected with n−1n−1 bidirectional roads in such that a way that any city is reachable from any other; in other words, the graph of cities and roads is a tree. Tr...
[ "data structures", "dfs and similar", "dp", "shortest paths", "trees" ]
// LUOGU_RID: 97737920 #include<bits/stdc++.h> using namespace std; typedef pair<int,int> Pair; const int N=2e5+500,INF=1e9; #define F first #define S second Pair f[N],g[N],pre[N],suf[N]; int dfn[N],ST[N<<1][19],Log[N<<1],dep[N],col[N],s[N],p[N],vis[N],vet[N],stk[N],que[N]; int n,m,k,q,u,v,L,top; vector<int> G...
cpp
1313
A
A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The ...
[ "brute force", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a[3]; cin >> a[0] >> a[1] >> a[2]; int ans = 0; for (int mask = 0; mask < (1 << 7); mask++) { int b[3] = {a[0], a[1], a[2]}; for (int i = 1; i <= 7; i++) if ((mask >> (i ...
cpp
1290
F
F. Making Shapestime limit per test5 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard outputYou are given nn pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin (0,0)(0,0). Choose a ve...
[ "dp" ]
// LUOGU_RID: 102614426 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 11 #define yu (998244353) ll n,m; ll ans=0; inline void add(ll &x,ll y){x+=y;if(x>=yu)x-=yu;return ;} ll xi[6],yi[6]; ll f[31][22][22][22][22][2][2]; inline ll dfs(ll pos,ll x,ll fx,ll y,ll fy,ll x1,ll x2){ i...
cpp
1292
D
D. Chaotic V.time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputÆsir - CHAOS Æsir - V."Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.The time right now...... 00:01:12......It's time."The emotion samples are now suffici...
[ "dp", "graphs", "greedy", "math", "number theory", "trees" ]
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt") #include <bits/stdc++.h> #define f first #define s second #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define sz(x) ((int) (...
cpp
1141
B
B. Maximal Continuous Resttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEach day in Berland consists of nn hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a1,a2,…,ana1,a2,…,an (each aiai is either 00 or 11)...
[ "implementation" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } #define endl '\n'; void solve() { int n; cin>>n; vector<int> v(n); for (auto &it : v) { cin >>it; } int maxi = 0; int result = 0; for (int i = 0; i...
cpp
1313
E
E. Concatenation with intersectiontime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVasya had three strings aa, bb and ss, which consist of lowercase English letters. The lengths of strings aa and bb are equal to nn, the length of the string ss is equal to mm. Vasya d...
[ "data structures", "hashing", "strings", "two pointers" ]
#include<bits/stdc++.h> using namespace std; typedef long long ll; //#include<bits/extc++.h> //__gnu_pbds #define int ll void zalgo(int *z,string s){ fill(z,z+s.size(),0); for(int i = 1,l = 0, r = 0;i<s.size();i++){ if(i<=r) z[i] = min(z[i-l],r-i+1); while(i+z[i]<s.size() && s[i+z[i]]==s[z[i]]) z[i]++; ...
cpp
1295
E
E. Permutation Separationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a permutation p1,p2,…,pnp1,p2,…,pn (an array where each integer from 11 to nn appears exactly once). The weight of the ii-th element of this permutation is aiai.At first, you sepa...
[ "data structures", "divide and conquer" ]
#include <bits/stdc++.h> #define int long long using namespace std; int n, i, ans = 1e18, a[1200001], p[1200001], pr[1200001], pos[1200001], t[1200001], d[1200001]; void push(int v) { if (!d[v]) return; t[v + v] += d[v]; t[v + v + 1] += d[v]; d[v + v] += d[v]; d[v + v + 1] += d[v]; d[v] = 0; } vo...
cpp
1290
C
C. Prefix Enlightenmenttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn lamps on a line, numbered from 11 to nn. Each one has an initial state off (00) or on (11).You're given kk subsets A1,…,AkA1,…,Ak of {1,2,…,n}{1,2,…,n}, such that the intersection of...
[ "dfs and similar", "dsu", "graphs" ]
/************************************* * author: marvinthang * * created: 10.01.2023 21:53:11 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___l...
cpp
1305
E
E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the "Proof by AC" team. All the preparation has been done; and he is discussing with the team about the score distrib...
[ "constructive algorithms", "greedy", "implementation", "math" ]
// LUOGU_RID: 98099727 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <random> #include <set> using namespace std; #define fi first #define sc second #define mkp make_pair #define pii pair<int,int> typedef long long ll; const int N=1e5+5,oo=1e8,mod=...
cpp
1286
E
E. Fedya the Potter Strikes Backtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFedya has a string SS, initially empty, and an array WW, also initially empty.There are nn queries to process, one at a time. Query ii consists of a lowercase English letter cici and a n...
[ "data structures", "strings" ]
// LUOGU_RID: 102578485 #include<bits/stdc++.h> using namespace std; #define ll long long #define lll __int128 #define inf 1e18 #define N 1000010 ll n; ll ji[N]; ll nxt[N],fl[N],nn[N]; const ll mask=(1ll<<30)-1; lll ans=0; struct node{ ll val[N<<2]; inline void pushup(ll x){ val[x]=min(val[x<<1],val[x...
cpp
1294
D
D. MEX maximizingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are prese...
[ "data structures", "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; //#define int long long typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define pb emplace_back #define mp make_pair #define f first #define endl '\n' #define s second #define all(c) (c).begin(), (c).end() #define MOD 1000000007 lo...
cpp
1320
D
D. Reachable Stringstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn this problem; we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a...
[ "data structures", "hashing", "strings" ]
#include <bits/stdc++.h> #include <utility> namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast modular multiplication by barrett reduction // Refere...
cpp
1290
A
A. Mind Controltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou and your n−1n−1 friends have found an array of integers a1,a2,…,ana1,a2,…,an. You have decided to share it in the following way: All nn of you stand in a line in a particular order. Each minute, the p...
[ "brute force", "data structures", "implementation" ]
/* ....###....########..##....##....###....##....##............##....#######. ...##.##...##.....##..##..##....##.##...###...##..........####...##.....## ..##...##..##.....##...####....##...##..####..##............##...##.....## .##.....##.########.....##....##.....##.##.##.##............##....######## .#########.#...
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type...
[ "data structures", "dfs and similar", "dp" ]
#include <bits/stdc++.h> using namespace std; const int N=1e6+10; int st[N]; int rs[N],dp[N]; int e[N][27]; vector<pair<int,int>> stk; int Rank; void dfs(int u) { if(stk.size()&&st[u]) dp[u]=min(dp[u],stk.back().second+Rank+1); if(stk.empty()||stk.back().second>dp[u]-Rank) stk.push_b...
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type...
[ "data structures", "dfs and similar", "dp" ]
#include<bits/stdc++.h> using namespace std; int const M=1000100;char ch; int i,n,k,x,a[M],f[M],g[M],s[M]; vector<pair<char,int> >G[M]; void dfs(int x,int p){ f[x]=f[p]+1;g[x]=min(f[x],g[p]+s[p]); if (s[x]) f[x]=min(f[x],g[x]+1); sort(G[x].begin(),G[x].end()); for (auto v:G[x]) dfs(v.second,x); s[p]+=s[x]; } int main(...
cpp
1313
D
D. Happy New Yeartime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBeing Santa Claus is very difficult. Sometimes you have to deal with difficult situations.Today Santa Claus came to the holiday and there were mm children lined up in front of him. Let's number them fr...
[ "bitmasks", "dp", "implementation" ]
#include<iostream> #include<cstring> #include<algorithm> using namespace std; using LL = long long; const int maxn = 2e5 + 5; pair<int, int> op[maxn]; int f[maxn][1 << 8]; int a[8]; int main(){ #ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif cin.tie(...
cpp
1285
C
C. Fadi and LCMtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Osama gave Fadi an integer XX, and Fadi was wondering about the minimum possible value of max(a,b)max(a,b) such that LCM(a,b)LCM(a,b) equals XX. Both aa and bb should be positive integers.LCM(a,b)L...
[ "brute force", "math", "number theory" ]
#include<bits/stdc++.h> #define ll long long #define el '\n' using namespace std; void solve(){ ll x; cin >> x; vector<ll>a; int cnt = 0; ll mn = 1000000000000; bool ok = false; for (ll i = 2; i * i <= x; i++){ if (x % i == 0){ ll y = x / i; if (...
cpp
1316
D
D. Nash Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands. This board game is played on the n×nn×n b...
[ "constructive algorithms", "dfs and similar", "graphs", "implementation" ]
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> #include <ext/rope> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <ext/pb_ds/priori...
cpp
1324
A
A. Yet Another Tetris Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given some Tetris field consisting of nn columns. The initial height of the ii-th column of the field is aiai blocks. On top of these columns you can place only figures of size 2×12...
[ "implementation", "number theory" ]
#include<bits/stdc++.h> #define deq deque<int #define vec vector<int #define pb push_back #define pf push_front #define sonic ios::sync_with_stdio(false); cin.tie(0); using namespace std; using ll = long long; using ull = unsigned long long; int main() { sonic; int t; cin >> t; while (t --) { int ...
cpp
1295
D
D. Same GCDstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers aa and mm. Calculate the number of integers xx such that 0≤x<m0≤x<m and gcd(a,m)=gcd(a+x,m)gcd(a,m)=gcd(a+x,m).Note: gcd(a,b)gcd(a,b) is the greatest common divisor of aa and bb.I...
[ "math", "number theory" ]
#include<iostream> using namespace std; #include <iostream> #include <cmath> #include <algorithm> #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sst string #define pb push_back #define maxco 100000+5 #define lld long double #define cha ios_base::sync_with_stdio(false); #define ...
cpp
1312
E
E. Array Shrinkingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,…,ana1,a2,…,an. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements ai=ai+1ai=ai+1 (if there is at least one such...
[ "dp", "greedy" ]
#include<bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; const ll base = 37;//进制 const ll mod = 998244353;//质数 const int inf = 1e9+5; const int maxn = 2e5+5; const ll linf = 1e18+5ll; int main() { cin.tie(NULL); cout.tie(NULL); ...
cpp
1313
A
A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The ...
[ "brute force", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; #define str(t) ll t; cin>>t;while(t--) #define ll long long int #define vll vector<ll> #define g(n) ll n;cin>>n #define f(i,a,b) for(ll i=a;i<b;i++) #define fin(i,a,b) for(int i=a;i<b;i++) #define vsort(a) sort(a.begin(),a.end()) #define vrsort(a) sort(a.begin(),a.end(),gr...
cpp
1290
C
C. Prefix Enlightenmenttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn lamps on a line, numbered from 11 to nn. Each one has an initial state off (00) or on (11).You're given kk subsets A1,…,AkA1,…,Ak of {1,2,…,n}{1,2,…,n}, such that the intersection of...
[ "dfs and similar", "dsu", "graphs" ]
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define x first #define y second #define pb push_back #define all(a) a.begin(),a.end() #define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) typedef long long ll;typedef pair<int,int> pii;typedef pair<ll,ll> pll; template<class T>inline...
cpp
1303
C
C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arrange...
[ "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define F first #define S second #define enter cout<<'\n'; #define INF 99999999999999999 #define MOD 1000000007 int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) { string s; ...
cpp
1303
C
C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arrange...
[ "dfs and similar", "greedy", "implementation" ]
//ANY TIME YOU SEE OPTIMIZATION PROBLEM -> BINARY SEARCH THE ANSWER!!! #include <bits/stdc++.h> using namespace std; #define ll long long #define No cout<<"No"<<endl; #define NO cout<<"NO"<<endl; #define Yes cout<<"Yes"<<endl; #define YES cout<<"YES"<<endl; #define MOD 1000000007 #define endl '\n' void solve...
cpp
1324
B
B. Yet Another Palindrome Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.Your task is to determine if aa has some subsequence of length at least 33 that is a palindrome.Recall that an array bb is called a s...
[ "brute force", "strings" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define fst first #define sec second #define mod (ll)(1e9+7) void solve(){ ll n;cin >> n; map<ll,vector<ll> > mp; for(ll i=0;i<n;i++) { ll data; cin >> data; mp[data].push_back(i); ...
cpp
1294
C
C. Product of Three Numberstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c and a⋅b⋅c=na⋅b⋅c=n or say that it is impossible to do it.If there are several answers, yo...
[ "greedy", "math", "number theory" ]
/*Pash_Mak*/ #include <bits/stdc++.h> using namespace std; [[maybe_unused]] const bool Tests = true; class Pash { public: int t; static void C() { int n , a = 2 , b = 3 , c = 4; cin >> n; if (n < 24){cout << "NO\n";return;} while(a * b * c <= n){ while(a * b * c...
cpp