id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
AIZU/p01422
# Beautiful Currency KM country has N kinds of coins and each coin has its value a_i. The king of the country, Kita_masa, thought that the current currency system is poor, and he decided to make it beautiful by changing the values of some (possibly no) coins. A currency system is called beautiful if each coin has an...
[ { "input": { "stdin": "3\n6 11 24" }, "output": { "stdout": "0.090909090909" } }, { "input": { "stdin": "3\n6 11 30" }, "output": { "stdout": "0.166666666667" } }, { "input": { "stdin": "3\n6 11 12" }, "output": { "stdout": "0.0...
{ "tags": [], "title": "Beautiful Currency" }
{ "cpp": "#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <cassert>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <deque>...
AIZU/p01576
# Exciting Bicycle You happened to get a special bicycle. You can run with it incredibly fast because it has a turbo engine. You can't wait to try it off road to enjoy the power. You planned to go straight. The ground is very rough with ups and downs, and can be seen as a series of slopes (line segments) when seen fr...
[ { "input": { "stdin": "2 10\n0 0\n10000 0" }, "output": { "stdout": "10000.00000000" } }, { "input": { "stdin": "4 50\n0 10000\n1 10000\n2 0\n10000 0" }, "output": { "stdout": "7741.23024274" } }, { "input": { "stdin": "5 10\n0 0\n10 10\n20 0...
{ "tags": [], "title": "Exciting Bicycle" }
{ "cpp": "#include <iostream>\n#include <iomanip>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <complex>\nusing namespace std;\nconst long double EPS = 1e-8;\nconst long double INF = 1e12;\n#define EQ(n,m) (abs((n)-(m)) < EPS)\ntypedef complex<long double> P;\ntypedef vector<P> VP;\n#define X ...
AIZU/p01738
# Gravity Point You are practicing a juggle that involves in a number of square tiles. They all look the same in their size, but actually there are three different kinds of the tiles, A, B and X. The kind of a tile can be distinguishable by its mass. All the tiles of the same kind have exactly the same mass. The mass ...
[ { "input": { "stdin": "3 3\n2 4 1 2 1\nXAX\nB.B\nXAX" }, "output": { "stdout": "0.0000000000000" } }, { "input": { "stdin": "2 6\n-3 5 0 4 1\nAYX\nB.B\nAXW" }, "output": { "stdout": "0.424952651515\n" } }, { "input": { "stdin": "6 9\n-3 1 1 2...
{ "tags": [], "title": "Gravity Point" }
{ "cpp": "#include <bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep1(i,n) for(int i=1;i<=(int)(n);i++)\n#define all(c) c.begin(),c.end()\n#define pb push_back\n#define fs first\n#define sc second\n#define show(x) cout << #x << \" = \" << x << endl\n#define chmin(x,y) x=min(x,y)\n#define chma...
AIZU/p01878
# Destiny Draw G: Destiny Draw- problem Mr. D will play a card game. This card game uses a pile of N cards. Also, the pile cards are numbered 1, 2, 3, ..., N in order from the top. He can't afford to be defeated, carrying everything that starts with D, but unfortunately Mr. D isn't good at card games. Therefore, I ...
[ { "input": { "stdin": "4 1 1 6\n3 2 3" }, "output": { "stdout": "1" } }, { "input": { "stdin": "8 0 1 24\n0 1 3" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 1 5 35\n1 1 3" }, "output": { "stdout": "0\n" } ...
{ "tags": [], "title": "Destiny Draw" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef complex<double> P;\ntypedef pair<int,int> pii;\n#define REP(i,n) for(ll i=0;i<n;++i)\n#define REPR(i,n) for(ll i=1;i<n;++i)\n#define FOR(i,a,b) for(ll i=a;i<b;++i)\n\n#define D...
AIZU/p02014
# Rough Sorting For skilled programmers, it is very easy to implement a sorting function. Moreover, they often avoid full sorting to reduce computation time if it is not necessary. Here, we consider "rough sorting" which sorts an array except for some pairs of elements. More formally, we define an array is "$K$-roughl...
[ { "input": { "stdin": "3 100\n3\n2\n1" }, "output": { "stdout": "3\n2\n1" } }, { "input": { "stdin": "3 1\n3\n2\n1" }, "output": { "stdout": "1\n3\n2" } }, { "input": { "stdin": "5 3\n5\n3\n2\n1\n4" }, "output": { "stdout": "1\n...
{ "tags": [], "title": "Rough Sorting" }
{ "cpp": " #include <bits/stdc++.h>\n\n // #include <boost/multiprecision/cpp_int.hpp>\n \n \t #define int long long\n #define inf 1000000007\n ...
AIZU/p02157
# Shuffle 2 Problem There is a bundle of $ n $ cards, with $ i $ written on the $ i $ th card from the bottom. Shuffle this bundle as defined below $ q $ times. * When looking at the bundle from below, consider a bundle $ x $ made by stacking odd-numbered cards on top, and a bundle $ y $ made by stacking even-numbe...
[ { "input": { "stdin": "4 2 3 1" }, "output": { "stdout": "0\n1" } }, { "input": { "stdin": "7834164883628 15 2189823423122 5771212644938" }, "output": { "stdout": "0\n1\n1\n1\n1\n1\n0\n1\n0\n1\n0\n0\n0\n0\n0" } }, { "input": { "stdin": "4 2 1...
{ "tags": [], "title": "Shuffle 2" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef pair<int,int> pii;\ntypedef pair<ll,ll> pll;\n\ntypedef int _loop_int;\n#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)\n#define FOR(i,a,b) for(_loop_int i=(_loop_int)...
AIZU/p02298
# Is-Convex For a given polygon g, print "1" if g is a convex polygon, "0" otherwise. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees. g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The lin...
[ { "input": { "stdin": "5\n0 0\n2 0\n1 1\n2 2\n0 2" }, "output": { "stdout": "0" } }, { "input": { "stdin": "4\n0 0\n3 1\n2 3\n0 3" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4\n0 6\n6 -2\n1 0\n-1 0" }, "output": { ...
{ "tags": [], "title": "Is-Convex" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define db long double\nconst db eps = 1e-6;\nint sgn(db a, db b = 0) {\n\ta -= b;\n\treturn (a > eps) - (a < -eps);\n}\n\nstruct poi {\n\tdb x, y;\n\tvoid r() { cin >> x; cin >> y; }\n\tvoid w() { cout << x << ' ' << y << '\\n'; }\n\tpoi operator -(poi p) {...
AIZU/p02445
# Swap Write a program which reads a sequence of integers $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and swap specified elements by a list of the following operation: * swapRange($b, e, t$): For each integer $k$ ($0 \leq k < (e - b)$, swap element $(b + k)$ and element $(t + k)$. Constraints * $1 \leq n \leq 1,000$ * $-1,0...
[ { "input": { "stdin": "11\n1 2 3 4 5 6 7 8 9 10 11\n1\n1 4 7" }, "output": { "stdout": "1 8 9 10 5 6 7 2 3 4 11" } }, { "input": { "stdin": "11\n-1 0 3 3 4 6 0 2 14 10 21\n0\n0 -1 0" }, "output": { "stdout": "-1 0 3 3 4 6 0 2 14 10 21\n" } }, { "in...
{ "tags": [], "title": "Swap" }
{ "cpp": "#include \"bits/stdc++.h\"\nusing namespace std;\n#define long int64_t\n\ntemplate<class T>\ninline istream& operator >>( istream& s, vector<T>& v ) {\n\tfor( size_t i = 0; i < v.size(); ++i ) { s >> v[i]; }\n\treturn s;\n}\n\ntemplate<class T>\ninline ostream& operator <<( ostream& s, vector<T>& v ) {\n\tf...
CodeChef/bogosort
Recently Johnny have learned bogosort sorting algorithm. He thought that it is too ineffective. So he decided to improve it. As you may know this algorithm shuffles the sequence randomly until it is sorted. Johnny decided that we don't need to shuffle the whole sequence every time. If after the last shuffle several fir...
[ { "input": { "stdin": "3\n2\n6\n10" }, "output": { "stdout": "2\n1826/189\n877318/35343\n" } }, { "input": { "stdin": "3\n2\n2\n12" }, "output": { "stdout": "2\n2\n19455868963/549972423\n" } }, { "input": { "stdin": "3\n4\n6\n19" }, "...
{ "tags": [], "title": "bogosort" }
{ "cpp": null, "java": null, "python": "def gcd(p,q):\n while(q):\n p,q = q,p%q;\n return p;\n \ndef main():\n num = [0]*160;den = [0]*160\n num[0] = 1;num[1] = 0;num[2] = 2;num[3] = 10;\n den[0] = 1;den[1] = 1;den[2] = 1;den[3] = 3;\n t = int(raw_input());\n for j in range(t):\n ...
CodeChef/colarr
Chef had a hard time arguing with his friend, and after getting a great old kick Chef saw a colored array with N cells, numbered from 1 to N. The kick was so strong that Chef suddenly understood the rules of the game. Each cell is painted with a color. Here the colors are numbered from 1 to M. For any cell i, Chef c...
[ { "input": { "stdin": "1\n4 2 1\n1 1 2 2\n1 1\n1 1\n1 1\n3 1\n0 1\n0 1\n1 0\n1 0" }, "output": { "stdout": "5" } } ]
{ "tags": [], "title": "colarr" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\nfor _ in range(t):\n n, m, k = map(int, raw_input().split())\n a = map(int, raw_input().split())\n b = [] # gain\n for _ in range(n):\n b.append(map(int, raw_input().split()))\n c = [] # loss\n for _ in range(n):\n c.a...
CodeChef/gameaam
Two players are playing a game. The game is played on a sequence of positive integer pairs. The players make their moves alternatively. During his move the player chooses a pair and decreases the larger integer in the pair by a positive multiple of the smaller integer in the pair in such a way that both integers in the...
[ { "input": { "stdin": "3\n1\n2 3\n2\n4 5\n5 6\n2\n2 3\n3 5" }, "output": { "stdout": "NO\nNO\nYES" } }, { "input": { "stdin": "3\n1\n2 3\n2\n4 5\n5 8\n2\n2 5\n3 5" }, "output": { "stdout": "NO\nNO\nYES\n" } }, { "input": { "stdin": "3\n1\n2 3...
{ "tags": [], "title": "gameaam" }
{ "cpp": null, "java": null, "python": "#!/usr/bin/env python\n#-*- coding:utf-8 -*-\n\n\ndef convert_pair(a, b):\n res = []\n if a < b:\n a, b = b, a\n while b:\n res.append(a / b)\n a, b = b, a % b\n res[-1] -= 1\n while res[-1] == 0:\n del res[-1]\n return tuple(re...
CodeChef/lucky5
Chef loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Chef has a positive integer N. He can apply any of the following operations as many times as he want...
[ { "input": { "stdin": "3\n25\n46\n99" }, "output": { "stdout": "2\n1\n2\n" } }, { "input": { "stdin": "3\n43\n46\n17" }, "output": { "stdout": "1\n1\n1\n" } }, { "input": { "stdin": "3\n0\n1\n104" }, "output": { "stdout": "1\n1\...
{ "tags": [], "title": "lucky5" }
{ "cpp": null, "java": null, "python": "t=int(raw_input())\nwhile t>0:\n\tt-=1\n\tl=[x for x in raw_input().strip()]\n\tc=0\n\tfor i in xrange(len(l)):\n\t\tif l[i]=='4' or l[i]=='7':\n\t\t\tc+=1\n\tprint i+1-c" }
CodeChef/proposal
In PrimeLand, there existed a very handsome young prince named Prima. He greatly desired the Princess of Mathematics – Facie. However, before accepting his hand in marriage, Facie asked Prima to solve the following problem: The figure below shows a simple multiplication problem. However, not all the decimal digits ar...
[ { "input": { "stdin": "5\n2 3 4 6 8" }, "output": { "stdout": "1" } }, { "input": { "stdin": "5\n4 6 12 9 3" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5\n4 6 8 9 6" }, "output": { "stdout": "0\n" } }, { ...
{ "tags": [], "title": "proposal" }
{ "cpp": null, "java": null, "python": "def isvalid(s, c):\n if c.difference(s):\n return False\n return True\ndef main():\n n = input()\n l = raw_input().split()\n s = set(l)\n answer = 0\n for _ in xrange(111, 1000):\n a = [i for i in str(_)]\n if isvalid(s, set(a)):\n ...
CodeChef/tech07
You are given a square with 'n' points on each side of the square. None of these points co-incide with the corners of this square. You have to compute the total number of triangles that can be formed using these '4n' points (n points on each side of the square) as vertices of the triangle. Input First line contains ...
[ { "input": { "stdin": "1\n1" }, "output": { "stdout": "4" } }, { "input": { "stdin": "1\n-32" }, "output": { "stdout": "-333824\n" } }, { "input": { "stdin": "1\n18" }, "output": { "stdout": "56376\n" } }, { "input":...
{ "tags": [], "title": "tech07" }
{ "cpp": null, "java": null, "python": "t = int(input())\nfor i in range(t):\n n = int(input())\n print(int(10 * n * n * n - 6 * n * n))" }
Codeforces/1012/B
# Chemical table Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table. Recently scientists discovered t...
[ { "input": { "stdin": "1 5 3\n1 3\n1 1\n1 5\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2 2 3\n1 2\n2 2\n2 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4 3 6\n1 2\n1 3\n2 2\n2 3\n3 1\n3 3\n" }, "ou...
{ "tags": [ "constructive algorithms", "dfs and similar", "dsu", "graphs", "matrices" ], "title": "Chemical table" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nclass DSU {\n int n;\n int* p;\n int* w;\n\n public:\n DSU(int _n) {\n n = _n;\n p = new int[n];\n w = new int[n];\n for (int i = 0; i < n; i++) {\n p[i] = i;\n w[i] = 1;\n }\n }\n int getNum(int a) {\n int b = a;\n while (...
Codeforces/1037/B
# Reach Median You are given an array a of n integers and an integer s. It is guaranteed that n is odd. In one operation you can either increase or decrease any single element by one. Calculate the minimum number of operations required to make the median of the array being equal to s. The median of the array with od...
[ { "input": { "stdin": "3 8\n6 5 8\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "7 20\n21 15 12 11 20 19 12\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "3 1\n1 2 5\n" }, "output": { "stdout": "1\n...
{ "tags": [ "greedy" ], "title": "Reach Median" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int MOD(long long int a, long long int b) {\n if (a > b)\n return a - b;\n else\n return b - a;\n}\nint max3(int a, int b, int c) { return max(c, max(a, b)); }\nlong long int power(long long int x, long long int y, long long int p) {\n long ...
Codeforces/105/C
# Item World Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider...
[ { "input": { "stdin": "4\nsword weapon 10 2 3 2\npagstarmor armor 0 15 3 1\niceorb orb 3 2 13 2\nlongbow weapon 9 1 2 1\n6\nmike gladiator 5 longbow\nbobby sentry 6 pagstarmor\npetr gladiator 7 iceorb\nteddy physician 6 sword\nblackjack sentry 8 sword\njoe physician 6 iceorb\n" }, "output": { ...
{ "tags": [ "brute force", "implementation", "sortings" ], "title": "Item World" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst string a1[3] = {\"weapon\", \"armor\", \"orb\"},\n a2[4] = {\"gladiator\", \"sentry\", \"physician\"};\nstruct ww {\n int a, b, id;\n} b[1010], c[1010];\nint i, j, k, n, m, t, S, sheng;\nint zhi[1010], id[1010], in[1010], an[3], re[3], F[101...
Codeforces/1081/D
# Maximum Distance Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago. You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k. Let's define the cost of the path as the maximum weight of the...
[ { "input": { "stdin": "4 5 3\n1 2 3\n1 2 5\n4 2 1\n2 3 2\n1 4 4\n1 3 3\n" }, "output": { "stdout": "3 3 3 " } }, { "input": { "stdin": "2 3 2\n2 1\n1 2 3\n1 2 2\n2 2 1\n" }, "output": { "stdout": "2 2 " } }, { "input": { "stdin": "4 4 3\n1 2 ...
{ "tags": [ "dsu", "graphs", "shortest paths", "sortings" ], "title": "Maximum Distance" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mx = 1e5 + 5, inf = 2e9;\nint n, vr[mx], m, k, maxx, dis[mx], cnt;\nvector<pair<int, int> > adj[mx];\nset<pair<int, int> > st;\nbool mark[mx], boo[mx];\nvoid dks(int v) {\n dis[v] = 0;\n st.insert(make_pair(0, v));\n while (cnt != n) {\n auto a...
Codeforces/1100/D
# Dasha and Chess This is an interactive task. Dasha and NN like playing chess. While playing a match they decided that normal chess isn't interesting enough for them, so they invented a game described below. There are 666 black rooks and 1 white king on the chess board of size 999 × 999. The white king wins if he g...
[ { "input": { "stdin": "999 999\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n&lt;...&gt;\n26 13\n26 14\n26 15\n26 16\n\n1 700 800\n\n2 1 2\n\n&lt;...&gt;\n\n-1 -1 -1\n" }, "output": { "stdout": "998 998\n" } }, { "input": { "stdin": "999 999\n1 1\n1 2\n2 1\n4 2\n1 3\n2 1\n&lt;...&gt;\n26 ...
{ "tags": [ "constructive algorithms", "games", "interactive" ], "title": "Dasha and Chess" }
{ "cpp": "#include <bits/stdc++.h>\nint x, y, k, c[4];\nbool ocp[1000][1000];\nstruct POS {\n int x, y;\n} p[667];\nvoid to(int dx, int dy) {\n x += dx, y += dy;\n if (ocp[x][y]) x -= dx;\n printf(\"%d %d\\n\", x, y);\n fflush(stdout);\n scanf(\"%d\", &k);\n if (k == -1) exit(0);\n if (k == 0) exit(0);\n ocp...
Codeforces/1129/A2
# Toy Train Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train wi...
[ { "input": { "stdin": "2 3\n1 2\n1 2\n1 2\n" }, "output": { "stdout": "5 6 " } }, { "input": { "stdin": "5 7\n2 4\n5 1\n2 3\n3 4\n4 1\n5 3\n3 5\n" }, "output": { "stdout": "10 9 10 10 9 " } }, { "input": { "stdin": "5 1\n3 2\n" }, "ou...
{ "tags": [ "brute force", "greedy" ], "title": "Toy Train" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nint m;\nvector<int> v[5002];\nint w[5002];\nint main() {\n cin >> n >> m;\n for (int i = 0; i < n; i++) w[i] = INT_MAX;\n for (int i = 0; i < m; i++) {\n int a, b;\n scanf(\"%d%d\", &a, &b);\n a--;\n b--;\n v[a].push_back(b);\n w[a...
Codeforces/1149/B
# Three Religions During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peac...
[ { "input": { "stdin": "6 8\nabdabc\n+ 1 a\n+ 1 d\n+ 2 b\n+ 2 c\n+ 3 a\n+ 3 b\n+ 1 c\n- 2\n" }, "output": { "stdout": "YES\nYES\nYES\nYES\nYES\nYES\nNO\nYES\n" } }, { "input": { "stdin": "6 8\nabbaab\n+ 1 a\n+ 2 a\n+ 3 a\n+ 1 b\n+ 2 b\n+ 3 b\n- 1\n+ 2 z\n" }, "output...
{ "tags": [ "dp", "implementation", "strings" ], "title": "Three Religions" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\ninline T CHMAX(T& x, const T y) {\n return x = (x < y) ? y : x;\n}\ntemplate <class T>\ninline T CHMIN(T& x, const T y) {\n return x = (x > y) ? y : x;\n}\nusing PII = pair<int, int>;\nusing PLL = pair<long long, long long>;\nusing LL = l...
Codeforces/1170/B
# Bad Days Every day Kotlin heroes analyze the statistics of their website. For n days, they wrote out n numbers a_1, a_2, ..., a_n, where a_i is the number of visits on the i-th day. They believe that a day is bad if there are at least 2 days before it with a strictly greater number of visits. For example, if n=8 an...
[ { "input": { "stdin": "8\n3 1 4 1 5 9 2 6\n" }, "output": { "stdout": "\n2\n" } }, { "input": { "stdin": "5\n1 1 1 1 1\n" }, "output": { "stdout": "\n0\n" } }, { "input": { "stdin": "13\n2 7 1 8 2 8 1 8 2 8 4 5 9\n" }, "output": { ...
{ "tags": [ "*special", "implementation" ], "title": "Bad Days" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1189/A
# Keanu Reeves After playing Neo in the legendary "Matrix" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem. Let's call a string consisting of only zeroes and ones good if it contains different numbers of zeroes a...
[ { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "6\n100011\n" }, "output": { "stdout": "2\n1 00011\n" } }, { "input": { "stdin": "2\n10\n" }, "output": { "stdout": "2\n1 0\n" } }...
{ "tags": [ "strings" ], "title": "Keanu Reeves" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar c[101];\nint main() {\n int n, nr0 = 0, nr1 = 0;\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> c[i];\n if (c[i] == '0')\n nr0++;\n else\n nr1++;\n }\n if (nr1 == nr0) {\n cout << \"2\\n\";\n cout << c[0] << \" \";\n ...
Codeforces/1208/A
# XORinacci Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: * f(0) = a; * f(1) = b; * f(n) = f(n-1) ⊕ f(n-2) when n > ...
[ { "input": { "stdin": "3\n3 4 2\n4 5 0\n325 265 1231232\n" }, "output": { "stdout": "7\n4\n76\n" } }, { "input": { "stdin": "10\n669924290 408119795 804030560\n663737793 250734602 29671646\n431160679 146708815 289491233\n189259304 606497663 379372476\n707829111 49504411 817...
{ "tags": [ "math" ], "title": "XORinacci" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int t, i, j, n, ans, a, b, t1, t2;\nint main() {\n ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);\n ;\n cin >> t;\n while (t--) {\n cin >> a >> b >> n;\n if ((n % 3) == 0) {\n cout << a << '\\n';\n } else if ((n % ...
Codeforces/1227/A
# Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis, [l_1; r_1], [l_2; r_2], …, [l_n; r_n]. The segment [l; r] includes the bounds, i.e. it is a set of such x that l ≤ x ≤ r. The length of the segment [l; r] is equal to r - l. Two segments [a; b] and [c; d] have a comm...
[ { "input": { "stdin": "4\n3\n4 5\n5 9\n7 7\n5\n11 19\n4 17\n16 16\n3 12\n14 17\n1\n1 10\n1\n1 1\n" }, "output": { "stdout": "2\n4\n0\n0\n" } }, { "input": { "stdin": "1\n2\n999999997 999999998\n999999999 1000000000\n" }, "output": { "stdout": "1\n" } }, ...
{ "tags": [ "math" ], "title": "Math Problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid sort(int& a, int& b, int& c) {\n if (a < b) swap(a, b);\n if (b < c) swap(b, c);\n if (a < b) swap(a, b);\n}\nint main() {\n int t;\n cin >> t;\n for (int i = 0; i < t; ++i) {\n int n, max_left = 0, min_right = 1e9;\n cin >> n;\n for (int j...
Codeforces/124/D
# Squares You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one. A square (x;...
[ { "input": { "stdin": "2 2 1 0 0 1\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "2 2 10 11 0 1\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "2 4 3 -1 3 7\n" }, "output": { "stdout": "2" } }, {...
{ "tags": [ "math" ], "title": "Squares" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long a, b, x1, y1, x2, y2;\n cin >> a >> b >> x1 >> y1 >> x2 >> y2;\n long long Sum1 = x1 + y1;\n long long Sum2 = x2 + y2;\n long long Min1 = x1 - y1;\n long long Min2 = x2 - y2;\n if (Sum1 < 0 || Sum2 < 0) {\n long long minSum =...
Codeforces/1269/B
# Modulo Equality You are given a positive integer m and two integer sequence: a=[a_1, a_2, …, a_n] and b=[b_1, b_2, …, b_n]. Both of these sequence have a length n. Permutation is a sequence of n different positive integers from 1 to n. For example, these sequences are permutations: [1], [1,2], [2,1], [6,7,3,4,1,2,5...
[ { "input": { "stdin": "3 2\n0 0 0\n1 1 1\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4 3\n0 0 2 1\n2 0 1 1\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "5 10\n0 0 0 1 2\n2 1 0 0 0\n" }, "output": { ...
{ "tags": [ "brute force", "sortings" ], "title": "Modulo Equality" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 4e3 + 1000;\nlong long a[maxn];\nlong long b[maxn];\nlong long sa[maxn];\nlong long sb[maxn];\nbool check(int pos1, int pos2, int N) {\n for (int i = 1; i <= N - 1; i++) {\n if (sb[pos1++] != sa[pos2++]) return false;\n }\n return true;\...
Codeforces/1291/B
# Array Sharpening You're given an array a_1, …, a_n of n non-negative integers. Let's call it sharpened if and only if there exists an integer 1 ≤ k ≤ n such that a_1 < a_2 < … < a_k and a_k > a_{k+1} > … > a_n. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: * The a...
[ { "input": { "stdin": "10\n1\n248618\n3\n12 10 8\n6\n100 11 15 9 7 8\n4\n0 1 1 0\n2\n0 0\n2\n0 1\n2\n1 0\n2\n1 1\n3\n0 1 0\n3\n1 0 1\n" }, "output": { "stdout": "Yes\nYes\nYes\nNo\nNo\nYes\nYes\nYes\nYes\nNo\n" } }, { "input": { "stdin": "10\n1\n248618\n3\n12 5 8\n6\n000 16...
{ "tags": [ "greedy", "implementation" ], "title": "Array Sharpening" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }\nlong long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }\nvoid solve() {\n long long n;\n cin >> n;\n long long a[n];\n for (long long i = 0; i < n; i++) {\n ...
Codeforces/1311/C
# Perform the Combo You want to perform the combo on your opponent in one popular fighting game. The combo is the string s consisting of n lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in s. I.e. if s="abca" then you have to press 'a', then 'b', 'c' and 'a' again...
[ { "input": { "stdin": "3\n4 2\nabca\n1 3\n10 5\ncodeforces\n2 8 3 2 9\n26 10\nqwertyuioplkjhgfdsazxcvbnm\n20 10 1 2 3 5 10 5 9 4\n" }, "output": { "stdout": "4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n\n0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 \n\n2 1 1 2 9 2 2 2 5 2 2 ...
{ "tags": [ "brute force" ], "title": "Perform the Combo" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long max(long long a, long long b) { return a > b ? a : b; }\nlong long min(long long a, long long b) { return a < b ? a : b; }\nlong long abso(long long a) { return a > 0 ? a : -a; }\nbool cmp(const pair<int, int> &a, const pair<int, int> &b) {\n if (a....
Codeforces/1334/A
# Level Statistics Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level. All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the numbe...
[ { "input": { "stdin": "6\n3\n0 0\n1 1\n1 2\n2\n1 0\n1000 3\n4\n10 1\n15 2\n10 2\n15 2\n1\n765 432\n2\n4 4\n4 3\n5\n0 0\n1 0\n1 0\n1 0\n1 0\n" }, "output": { "stdout": "NO\nYES\nNO\nYES\nNO\nYES\n" } }, { "input": { "stdin": "1\n2\n110 2\n115 112\n" }, "output": { ...
{ "tags": [ "implementation", "math" ], "title": "Level Statistics" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(2)\n#pragma GCC optimize(3)\nusing namespace std;\ntemplate <class nmsl>\ninline void read(nmsl &x) {\n x = 0;\n char ch = getchar(), w = 0;\n while (!isdigit(ch)) w = (ch == '-'), ch = getchar();\n while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), c...
Codeforces/1354/C2
# Not So Simple Polygon Embedding The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, n is always even, and in C2, n is always odd. You are given a regular polygon with 2 ⋅ n vertices (it's convex and has equal sides and equal angles) and all its sides...
[ { "input": { "stdin": "3\n3\n5\n199\n" }, "output": { "stdout": "1.931851653\n3.196226611\n126.687663797\n" } }, { "input": { "stdin": "99\n3\n5\n7\n9\n11\n13\n15\n17\n19\n21\n23\n25\n27\n29\n31\n33\n35\n37\n39\n41\n43\n45\n47\n49\n51\n53\n55\n57\n59\n61\n63\n65\n67\n69\n71...
{ "tags": [ "binary search", "brute force", "geometry", "math" ], "title": "Not So Simple Polygon Embedding" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst double pi = acos(-1.0);\nconst double eps = pi / 180.0;\nint main() {\n int t;\n cin >> t;\n while (t--) {\n double n;\n cin >> n;\n double angle = 180.0 / n;\n double first = 1.0 / 2.0 / sin(angle / 2.0 * eps);\n double a1 = 180.0 / n ...
Codeforces/1374/B
# Multiply by 2, divide by 6 You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder). Your task is to find the minimum number of moves needed to obtain 1 from n or determine if it's impossible to do that. You have to answer t independ...
[ { "input": { "stdin": "7\n1\n2\n3\n12\n12345\n15116544\n387420489\n" }, "output": { "stdout": "0\n-1\n2\n-1\n-1\n12\n36\n" } }, { "input": { "stdin": "1\n999838675\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "7\n3\n2\n5\n2\n...
{ "tags": [ "math" ], "title": "Multiply by 2, divide by 6" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long MOD = 1e9 + 7;\nlong long GCD(long long x, long long y) {\n if (!y) return x;\n return GCD(y, x % y);\n}\nlong long LCM(long long x, long long y) { return (x * y) / GCD(x, y); }\nvoid solve() {\n long long n, m = 1e9, i = 0, j = 0, k = 0, p, x, a ...
Codeforces/1397/C
# Multiples of Length You are given an array a of n integers. You want to make all elements of a equal to zero by doing the following operation exactly three times: * Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be...
[ { "input": { "stdin": "4\n1 3 2 4\n" }, "output": { "stdout": "1 1\n-1\n2 4\n9 6 12 \n1 4\n0 -12 -8 -16 \n" } }, { "input": { "stdin": "2\n-492673762 -496405053\n" }, "output": { "stdout": "1 2\n985347524 992810106 \n1 1\n-492673762 \n2 2\n-496405053 \n" }...
{ "tags": [ "constructive algorithms", "greedy", "number theory" ], "title": "Multiples of Length" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e6 + 10;\nint a[N];\nlong long ans[N];\nint main() {\n int n;\n cin >> n;\n for (int i = 1; i <= n; i++) {\n cin >> a[i];\n }\n if (n == 1) {\n cout << 1 << ' ' << 1 << '\\n';\n cout << 0 << '\\n';\n cout << 1 << ' ' << 1 << '\\...
Codeforces/1420/D
# Rescue Nibel! Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make...
[ { "input": { "stdin": "3 3\n1 3\n2 3\n3 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 1\n1 1\n2 2\n3 3\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "7 3\n1 7\n3 8\n4 5\n6 7\n1 3\n5 10\n8 9\n" }, "ou...
{ "tags": [ "combinatorics", "data structures", "sortings" ], "title": "Rescue Nibel!" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx,avx2,fma\")\n#pragma GCC optimization(\"unroll-loops\")\nusing namespace std;\nconst int maxn = 3e5 + 5;\nconst long long mod = 998244353;\nconst double eps = 1e-6L;\nstruct node {\n int l, r;\n bool operator<(const node& ...
Codeforces/1439/C
# Greedy Shopping You are given an array a_1, a_2, …, a_n of integers. This array is non-increasing. Let's consider a line with n shops. The shops are numbered with integers from 1 to n from left to right. The cost of a meal in the i-th shop is equal to a_i. You should process q queries of two types: * 1 x y: for...
[ { "input": { "stdin": "10 6\n10 10 10 6 6 5 5 5 3 1\n2 3 50\n2 4 10\n1 3 10\n2 2 36\n1 4 7\n2 2 17\n" }, "output": { "stdout": "8\n3\n6\n2\n" } }, { "input": { "stdin": "1 1\n5\n2 1 6\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin...
{ "tags": [ "binary search", "data structures", "divide and conquer", "greedy", "implementation" ], "title": "Greedy Shopping" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconstexpr int seed = 131;\nconstexpr long long LLMAX = 2e18;\nconstexpr int MOD = 1e9 + 7;\nconstexpr double eps = 1e-8;\nconstexpr int MAXN = 1e6 + 10;\nconstexpr int hmod1 = 0x48E2DCE7;\nconstexpr int hmod2 = 0x60000005;\ninline long long sqr(long long x) { ...
Codeforces/1466/C
# Canine poetry After his wife's tragic death, Eurydice, Orpheus descended to the realm of death to see her. Reaching its gates was uneasy, but passing through them proved to be even more challenging. Mostly because of Cerberus, the three-headed hound of Hades. Orpheus, a famous poet, and musician plans to calm Cerb...
[ { "input": { "stdin": "7\nbabba\nabaac\ncodeforces\nzeroorez\nabcdcba\nbbbbbbb\na\n" }, "output": { "stdout": "\n1\n1\n0\n1\n1\n4\n0\n" } }, { "input": { "stdin": "7\nbbbaa\nabaac\ncodeforces\nyeroorez\nabadcbc\nbbabbbb\na\n" }, "output": { "stdout": "3\n1\n0\...
{ "tags": [ "dp", "greedy", "strings" ], "title": "Canine poetry" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <tuple>\n#include <utility>\n#include <cmath>\n#include <bitset>\nusing namespace std;\n\n\n\nint main() {\n ios_base::sync_with_stdio(false);\n ...
Codeforces/1490/D
# Permutation Transformation A permutation — is a sequence of length n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3, 5, 2, 1, 4], [1, 3, 2] — permutations, and [2, 3, 2], [4, 3, 1], [0] — no. Polycarp was recently gifted a permutation a[1 ... n] of length n. Polycarp likes t...
[ { "input": { "stdin": "3\n5\n3 5 2 1 4\n1\n1\n4\n4 3 1 2\n" }, "output": { "stdout": "\n1 0 2 3 1 \n0 \n0 1 3 2 \n" } }, { "input": { "stdin": "3\n5\n4 5 2 1 3\n1\n1\n4\n4 3 1 2\n" }, "output": { "stdout": "1 0 2 3 1\n0\n0 1 3 2\n" } }, { "input": ...
{ "tags": [ "dfs and similar", "divide and conquer", "implementation" ], "title": "Permutation Transformation" }
{ "cpp": "#include \"bits/stdc++.h\"\n#include <cstring>\nusing namespace std;\ntypedef long long ll;\n#define FastIO cin.sync_with_stdio(false), cin.tie(nullptr)\n#define pb push_back\n#define fs first\n#define sc second\n#define mkp make_pair\n#define endl '\\n'\n\n// IO\n#define multitest signed __T; cin >> __T; ...
Codeforces/1512/G
# Short Task Let us denote by d(n) the sum of all divisors of the number n, i.e. d(n) = ∑_{k | n} k. For example, d(1) = 1, d(4) = 1+2+4=7, d(6) = 1+2+3+6=12. For a given number c, find the minimum n such that d(n) = c. Input The first line contains one integer t (1 ≤ t ≤ 10^4). Then t test cases follow. Each tes...
[ { "input": { "stdin": "12\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n39\n691\n" }, "output": { "stdout": "\n1\n-1\n2\n3\n-1\n5\n4\n7\n-1\n-1\n18\n-1\n" } }, { "input": { "stdin": "12\n2\n2\n3\n4\n6\n9\n8\n14\n4\n6\n33\n1097\n" }, "output": { "stdout": "-1\n-1\n2\n3\n5\n-...
{ "tags": [ "brute force", "dp", "math", "number theory" ], "title": "Short Task" }
{ "cpp": "#include <cstdio>\n#include <algorithm>\n#define ll long long int\nusing namespace std;\nconst int MAX = 1e7+10;\nint t, c;\nint ans[MAX];\nint prime[MAX], pcount;\nbool vis[MAX];\nll d[MAX], f[MAX];\nvoid eular() {\n for (int i = 1; i <= MAX; i++) ans[i] = MAX;\n ans[1] = 1;\n for (int i = 2; i < ...
Codeforces/153/E
# Euclidean Distance You are given a multiset of points on the plane with integer coordinates. Find the maximum distance between two points from this multiset. Input The first line of input contains the number of points n (2 ≤ n ≤ 50). Next, n pairs of lines follow, each describing a single point: the first line con...
[ { "input": { "stdin": "3\n0\n1\n2\n3\n4\n5\n" }, "output": { "stdout": "5.656854249\n" } }, { "input": { "stdin": "3\n10\n12\n-5\n8\n10\n12\n" }, "output": { "stdout": "15.5241747\n" } } ]
{ "tags": [ "*special" ], "title": "Euclidean Distance" }
{ "cpp": null, "java": null, "python": null }
Codeforces/167/C
# Wizards and Numbers In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it — a and b. The order of the numbers is not important. Let's consider a ≤ b for the sake of definiteness. The players can cast one of the two spells in turns: * Replace b with b - ak. Nu...
[ { "input": { "stdin": "4\n10 21\n31 10\n0 1\n10 30\n" }, "output": { "stdout": "First\nSecond\nSecond\nFirst\n" } }, { "input": { "stdin": "7\n576460752303423487 2\n82 9\n101 104\n10 21\n31 10\n0 1\n10 30\n" }, "output": { "stdout": "First\nSecond\nSecond\nFir...
{ "tags": [ "games", "math" ], "title": "Wizards and Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint dfs(long long a, long long b) {\n if (!a) return 1;\n if (dfs(b % a, a)) return 0;\n if (a & 1) return (b / a) & 1;\n return (b / a) % (a + 1) & 1;\n}\nint main() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long a, b;\n scanf(\"%lld...
Codeforces/188/D
# Asterisks You are given a number n. Print n lines, i-th line should consist of i characters "*". Lines' indices are 1-based. Input The only line of input contains an integer n (1 ≤ n ≤ 50). Output Output the described pattern. Examples Input 3 Output * ** *** Input 6 Output * ** *** **** ***** ******
[ { "input": { "stdin": "3\n" }, "output": { "stdout": "*\n**\n***\n" } }, { "input": { "stdin": "6\n" }, "output": { "stdout": "*\n**\n***\n****\n*****\n******\n" } } ]
{ "tags": [ "*special", "implementation" ], "title": "Asterisks" }
{ "cpp": null, "java": null, "python": null }
Codeforces/209/B
# Pixels Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors x and y (x ≠ y) meet in a violent fight, then t...
[ { "input": { "stdin": "1 1 1\n" }, "output": { "stdout": " 1\n" } }, { "input": { "stdin": "3 1 0\n" }, "output": { "stdout": " 3\n" ...
{ "tags": [ "constructive algorithms", "math" ], "title": "Pixels" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T1>\nvoid deb(T1 e) {\n cout << \"-->\" << e << endl;\n}\ntemplate <class T1, class T2>\nvoid deb(T1 e1, T2 e2) {\n cout << \"-->\" << e1 << \" \" << e2 << endl;\n}\ntemplate <class T1, class T2, class T3>\nvoid deb(T1 e1, T2 e2, T3 e3) {\n ...
Codeforces/235/A
# LCM Challenge Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you h...
[ { "input": { "stdin": "7\n" }, "output": { "stdout": "210\n" } }, { "input": { "stdin": "9\n" }, "output": { "stdout": "504\n" } }, { "input": { "stdin": "447244\n" }, "output": { "stdout": "89460162932862372\n" } }, { ...
{ "tags": [ "number theory" ], "title": "LCM Challenge" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long gcd(long long A, long long B) {\n if (A < B) return gcd(B, A);\n if (B == 0) return A;\n return gcd(B, A % B);\n}\nlong long lcm(long long A, long long B) {\n long long Tmp = gcd(A, B);\n return ((A / Tmp) * B);\n}\nint main() {\n ios::sync_wit...
Codeforces/25/D
# Roads not only in Berland Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in tota...
[ { "input": { "stdin": "7\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n" }, "output": { "stdout": "1\n3 1 1 4 " } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n1 4\n3 1\n3 4\n" }, "output": { ...
{ "tags": [ "dsu", "graphs", "trees" ], "title": "Roads not only in Berland" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nint A[1005];\nint m;\nint visited[1005];\nvector<int> adj[1005];\nvector<pair<int, int>> unused;\nvector<int> plans;\nmap<pair<int, int>, int> used;\nvoid initialize(int Arr[], int N) {\n for (int i = 0; i < N; i++) Arr[i] = i;\n}\nbool Find(int Arr[]...
Codeforces/283/D
# Cows and Cool Sequences Bessie and the cows have recently been playing with "cool" sequences and are trying to construct some. Unfortunately they are bad at arithmetic, so they need your help! A pair (x, y) of positive integers is "cool" if x can be expressed as the sum of y consecutive integers (not necessarily po...
[ { "input": { "stdin": "3\n6 4 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n20 6 3 4\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5\n1 8 1 7 9\n" }, "output": { "stdout": "3\n" } }, ...
{ "tags": [ "dp", "math", "number theory" ], "title": "Cows and Cool Sequences" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MaxN = 5010;\nint n;\nlong long a[MaxN];\nint F[MaxN];\nbool u[MaxN][MaxN];\nint main() {\n cin >> n;\n for (int i = 1; i <= n; ++i) cin >> a[i];\n for (int i = n; i >= 1; --i) {\n long long k = a[i];\n for (int j = i - 1; j >= 1; --j) {\n ...
Codeforces/306/C
# White, Black and White Again Polycarpus is sure that his life fits the description: "first there is a white stripe, then a black one, then a white one again". So, Polycarpus is sure that this rule is going to fulfill during the next n days. Polycarpus knows that he is in for w good events and b not-so-good events. A...
[ { "input": { "stdin": "3 2 1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3 2 2\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "4 2 2\n" }, "output": { "stdout": "4\n" } }, { "input": {...
{ "tags": [ "combinatorics", "number theory" ], "title": "White, Black and White Again" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint m[5000][5000];\nchar v[5000][5000];\nlong long int force(int p, int t) {\n if (p == 0 && t == 0) return 1;\n if (p == 0 || t == 0) return 0;\n if (p > t) return 0;\n if (v[p][t]) return m[p][t];\n long long int s = 0;\n s += t * force(p - 1, t - 1);\...
Codeforces/331/D2
# Escaping on Beaveractor Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a b × b square on a plane. Each point x, y (0 ≤ x, y ≤ b) belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and ...
[ { "input": { "stdin": "3 3\n0 0 0 1\n0 2 2 2\n3 3 2 3\n12\n0 0 L 0\n0 0 L 1\n0 0 L 2\n0 0 L 3\n0 0 L 4\n0 0 L 5\n0 0 L 6\n2 0 U 2\n2 0 U 3\n3 0 U 5\n1 3 D 2\n1 3 R 2\n" }, "output": { "stdout": " 0 ...
{ "tags": [ "graphs" ], "title": "Escaping on Beaveractor" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = ~0U >> 1;\nconst long long INF = ~0ULL >> 1;\nconst int maxn = 120000;\nint x2[maxn], y2[maxn], nxtid[maxn];\nint Dir[maxn];\nint M, N;\nint f[maxn][52];\nlong long g[maxn][52];\nset<pair<int, int> > S[4][maxn];\nconst int dx[4] = {0, 0, 1, -1}...
Codeforces/354/B
# Game with Strings Given an n × n table T consisting of lowercase English letters. We'll consider some string s good if the table contains a correct path corresponding to the given string. In other words, good strings are all strings we can obtain by moving from the left upper cell of the table only to the right and ...
[ { "input": { "stdin": "2\nxa\nay\n" }, "output": { "stdout": "FIRST\n" } }, { "input": { "stdin": "2\nab\ncd\n" }, "output": { "stdout": "DRAW\n" } }, { "input": { "stdin": "3\naab\nbcb\nbac\n" }, "output": { "stdout": "DRAW\n" ...
{ "tags": [ "bitmasks", "dp", "games" ], "title": "Game with Strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint const B = 20, N = B + 3, M = 2 * B + 3, MASK = (1 << B) + 20,\n alpha = 26 + 4;\nint INF = 2e9;\nint n, have[M][alpha], dp[2][MASK];\nstring s[N];\ninline bool in(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < n); }\nint main() {\n ios:...
Codeforces/378/C
# Maze Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to...
[ { "input": { "stdin": "3 4 2\n#..#\n..#.\n#...\n" }, "output": { "stdout": "#..#\n..#X\n#..X\n" } }, { "input": { "stdin": "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#\n" }, "output": { "stdout": "#...\n#.#.\nX#..\nXX.#\nX#X#\n" } }, { "input": { "st...
{ "tags": [ "dfs and similar" ], "title": "Maze" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, k, s, num;\nconst int h[4] = {1, 0, -1, 0};\nconst int v[4] = {0, 1, 0, -1};\nchar maze[505][505];\nint mark[505][505];\nint nei(int x, int y) {\n int ans = 0;\n for (int i = 0; i < 4; i++)\n if (x + h[i] > -1 && x + h[i] < n && y + v[i] > -1 &&...
Codeforces/39/E
# What Has Dirichlet Got to Do with That? You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1 items, that leads to the existence of a box in which there are at least two items. Having heard of that principle, but having not mastered the technique of logical thinking, 8 ...
[ { "input": { "stdin": "3 1 4\n" }, "output": { "stdout": "Stas\n\n" } }, { "input": { "stdin": "2 2 10\n" }, "output": { "stdout": "Masha\n\n" } }, { "input": { "stdin": "5 5 16808\n" }, "output": { "stdout": "Masha\n\n" } ...
{ "tags": [ "dp", "games" ], "title": "What Has Dirichlet Got to Do with That?" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool **dyn;\nlong long a, b;\nlong long n;\nbool isp(long long a1, long long b1) {\n long long ans = 1;\n for (long long i = 0; i < b1; i++) {\n ans *= a1;\n if (ans >= n) return true;\n }\n return false;\n}\nint main() {\n cin >> a >> b >> n;\n lo...
Codeforces/425/A
# Sereja and Swaps As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i ≠ j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of fun...
[ { "input": { "stdin": "5 10\n-1 -1 -1 -1 -1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "10 2\n10 -1 2 2 2 2 2 2 -1 10\n" }, "output": { "stdout": "32\n" } }, { "input": { "stdin": "1 10\n1\n" }, "output": { "s...
{ "tags": [ "brute force", "sortings" ], "title": "Sereja and Swaps" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m, a[205];\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < n; i++) scanf(\"%d\", &a[i]);\n int t1[205], t2[205], ans = -999999999;\n for (int i = 0; i < n; ++i) {\n for (int j = i; j < n; ++j) {\n int n1 = 0, n2 = 0, tp = 0;...
Codeforces/449/D
# Jzzhu and Numbers Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n) a group of size k. Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 ≤ k ≤ n)? Help him and print this number modulo 1000000007 (109 + 7)....
[ { "input": { "stdin": "3\n2 3 3\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n0 1 2 3\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "6\n5 2 0 5 2 1\n" }, "output": { "stdout": "53\n" } },...
{ "tags": [ "bitmasks", "combinatorics", "dp" ], "title": "Jzzhu and Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = 1000000007;\nconst int N = (1 << 20) + 100;\nbool one(int mask, int i) { return mask & (1 << i); }\nint n, a[N], d[N];\nint p[N];\nint main() {\n scanf(\"%d\", &n);\n p[0] = 1;\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", &a[i]);\n ...
Codeforces/470/C
# Eval You are given a simple arithmetic expression of the form a?b, where a and b are integer constants, and ? can be one of the following operations: '+' (addition), '-' (subtraction), '*' (multiplication), '/' (integer division) or '%' (modulo operation). Output the result of evaluation of this expression. Input ...
[ { "input": { "stdin": "123+456\n" }, "output": { "stdout": "579\n" } }, { "input": { "stdin": "192/5\n" }, "output": { "stdout": "38\n" } }, { "input": { "stdin": "945%19\n" }, "output": { "stdout": "14\n" } } ]
{ "tags": [ "*special" ], "title": "Eval" }
{ "cpp": null, "java": null, "python": null }
Codeforces/494/A
# Treasure Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found o...
[ { "input": { "stdin": "(((#)((#)\n" }, "output": { "stdout": "1\n2\n" } }, { "input": { "stdin": "#\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "()((#((#(#()\n" }, "output": { "stdout": "1\n1\n3\n" } }, ...
{ "tags": [ "greedy" ], "title": "Treasure" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 1e5 + 10;\nchar a[maxn];\nint N, cnt[maxn];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n cin >> (a + 1);\n N = strlen(a + 1);\n int last_hash = 0;\n vector<int> ans;\n for (int i = 1; i <= N; +...
Codeforces/518/E
# Arthur and Questions After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elem...
[ { "input": { "stdin": "3 2\n? 1 2\n" }, "output": { "stdout": "0 1 2\n" } }, { "input": { "stdin": "5 1\n-10 -9 ? -7 -6\n" }, "output": { "stdout": "-10 -9 -8 -7 -6\n" } }, { "input": { "stdin": "5 3\n4 6 7 2 9\n" }, "output": { ...
{ "tags": [ "greedy", "implementation", "math", "ternary search" ], "title": "Arthur and Questions" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, k;\nlong long a[300002];\nbool b[300002];\nint main() {\n cin >> n >> k;\n for (int i = 1; i <= n; i++) {\n string s;\n cin >> s;\n if (s == \"?\") {\n b[i] = true;\n } else {\n int t = 0;\n if (s[0] == '-') {\n t = 1...
Codeforces/544/E
# Remembering Strings You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has ...
[ { "input": { "stdin": "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n" }, "output": { "stdout": "3" } ...
{ "tags": [ "bitmasks", "dp" ], "title": "Remembering Strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read() {\n int x = 0, f = 1;\n char ch = getchar();\n while (ch < '0' || ch > '9') {\n if (ch == '-') f = -1;\n ch = getchar();\n }\n while (ch >= '0' && ch <= '9') {\n x = x * 10 + ch - '0';\n ch = getchar();\n }\n return x * f;\...
Codeforces/571/B
# Minimization You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n. You need to permute the array elements so that value <image> became minimal possible. In particular, it is allowed not to change order of elements at all. Input The first line contains...
[ { "input": { "stdin": "5 2\n3 -5 3 -5 3\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "3 2\n1 2 4\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "6 3\n4 3 4 3 2 5\n" }, "output": { "stdout": "3\n" ...
{ "tags": [ "dp", "greedy", "sortings" ], "title": "Minimization" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst long long LINF = 0x3f3f3f3f3f3f3f3fll;\nconst int MAXK = 5010;\nconst int MAXN = 3e5 + 10;\nlong long memo[MAXK][MAXK];\nlong long pref[MAXN];\nlong long query(int l, int r) {\n if (l == r) return 0;\n return pref[r] - pref...
Codeforces/592/E
# BCPC BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces. BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team. <imag...
[ { "input": { "stdin": "7 6 6\n3 2\n1 7\n5 7\n3 7\n6 4\n8 9\n8 5\n" }, "output": { "stdout": "11\n" } }, { "input": { "stdin": "5 2 2\n1 1\n4 1\n2 3\n3 2\n3 4\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "8 61 898\n315 863\n524...
{ "tags": [ "binary search", "geometry", "two pointers" ], "title": "BCPC" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long double PI = atan2((long double)0, (long double)-1);\nconst long double EPS = 1e-18;\nvector<long double> fi;\nlong long N, b, c;\ninline long double ang(pair<long long, long long> p) {\n return atan2(p.second, p.first);\n}\nint main() {\n cin >> N...
Codeforces/614/C
# Peter and Snow Blower Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then swi...
[ { "input": { "stdin": "3 0 0\n0 1\n-1 2\n1 2\n" }, "output": { "stdout": "12.566370614359176\n" } }, { "input": { "stdin": "4 1 -1\n0 0\n1 2\n2 0\n1 1\n" }, "output": { "stdout": "21.991148575128552\n" } }, { "input": { "stdin": "4 0 0\n1 -1\...
{ "tags": [ "binary search", "geometry", "ternary search" ], "title": "Peter and Snow Blower" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 1e9 + 7;\nconst int N = 1e5 + 5;\nconst double PI = acos(-1);\nstruct point {\n double x, y;\n};\ndouble dist(point a, point b) {\n return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));\n}\ndouble scal(double x, double y, double...
Codeforces/633/H
# Fibonacci-ish II Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials. Fibonacci-ish potential of an array ai is computed as follows: 1. Remove all elements j if there exists i < j such that ai = aj. ...
[ { "input": { "stdin": "5 10\n2 1 2 1 2\n2\n2 4\n4 5\n" }, "output": { "stdout": "3\n3\n" } }, { "input": { "stdin": "5 10\n3 1 2 2 1\n2\n1 4\n4 5\n" }, "output": { "stdout": "9\n3\n" } }, { "input": { "stdin": "5 10\n3 2 4 2 1\n2\n2 1\n4 5\n"...
{ "tags": [ "data structures", "implementation" ], "title": "Fibonacci-ish II" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint N, M = 1 << 30, A[30000], B[30000], NB, Q, QL[30000], QR[30000],\n order[30000], ans[30000];\nstruct mint {\n int x;\n mint(const int& x = 0) : x(x % M) {}\n friend mint operator+(const mint& a, const mint& b) {\n return (a.x + b.x) % M;\n }\n...
Codeforces/662/C
# Binary Table You are given a table consisting of n rows and m columns. Each cell of the table contains either 0 or 1. In one move, you are allowed to pick any row or any column and invert all values, that is, replace 0 by 1 and vice versa. What is the minimum number of cells with value 1 you can get after applying ...
[ { "input": { "stdin": "3 4\n0110\n1010\n0111\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "20 20\n00010110100100010111\n11110000001100101110\n01110101110001111000\n10111101000111111110\n10000101011000000011\n00010010001011010000\n00000110010110111111\n010101...
{ "tags": [ "bitmasks", "brute force", "divide and conquer", "dp", "fft", "math" ], "title": "Binary Table" }
{ "cpp": "#include <bits/stdc++.h>\nclass Input {\n private:\n char buf[1000000], *p1, *p2;\n\n public:\n inline char getc() {\n if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);\n return p1 == p2 ? EOF : *(p1++);\n }\n template <typename tp>\n inline Input &operator>>(tp &n) {\n n = 0;\n ...
Codeforces/687/A
# NP-Hard Problem Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting. Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e....
[ { "input": { "stdin": "4 2\n1 2\n2 3\n" }, "output": { "stdout": "3\n1 3 4 \n1\n2 " } }, { "input": { "stdin": "3 3\n1 2\n2 3\n1 3\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "10 16\n6 10\n5 2\n6 4\n6 8\n5 3\n5 4\n6 2\n5 9\n5 ...
{ "tags": [ "dfs and similar", "graphs" ], "title": "NP-Hard Problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read() {\n int x = 0;\n char ch = getchar();\n bool positive = 1;\n for (; ch < '0' || ch > '9'; ch = getchar())\n if (ch == '-') positive = 0;\n for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';\n return positive ? x : ...
Codeforces/709/E
# Centroids Tree is a connected acyclic graph. Suppose you are given a tree consisting of n vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed <image>. You are given a tree of size n and can perform no more t...
[ { "input": { "stdin": "3\n1 2\n2 3\n" }, "output": { "stdout": "1 1 1\n" } }, { "input": { "stdin": "5\n1 2\n1 3\n1 4\n1 5\n" }, "output": { "stdout": "1 0 0 0 0\n" } }, { "input": { "stdin": "8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n" }, ...
{ "tags": [ "data structures", "dfs and similar", "dp", "graphs", "greedy", "trees" ], "title": "Centroids" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int N = 4e5 + 5;\nint head[N], to[N * 2], nxt[N * 2], tot, n;\nint son[N], ans[N], down[N], up[N], tmp[N];\nvoid init() {\n for (int i = 1; i <= n; ++i) head[i] = -1;\n tot = 0;\n}\nvoid add_edge(int u, int v) {\n to[tot] ...
Codeforces/730/G
# Car Repair Shop Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered f...
[ { "input": { "stdin": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000\n" }, "output": { "stdout": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000\n" } }, { "input": { "stdin": "3\n9 2\n7 3\n2 4\n" }, "output"...
{ "tags": [ "implementation" ], "title": "Car Repair Shop" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct span {\n int s;\n int d;\n};\nstruct range {\n int s;\n int e;\n};\nspan* makeSpan(int s, int d) {\n span* out = (span*)malloc(sizeof(span));\n out->s = s;\n out->d = d;\n return out;\n}\nint main() {\n int n;\n cin >> n;\n vector<span> spans...
Codeforces/754/B
# Ilya and tic-tac-toe game Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya coul...
[ { "input": { "stdin": "o.x.\no...\n.x..\nooxx\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "x.ox\nox..\nx.o.\noo.x\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "xx..\n.oo.\nx...\noox.\n" }, "output": ...
{ "tags": [ "brute force", "implementation" ], "title": "Ilya and tic-tac-toe game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[5][5];\nint judge() {\n int tmp;\n for (int i = 0; i < 4; ++i) {\n if ((tmp = a[i][0] + a[i][1] + a[i][2]) == 3)\n return 1;\n else if (tmp == -3)\n return 0;\n if ((tmp = a[i][3] + a[i][1] + a[i][2]) == 3)\n return 1;\n else...
Codeforces/774/K
# Stepan and Vowels Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa". Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a s...
[ { "input": { "stdin": "18\naeiouyaaeeiioouuyy\n" }, "output": { "stdout": "aeiouyaeeioouy" } }, { "input": { "stdin": "22\niiiimpleeemeentatiioon\n" }, "output": { "stdout": "implemeentatioon" } }, { "input": { "stdin": "13\npobeeeedaaaaa\n" ...
{ "tags": [ "*special", "implementation", "strings" ], "title": "Stepan and Vowels" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint const M = 1 << 16;\nint md = 1e9 + 7;\nint i, j, n, m, N, t, x, y, k, nom;\nint main() {\n cin >> n;\n string s, s1;\n cin >> s;\n for (int i = 0; i < n; i++)\n if (s[i] == 'a' || s[i] == 'o' || s[i] == 'e' || s[i] == 'i' ||\n s[i] == 'u' || ...
Codeforces/79/D
# Password Finally Fox Ciel arrived in front of her castle! She have to type a password to enter her castle. An input device attached to her castle is a bit unusual. The input device is a 1 × n rectangle divided into n square panels. They are numbered 1 to n from left to right. Each panel has a state either ON or OF...
[ { "input": { "stdin": "3 2 1\n1 2\n3\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "10 8 2\n1 2 3 5 6 7 8 9\n3 5\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "10000 10 100\n2339 4118 4595 5499 5582 6055 8693 9002 9...
{ "tags": [ "bitmasks", "dp", "shortest paths" ], "title": "Password" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 30000;\nint q[maxn], val[maxn], b[maxn], n, k, l, dp[1 << 21], D[21][21], dist[maxn];\nvector<int> g[maxn], ones;\nint main() {\n scanf(\"%d%d%d\", &n, &k, &l);\n for (int i = 0; i < k; ++i) {\n int x;\n scanf(\"%d\", &x);\n val[x] ...
Codeforces/820/A
# Mister B and Book Reading Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages. At first day Mister B read v0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day ...
[ { "input": { "stdin": "5 5 10 5 4\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "12 4 12 4 1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "15 1 100 0 0\n" }, "output": { "stdout": "15\n" } },...
{ "tags": [ "implementation" ], "title": "Mister B and Book Reading" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int c, v0, v1, a, l, i, j, count = 1, h;\n cin >> c >> v0 >> v1 >> a >> l;\n c = c - v0;\n j = v0;\n while (c > 0) {\n i = j + a - l;\n if (j + a > v1) i = v1 - l;\n j = i + l;\n c = c - i;\n count++;\n }\n cout << count;\n...
Codeforces/846/B
# Math Show Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him tj minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order. ...
[ { "input": { "stdin": "5 5 10\n1 2 4 8 16\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "3 4 11\n1 2 3 4\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1 3 0\n6 3 4\n" }, "output": { "stdout": "0\n"...
{ "tags": [ "brute force", "greedy" ], "title": "Math Show" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int gcd(int x, int y) {\n int z;\n while (y) {\n z = x % y;\n x = y;\n y = z;\n }\n return x;\n}\nlong long a[48];\nlong long s;\nint main() {\n ios_base::sync_with_stdio(false);\n long long n, k, m;\n int t = 0;\n cin >> n >> k >> m;\n...
Codeforces/867/D
# Gotta Go Fast You're trying to set the record on your favorite video game. The game consists of N levels, which must be completed sequentially in order to beat the game. You usually complete each level as fast as possible, but sometimes finish a level slower. Specifically, you will complete the i-th level in either ...
[ { "input": { "stdin": "1 8\n2 8 81\n" }, "output": { "stdout": "3.140000000000\n" } }, { "input": { "stdin": "2 30\n20 30 80\n3 9 85\n" }, "output": { "stdout": "31.400000000000\n" } }, { "input": { "stdin": "4 319\n63 79 89\n79 97 91\n75 87 ...
{ "tags": [ "binary search", "dp" ], "title": "Gotta Go Fast" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pdd = pair<double, double>;\nusing vi = vector<int>;\nusing vii = vector<ii>;\nusing vs = vector<string>;\nconst double PI = acos(-1.0);\nconst double EPS = 1e-14;\nconst int M...
Codeforces/893/A
# Chess For Three Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three. So they play with each other according to f...
[ { "input": { "stdin": "2\n1\n2\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "3\n1\n1\n2\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "99\n1\n3\n2\n2\n3\n1\n1\n3\n3\n3\n3\n3\n3\n1\n1\n3\n3\n3\n3\n1\n1\n3\n2\n...
{ "tags": [ "implementation" ], "title": "Chess For Three" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint f(int a, int v, int b) {\n if (a == v) {\n return b;\n } else {\n return a;\n }\n}\nint main() {\n int n, a = 1, b = 2, c = 3, v, t;\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> v;\n if (v == c) {\n cout << \"NO\";\n ret...
Codeforces/914/F
# Substrings in a String Given a string s, process q queries, each having one of the following forms: * 1 i c — Change the i-th character in the string to c. * 2 l r y — Consider the substring of s starting at position l and ending at position r. Output the number of times y occurs as a substring in it. Input ...
[ { "input": { "stdin": "ababababa\n3\n2 1 7 aba\n1 5 c\n2 1 7 aba\n" }, "output": { "stdout": "3\n1\n" } }, { "input": { "stdin": "abcdcbc\n5\n2 1 7 bc\n1 4 b\n2 4 7 bc\n1 2 a\n2 1 4 aa\n" }, "output": { "stdout": "2\n2\n1\n" } }, { "input": { ...
{ "tags": [ "bitmasks", "brute force", "data structures", "string suffix structures", "strings" ], "title": "Substrings in a String" }
{ "cpp": "#include <bits/stdc++.h>\nconst int B = 2;\nconst int C = 26;\nconst int N = 100005;\nchar v[B];\nchar s[N], now[N];\nint n, q, x, y, o;\nstd::bitset<N> a;\nstd::bitset<N> se[C];\nint main() {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0), std::cout.tie(0);\n std::cin >> s + 1, n = strlen(s + 1);\n ...
Codeforces/937/D
# Sleepy Game Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and m edges. One of the vertices contains a chip. Initially the chip is located at vertex s. Players take turns moving the chip along some edge of the graph. Petya goes first. Pla...
[ { "input": { "stdin": "3 2\n1 3\n1 1\n0\n2\n" }, "output": { "stdout": "Lose\n" } }, { "input": { "stdin": "2 2\n1 2\n1 1\n1\n" }, "output": { "stdout": "Draw\n" } }, { "input": { "stdin": "5 6\n2 2 3\n2 4 5\n1 4\n1 5\n0\n1\n" }, "out...
{ "tags": [ "dfs and similar", "dp", "games", "graphs" ], "title": "Sleepy Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 2e5 + 10;\nstruct point {\n int to;\n int nxt;\n} edge[maxn];\nint n, m, k, tot, sta, is_draw;\nint head[maxn], vis[maxn][2], out[maxn];\nvector<int> st;\ninline void add(int u, int v) {\n tot++;\n edge[tot].nxt = head[u];\n edge[tot].to ...
Codeforces/962/F
# Simple Cycles Edges You are given an undirected graph, consisting of n vertices and m edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself). A cycle in a graph is called a ...
[ { "input": { "stdin": "6 7\n2 3\n3 4\n4 2\n1 2\n1 5\n5 6\n6 1\n" }, "output": { "stdout": "6\n1 2 3 5 6 7 \n" } }, { "input": { "stdin": "3 3\n1 2\n2 3\n3 1\n" }, "output": { "stdout": "3\n1 2 3 \n" } }, { "input": { "stdin": "5 6\n1 2\n2 3\n...
{ "tags": [ "dfs and similar", "graphs", "trees" ], "title": "Simple Cycles Edges" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 100010;\nint n, m, cnt, head[N], dfn[N], dep[N], s[N], f[N], g[N], fa[N];\nbool vis[N];\nstruct edge {\n int to, next;\n bool tag;\n} e[N << 1];\nint find(int x) {\n if (f[x] == x)\n return x;\n else\n return f[x] = find(f[x]);\n}\nvoid...
Codeforces/990/B
# Micro-World You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them. You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive ...
[ { "input": { "stdin": "6 5\n20 15 10 15 20 25\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "7 1\n101 53 42 102 101 55 54\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "7 1000000\n1 1 1 1 1 1 1\n" }, "out...
{ "tags": [ "greedy", "sortings" ], "title": "Micro-World" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ndouble const EPS = 1.0E-9;\nint const MOD = (int)1e9 + 7;\ninline void read(int &x) { scanf(\"%d\", &(x)); }\ninline void read(long long &x) { scanf(\"%lld\", &x); }\ninline void read(double &x) { scanf(\"%lf\", &x); }\ninline long long gcd(long long x, long l...
HackerEarth/ankit-and-race-team-10
Coach Ankit is forming a team for the Annual Inter Galactic Relay Race. He has N students that train under him and he knows their strengths. The strength of a student is represented by a positive integer. The coach has to form a team of K students. The strength of a team is defined by the strength of the weakest stude...
[ { "input": { "stdin": "2\n2 1\n5 4\n3 2\n1 0 2\n\nSAMPLE" }, "output": { "stdout": "9\n1\n" } }, { "input": { "stdin": "2\n2 2\n3 -1\n3 2\n1 1 2\n\nELPMAS" }, "output": { "stdout": "1000000006\n3\n" } }, { "input": { "stdin": "2\n2 1\n6 3\n3 ...
{ "tags": [], "title": "ankit-and-race-team-10" }
{ "cpp": null, "java": null, "python": "\"\"\"\ncalculate ncr%p where p is prime using inverse modulo\n\n1)nck = n*(n-1)*(n-2)...(n-k+1)\n ---------------------- = num/den\n 1*2*............k\n \n2)nck = num*den^-1\n\n3)nck%p = ((num%p)*(den^-1%p))%p\n\n4)den^-1 = den^(p-2)=den^b\n\n4)nck%p =...
HackerEarth/chandu-and-daspal-2
Continuing from previous version of codeXplod series i.e. CodeXplod 1.0,Chandu and daspal are still fighting over a matter of MOMO's(they are very fond of Mo Mos of sector 3..:P).This time the fight became so savior that they want to kill each other.As we all know that during a fight it is most probable outcome that b...
[ { "input": { "stdin": "1\n2 3 Chandu\n\nSAMPLE" }, "output": { "stdout": "Daspal\n" } }, { "input": { "stdin": "51\n8 10 Chandu\n2 2959 Daspal\n2903 6 Chandu\n9 2938 Daspal\n10 2907 Daspal\n8 2966 Daspal\n8 2938 Daspal\n4 2984 Daspal\n5 2939 Daspal\n2 2975 Daspal\n2925 10 C...
{ "tags": [], "title": "chandu-and-daspal-2" }
{ "cpp": null, "java": null, "python": "def gcd(x,y):\n if y==0: return x\n else: return gcd(y,x%y)\nt=int(input())\nfor zz in range(t):\n x,y,win=raw_input().split()\n x=int(x);y=int(y)\n while True:\n k=gcd(x,y)\n x-=k;y-=k\n if x==0 or y==0:\n break\n if wi...
HackerEarth/digital-numbers
Bosky is a very curious child who turned 13 yesterday. His parents gifted him a digital watch which he really liked. He was amazed to see how each number can be represented inside one cell only by switching different edges on and off. Today, while he was in his home alone, getting bored looking at his watch, an idea p...
[ { "input": { "stdin": "4\n1 14\n1 50\n4 28\n5 700\n\nSAMPLE" }, "output": { "stdout": "1\n10\n3\n32491" } }, { "input": { "stdin": "5\n1 14\n1 50\n4 28\n5 700\n3 147" }, "output": { "stdout": "1\n10\n3\n32491\n739" } } ]
{ "tags": [], "title": "digital-numbers" }
{ "cpp": null, "java": null, "python": "digits=[6,2,5,5,4,5,6,3,7,6]\nvalids=[{} for i in xrange(11)]\nst0={}\nfor i in xrange(10):\n valids[1][1<<i]=[digits[i],1]\nfor l in xrange(2,11):\n for k in xrange(10):\n for prev in valids[l-1]:\n if prev&(1<<k)==0:\n act=prev|(1<<k)\n prevdata=va...
HackerEarth/give-me-my-test-monk
Professor just has checked all the N students tests. Everything was fine but then he realised that none of the students had signed their papers, so he doesn't know which test belongs to which student. But it's definitely not professors's job to catch every student and asked him to find his paper! So he will hand out...
[ { "input": { "stdin": "3 1 3\n\nSAMPLE" }, "output": { "stdout": "833333340" } }, { "input": { "stdin": "60 0 58" }, "output": { "stdout": "630738177" } }, { "input": { "stdin": "7 3 7" }, "output": { "stdout": "195634923" }...
{ "tags": [], "title": "give-me-my-test-monk" }
{ "cpp": null, "java": null, "python": "mod=10**9+7\nA=[[0 for i in range(105)] for j in range(105)]\nB=[0]*105\n\nfor i in range(0,102):\n\tA[i][0]=1\n\tfor j in range(1,i+1):\n\t\tA[i][j]=A[i-1][j]+A[i-1][j-1]\n\t\nB[0]=1\nfor i in range(1,102):\n\tB[i]=B[i-1]*i\ndef ans(n,x):\n\ty=0\n\tfor i in range(0,x+1):\n...
HackerEarth/lexicographically-preceding-permutation
Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given permutation is the lexicographically least permutation, then print the input permutation itself. Input Format: First line is the test c...
[ { "input": { "stdin": "1\n3\n1 3 2\n\nSAMPLE" }, "output": { "stdout": "1 2 3" } } ]
{ "tags": [], "title": "lexicographically-preceding-permutation" }
{ "cpp": null, "java": null, "python": "for _ in xrange(input()):\n n= input()\n a=map(int,raw_input().split())\n b=[]\n flag=0\n for x in xrange(n-1,0,-1):\n b=a[:x-1]\n if a[x]<a[x-1]:\n for y in xrange(n-1,x-1,-1):\n if a[x-1]>a[y]:\n b....
HackerEarth/monks-birthday-party
Monk's birthday is coming this weekend! He wants to plan a Birthday party and is preparing an invite list with his friend Puchi. He asks Puchi to tell him names to add to the list. Puchi is a random guy and keeps coming up with names of people randomly to add to the invite list, even if the name is already on the list...
[ { "input": { "stdin": "1\n7\nchandu\nparo\nrahul\nmohi\nparo\narindam\nrahul\n\nSAMPLE" }, "output": { "stdout": "arindam\nchandu\nmohi\nparo\nrahul\n" } }, { "input": { "stdin": "5\n10\nbb\nbb\nab\naa\nbb\nbb\nba\naa\nbb\nab\n10\nbb\nab\nba\nab\nba\nba\nab\nba\nba\nba\n10\...
{ "tags": [], "title": "monks-birthday-party" }
{ "cpp": null, "java": null, "python": "for t in xrange(input()):\n\tfor j in sorted(set([raw_input() for i in xrange(input())])):\n\t\tprint j" }
HackerEarth/play-with-string
Suppose you have a string S which has length N and is indexed from 0 to N−1. String R is the reverse of the string S. The string S is funny if the condition |Si−Si−1|=|Ri−Ri−1| is true for every i from 1 to N−1. (Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denote...
[ { "input": { "stdin": "2\nacxz\nbcxz\n\nSAMPLE" }, "output": { "stdout": "Funny\nNot Funny\n" } }, { "input": { "stdin": "2\nacxz\nbcxz" }, "output": { "stdout": "Funny\nNot Funny\n" } }, { "input": { "stdin": "2\nywea\nzcwb\n\nLSMAQD" },...
{ "tags": [], "title": "play-with-string" }
{ "cpp": null, "java": null, "python": "n=int(raw_input())\ncount=0\n\nfor i in range(n):\n\tstrings=raw_input()\n\tf=0\n\tif len(strings) <2:\n\t\tprint \"Not Funny\"\n\telse:\n\t\treverse_s=strings[::-1]\n\t\tfor s in range(1,len(strings)):\n\t\t\tcheck=abs(ord(strings[s])-ord(strings[s-1]))==abs(ord(reverse_s[...
HackerEarth/roy-and-wobbly-numbers
Roy is looking for Wobbly Numbers. An N-length wobbly number is of the form "ababababab..." and so on of length N, where a != b. A 3-length wobbly number would be of form "aba". Eg: 101, 121, 131, 252, 646 etc But 111, 222, 999 etc are not 3-length wobbly number, because here a != b condition is not satisfied. ...
[ { "input": { "stdin": "6\n3 1\n3 2\n3 100\n4 3\n4 4\n5 2\n\nSAMPLE" }, "output": { "stdout": "101\n121\n-1\n1313\n1414\n12121\n" } }, { "input": { "stdin": "6\n12 1\n5 2\n5 100\n4 2\n4 4\n5 2\n\nSAMPLE" }, "output": { "stdout": "101010101010\n12121\n-1\n1212\n...
{ "tags": [], "title": "roy-and-wobbly-numbers" }
{ "cpp": null, "java": null, "python": "def getNDigitWobble(a,b,n):\n\tnum=a\n\ti=1\n\twhile(i<n):\n\t\tif(i%2==1):\n\t\t\tnum=num*10+b\n\t\telse:\n\t\t\tnum=num*10+a\n\t\ti=i+1\n\treturn num\n\t\n\nt=input()\nwhile(t>0):\n\tn,k=(int(x) for x in raw_input().split())\n\tli=list()\n\tfor a in range(9):\n\t\tfor b i...
HackerEarth/substrings-count-3
Jack is the most intelligent student in the class.To boost his intelligence,his class teacher gave him a problem named "Substring Count". Problem : His Class teacher gave him n strings numbered from 1 to n which consists of only lowercase letters (each having length not more than 10) and then ask Q questions related t...
[ { "input": { "stdin": "3\ncode\ncoder\ncoding\n2\n1 3 code\n1 3 co\n\nSAMPLE" }, "output": { "stdout": "2 \n3" } }, { "input": { "stdin": "1000\nspnogbxcrp\nbaukbqnmyu\nogxdxhtcwz\nbsjufplykz\nkhicdrdixl\nzookiqggel\nfczygjfhfa\nkobjmuswpw\ndypvhmsjlh\nvbcnnpwmgm\nvzvkarnyq...
{ "tags": [], "title": "substrings-count-3" }
{ "cpp": null, "java": null, "python": "##def bs(temp,i,j,k):\n## if k>=j:\n## return j\n## if k<i:\n## return 0\n## while j-i>1:\n## mid=(i+j)/2\n## if temp[mid]<k:\n## i=mid+1\n## elif temp[mid]>k:\n## j=mid-1\n## else:\n## i=mi...
HackerEarth/vibhu-and-his-mathematics
In code world all genders are considered equal ( It means their is nothing like male or female). Now their are N distinct persons living in this hypothetical world. Each person can pair up with any other person or can even remain single. One day Vbhu planned to visit code world. Being a maths guy , he always try to b...
[ { "input": { "stdin": "2\n2\n3\n\n\nSAMPLE" }, "output": { "stdout": "2\n4\n" } }, { "input": { "stdin": "2\n13\n3\n\n\nEAOOLS" }, "output": { "stdout": "568504\n4\n" } }, { "input": { "stdin": "2\n1\n15\n\n\nLBTQNE" }, "output": { ...
{ "tags": [], "title": "vibhu-and-his-mathematics" }
{ "cpp": null, "java": null, "python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\n#print 'Hello World!'\nt = int(raw_input())\nmaxi = 0\nnum = [0 for i in range(t)]\nfor i in range(t):\n\tnum[i] = int(raw_input())\...
p02610 AIsing Programming Contest 2020 - Camel Train
We have N camels numbered 1,2,\ldots,N. Snuke has decided to make them line up in a row. The happiness of Camel i will be L_i if it is among the K_i frontmost camels, and R_i otherwise. Snuke wants to maximize the total happiness of the camels. Find the maximum possible total happiness of the camel. Solve this probl...
[ { "input": { "stdin": "3\n2\n1 5 10\n2 15 5\n3\n2 93 78\n1 71 59\n3 57 96\n19\n19 23 16\n5 90 13\n12 85 70\n19 67 78\n12 16 60\n18 48 28\n5 4 24\n12 97 97\n4 57 87\n19 91 74\n18 100 76\n7 86 46\n9 100 57\n3 76 73\n6 84 93\n1 6 84\n11 75 94\n19 15 3\n12 11 34" }, "output": { "stdout": "25\n22...
{ "tags": [], "title": "p02610 AIsing Programming Contest 2020 - Camel Train" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing ll = long long;\nusing p = pair<ll, ll>;\n\n// \nll f(vector<p>& pp){\n // 独立に行っているのに、番号n-1(0-origin)より上は意味がないので矯正\n for(auto& elm : pp){\n elm.first = min(elm.first, (ll)(pp.size()));\n }\n\n vector<vector<ll> > s(pp.size()+1);\n ...
p02741 Panasonic Programming Contest 2020 - Kth Term
Print the K-th element of the following sequence of length 32: 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 Constraints * 1 \leq K \leq 32 * All values in input are integers. Input Input is given from Standard Input in the following format: K Output Print ...
[ { "input": { "stdin": "27" }, "output": { "stdout": "5" } }, { "input": { "stdin": "6" }, "output": { "stdout": "2" } }, { "input": { "stdin": "-13" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "24...
{ "tags": [], "title": "p02741 Panasonic Programming Contest 2020 - Kth Term" }
{ "cpp": "#include <iostream>\nusing namespace std;\n\nint main(){\n int aa[33]={0,1,1,1,2,1,2,1,5,2,2,1,5,1,2,1,14,1,5,1,5,2,2,1,15,2,2,5,4,1,4,1,51};\n int x;\n cin>>x;\n cout<<aa[x];\n return 0;\n}\n", "java": "import java.util.Scanner;\npublic class Main {\n public static void main(String[] ar...
p02876 AtCoder Grand Contest 040 - Balance Beam
We have N balance beams numbered 1 to N. The length of each beam is 1 meters. Snuke walks on Beam i at a speed of 1/A_i meters per second, and Ringo walks on Beam i at a speed of 1/B_i meters per second. Snuke and Ringo will play the following game: * First, Snuke connects the N beams in any order of his choice and m...
[ { "input": { "stdin": "3\n4 1\n5 2\n6 3" }, "output": { "stdout": "0 1" } }, { "input": { "stdin": "4\n1 5\n4 7\n2 1\n8 4" }, "output": { "stdout": "1 2" } }, { "input": { "stdin": "2\n3 2\n1 2" }, "output": { "stdout": "1 4" ...
{ "tags": [], "title": "p02876 AtCoder Grand Contest 040 - Balance Beam" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef double D;\ntypedef pair<ll,ll> P;\n#define M 998244353\n#define F first\n#define S second\n#define PB push_back\n#define INF 100000000000000000\nstruct PQ{\n\tpriority_queue<ll>d;\n\tpriority_queue<ll,vector<ll>,greater<ll>>u;\n\...
p03010 diverta 2019 Programming Contest 2 - Diverta City
Diverta City is a new city consisting of N towns numbered 1, 2, ..., N. The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length of each road is undecided. A Hamiltonian path is a path that starts at one of the towns and visits each of the other towns exactly once...
[ { "input": { "stdin": "4" }, "output": { "stdout": "0 111 157 193\n111 0 224 239\n157 224 0 258\n193 239 258 0" } }, { "input": { "stdin": "3" }, "output": { "stdout": "0 6 15\n6 0 21\n15 21 0" } }, { "input": { "stdin": "10" }, "outp...
{ "tags": [], "title": "p03010 diverta 2019 Programming Contest 2 - Diverta City" }
{ "cpp": "#include<iostream>\n\n#define REP(i,j,k) for(int i=j;i<=k;++i)\nint A[9]={1,2,4,7,12,20,29,38,52}; long long B[11][11],N;\n\nint main(){\n\tstd::cin>>N; long long MX=1;\n\tREP(i,2,N){REP(j,1,i-1){B[i][j]=B[j][i]=MX*A[j - 1];}MX+=B[i][i-1]+B[i][i-2]-B[i-1][i-2];}\n\tREP(i,1,N) REP(j,1,N) std::cout << B[i][j]...
p03150 KEYENCE Programming Contest 2019 - KEYENCE String
A string is called a KEYENCE string when it can be changed to `keyence` by removing its contiguous substring (possibly empty) only once. Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string. Constraints * The length of S is between 7 and 100 (inclusive). * S consists of lowerc...
[ { "input": { "stdin": "keyence" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "ashlfyha" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "keyofscience" }, "output": { "stdout": "YES" } }, { "inpu...
{ "tags": [], "title": "p03150 KEYENCE Programming Contest 2019 - KEYENCE String" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n string s,t=\"keyence\";\n cin>>s;\n if(s.substr(s.size()-7,7)==t||s.substr(0,7)==t)cout<<\"YES\";\n else{\n int a=0,b=0;\n while(a<7&&s[a]==t[a])a++;\n while(b<7&&s[s.size()-1-b]==t[6-b])b++;\n if(a+b>=7)cout<<\"YES\";\n else co...
p03294 AtCoder Beginner Contest 103 - Modulo Summation
You are given N positive integers a_1, a_2, ..., a_N. For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N). Here, X\ mod\ Y denotes the remainder of the division of X by Y. Find the maximum value of f. Constraints * All values in input are integers. * 2 \leq N \leq 3000 * 2 ...
[ { "input": { "stdin": "5\n7 46 11 20 11" }, "output": { "stdout": "90" } }, { "input": { "stdin": "7\n994 518 941 851 647 2 581" }, "output": { "stdout": "4527" } }, { "input": { "stdin": "3\n3 4 6" }, "output": { "stdout": "10"...
{ "tags": [], "title": "p03294 AtCoder Beginner Contest 103 - Modulo Summation" }
{ "cpp": "#include<bits/stdc++.h> \nusing namespace std;\nint main()\n{\n\tint n;\n\tscanf(\"%d\",&n);\n\tint ans=0,a;\n\tfor(int i=1;i<=n;i++)\n\t\tscanf(\"%d\",&a),ans+=a-1;\n\tcout<<ans<<endl;\n}", "java": "import java.util.*;\n\npublic class Main{\n public static void main(String[] args){\n Scanner sc = new...
p03452 AtCoder Regular Contest 090 - People on a Line
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate. You will given M pieces of information regarding the positions of these people. The i-th piece of...
[ { "input": { "stdin": "3 3\n1 2 1\n2 3 1\n1 3 5" }, "output": { "stdout": "No" } }, { "input": { "stdin": "100 0" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "10 3\n8 7 100\n7 9 100\n9 8 100" }, "output": { "stdout...
{ "tags": [], "title": "p03452 AtCoder Regular Contest 090 - People on a Line" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nconst int N = 1e5 + 10;\n\nint p[N], rec[N];\n\nint find(int x){\n\tif (p[x] == x) return x;\n\tint fa = find(p[x]);\n\trec[x] += rec[p[x]];\n\treturn p[x] = fa;\n}\n\nbool same(int x, int y){\n\treturn find(x) == find(y);\n}\n\nvoid unite(int x, int y, int ...
p03612 AtCoder Beginner Contest 072 - Derangement
You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero): Operation: Swap two adjacent elements in the permutation. You want to have p_i ≠ i for all 1≤i≤N. Find the minimum required number of operations to achieve this. Constraint...
[ { "input": { "stdin": "5\n1 4 3 5 2" }, "output": { "stdout": "2" } }, { "input": { "stdin": "9\n1 2 4 9 5 8 7 3 6" }, "output": { "stdout": "3" } }, { "input": { "stdin": "2\n1 2" }, "output": { "stdout": "1" } }, { ...
{ "tags": [], "title": "p03612 AtCoder Beginner Contest 072 - Derangement" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n \nint main() {\n int a,b;\n cin>>a;\n b=0;\n vector<int>vec(a);\n for(int i=0;i<a;i++){\n cin>>vec.at(i);\n }\n for(int j=0;j<a-1;j++){\n if(vec.at(j)==j+1){\n swap(vec.at(j),vec.at(j+1));\n b++;\n }\n }\n if(vec.at(a-1)==a){\n ...
p03771 AtCoder Grand Contest 012 - Camel and Oases
There are N oases on a number line. The coordinate of the i-th oases from the left is x_i. Camel hopes to visit all these oases. Initially, the volume of the hump on his back is V. When the volume of the hump is v, water of volume at most v can be stored. Water is only supplied at oases. He can get as much water as he...
[ { "input": { "stdin": "7 2\n-10 -4 -2 0 2 4 10" }, "output": { "stdout": "Impossible\nPossible\nPossible\nPossible\nPossible\nPossible\nImpossible" } }, { "input": { "stdin": "16 19\n-49 -48 -33 -30 -21 -14 0 15 19 23 44 52 80 81 82 84" }, "output": { "stdout"...
{ "tags": [], "title": "p03771 AtCoder Grand Contest 012 - Camel and Oases" }
{ "cpp": "#include<cstdio>\n#include<algorithm>\n#define N_ 201000\nusing namespace std;\nint n, V, w[N_], m;\nint C[20][N_], L[20], PP[30];\nint BE[20][N_], ED[20][N_];\nint D1[1048576], D2[1048576];\nint main(){\n int i, j;\n scanf(\"%d%d\",&n,&V);\n for(i=1;i<=n;i++)scanf(\"%d\",&w[i]);\n L[0] = V;\n ...
p03940 AtCoder Grand Contest 007 - Shik and Game
Imagine a game played on a line. Initially, the player is located at position 0 with N candies in his possession, and the exit is at position E. There are also N bears in the game. The i-th bear is located at x_i. The maximum moving speed of the player is 1 while the bears do not move at all. When the player gives a c...
[ { "input": { "stdin": "3 9 1\n1 3 8" }, "output": { "stdout": "12" } }, { "input": { "stdin": "2 1000000000 1000000000\n1 999999999" }, "output": { "stdout": "2999999996" } }, { "input": { "stdin": "3 9 3\n1 3 8" }, "output": { ...
{ "tags": [], "title": "p03940 AtCoder Grand Contest 007 - Shik and Game" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\n\nconst int MAXN = 100005;\n\nint n, e, T;\nint x[MAXN];\n\nvoid input()\n{\n\tscanf(\"%d%d%d\", &n, &e, &T);\n\tfor(int i = 1; i <= n; i++)\n\t\tscanf(\"%d\", &x[i]);\n}\n\nll f[MAXN];\n\nvoid solve()\n{\n\tf[0] = 0;\n\tll minv = 1e18...
AIZU/p00032
# Plastic Board There is a factory that inputs the data of the side and diagonal lengths to the machine and cuts out the plastic plate. At this factory, we cut out only parallelogram molds, although they vary in size. You have been ordered by your boss to count the number of rectangles and rhombuses produced among the...
[ { "input": { "stdin": "3,4,5\n5,5,8\n4,4,4\n5,4,3" }, "output": { "stdout": "1\n2" } }, { "input": { "stdin": "4,5,4\n8,5,5\n4,5,4\n3,4,6" }, "output": { "stdout": "0\n0\n" } }, { "input": { "stdin": "5,4,3\n8,5,5\n4,5,4\n5,4,3" }, "o...
{ "tags": [], "title": "Plastic Board" }
{ "cpp": "#include <iostream>\n#include <cstdio>\nusing namespace std;\n\nint main(){\n int a,b,c;\n int lozenges = 0,rectangle = 0;\n while(~scanf(\"%d,%d,%d\",&a,&b,&c)){\n if(a == b)lozenges++; \n if(c*c == (a*a + b*b) )rectangle++;\n }\n cout << rectangle << endl;\n cout << lozenges << endl;\n}", "java"...
AIZU/p00163
# Highway Toll In 20XX, the Aizu Chuo Road, which has a total distance of 58km and 6 sections from Atsushiokanomachi, Kitakata City to Minamiaizucho, is scheduled to be completed and opened. For half a year after opening, the toll will be halved for vehicles that pass the departure IC or arrival IC between 17:30 and ...
[ { "input": { "stdin": "2\n17 25\n4\n17 45\n4\n17 25\n7\n19 35\n0" }, "output": { "stdout": "250\n1300" } }, { "input": { "stdin": "2\n21 66\n5\n17 45\n0\n1 43\n0\n3 54\n2" }, "output": { "stdout": "300\n" } }, { "input": { "stdin": "2\n0 13\n...
{ "tags": [], "title": "Highway Toll" }
{ "cpp": "#include<iostream>\n#define N 7\n\nusing namespace std;\n\nint cost[N][N];\nint dis[N][N];\n\nvoid InitCost(){\n for(int i=0; i<N; i++) cost[i][i] = 0;\n\n cost[0][1] = 300;\n cost[0][2] = 500;\n cost[0][3] = 600;\n cost[0][4] = 700;\n cost[0][5] = 1350;\n cost[0][6] = 1650;\n\n cost[1][2] = 350;\n ...
AIZU/p00320
# Cuboid The educational program (AHK Education) of the Aiz Broadcasting Corporation broadcasts a program called "Play with Tsukuro" for children. Today is the time to make a box with drawing paper, but I would like to see if the rectangular drawing paper I prepared can make a rectangular parallelepiped. However, do n...
[ { "input": { "stdin": "2 2\n2 3\n2 3\n2 3\n2 2\n3 2" }, "output": { "stdout": "yes" } }, { "input": { "stdin": "2 2\n2 3\n2 3\n2 3\n2 2\n2 2" }, "output": { "stdout": "no" } }, { "input": { "stdin": "3 1\n4 5\n-2 2\n0 4\n2 3\n21 2" }, ...
{ "tags": [], "title": "Cuboid" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int h[7],w[7];\n int hantei1=0,hantei2=0,hantei3=0;\n int cnt1=0,cnt2=0,cnt3=0;\n int test1=0,test2=0,test3=0;\n bool flg = true;\n int sqrcnt=0;\n \n long long temp=0;\n\n for (int i=1 ; i<=6 ;i++) {\n cin >>...
AIZU/p00490
# Best Pizza problem Chairman K is a regular customer of the JOI pizza shop in the center of JOI city. For some reason, he decided to start a life-saving life this month. So he wanted to order the pizza with the highest calories per dollar among the pizzas he could order at the JOI pizza store. Let's call such a pizz...
[ { "input": { "stdin": "3\n12 2\n200\n50\n300\n100" }, "output": { "stdout": "37" } }, { "input": { "stdin": "3\n1 3\n1\n0\n10\n111" }, "output": { "stdout": "28\n" } }, { "input": { "stdin": "3\n4 2\n38\n50\n507\n010" }, "output": { ...
{ "tags": [], "title": "Best Pizza" }
{ "cpp": "#include<stdio.h>\n#include<string.h>\n#include<iostream>\n#include<algorithm>\nusing namespace std;\nint p[101]={0},t[101];\nint main(){\nint n,a,b,i,j,c,l,g;\ncin>>n>>a>>b>>c;\np[0]=c/a;\nfor(i=1;i<=n;i++)\n{\ncin>>t[i];\n}\nfor(i=1;i<=n;i++){\n\tfor(j=i+1;j<=n;j++){\n\t\tif(t[i]<t[j]){\n\t\t\tg=t[i];\n\t...
AIZU/p00676
# KND is So Sexy Problem KND is a student programmer at the University of Aizu. His chest is known to be very sexy. <image> For simplicity, the part of the skin that can be seen from the chest is represented by the isosceles triangle ABC in the figure. However, due to the slack in the clothes, the two sides AC and...
[ { "input": { "stdin": "2 2 1\n2 3 1\n3 2 3\n2 3 5" }, "output": { "stdout": "3.9681187851\n6.7970540913\n6.5668891783\n13.9527248554" } }, { "input": { "stdin": "4 2 2\n2 1 1\n1 2 5\n1 3 5" }, "output": { "stdout": "3.46410161514\n0.866025403784\n7.67644976905...
{ "tags": [], "title": "KND is So Sexy" }
{ "cpp": "#include<iostream>\n#include<algorithm>\n#include<cstdio>\n#include<cmath>\nusing namespace std;\n\nint main(void){\n\n\tdouble a, l, x;\n\twhile (cin >> a){\n\t\tcin >> l >> x;\n\t\tdouble s = (2 * l + a)/2;\n\t\tdouble A = sqrt(s*(s - a)*(s - l)*(s - l));\n\t\ts = (2 * l + x) / 2;\n\t\tdouble B = sqrt(s*(...