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
13
C
C. Sequencetime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play very much. And most of all he likes to play the following game:He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. ...
[ "dp", "sortings" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll N = 1e9 + 7; ll bpow(ll a, ll b, ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll modinv(ll n, ll p) ...
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; vector<vector<int>> grafo; vector<vector<int>> grafoOr; vector<int> dis; int inf = 2147483640; void start(int n){ for(int i = 0; i<n;i++){ grafo.push_back({}); grafoOr.push_back({}); dis.push_back(inf); } } void conect(int a, int b){ gr...
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> typedef long long ll; typedef unsigned long long ull; using namespace std; const ll mod = 998244353; const int mm = 1e5 + 10; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); ll x; cin>>x; pair<ll,ll>ans={1,x}; vector<ll>v; for(int i=2;i<=x/i...
cpp
1304
F2
F2. Animal Observation (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The ...
[ "data structures", "dp", "greedy" ]
#include <bits/stdc++.h> // v.erase( unique(all(v)) , v.end() ) -----> removes duplicates and resizes the vector as so using namespace std; #define ll long long #define lld long double const lld pi = 3.14159265358979323846; #define pb push_back #define pf push_front #define all(a) a.begin(),a.end() #defin...
cpp
1325
E
E. Ehab's REAL Number Theory Problemtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this...
[ "brute force", "dfs and similar", "graphs", "number theory", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; const int MN=8e4; int N, at=170, dist[MN], ans = MN; bool pvis[1001], vis[MN]; set<int> start; vector<int> prime, vals, adj[MN]; unordered_map<int, int> ind; // (value, index it maps to) queue<pair<int, pair<int, int> > > next1; // (distance, (node, parent)) void ...
cpp
1301
A
A. Three Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three strings aa, bb and cc of the same length nn. The strings consist of lowercase English letters only. The ii-th letter of aa is aiai, the ii-th letter of bb is bibi, the ii-th letter of...
[ "implementation", "strings" ]
#include <iostream> using namespace std; int main() { int n; cin >> n; for(int i = 0; i < n; i++) { string a, b, c; cin >> a >> b >> c; int check = 0; for(int i = 0; i < int(a.size()); i++) { if(c[i] != a[i] && c[i] != b[i]) { check = 1...
cpp
1286
C2
C2. Madhouse (Hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is different with easy version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse; in which orderlies play with patients th...
[ "brute force", "constructive algorithms", "hashing", "interactive", "math" ]
// LUOGU_RID: 99372543 #include<bits/stdc++.h> using namespace std; string s; multiset<string> st; string ss[110]; vector<string> v[110]; char ans[1000]; bool cmp(string s1,string s2) { return s1.size()<s2.size(); } int a[1100]; int n; void branch() { int n1=n; n=(n+1)/2; cout<<"? "<<1<...
cpp
1292
B
B. Aroma's Searchtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTHE SxPLAY & KIVΛ - 漂流 KIVΛ & Nikki Simmons - PerspectivesWith a new body; our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space.The space can...
[ "brute force", "constructive algorithms", "geometry", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f3f3f3f3f int main() { int64_t x0, y0, ax, ay, bx, by, xs, ys, t; cin >> x0 >> y0 >> ax >> ay >> bx >> by; cin >> xs >> ys >> t; vector<int64_t> x(1, x0), y(1, y0); while ((INF - bx) / ax >= x.back() && (INF - by) / ay >= ...
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> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ld long double #define int long long #define uint unsigned long long #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() co...
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" ]
#include<bits/stdc++.h> using namespace std; const int N=6,V=5,L=31,mod=998244353; int f[L][N*V][N*V][N*V][N*V][2][2]; int x[N],y[N]; int n,m; int calc(int p,int px,int nx,int py,int ny,int gx,int gy){ if(p==L-1)return (!px&&!nx&&!py&&!ny&&!gx&&!gy); int &f=::f[p][px][nx][py][ny][gx][gy]; if(f!=-1)return f; f=0; int ...
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" ]
//Author: Frustrated_EH //Complexity: O(?) #include <bits/stdc++.h> //#define int long long using namespace std; const int mod = 998244353; namespace FastIO { template<typename T>inline T read(){T x=0,w=1;char c=0;while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getc...
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; #define int long long #define mp make_pair #define inf 1e9 #define pii pair <int, int> const int mod = 998244353; inline int read () { int x = 0, f = 1; char ch = getchar (); while (ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar (); while (ch ...
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; #define watch(x) cout << __LINE__ << "| " << #x << " = " << x << endl #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define YES cout << "YES\n" #define NO cout << "NO\n" void solve() { int x, y; cin >> x >> y; if (x == y) { co...
cpp
1324
D
D. Pair of Topicstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.The pair of topics ii and jj (i<ji...
[ "binary search", "data structures", "sortings", "two pointers" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using uint = unsigned int; using vi = vector<int>; using pi = pair<int, int>; #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) bool is_prime(ll x) { if (x <= 1) return 0; ...
cpp
1322
B
B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she cam...
[ "binary search", "bitmasks", "constructive algorithms", "data structures", "math", "sortings" ]
#include<stdio.h> int n,a[400002],f[400002],c[1<<26],ans; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int k=0;k<25;k++) { long long sum1=0,sum2=0; if(k>0) { for(int i=0;i<(1<<k);i++) c[i]=0; for(int i=1;i<=n;i++) c[a[i]&((1<<k)-1)]++; for(int i=(1<<k)-1;i;i--) c[i]+=c[i+1]; for(int i=1;...
cpp
1305
G
G. Kuroni and Antihypetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni isn't good at economics. So he decided to found a new financial pyramid called Antihype. It has the following rules: You can join the pyramid for free and get 00 coins. If you are already...
[ "bitmasks", "brute force", "dp", "dsu", "graphs" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 18; ll n, ans, a[1 << N], cnt[1 << N]; struct dsu { vector < int > link, rang; dsu(int sz) { link.resize(sz + 1); rang.resize(sz + 1, 1); for (int i = 0; i <= sz; i++) link[i] = i;...
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<stdio.h> int d[1000002],h[1000002][2],n,v=0,s; int main(){ scanf("%d",&n);s=n-1; for(int i=1;i<n;i++){ scanf("%d%d",&h[i][0],&h[i][1]); d[h[i][0]]++;d[h[i][1]]++; }for(int i=1;i<n;i++){ if(d[h[i][0]]==1||d[h[i][1]]==1)printf("%d\n",v++); else printf("%d\n",--s); } }
cpp
1295
B
B. Infinite Prefixestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given string ss of length nn consisting of 0-s and 1-s. You build an infinite string tt as a concatenation of an infinite number of strings ss, or t=ssss…t=ssss… For example, if s=s= 10010, ...
[ "math", "strings" ]
#include <bits/stdc++.h> #define int long long int #define endl "\n" #define pb push_back #define pf push_front #define ff first #define ss second #define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(__null); #define cnt1(x) __builtin_popcountll(x) using namespace std; const int mod ...
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> typedef long long ll; typedef unsigned long long ull; using namespace std; const ll mod = 998244353; const int mm = 1e5 + 10; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); ll x; cin>>x; ll ans=1; for(ll i=1;i<=x/i;i++){ if(x%i==0){ if...
cpp
1307
C
C. Cow and Messagetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However; Bessie is sure that there is a secret message hidden inside.The text is a string ss of lowercase Latin letter...
[ "brute force", "dp", "math", "strings" ]
//header files// #include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll INF = 1e9 + 7; const ll MOD = 998244353; // definition files//; #define int long long #define loop(i, a, b) for(ll i = (ll)a; i <(ll) b; ++i) #define fl(i,n) loop(i, 0, n) #define rfl(i,n) for(ll i=(ll)(n)-1;i>...
cpp
1313
C2
C2. Skyscrapers (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is a harder version of the problem. In this version n≤500000n≤500000The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the constru...
[ "data structures", "dp", "greedy" ]
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> #include <list> #include <cassert> #include <...
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> #define maxn 300086 using namespace std; int n, k; char s[maxn]; int fa[maxn], val[maxn], siz[maxn], cnt[maxn], sum[maxn]; vector<int> a[maxn]; int x, y; int ans; int find(int x){ return x == fa[x] ? x : find(fa[x]); } int get(int x){ return x == fa[x] ? 0 : val[x] ^ get(fa[x]); } void ad...
cpp
1284
A
A. New Year and Namingtime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputHappy new year! The year 2020 is also known as Year Gyeongja (경자년; gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Ko...
[ "implementation", "strings" ]
#include <bits/stdc++.h> #define ll long long #define yes cout << "YES\n" #define no cout << "NO\n" #define pb push_back #define mod 998244353 using namespace std; mt19937 rng(time(0)); int random(int l, int r) { return rng() % (r - l + 1) + l; } int main() { int n,m; cin>>n>>m; string a[n],b[m...
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: 92552939 #include <bits/stdc++.h> #define P 998244353ll #define ll long long using namespace std; namespace QYB { void exgcd(ll a, ll b, ll &x, ll &y) { if (!b) return x = 1, y = 0, void(); exgcd(b, a % b, y, x); y -= a / b * x; } ll inv(ll a) { ll x, y; exgcd(a, P, x, y); ...
cpp
1294
F
F. Three Paths on a Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices a,b,ca,b,c on this tree such t...
[ "dfs and similar", "dp", "greedy", "trees" ]
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define endl '\n' #define ilihg ios_base::sync_with_...
cpp
1303
A
A. Erasing Zeroestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a cont...
[ "implementation", "strings" ]
#include<bits/stdc++.h> using namespace std; #define ll long long int main () { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); ll t; cin >> t; while( t-- ){ string s; cin >> s; ll cnt = 0, z = 0, cal = 0 , ans = 0; for(ll i = 0; i < s.size(); i++)if(s[i] == '1') {cal = i, z++; bre...
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
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" ]
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ll long long #define ull unsigned long long #define fi first #define se second #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define MASK(i) ((1LL)<<(i)) #define GETBIT(x,i) (((x)>>(i))&1) #define TURNOFF(x,i) (...
cpp
1287
A
A. Angry Studentstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt's a walking tour day in SIS.Winter; so tt groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are an...
[ "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; int A,B,C,W; string s; int main() { cin>>A; while(A--) { cin>>B>>s;C=W=0; while(B--) { if(s[B]=='P')C++; else W=max(C,W),C=0; } cout<<W<<endl; }}
cpp
1284
C
C. New Year and Permutationtime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputRecall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a ...
[ "combinatorics", "math" ]
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long #define ld long double #define pb push_back #define endl '\n' #define mpp make_pair #define go continue #define ff first #define ss second #define pii pair<int,int> #define pll pair<ll,ll> #define umap un...
cpp
1305
G
G. Kuroni and Antihypetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni isn't good at economics. So he decided to found a new financial pyramid called Antihype. It has the following rules: You can join the pyramid for free and get 00 coins. If you are already...
[ "bitmasks", "brute force", "dp", "dsu", "graphs" ]
// LUOGU_RID: 100970504 #include <bits/stdc++.h> #define sz(v) (int) (v).size() #define all(v) begin(v), end(v) #define view(v, l, r) begin(v) + l, begin(v) + r + 1 #define dbg(fmt...) fprintf(stderr, fmt) #define fi first #define se second using namespace std; using i64 = long long; using i128 = __int128_t; ...
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" ]
#include<bits/stdc++.h> #define int long long using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0' or ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' and ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x*f; } void write(int x){ if(x<0) putchar('-'),x=-x; if(x>=10) write(x...
cpp
13
A
A. Numberstime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.Now he wonders what is an average value of ...
[ "implementation", "math" ]
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include<string> #include<map> //#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(0),cin.tie(0) typedef long long ll; const int N = 2e5 + 9, M = 2e5 + 6, MOD = 998244353, OO = 0x3f3f3f3f, SQR = 320; const ...
cpp
1290
E
E. Cartesian Tree time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIldar is the algorithm teacher of William and Harris. Today; Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be...
[ "data structures" ]
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #define ll long long #define int ll #define Inf 0x3f3f3f3f using namespace std; const int N=2e5+60,M=1e6+20,mod=998244353; inline int read(){ int d=0,f=1;char x=getchar(); while(x<'0'||x>'9'){if(x=='-'){f=-1;}x=getchar();} ...
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" ]
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define N 2501 complex<double> points[N]; double Pi = arg(complex<double>(-1, 0)); int nC2(int n){ return (n * (n - 1)) / 2; } bool cmp(pair<double, complex<double>> a, pair<double, complex<double>> b){ ...
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" ]
// LUOGU_RID: 94145794 #include<cstdio> #include<cstring> #include<iostream> #define ll long long using namespace std; char s1[500005], s2[500005], s3[1000005], s4[1000005]; int z1[500005], z2[500005], z3[1000005], z4[1000005]; void calcz(int n1, int n2, char s1[], int z1[], char s2[], int z2[]) { int pos = 0...
cpp
1291
A
A. Even But Not Eventime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's define a number ebne (even but not even) if and only if its sum of digits is divisible by 22 but the number itself is not divisible by 22. For example, 1313, 12271227, 185217185217 are ebne num...
[ "greedy", "math", "strings" ]
//jai shree ram #include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long long LL; typedef long double ld; typedef vector<long long> vLL; typedef vector<long long int> vll; typedef vector<int> vi; #define pb push_back #define mk make_pair int bisearch(ll arr[],ll x,int n){ i...
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> template<class T> inline void read(T &x){ x=0;register char c=getchar();register bool f=0; while(!isdigit(c))f^=c=='-',c=getchar(); while(isdigit(c))x=x*10+c-'0',c=getchar();if(f)x=-x; } template<class T> inline void print(T x){ if(x<0)putchar('-'),x=-x; if(x>9)print(x/10);putchar(x...
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> #define endl '\n' #define int long long using namespace std; const int N=3e5+3; int n,m,x; int vis[N],a[N][10]; bool check(int mid) { memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) { int ans=0; for(int j=0; j<m; j++) { if(a[i][j]>=mid) ans+=(1<<j); } vis[ans]=1; ...
cpp
1305
H
H. Kuroni the Private Tutortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs a professional private tutor; Kuroni has to gather statistics of an exam. Kuroni has appointed you to complete this important task. You must not disappoint him.The exam consists of nn ques...
[ "binary search", "greedy" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define fi first #define se second #define mp make_pair #define pb push_back #define fbo find_by_order #define ook order_of_key typedef long long ll...
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 <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimization ("unroll-loops") #pragma GCC target("avx,avx2,fma") using namespace std; using namespace __gnu_pbds; template<typename T> using orde...
cpp
1285
A
A. Mezo Playing Zomatime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Mezo is playing a game. Zoma, a character in that game, is initially at position x=0x=0. Mezo starts sending nn commands to Zoma. There are two possible commands: 'L' (Left) sets the position...
[ "math" ]
#define lli long long int #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); lli n, lc = 0, rc = 0; cin>>n; string s; cin>>s; for(int i=0;i<n;i++) { if(s[i] == 'L') { lc++; } else { rc++; ...
cpp
1288
F
F. Red-Blue Graphtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a bipartite graph: the first part of this graph contains n1n1 vertices, the second part contains n2n2 vertices, and there are mm edges. The graph can contain multiple edges.Initially, each...
[ "constructive algorithms", "flows" ]
#include <bits/stdc++.h> using namespace std; #define Int register int #define inf 0x3f3f3f3f #define int long long #define MAXM 50005 #define MAXN 5005 template <typename T> void read (T &x){char c = getchar ();x = 0;int f = 1;while (c < '0' || c > '9') f = (c == '-' ? -1 : 1),c = getchar ();while (c >= '0'...
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" ]
// LUOGU_RID: 101673521 #include<bits/stdc++.h> #include<unordered_map> #include<algorithm> using namespace std; #define ll long long #define pii pair<ll,ll> ll n,m; void solve() { cin >> n >> m; vector<ll> a(n + 1); vector<set<int>> s(n + 1); for (int i = 1; i <= n; i++) cin >> a[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" ]
#pragma GCC optimize("O3,inline") #pragma GCC target("bmi,bmi2,lzcnt,popcnt") #include<bits/stdc++.h> using namespace std; typedef long long ll; #define all(a) a.begin(), a.end() #define lpr(i,a,b) for(ll i = a; i >= b; i--) #define lp(i,a,b) for(ll i=a;i<b;i++) #define pb push_back #define pp pop_back #defin...
cpp
1325
E
E. Ehab's REAL Number Theory Problemtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this...
[ "brute force", "dfs and similar", "graphs", "number theory", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pi; const int MN=8e4; int N, at=170, dist[MN], ans = MN; bool pvis[1001], vis[MN]; set<int> start; vector<int> prime, vals, adj[MN]; queue<pi> q; unordered_map<int, int> ind; // (value, index it maps to) void bfs(int r){ memset(dist, 0, sizeo...
cpp
1316
A
A. Grade Allocationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputnn students are taking an exam. The highest possible score at this exam is mm. Let aiai be the score of the ii-th student. You have access to the school database which stores the results of all studen...
[ "implementation" ]
#include <iostream> #include<bits/stdc++.h> using namespace std; #define ll long long typedef vector<int> BigInt; #define ACPC ios_base::sync_with_stdio(false); cin.tie(NULL); ll mod=1e9+7; bool PowerOfTwo(ll x){ return !(x&(x-1)); } ll FastPower(ll base,ll power) { if (power == 0) retur...
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 <cstdio> #include <cstdlib> #include <algorithm> #include<fstream> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <iomanip> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring...
cpp
1294
E
E. Obtain a Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a rectangular matrix of size n×mn×m consisting of integers from 11 to 2⋅1052⋅105.In one move, you can: choose any element of the matrix and change its value to any integer between ...
[ "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define MOD 1000000007 #define MOD1 998244353 #define INF 1e18 #define nline "\n" #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #def...
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> using namespace std; #define forn for (int i = 0; i < n; i++) #define int long long #define ed "\n" #define cyes cout << "YES\n" #define cno cout << "NO\n" #define pb push_back #define deb cout << "Hi\n" #define pint pair<int, int> const long long MOD = 1000000007; //--------------...
cpp
13
E
E. Holestime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for one person with following rules:There are N holes located in a single row and numbered from left to right with numbers from 1 to N. ...
[ "data structures", "dsu" ]
#include <vector> #include <string> #include <algorithm> #include <cmath> #include <numeric> #include <iomanip> #include <set> #include <map> #include <queue> #include <unordered_map> #include <stack> #include <cassert> #include <random> #include <iostream> using namespace std; //#define int long long...
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" ]
// Problem: D. Same GCDs // Contest: Codeforces - Educational Codeforces Round 81 (Rated for Div. 2) // URL: https://codeforces.com/problemset/problem/1295/D // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include<bits/stdc++.h> #define pb push_back #define ...
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" ]
//In The Name of ALLAH #include <bits/stdc++.h> using namespace std; #define sahadat() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ll long long int main() { sahadat(); ll t;cin>>t; while(t--){ ll n;cin>>n; cout<<1<<" "<<n-1<<endl; } return 0; }
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; typedef long long ll; const ll MOD = 1000000007; const ll eps = 1e-9; typedef vector<int>vi; typedef vector<ll>vl; #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define read(v,n) for(ll i=0;i<n;i++)cin>>v[i]; #define aff(v) for(int i : v) ...
cpp
1301
E
E. Nanosofttime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWarawreh created a great company called Nanosoft. The only thing that Warawreh still has to do is to place a large picture containing its logo on top of the company's building.The logo of Nanosoft can be des...
[ "binary search", "data structures", "dp", "implementation" ]
#include<iostream> #include <bits/stdc++.h> #include <ext/numeric> using namespace std; using L = __int128; #include<ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using ld = long double; #define nd "\n" #...
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> using namespace std; using ll = long long; using ii = tuple<int, int>; using vi = vector<ll>; using vii = vector<ii>; using vvi = vector<vi>; using si = set<ll>; int main() { cin.tie(0), ios::sync_with_stdio(0); ll t, n, c; string s, best; cin >> t; while (t--) { ...
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> #include <cstdio> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<pair<long long,long long>, null_type,less<pair<long long,long long> >, rb_tree_tag,tree_order_statistics_node_updat...
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> #pragma GCC target("sse,sse2,avx2") #pragma GCC optimize("unroll-loops,O3") #define rep(i,l,r) for (int i = l; i < r; i++) #define repr(i,r,l) for (int i = r; i >= l; i--) #define X first #define Y second #define all(x) (x).begin() , (x).end() #define pb push_back #define endl '\n' #define debu...
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; int main() { cin.tie(0), ios::sync_with_stdio(0); int n, m; cin >> n >> m; vector<string> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; reverse(b[i].begin(), b[i].end()); } vector<tuple<int, int>> ps; string...
cpp
1310
A
A. Recommendationstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK news recommendation system daily selects interesting publications of one of nn disjoint categories for each user. Each publication belongs to exactly one category. For each category ii batch algori...
[ "data structures", "greedy", "sortings" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define MOD 1000000007 using namespace std; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; map <int, int> m; priori...
cpp
1321
C
C. Remove Adjacenttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s||s|. You may perform several operations on this string.In one operation, you can choose some index ii and re...
[ "brute force", "constructive algorithms", "greedy", "strings" ]
/** * author: Pratham_Shah10 * date: 10.02.2023 **/ #include<bits/stdc++.h> using namespace std; #define fastio() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define endl "\n" #define pb push_back #define ff first #define ss second #define sz(x) ((ll)(x).size()) #define all(x) x.begin(),x.end...
cpp
1310
A
A. Recommendationstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK news recommendation system daily selects interesting publications of one of nn disjoint categories for each user. Each publication belongs to exactly one category. For each category ii batch algori...
[ "data structures", "greedy", "sortings" ]
#pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("popcnt") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2") // #pragma GCC target("avx2") #include <bits/...
cpp
1294
B
B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is ...
[ "implementation", "sortings" ]
#include<iostream> #include<cstdio> #include<bitset> #include<vector> #include<math.h> #include<algorithm> #include<stack> #include<set> #include<map> #include<queue> #include<unordered_set> #include <cstring> #include <iomanip> //#include <bits/stdc++.h> #define BATSY ios_base::sync_with_stdio(0);cin.tie...
cpp
1300
B
B. Assigning to Classestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputReminder: the median of the array [a1,a2,…,a2k+1][a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1][b1,b2,…,b2k+1] be the elements of the array in the sorted ord...
[ "greedy", "implementation", "sortings" ]
#define llt long long i using namespace std; //s //h #include<vector> //w //a #include<map> //a //r #include<list> //r //s #include<set> //a //h #include<algorithm> //j //i #include<queue> //t #include<cstring> #include<cmath> ...
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" ]
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <queue> #include <deque> #include <bitset> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <functional> #include <numeric> #include <utility> #include <limits> #include <t...
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> #define ll long long #define ull unsigned long long #define Ta7a ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL); // EL_MEXICY using namespace std; int main() { Ta7a; ll t = 1; cin >> t; while (t--) { string s;cin >>s; int cnt = 0,res =...
cpp
1141
A
A. Game 23time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp plays "Game 23". Initially he has a number nn and his goal is to transform it to mm. In one move, he can multiply nn by 22 or multiply nn by 33. He can perform any number of moves.Print the number of ...
[ "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; #pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // Datatypes Begin #define int...
cpp
1294
E
E. Obtain a Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a rectangular matrix of size n×mn×m consisting of integers from 11 to 2⋅1052⋅105.In one move, you can: choose any element of the matrix and change its value to any integer between ...
[ "greedy", "implementation", "math" ]
// #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") // #pragma GCC optimize("unroll-loops") #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;...
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" ]
#pragma GCC optimize("O3,unroll-loops") #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace chrono; using namespace __gnu_pbds; #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define MOD 10...
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
1300
B
B. Assigning to Classestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputReminder: the median of the array [a1,a2,…,a2k+1][a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1][b1,b2,…,b2k+1] be the elements of the array in the sorted ord...
[ "greedy", "implementation", "sortings" ]
#include <bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define dbg(x) \ cerr << #x << " "; \ _print(x); \ cerr << endl; #else #define dbg(x) ; #endif typedef long long ll; void _print(ll t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t...
cpp
1305
A
A. Kuroni and the Giftstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni has nn daughters. As gifts for them, he bought nn necklaces and nn bracelets: the ii-th necklace has a brightness aiai, where all the aiai are pairwise distinct (i.e. all aiai are differen...
[ "brute force", "constructive algorithms", "greedy", "sortings" ]
#include<bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; cin>>t; while(t--) { ll n; cin>>n; ll arr1[n], arr2[n]; for(ll i = 0; i < n; i++) cin>>arr1[i]; fo...
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" ]
// LUOGU_RID: 102062552 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; typedef pair<int,int> P; const int N=3e5+5; struct edge{ int v,nx; }e[N<<1],g[N<<1]; int n,ne,cnt,fe[N],fg[N],p[N],sz[N],son[N],deep[N],fa[N],top[N],id[N],...
cpp
1284
C
C. New Year and Permutationtime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputRecall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a ...
[ "combinatorics", "math" ]
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ss string #define loop1(i,n) for(ll i=0;i<n;++i) #define loop2(i,n) for(ll i=n-1;i>=0;--i) #define loop3(i,a,b) for(ll i=a;i<b;i++) #define loop4(i,a,b) for(ll i=a;i>=b;i--) #define NO cout<<"NO"<<endl; #define YES cout<<"YES"<<endl...
cpp
1307
F
F. Cow and Vacationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie is planning a vacation! In Cow-lifornia; there are nn cities, with n−1n−1 bidirectional roads connecting them. It is guaranteed that one can reach any city from any other city. Bessie is consi...
[ "dfs and similar", "dsu", "trees" ]
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long //#define endl '\n' #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); void dfs(int u, int fa, vector<int>& depth, vector<vector<int>>& f, vector<vector<int>>& g){ depth[u] = depth[fa] + 1; f[u...
cpp
1305
A
A. Kuroni and the Giftstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni has nn daughters. As gifts for them, he bought nn necklaces and nn bracelets: the ii-th necklace has a brightness aiai, where all the aiai are pairwise distinct (i.e. all aiai are differen...
[ "brute force", "constructive algorithms", "greedy", "sortings" ]
#include <bits/stdc++.h> using namespace std; int t,n,a[101],b[101]; int main() { cin >> t; while(t--){ cin >> n; for(int i=1; i<=n; ++i){ cin >> a[i]; } for(int i=1; i<=n; ++i){ cin >> b[i]; } sort(a+1, a+n+1); sort(b+1, b+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" ]
// LUOGU_RID: 101244776 #include<iostream> #include<vector> using namespace std; int n; vector<int>a; int solve(vector<int>p, int k) { if (p.size() == 0 || k < 0)return 0; vector<int>p1, p0; for (int i : p) { if (i & (1 << k))p1.push_back(i); else p0.push_back(i); } if (p1.size() == 0)return solve(p...
cpp
1141
G
G. Privatization of Roads in Treelandtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTreeland consists of nn cities and n−1n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are righ...
[ "binary search", "constructive algorithms", "dfs and similar", "graphs", "greedy", "trees" ]
#include<bits/stdc++.h> using namespace std; int i,n,k,x,y,d[200200],e[200200]; vector<int> a[200200],A[200200]; void dfs(int t,int w,int s){ for (int i=0;i<a[t].size();i++) if (a[t][i]!=w) e[A[t][i]]=(++s>d[n-k])?s=1:s,dfs(a[t][i],t,s); return ; } int main(){ scanf("%d %d",&n,&k); for (i=1;i<n;i++){ scanf("%d %d",&x,...
cpp
1307
G
G. Cow and Exercisetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFarmer John is obsessed with making Bessie exercise more!Bessie is out grazing on the farm; which consists of nn fields connected by mm directed roads. Each road takes some time wiwi to cross. She is ...
[ "flows", "graphs", "shortest paths" ]
#include <bits/stdc++.h> #define N 2505 using namespace std; typedef long long ll; template <typename T> inline void read(T &num) { T x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ ...
cpp
1291
B
B. Array Sharpeningtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given an array a1,…,ana1,…,an of nn non-negative integers.Let's call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…<ak and ak>ak+1>…>anak>ak+1>…>an. ...
[ "greedy", "implementation" ]
#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;...
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> #ifdef ONPC #include "t_debug.cpp" #else #define debug(...) 42 #endif using namespace std; //namespace pbds = __gnu_pbds; using ll = long long; const int inf = 1e9; const ll infl = 1e18; const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count(); mt19...
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" ]
// LUOGU_RID: 95932192 #include<bits/stdc++.h> using namespace std; #define int long long int const N=5e5+10; int c[N]; set<int>a[N]; map< set<int>,int >mp; inline void solve(){ mp.clear(); int n,m;cin>>n>>m; for (int i=1;i<=n;++i) cin>>c[i],a[i].clear(); while (m--){ int u,v;cin>>u>>v; ...
cpp
1307
C
C. Cow and Messagetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However; Bessie is sure that there is a secret message hidden inside.The text is a string ss of lowercase Latin letter...
[ "brute force", "dp", "math", "strings" ]
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1e9 + 7 void solve(){ string s; cin >> s; vector<ll> cnt(26); vector<vector<ll>> dp(26ll, vector<ll>(26)); for(int i=0; i<s.size(); ++i){ int x = s[i] - 'a'; for(int j=0; j<26; ++j){...
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" ]
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include...
cpp
1294
B
B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is ...
[ "implementation", "sortings" ]
#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> #include <cmath> typedef long long integerType; typedef integerType z; type...
cpp
1295
B
B. Infinite Prefixestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given string ss of length nn consisting of 0-s and 1-s. You build an infinite string tt as a concatenation of an infinite number of strings ss, or t=ssss…t=ssss… For example, if s=s= 10010, ...
[ "math", "strings" ]
#include<bits/stdc++.h> using namespace std; #define X ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long void solve() { int n, m; cin >> n >> m; string s; cin >> s; vector<int> arr(n); int one = 0; int zero = 0; for (int e = 0; e < n; e++) { ...
cpp
1141
F1
F1. Same Sum Blocks (Easy)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is given in two editions; which differ exclusively in the constraints on the number nn.You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence ...
[ "greedy" ]
#include<iostream> #include<vector> #include<cmath> #include<map> #include<unordered_map> #include<unordered_set> #include<set> #include<string> #include<stack> #include<algorithm> #include<bitset> #include<queue> #include<iomanip> #include<cstring> #include <numeric> #include <functional> using namespa...
cpp
1323
B
B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e....
[ "binary search", "greedy", "implementation" ]
# include <bits/stdc++.h> # define ll long long # define fi first # define se second using namespace std; int n, m , k; vector<pair<int, int>> fac; int sub[40003], cub[40003]; void factors(ll a) { for(int i=1; i<=sqrt(a); i++) { if(a%i==0) { if(i<=n && (a/i)<=m) { fac.push_back({i, a/i}); } ...
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; #define fast ios_base::sync_with_stdio(0); cin.tie(0) #define ff first #define ss second #define pb push_back #define pf push_front #define all(s) s.begin(),s.end() typedef long long ll; const double pi = acos(-1),ef=1e-9; const ll N=100+5,M=1e9+7; ll n,a[N],mm[N][N][N][2]; ...
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; int main(){ int T; long long n,d; for(cin>>T;T--;puts(((ceil(sqrt(d)*2)<=n+1)?("YES"):("NO"))))cin>>n>>d; }
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<iostream> #include<cstdio> #include<bitset> #include<vector> #include<math.h> #include<algorithm> #include<stack> #include<set> #include<map> #include<queue> #include<unordered_set> #include <cstring> #include <iomanip> //#include <bits/stdc++.h> #define BATSY ios_base::sync_with_stdio(0);cin.tie...
cpp
1141
F1
F1. Same Sum Blocks (Easy)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is given in two editions; which differ exclusively in the constraints on the number nn.You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence ...
[ "greedy" ]
/** * Created by megurine on 2023/02/09 09:14:06. * 诸天神佛,佑我上分! **/ #include <bits/stdc++.h> using namespace std; #define fastIO() ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr) #define itr(it) begin(it), end(it) typedef long long ll; typedef pair<int, int> PII; typedef double db; typedef long doubl...
cpp
1307
D
D. Cow and Fieldstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie is out grazing on the farm; which consists of nn fields connected by mm bidirectional roads. She is currently at field 11, and will return to her home at field nn at the end of the day.The Cowfe...
[ "binary search", "data structures", "dfs and similar", "graphs", "greedy", "shortest paths", "sortings" ]
#include<bits/stdc++.h> using namespace std; const int MAXN = 2.1e5; int N, M, K; vector<int> adj[MAXN]; int dist[MAXN][2]; int A[MAXN]; pair<int, int> val[MAXN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N >> M >> K; for (int i = 0; i < K; i++) { cin >> A[i]; A[i...
cpp
1313
B
B. Different Rulestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNikolay has only recently started in competitive programming; but already qualified to the finals of one prestigious olympiad. There going to be nn participants, one of whom is Nikolay. Like any good o...
[ "constructive algorithms", "greedy", "implementation", "math" ]
#ifdef local #define _GLIBCXX_DEBUG 1 #endif #pragma GCC optimize("Ofast", "unroll-loops") #include <bits/stdc++.h> using namespace std; #define int int64_t #define double long double using pii = pair<int, int>; template <typename T> using Prior = std::priority_queue<T>; template <typename T> using prior = ...
cpp
1294
B
B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is ...
[ "implementation", "sortings" ]
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef string str; typedef long double ld; typedef vector<ll> vll; typedef pair<ll,ll> pll; typedef vector<pair<ll,ll>> vpll; #define en "\n" #define pb push_back #define popb pop_back #define F first #define S sec...
cpp
1291
B
B. Array Sharpeningtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given an array a1,…,ana1,…,an of nn non-negative integers.Let's call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…<ak and ak>ak+1>…>anak>ak+1>…>an. ...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; int main() { cin.tie(0), ios::sync_with_stdio(0); int t, n, x; cin >> t; while (t--) { cin >> n; int m = n+1, M = -1; for (int i = 0; i < n; i++) { cin >> x; if (x - i < 0) m = min(m, i); if (x -...
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" ]
//Athour : 7amok4a #include <ext/pb_ds/assoc_container.hpp> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define mod_mul(a,b,m) ((a%m)*(b%m))%m #define mod_add...
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; #define int long long signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector <pair <pair <int, int>, int > > events, events1; vector <pair <int, int> > qw(n); vector <pair <int, int> > qw1(n); for(int ...
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" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = tuple<int, int>; using vi = vector<ll>; using vii = vector<ii>; using vvi = vector<vi>; using si = set<ll>; using qii = queue<ii>; int n; vvi xs, ys, zs; vvi s; bool dfs(int y, int x) { s[y][x] = 1; int dys[] = {-1,0,...
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" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef pair<int, int> pii; #define IOS ios::sync_with_stdio(0) #define For(i,a,b) for (int i=(a),_b=(b);i<=_b;i++) #define FOr For #define Iter(i,a,b) for (int i...
cpp