id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
p03016 AtCoder Beginner Contest 129 - Takahashi's Basics in Education and Learning
There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}. The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 woul...
[ { "input": { "stdin": "4 8 1 1000000" }, "output": { "stdout": "891011" } }, { "input": { "stdin": "107 10000000000007 1000000000000007 998244353" }, "output": { "stdout": "39122908" } }, { "input": { "stdin": "5 3 4 10007" }, "output...
{ "tags": [], "title": "p03016 AtCoder Beginner Contest 129 - Takahashi's Basics in Education and Learning" }
{ "cpp": "/*\n[abc129] F - Takahashi's Basics in Education and Learning\n*/\n\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, int> pli;\ntypedef pair<ll, ll> pll;\n\nll L, A, B;\nint MOD;\n\nclass gf {\npublic:\n int n;\n gf() : n(0) {}\n...
p03156 AISing Programming Contest 2019 - Contests
You have written N problems to hold programming contests. The i-th problem will have a score of P_i points if used in a contest. With these problems, you would like to hold as many contests as possible under the following condition: * A contest has three problems. The first problem has a score not greater than A poin...
[ { "input": { "stdin": "3\n5 6\n5 6 10" }, "output": { "stdout": "1" } }, { "input": { "stdin": "7\n5 15\n1 10 16 2 7 20 12" }, "output": { "stdout": "2" } }, { "input": { "stdin": "8\n3 8\n5 5 5 10 10 10 15 20" }, "output": { "s...
{ "tags": [], "title": "p03156 AISing Programming Contest 2019 - Contests" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int N,a,b;\n cin>>N>>a>>b;\n int c=0;\n int d=0;\n int e=0;\n for(int i=0;i<N;i++){\n int p;\n cin>>p;\n if(p<=a){\n c++;\n }\n else if(p>a&&p<=b){\n d++;\n }\n else{\n e++;\n }\n }\n cout<<min({c,d,e...
p03299 AtCoder Grand Contest 026 - Histogram Coloring
Let us consider a grid of squares with 10^9 rows and N columns. Let (i, j) be the square at the i-th column (1 \leq i \leq N) from the left and j-th row (1 \leq j \leq 10^9) from the bottom. Snuke has cut out some part of the grid so that, for each i = 1, 2, ..., N, the bottom-most h_i squares are remaining in the i-t...
[ { "input": { "stdin": "9\n2 3 5 4 1 2 4 2 1" }, "output": { "stdout": "12800" } }, { "input": { "stdin": "2\n2 2" }, "output": { "stdout": "6" } }, { "input": { "stdin": "9\n27 18 28 18 28 45 90 45 23" }, "output": { "stdout": "...
{ "tags": [], "title": "p03299 AtCoder Grand Contest 026 - Histogram Coloring" }
{ "cpp": "#include <cstdio>\n#include <iostream>\n#include <algorithm>\n#include <cstring>\n#define ll long long\nusing namespace std;\ntemplate <class T>\ninline void rd(T &x) {\n\tx=0; char c=getchar(); int f=1;\n\twhile(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }\n\twhile(isdigit(c)) x=x*10-'0'+c,c=getchar(); x...
p03457 AtCoder Beginner Contest 086 - Traveling
AtCoDeer the deer is going on a trip in a two-dimensional plane. In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i. If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1...
[ { "input": { "stdin": "2\n3 1 2\n6 1 1" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "2\n5 1 1\n100 1 1" }, "output": { "stdout": "No" } }, { "input": { "stdin": "1\n2 100 100" }, "output": { "stdout": "No" } ...
{ "tags": [], "title": "p03457 AtCoder Beginner Contest 086 - Traveling" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint n,t[1000000],x[1000000],y[1000000];\nint main(){\n\tint i,j;\n\tcin>>n;\n\tfor(i=1;i<=n;i++){\n\t\tcin>>t[i]>>x[i]>>y[i];\n\t}\n\tfor(i=1;i<=n;i++){\n\t\tint dt=t[i]-t[i-1];\n\t\tdt-=abs(x[i]-x[i-1]);\n\t\tdt-=abs(y[i]-y[i-1]);\n\t\tif(dt<0||dt%2){\n\t\t\tc...
p03618 AtCoder Grand Contest 019 - Reverse and Compare
You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters. You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j. You can perform this operation at most once. How many different strings can you obtain? Constraints * 1 \leq |A| \leq 200...
[ { "input": { "stdin": "xxxxxxxxxx" }, "output": { "stdout": "1" } }, { "input": { "stdin": "aatt" }, "output": { "stdout": "5" } }, { "input": { "stdin": "abracadabra" }, "output": { "stdout": "44" } }, { "input": { ...
{ "tags": [], "title": "p03618 AtCoder Grand Contest 019 - Reverse and Compare" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 2e5 + 7;\n\nchar s[N];\nint num[100];\n\nint main(){\n\tscanf(\"%s\", s);\n\tint n = strlen(s);\n\tlong long ans = 1;\n\tfor (int i = 0; i < n; i++){\n\t\tans += i - num[s[i] - 'a'];\n\t\tnum[s[i] - 'a']++;\n\t}\n\tprintf(\"%lld\", ans);\n\tretur...
p03776 AtCoder Beginner Contest 057 - Maximum Average Sets
You are given N items. The value of the i-th item (1 \leq i \leq N) is v_i. Your have to select at least A and at most B of these items. Under this condition, find the maximum possible arithmetic mean of the values of selected items. Additionally, find the number of ways to select items so that the mean of the values o...
[ { "input": { "stdin": "4 2 3\n10 20 10 10" }, "output": { "stdout": "15.000000\n3" } }, { "input": { "stdin": "5 1 5\n1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996" }, "output": { "stdout": "1000000000000000.000000\n1" } ...
{ "tags": [], "title": "p03776 AtCoder Beginner Contest 057 - Maximum Average Sets" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nlong long comb(long long x,long long y){\n long long z = 1;\n for(long long i=0;i<y;i++) z = z*(x-i)/(i+1);\n return z;\n}\nint main(){\n cout << fixed << setprecision(10);\n long long n,a,b,i,ans = 0,al,c = 0;cin >> n >> a >> b;\n long long v[n];\n for(...
p03945 AtCoder Beginner Contest 047 - 1D Reversi
Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones b...
[ { "input": { "stdin": "WWWWWW" }, "output": { "stdout": "0" } }, { "input": { "stdin": "WBWBWBWBWB" }, "output": { "stdout": "9" } }, { "input": { "stdin": "BBBWW" }, "output": { "stdout": "1" } }, { "input": { ...
{ "tags": [], "title": "p03945 AtCoder Beginner Contest 047 - 1D Reversi" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nstring s;\nint ans;\n\nint main(){\n\tcin>>s;\n\tint len=s.size();\n\tfor(int i=1;i<len;i++)\n\t\tif(s[i]!=s[i-1])\n\t\t\tans++;\n\tcout<<ans<<endl;\n\treturn 0;\n}", "java": "import java.util.Scanner;\n\npublic class Main {\n\t\n\tpublic static void main(Str...
AIZU/p00037
# Path on a Grid Seen from above, there is a grid-like square shaped like Figure 1. The presence or absence of "walls" on each side of this grid is represented by a sequence of 0s and 1s. Create a program that stands at point A, puts your right hand on the wall, keeps walking in the direction of the arrow, and outputs...
[ { "input": { "stdin": "1111\n00001\n0110\n01011\n0010\n01111\n0010\n01001\n0111" }, "output": { "stdout": "RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL" } }, { "input": { "stdin": "1111\n00110\n0011\n00111\n1111\n01011\n1110\n00110\n1101" }, "output": { "stdout": "R...
{ "tags": [], "title": "Path on a Grid" }
{ "cpp": "#include<iostream>\n#include<cassert>\n#define REP(i,s,n) for(int i=s;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n#define inf (1<<29)\n#define MAX 100 \nusing namespace std;\n\nstruct You\n{//??????????????????????????§?¨????dx[(dir+1)%4],dy[(dir+1)%4]??????????????´???\n int x,y,dir;\n You(int x=-inf,int y=-i...
AIZU/p00169
# Blackjack Blackjack is a type of card game played in casinos, where the game is played using cards with numbers from 1 to 13. The score of each card is decided as follows. * 1 is 1 point or 11 points * From 2 to 9, the score is as written. * 10 points from 10 to 13 There are several participants in this game, in...
[ { "input": { "stdin": "1\n7 7 7\n7 7 8\n12 1\n10 1 1\n0" }, "output": { "stdout": "11\n21\n0\n21\n12" } }, { "input": { "stdin": "1\n7 7 7\n7 10 8\n2 2\n10 3 2\n0" }, "output": { "stdout": "11\n21\n0\n4\n15\n" } }, { "input": { "stdin": "1\n7...
{ "tags": [], "title": "Blackjack" }
{ "cpp": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\n#include <string>\n#include <cmath>\n#include <stack>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <list>\n#include <iostream>\n#include <sstream>\n#include <climits>\n#include <cfloat>\n#include <complex>\n\nusin...
AIZU/p00325
# Halting Problem Have you ever had an infinite loop when you ran a hard-working program? It would be convenient to be able to determine in advance whether a program will stop executing without having to execute it. Unfortunately, it is not possible to make such a decision for any program in the programming language ...
[ { "input": { "stdin": "3\n111 SET c 1\n12 SUB c c 2\n777 SET a 4" }, "output": { "stdout": "a=0\nc=1" } }, { "input": { "stdin": "3\n10 SET c 1\n120 IF c 10\n20 HALT" }, "output": { "stdout": "inf" } }, { "input": { "stdin": "6\n10 SET c 1\n2...
{ "tags": [], "title": "Halting Problem" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long int64;\n\nmap< string, int > ff;\n\nvoid stop()\n{\n int ret = 0;\n for(auto &c : ff) {\n if(isalpha(c.first[0])) {\n ++ret;\n printf(\"%c=%d\\n\", c.first[0], c.second);\n }\n }\n exit(0);\n}\n\n\nint main()\n{\n int...
AIZU/p00497
# Nails JOI is playing with a nail in the board. As shown in the figure below, JOI stabbed nails in the shape of an equilateral triangle with N sides. A nails are lined up in the ath line (1 ≤ a ≤ N) from the top. The bth nail (1 ≤ b ≤ a) from the left is represented by (a, b). <image> Figure 1: Arrangement of nails ...
[ { "input": { "stdin": "None" }, "output": { "stdout": "None" } }, { "input": { "stdin": "5 2\n2 2 1\n2 1 3" }, "output": { "stdout": "12" } }, { "input": { "stdin": "Nnar" }, "output": { "stdout": "0\n" } }, { "input...
{ "tags": [], "title": "Nails" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define REP(i, n) FOR(i, 0, n)\n#define FOR(i, a, b) for(int i=a; i<b; i++)\n\n#define N 5123\nint n, m;\nint l[N][N], r[N][N];\n\nint main(){\n\tscanf(\"%d%d\", &n, &m);\n\tREP(i, N) REP(j, N) l[i][j]=r[i][j]=0;\n\tREP(i, m){\n\t\tint a, b, x;\n\t\tscanf(\"%d...
AIZU/p00682
# Area of Polygons Polygons are the most fundamental objects in geometric processing. Complex figures are often represented and handled as polygons with many short sides. If you are interested in the processing of geometric data, you'd better try some programming exercises about basic operations on polygons. Your job...
[ { "input": { "stdin": "3\n1 1\n3 4\n6 0\n\n7\n0 0\n10 10\n0 20\n10 30\n0 40\n100 40\n100 0\n\n0" }, "output": { "stdout": "1 8.5\n2 3800.0" } }, { "input": { "stdin": "3\n1 1\n3 4\n6 0\n\n7\n0 0\n10 10\n0 20\n10 33\n0 40\n100 40\n100 0\n\n0" }, "output": { "st...
{ "tags": [], "title": "Area of Polygons" }
{ "cpp": "#include<bitset>\n#include<cstdio>\n#include<cstring>\n#include<cstdlib>\n#include<iostream>\n#include<algorithm>\nusing namespace std;\n\n/* AOJ 1100 */\n\nconst int N = 55;\n\nint n, cnt = 0;\ndouble ans;\nstruct Point {\n\tint x, y;\n\tvoid read(void) {\n\t\tscanf(\"%d%d\", &x, &y);\n\t}\n\tfriend int op...
AIZU/p00824
# Gap Let’s play a card game called Gap. You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card. First, you shuffle the cards and lay them face up on the table in four rows of seven cards, le...
[ { "input": { "stdin": "4\n\n12 13 14 15 16 17 21\n22 23 24 25 26 27 31\n32 33 34 35 36 37 41\n42 43 44 45 46 47 11\n\n26 31 13 44 21 24 42\n17 45 23 25 41 36 11\n46 34 14 12 37 32 47\n16 43 27 35 22 33 15\n\n17 12 16 13 15 14 11\n27 22 26 23 25 24 21\n37 32 36 33 35 34 31\n47 42 46 43 45 44 41\n\n27 14 22...
{ "tags": [], "title": "Gap" }
{ "cpp": "#include <cstdio>\n#include <vector>\n#include <set>\n#include <queue>\nusing namespace std;\n\n\nint main(){\n\tvector<char> goal(32);\n\tfor(int i = 0; i < 4; ++i){\n\t\tfor(int j = 0; j < 7; ++j){\n\t\t\tgoal[i * 8 + j] = (i + 1) * 10 + j + 1;\n\t\t}\n\t}\n\n\tint n;\n\tfor( scanf(\"%d\", &n); n--; ){\n\...
AIZU/p00955
# Cover the Polygon with Your Disk Example Input 4 4 0 0 6 0 6 6 0 6 Output 35.759506
[ { "input": { "stdin": "4 4\n0 0\n6 0\n6 6\n0 6" }, "output": { "stdout": "35.759506" } }, { "input": { "stdin": "4 2\n1 -2\n4 -4\n2 6\n-2 16" }, "output": { "stdout": "9.3054881039\n" } }, { "input": { "stdin": "4 4\n1 1\n6 -1\n6 2\n0 6" ...
{ "tags": [], "title": "Cover the Polygon with Your Disk" }
{ "cpp": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\nstruct Point{\n\tPoint(double arg_x,double arg_y){\n\t\tx = arg_x;\n\t\ty = ...
AIZU/p01088
# 500-yen Saving 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. So...
[ { "input": { "stdin": "4\n800\n700\n1600\n600\n4\n300\n700\n1600\n600\n4\n300\n700\n1600\n650\n3\n1000\n2000\n500\n3\n250\n250\n1000\n4\n1251\n667\n876\n299\n0" }, "output": { "stdout": "2 2900\n3 2500\n3 3250\n1 500\n3 1500\n3 2217" } }, { "input": { "stdin": "4\n800\n772\...
{ "tags": [], "title": "500-yen Saving" }
{ "cpp": "// Ryo Kamoi\n// #define DEBUG\n\n#include<iostream>\n#include<math.h>\n#include<vector>\n#include<string>\n#include<algorithm>\n#include<map>\n#include<queue>\n\nusing namespace std;\n\n#define REP(i, n) for(int i=0; i<n; i++)\n\ntypedef pair<int, int> pii;\ntypedef long long ll;\ntypedef pair<ll, ll> pll;...
AIZU/p01224
# Perfect Number Let S be the sum of divisors of an integer N excluding the number itself. When N = S, N is called a perfect number, when N> S, N is called a defendant number, and when N <S, N is called an abundant number. Create a program that determines whether a given integer is a perfect number, a missing number, ...
[ { "input": { "stdin": "1\n2\n3\n4\n6\n12\n16\n28\n33550336\n99999998\n99999999\n100000000\n0" }, "output": { "stdout": "deficient number\ndeficient number\ndeficient number\ndeficient number\nperfect number\nabundant number\ndeficient number\nperfect number\nperfect number\ndeficient number\...
{ "tags": [], "title": "Perfect Number" }
{ "cpp": "#include <iostream>\nusing namespace std;\n\nint main(void){\n\n int N,S;\n int temp;\n int sum;\n\n while(1){\n\n int temp = 0;\n\n cin >> N;\n\n if( N == 0 ) break;\n\n sum = 0;\n\n for( int j = 1 ; j*j <= N; j++ ){\n\t\n\tif( N % j == 0 ){ \n\t sum += j;\n\t if( j*j != N ) sum +=...
AIZU/p01358
# Usaneko Matrix Rabbits and cats are competing. The rules are as follows. First, each of the two animals wrote n2 integers on a piece of paper in a square with n rows and n columns, and drew one card at a time. Shuffle two cards and draw them one by one alternately. Each time a card is drawn, the two will mark it if...
[ { "input": { "stdin": "3 2 2 10\n1 2 3\n4 5 6\n7 8 9\n1 2 3\n6 5 4\n7 8 9\n11\n4\n7\n5\n10\n9\n2\n1\n3\n8" }, "output": { "stdout": "USAGI" } }, { "input": { "stdin": "3 2 1 10\n1 2 3\n4 5 6\n7 8 9\n1 2 3\n6 5 4\n7 8 9\n11\n4\n7\n5\n10\n9\n2\n1\n3\n8" }, "output": {...
{ "tags": [], "title": "Usaneko Matrix" }
{ "cpp": "#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<list>\n#include<queue>\n#include<deque>\n#include<algorithm>\n#include<numeric>\n#include<utility>\n#include<complex>\n#include<functional>\...
AIZU/p01540
# Treasure Hunt Taro came to a square to look for treasure. There are many treasures buried in this square, but Taro has the latest machines, so he knows everything about where the treasures are buried. Since the square is very wide Taro decided to look for the treasure to decide the area, but the treasure is what tre...
[ { "input": { "stdin": "5 5\n10 5\n-3 -8\n2 11\n6 0\n-1 3\n-3 1 3 13\n-1 -1 9 5\n-3 -8 10 11\n0 0 5 5\n-10 -9 15 10" }, "output": { "stdout": "2\n2\n5\n0\n4" } }, { "input": { "stdin": "3 1\n1 1\n2 4\n5 3\n0 0 5 5" }, "output": { "stdout": "3" } }, { ...
{ "tags": [], "title": "Treasure Hunt" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n#define all(vec) vec.begin(),vec.end()\nint main(){\n int n,m;cin >> n >> m;\n\n vector<int> index_of_x(n),index_of_y(n);\n for(int i=0;i<n;i++){\n cin >> index_of_x[i] >> index_of_y[i];\n }\n vecto...
AIZU/p01696
# Broken Cipher Generator Broken crypto generator JAG (Japanese Alumni Group) is a mysterious organization composed of many programmers, and in order to enter the building where the headquarters of this organization is located, it is necessary to solve the ciphertext generated by a certain machine every time. This ci...
[ { "input": { "stdin": "A+A++A\nZ-Z--Z+-Z\n[ESREVER]\nJ---?---J\n++++++++A+++Z-----------A+++Z\n[[++-+--?[--++-?++-+++L]][-+-----+-O]]++++---+L\n." }, "output": { "stdout": "ABC\nZYXZ\nREVERSE\nJAG\nICPC\nJAPAN" } }, { "input": { "stdin": "A++A+A\nZ--Z-Z+-Z\n[FTQWEER]\nJ---?...
{ "tags": [], "title": "Broken Cipher Generator" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ninline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}\ntemplate<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}\ntemplate<class T> inline T sqr(T x) {return x*x;}\n\ntypedef vector<int> vi;\ntypedef ...
AIZU/p01840
# Delivery to a Luxurious House B-Mansion and courier Problem Statement Taro lives alone in a mansion. Taro, who loves studying, intends to study in his study in the house today. Taro can't concentrate outside the study, so he always studies in the study. However, on this day, $ N $ of courier service to Taro arriv...
[ { "input": { "stdin": "1 1 5\n3" }, "output": { "stdout": "3" } }, { "input": { "stdin": "1 2 10\n6" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1 4 192\n8" }, "output": { "stdout": "184\n" } }, { "inp...
{ "tags": [], "title": "Delivery to a Luxurious House" }
{ "cpp": "#include \"bits/stdc++.h\"\nusing namespace std;\n\nint main() {\n\tint N, M, T;\n\tcin >> N >> M >> T;\n\tvector<int> A(N);\n\tfor (int i = 0; i < N; i++) cin >> A[i];\n\tint ANS = 0;\n\tANS += A[0] - M;\n\tANS += max(0, T - M - A[N - 1]);\n\tfor (int i = 0; i < N - 1; i++) {\n\t\tANS += max(0, A[i + 1] - ...
AIZU/p01976
# Anagram problem Given a sequence $ a_i $ of length $ N $. Output all integers $ K (1 \ le K \ le N) $ that satisfy the following conditions. Condition: Well sorted $ a_1, \ cdots, a_K $ matches $ a_ {N-K + 1}, \ cdots, a_N $. Example Input 8 5 2 4 9 4 9 2 5 Output 1 2 4 6 7 8
[ { "input": { "stdin": "8\n5 2 4 9 4 9 2 5" }, "output": { "stdout": "1 2 4 6 7 8" } }, { "input": { "stdin": "8\n-13 1 2 32 0 2 -1 0" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "8\n-10 -1 3 42 1 4 -2 -1" }, "output": { ...
{ "tags": [], "title": "Anagram" }
{ "cpp": "// 基本テンプレート\n \n#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include ...
AIZU/p02122
# RMQ 2 Problem Given two sequences of length $ N $, $ A $ and $ B $. First, the $ i $ item in the sequence $ A $ is $ a_i $, and the $ i $ item in the sequence $ B $ is $ b_i $. Since a total of $ Q $ of statements of the following format are given, create a program that processes in the given order. Each statemen...
[ { "input": { "stdin": "5\n1 3 5 7 9\n6 2 3 2 6\n10\n1 3 4\n3 4 5\n4 2 3\n5 -1 -1\n2 3 8\n3 2 5\n4 3 3\n1 1 1\n6 -1 -1\n3 1 5" }, "output": { "stdout": "7\n2\n2\n8\n1" } }, { "input": { "stdin": "5\n1 3 5 14 9\n7 2 0 2 6\n4\n1 3 6\n3 4 5\n3 2 3\n5 -1 -1\n2 6 8\n3 0 5\n7 3 3\...
{ "tags": [], "title": "RMQ 2" }
{ "cpp": "#include<bits/stdc++.h>\n#define rep(i,a,b) for(int i=a;i<b;i++)\n#define rrep(i,a,b) for(int i=a;i>=b;i--)\n#define fore(i,a) for(auto &i:a)\n#pragma GCC optimize (\"-O3\")\nusing namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }\n//------------------------------...
AIZU/p02262
# Shell Sort Shell Sort Shell Sort is a generalization of Insertion Sort to arrange a list of $n$ elements $A$. 1 insertionSort(A, n, g) 2 for i = g to n-1 3 v = A[i] 4 j = i - g 5 while j >= 0 && A[j] > v 6 A[j+g] = A[j] 7 j = j - g 8 cnt++ 9 ...
[ { "input": { "stdin": "5\n5\n1\n4\n3\n2" }, "output": { "stdout": "2\n4 1\n3\n1\n2\n3\n4\n5" } }, { "input": { "stdin": "3\n3\n2\n1" }, "output": { "stdout": "1\n1\n3\n1\n2\n3" } }, { "input": { "stdin": "5\n6\n1\n0\n7\n2" }, "output"...
{ "tags": [], "title": "Shell Sort" }
{ "cpp": "#include<iostream>\nusing namespace std;\n\nint n, A[1001000];\nint G[110];\n\nint insertionSort(int g) {\n int cnt = 0;\n for(int i=g;i<n;i++){\n int v = A[i];\n int j = i-g;\n while(j>=0&&A[j]>v){\n A[j+g]=A[j];\n j -= g;\n cnt++;\n }\n ...
AIZU/p02410
# Matrix Vector Multiplication Write a program which reads a $ n \times m$ matrix $A$ and a $m \times 1$ vector $b$, and prints their product $Ab$. A column vector with m elements is represented by the following equation. \\[ b = \left( \begin{array}{c} b_1 \\\ b_2 \\\ : \\\ b_m \\\ \end{array} \right) \\] A $n \ti...
[ { "input": { "stdin": "3 4\n1 2 0 1\n0 3 0 1\n4 1 1 0\n1\n2\n3\n0" }, "output": { "stdout": "5\n6\n9" } }, { "input": { "stdin": "3 4\n2 4 -2 1\n0 3 0 1\n4 1 1 0\n1\n2\n3\n0" }, "output": { "stdout": "4\n6\n9\n" } }, { "input": { "stdin": "3 ...
{ "tags": [], "title": "Matrix Vector Multiplication" }
{ "cpp": "#include<iostream>\nusing namespace std;\n\nint main()\n{\n\tint m, n, a[100][100], b[100];\n\tcin >> n >> m;\n\tfor (int i = 0;i < n;i++)\n\t{\n\t\tfor (int j = 0;j < m;j++)\n\t\t{\n\t\t\tcin >> a[i][j];\n\t\t}\n\t}\n\tfor (int i = 0;i < m;i++)\n\t{\n\t\tcin >> b[i];\n\t}\n\tfor (int i = 0;i < n;i++)\n\t{\...
CodeChef/arithm
Chef's encounters with sweets continue with this problem! This time, he wants to distribute chocolates to his N students sitting on a long bench. The students are ordered according to the scores they got from the last exam. Chef wants to give more chocolates to the higher-scoring students. He also has a few more restr...
[ { "input": { "stdin": "2\n4 24\n2 2" }, "output": { "stdout": "Yes\nNo" } } ]
{ "tags": [], "title": "arithm" }
{ "cpp": null, "java": null, "python": "t=input()\nwhile t!=0:\n n,c=raw_input().split(' ')\n n=int(n)\n c=int(c)\n s=0\n flag=0\n d=1\n first=0\n last=0\n while d<=2:\n first=(2*c-n*(n-1)*d)/(2*n)\n if first<1:\n flag=0\n break\n \n las...
CodeChef/clco03
Arush was not always poor at Mathematics but his recent performances had not been that good and he had lost his confidence. Now his elder brother was determined to bring back his confidence back in Mathematics. So he made a tricky question and made sure that Arush would be able to do solve it. The factorial of a non-ne...
[ { "input": { "stdin": "2\n1\n6\n3\n006" }, "output": { "stdout": "53\n53" } } ]
{ "tags": [], "title": "clco03" }
{ "cpp": null, "java": null, "python": "t=input()\nfor x in range(t):\n\tl=input()\n\tr=raw_input()\n\ts=''\n\tfor i in range(l):\n#\t\tif i==0 or i==1:\n#\t\t\tcontinue\n\t\tif r[i]=='2' or r[i]=='3' or r[i]=='5' or r[i]=='7':\n\t\t\ts=s+r[i]\n\t\telif r[i]=='4':\n\t\t\ts=s+'322'\n\t\telif r[i]=='6':\n\t\t\ts+='...
CodeChef/fctrl2
A tutorial for this problem is now available on our blog. Click here to read it. You are asked to calculate factorials of some small positive integers. Input An integer t, 1 ≤ t ≤ 100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1 ≤ n ≤ 100. Output For each integer n ...
[ { "input": { "stdin": "4\n1\n2\n5\n3" }, "output": { "stdout": "1\n2\n120\n6\n" } }, { "input": { "stdin": "4\n2\n2\n6\n4" }, "output": { "stdout": "2\n2\n720\n24\n" } }, { "input": { "stdin": "4\n1\n2\n5\n6" }, "output": { "std...
{ "tags": [], "title": "fctrl2" }
{ "cpp": null, "java": null, "python": "t=input()\nfor j in range(0,t):\n n=input()\n x=1\n for i in range(0,n):\n x=x*n\n n=n-1\n print x" }
CodeChef/lebamboo
Problem Statement Little Elephant from Zoo of Lviv likes bamboo very much. He currently has n stems of bamboo, Hi - height of i-th stem of bamboo (0-based numeration). Today inspector Andrii from World Bamboo Association is visiting the plantation. He doesn't like current situation. He wants the height of i-th stem t...
[ { "input": { "stdin": "3\n1\n1\n2\n2\n1 2\n2 1\n3\n3 2 2\n4 5 3" }, "output": { "stdout": "-1\n1\n5" } }, { "input": { "stdin": "3\n1\n2\n2\n2\n0 2\n-2 1\n1\n3 2 2\n5 3 3" }, "output": { "stdout": "0\n-1\n-1\n" } }, { "input": { "stdin": "3\n...
{ "tags": [], "title": "lebamboo" }
{ "cpp": null, "java": null, "python": "for t in xrange(int(raw_input())):\n n = int(raw_input())\n h = map(int, raw_input().split())\n d = map(int, raw_input().split())\n if (n != 2):\n if ((sum(d) - sum(h)) % abs(n-2) != 0):\n print -1\n else:\n s = (sum(d) - sum(...
CodeChef/plgrm
For Turbo C++ Users : Read the following document before attempting the question : Problem Description N-Boy is very eccentric when it comes to strings. These days, he spends most of his time studying palindromes and pangrams (for his IP assignment at IIITD). For those of you who don’t know, a palindrome is a word ...
[ { "input": { "stdin": "3\nabba\nabcdefghijklmnopqrstuvwxyz\nqwerty" }, "output": { "stdout": "palindrome\npangram\nnone\n" } }, { "input": { "stdin": "3\naaba\ngbcdeaghiyklmnopqrsttvxxjz\nztrewq" }, "output": { "stdout": "none\nnone\nnone\n" } }, { ...
{ "tags": [], "title": "plgrm" }
{ "cpp": null, "java": null, "python": "test = int(raw_input())\nwhile test>0 :\n\ts = raw_input()\n\tp = 1\n\tg = 1\n\tss = s[::-1]\n\tif s != ss :\n\t\tp = 0\n\tli = 'abcdefghijklmnopqrstuvwxyz'\n\tfor i in li:\n\t\tif i not in s:\n\t\t\tg = 0\n\t\t\tbreak\n\tif p == 1 and g==1:\n\t\tprint('palingram')\n\telif ...
CodeChef/sub_perm
A factory called 'IP Labs Pvt. Ltd.' has produced some material, which is in the form of blocks. Each block is labeled by an alphanumeric character. You've been recently hired as Packaging Manager, and your job is to rearrange the huge lot of manufactured blocks and then extract out the useful blocks from it which can ...
[ { "input": { "stdin": "4\nareyouafoobarmember\nbarfoo\nfofoforabrabbbbbbfoooooooaaa\nfoobar\nthisisiplab\nsi\nababa\naa" }, "output": { "stdout": "1\n2\n2\n1" } }, { "input": { "stdin": "4\naeeyouagooabrlsmber\nnogsab\ngofoebrbaraobbbbbfooooonoaab\noobf`r\nbamphsisiht\nit\n...
{ "tags": [], "title": "sub_perm" }
{ "cpp": null, "java": null, "python": "import sys \nt=int(input())\nwhile t>0:\n\tA=list(raw_input())\n\tB=list(raw_input())\n\tC=list(set(B))\n\tD=[]\n\tF=[]\n\tArr=[]\n\tfor i in xrange(len(C)):\n\t\tx=(A.count(C[i]))/(B.count(C[i]))\n\t\tArr.append(x)\n\tprint min(Arr)\n\tt=t-1" }
Codeforces/1009/G
# Allowed Letters Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup! Actually, Polycarp has already came up with the name bu...
[ { "input": { "stdin": "abacaba\n0\n" }, "output": { "stdout": "aaaabbc" } }, { "input": { "stdin": "bedefead\n5\n2 e\n1 dc\n5 b\n7 ef\n6 ef\n" }, "output": { "stdout": "deadbeef" } }, { "input": { "stdin": "fc\n2\n1 cfab\n2 f\n" }, "o...
{ "tags": [ "bitmasks", "flows", "graph matchings", "graphs", "greedy" ], "title": "Allowed Letters" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 100010;\ntemplate <typename Tp>\nint getmin(Tp &x, Tp y) {\n return y < x ? x = y, 1 : 0;\n}\ntemplate <typename Tp>\nint getmax(Tp &x, Tp y) {\n return y > x ? x = y, 1 : 0;\n}\ntemplate <typename Tp>\nvoid read(Tp &x) {\n x = 0;\n char c...
Codeforces/1032/D
# Barcelonian Distance In this problem we consider a very simplified model of Barcelona city. Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue ...
[ { "input": { "stdin": "3 1 -9\n0 3 3 -1\n" }, "output": { "stdout": "6.16227766016838\n" } }, { "input": { "stdin": "1 1 -3\n0 3 3 0\n" }, "output": { "stdout": "4.242640687119285\n" } }, { "input": { "stdin": "1 -1 0\n-1 0 2 1\n" }, ...
{ "tags": [ "geometry", "implementation" ], "title": "Barcelonian Distance" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong double d[6][6];\nvector<pair<long double, long double>> v;\nint main() {\n cout.precision(20);\n long long i, j, k, a, b, c;\n long double x[2], y[2];\n cin >> a >> b >> c >> x[0] >> y[0] >> x[1] >> y[1];\n for (i = 0; i < 2; i++) v.push_back({x[i], ...
Codeforces/1055/B
# Alice and Hairdresser Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic... To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most l centimeters after haircut, where l is her favorite number. Sup...
[ { "input": { "stdin": "4 7 3\n1 2 3 4\n0\n1 2 3\n0\n1 1 3\n0\n1 3 1\n0\n" }, "output": { "stdout": "1\n2\n2\n1\n" } }, { "input": { "stdin": "10 24 2\n1 1 1 1 1 1 1 1 1 1\n0\n1 1 1\n0\n1 1 1\n0\n1 3 2\n1 5 2\n1 7 2\n1 9 2\n0\n1 10 1\n0\n1 10 1\n0\n1 10 1\n0\n1 1 1\n0\n1 2 2...
{ "tags": [ "dsu", "implementation" ], "title": "Alice and Hairdresser" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct fastIO {\n fastIO() {}\n};\nvoid no() {\n cout << \"NO\";\n exit(0);\n}\nvoid yes() {\n cout << \"YES\";\n exit(0);\n}\nconst int MAXN = 1e6 * 2;\nlong long n, x, a[MAXN], b[MAXN], m, l, ans, y, z;\nint main() {\n if (0) cerr << fixed << setprecis...
Codeforces/1077/C
# Good Array Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3. You are given an array a consisting of n integers. Your task is to print all indic...
[ { "input": { "stdin": "5\n2 5 1 2 2\n" }, "output": { "stdout": "3\n1 4 5\n" } }, { "input": { "stdin": "4\n8 3 5 2\n" }, "output": { "stdout": "2\n1 4\n" } }, { "input": { "stdin": "5\n2 1 2 4 3\n" }, "output": { "stdout": "0\n...
{ "tags": [], "title": "Good Array" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n long long sum = 0, sum1, a;\n vector<long long> v(n);\n vector<int> ans;\n map<long long, set<int> > mp;\n for (int i = 0; i < n; i++) {\n cin >> a;\n v[i] = a;\n sum += a;\n mp[a].insert(i);\n }\n for (...
Codeforces/1098/B
# Nice table You are given an n × m table, consisting of characters «A», «G», «C», «T». Let's call a table nice, if every 2 × 2 square contains all four distinct characters. Your task is to find a nice table (also consisting of «A», «G», «C», «T»), that differs from the given table in the minimum number of characters....
[ { "input": { "stdin": "2 2\nAG\nCT\n" }, "output": { "stdout": "AG\nCT\n" } }, { "input": { "stdin": "3 5\nAGCAG\nAGCAG\nAGCAG\n" }, "output": { "stdout": "AGCTC\nCTAGA\nAGCTC\n" } }, { "input": { "stdin": "2 2\nTG\nAC\n" }, "output":...
{ "tags": [ "brute force", "constructive algorithms", "greedy", "math" ], "title": "Nice table" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};\nint dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};\nconst int SIZE = 100050;\nint bitcount32(int dw32) {\n dw32 = ((dw32 & 0xAAAAAAAA) >> 1) + (dw32 & 0x55555555);\n dw32 = ((dw32 & 0xCCCCCCCC) >> 2) + (dw32 & 0x33333333);\n ...
Codeforces/1119/C
# Ramesses and Corner Inversion Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task. You are given two matrices A and B of size n × m, each of which consists of 0 and 1 only. You can apply the following operation to the matrix A arbitrar...
[ { "input": { "stdin": "3 4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 1 1 1\n1 1 1 1\n1 1 1 1\n" }, "output": { "stdout": "No\n" } }, { "input": { "stdin": "6 7\n0 0 1 1 0 0 1\n0 1 0 0 1 0 1\n0 0 0 1 0 0 1\n1 0 1 0 1 0 0\n0 1 0 0 1 0 1\n0 1 0 1 0 0 1\n1 1 0 1 0 1 1\n0 1 1 0 1 0 0\n1 1 0...
{ "tags": [ "constructive algorithms", "greedy", "implementation", "math" ], "title": "Ramesses and Corner Inversion" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m;\n cin >> n >> m;\n int a[n + 10][m + 10];\n int b[n + 10][m + 10];\n int c[n + 10][m + 10];\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++) cin >> a[i][j];\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m;...
Codeforces/1145/G
# AI Takeover The recent advances in AI research has brought humanity to the point when the AIs finally attempt a takeover. Their weapon of choice? The [most intellectually challenging game in the world](//codeforces.com/contest/409/problem/A), rock-paper-scissors! The future of humanity looks bleak, given the existe...
[ { "input": { "stdin": "5\n" }, "output": { "stdout": "R\nR\nR\nR\nP\nS\nS\nP\nR\nR\n\u0000\n\u0000\n\u0000\n\u0000\n\u0000\n\u0000\n\u0000\n\u0000\n\u0000\n\u0000\n" } }, { "input": { "stdin": "6\n" }, "output": { "stdout": "R\nR\nR\nR\nP\nS\nS\nP\nR\nR\n\u000...
{ "tags": [ "interactive" ], "title": "AI Takeover" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read() {\n int x = 0, k = 1;\n char c = getchar();\n while (c < '0' || c > '9') {\n if (c == '-') k = -1;\n c = getchar();\n }\n while (c >= '0' && c <= '9') {\n x = (x << 1) + (x << 3) + (c ^ 48);\n c = getchar();\n }\n return x ...
Codeforces/1166/F
# Vicky's Delivery Service In a magical land there are n cities conveniently numbered 1, 2, ..., n. Some pairs of these cities are connected by magical colored roads. Magic is unstable, so at any time, new roads may appear between two cities. Vicky the witch has been tasked with performing deliveries between some pai...
[ { "input": { "stdin": "4 3 2 4\n1 2 1\n2 3 1\n3 4 2\n? 1 4\n? 4 1\n+ 3 1 2\n? 4 1\n" }, "output": { "stdout": "Yes\nNo\nYes\n" } }, { "input": { "stdin": "2 1 1 5\n2 1 1\n? 2 1\n? 1 2\n? 1 2\n? 2 1\n? 1 2\n" }, "output": { "stdout": "Yes\nYes\nYes\nYes\nYes\n"...
{ "tags": [ "data structures", "dsu", "graphs", "hashing" ], "title": "Vicky's Delivery Service" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int nax = 1e5 + 5;\nint n, m, k, q;\nset<int> zbior[nax];\nint wjakiejskladowejjestem[nax];\nvector<int> skladowe[nax];\nmap<int, int> krawedzie[nax];\nint rozmiar[nax];\nvoid pre() {\n cin >> n >> m >> k >> q;\n for (int i = 1; i <= n; i++) {\n zbi...
Codeforces/1185/C2
# Exam in BerSU (hard version) The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine ...
[ { "input": { "stdin": "7 15\n1 2 3 4 5 6 7\n" }, "output": { "stdout": "0 0 0 0 0 2 3 \n" } }, { "input": { "stdin": "5 100\n80 40 40 40 60\n" }, "output": { "stdout": "0 1 1 2 3 \n" } }, { "input": { "stdin": "3 299\n100 100 100\n" }, ...
{ "tags": [ "brute force", "data structures", "greedy", "math" ], "title": "Exam in BerSU (hard version)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 500005;\nint qtd[110];\nint main() {\n int n, m;\n scanf(\"%d %d\", &n, &m);\n int sum = 0;\n for (int i = 0; i < n; i++) {\n if (i) printf(\" \");\n int x;\n scanf(\"%d\", &x);\n sum += x;\n int resp = 0;\n if (sum > m) {\n...
Codeforces/1204/A
# BowWow and the Timetable In the city of Saint Petersburg, a day lasts for 2^{100} minutes. From the main station of Saint Petersburg, a train departs after 1 minute, 4 minutes, 16 minutes, and so on; in other words, the train departs at time 4^k for each integer k ≥ 0. Team BowWow has arrived at the station at the t...
[ { "input": { "stdin": "100000000\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "101\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "10100\n" }, "output": { "stdout": "3\n" } }, { "input":...
{ "tags": [ "math" ], "title": "BowWow and the Timetable" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int t;\n int i, j, k;\n string s;\n cin >> s;\n for (i = 0; i < s.length(); i++) {\n if (s[i] == '1') k++;\n }\n if (k > 1)\n cout << (s.length() + 1) / 2;\n else\n cout << (s.length()) / 2;\n return 0;\n}\n", "java": "import...
Codeforces/1220/F
# Gardener Alex Gardener Alex loves to grow trees. We remind that tree is a connected acyclic graph on n vertices. Today he decided to grow a rooted binary tree. A binary tree is a tree where any vertex has no more than two sons. Luckily, Alex has a permutation of numbers from 1 to n which he was presented at his la...
[ { "input": { "stdin": "4\n1 2 3 4\n" }, "output": { "stdout": "3 2\n" } }, { "input": { "stdin": "127\n1 70 69 71 68 73 72 74 67 77 76 78 75 80 79 81 66 85 84 86 83 88 87 89 82 92 91 93 90 95 94 96 65 101 100 102 99 104 103 105 98 108 107 109 106 111 110 112 97 116 115 117 ...
{ "tags": [ "binary search", "data structures" ], "title": "Gardener Alex" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma optimize(\"TKACHENKO-GORYACHENKO\")\n#pragma GCC optimize(\"O3\")\nusing namespace std;\nconst long long maxn = 2e5 + 100;\nconst long long inf = 1e15;\nlong long n, m, k;\nlong long V[4 * maxn];\nvector<long long> A;\nvector<long long> A1;\nlong long C[maxn];\nvoid build(l...
Codeforces/1246/B
# Power Products You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k. Input The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100). The second line contains n integers a...
[ { "input": { "stdin": "6 3\n1 3 9 8 24 1\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "10 2\n7 4 10 9 2 8 8 7 3 7\n" }, "output": { "stdout": "7" } }, { "input": { "stdin": "100 3\n94 94 83 27 80 73 61 38 34 95 72 96 59 36 78 15...
{ "tags": [ "hashing", "math", "number theory" ], "title": "Power Products" }
{ "cpp": "#include <bits/stdc++.h>\nstd::vector<uint_fast64_t> get_primes_linear(uint_fast64_t limit) {\n std::vector<uint_fast64_t> lp(limit + 1);\n std::vector<uint_fast64_t> pr;\n for (uint_fast64_t i = 2; i <= limit; ++i) {\n if (lp[i] == 0) {\n lp[i] = i;\n pr.push_back(i);\n }\n for (uint_...
Codeforces/1265/F
# Beautiful Bracket Sequence (easy version) This is the easy version of this problem. The only difference is the limit of n - the length of the input string. In this version, 1 ≤ n ≤ 2000. The hard version of this challenge is not offered in the round for the second division. Let's define a correct bracket sequence ...
[ { "input": { "stdin": "(?(?))\n" }, "output": { "stdout": "9" } }, { "input": { "stdin": "??\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "???)\n" }, "output": { "stdout": "8" } }, { "input": { "s...
{ "tags": [ "combinatorics", "dp", "probabilities" ], "title": "Beautiful Bracket Sequence (easy version)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long mod = 998244353;\nlong long **num_of_substrs;\nlong long **dp;\nvoid compute_substrs(string s, int n) {\n for (int i = 0; i < n; i++) {\n long long ans = 1;\n for (int j = i; j < n; j++) {\n if (s[j] == '?') ans = (ans * 2) % mod;\n ...
Codeforces/1287/B
# Hyperset Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a se...
[ { "input": { "stdin": "3 3\nSET\nETS\nTSE\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 4\nSETE\nETSE\nTSES\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 4\nSETT\nTEST\nEEET\nESTE\nSTES\n" }, "outpu...
{ "tags": [ "brute force", "data structures", "implementation" ], "title": "Hyperset" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n int n, k;\n cin >> n >> k;\n vector<string> v(n);\n for (int i = 0; i < n; i++) {\n cin >> v[i];\n }\n unordered_map<string, int> mp;\n for (int i = 0; i < n; i++) {\n mp[v[i]] = i;\n }\n long long ans = 0;\n for (int i = 0; i ...
Codeforces/1307/B
# Cow and Friend Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically, he wants to get from (0,0) to (x,0) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Eucli...
[ { "input": { "stdin": "4\n2 4\n1 3\n3 12\n3 4 5\n1 5\n5\n2 10\n15 4\n" }, "output": { "stdout": "2\n3\n1\n2\n" } }, { "input": { "stdin": "1\n10 999999733\n25 68 91 55 36 29 96 4 63 3\n" }, "output": { "stdout": "10416664\n" } }, { "input": { ...
{ "tags": [ "geometry", "greedy", "math" ], "title": "Cow and Friend" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long t;\n cin >> t;\n while (t--) {\n long long n, x;\n cin >> n >> x;\n vector<long long> a(n);\n for (int i = (0); i < (n); i++) cin >> a[i];\n long long mx = -1;\n bool eq = false;\n for (int i = (0); i < (n); i++...
Codeforces/1330/B
# Dreamoon Likes Permutations The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation. Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2. Now Dreamoon concatenates these two permutations in...
[ { "input": { "stdin": "6\n5\n1 4 3 2 1\n6\n2 4 1 3 2 1\n4\n2 1 1 3\n4\n1 3 3 1\n12\n2 1 3 4 5 6 7 8 9 1 10 2\n3\n1 1 1\n" }, "output": { "stdout": "2\n1 4\n4 1\n1\n4 2\n0\n0\n1\n2 10\n0\n" } }, { "input": { "stdin": "6\n5\n1 4 3 2 1\n6\n2 4 1 3 3 1\n4\n2 1 1 3\n4\n1 3 3 1\n...
{ "tags": [ "implementation", "math" ], "title": "Dreamoon Likes Permutations" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool check(int a[], int start, int end) {\n int len = end - start;\n vector<bool> used(len + 1, false);\n for (int i = start; i < end; i++) {\n if (a[i] > len || used[a[i]]) return false;\n used[a[i]] = true;\n }\n}\nint main() {\n int t;\n cin >> ...
Codeforces/1350/B
# Orac and Models There are n models in the shop numbered from 1 to n, with sizes s_1, s_2, …, s_n. Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes). Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indic...
[ { "input": { "stdin": "4\n4\n5 3 4 6\n7\n1 4 2 3 6 4 9\n5\n5 4 3 2 1\n1\n9\n" }, "output": { "stdout": "2\n3\n1\n1\n" } }, { "input": { "stdin": "4\n23\n3198 4895 1585 3881 2650 2366 876 2731 1052 126 2152 3621 2169 4103 1074 2594 2897 3983 3830 3460 729 576 3281\n1\n100000...
{ "tags": [ "dp", "math", "number theory" ], "title": "Orac and Models" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nint len[1000000 + 5];\nint input[1000000 + 5];\nint ans;\nint get_len(int idx) {\n if (len[idx] != -1) return len[idx];\n len[idx] = 1;\n for (int k = 2; 1LL * k * idx <= n; ++k) {\n int cur = get_len(k * idx) + 1;\n if (input[idx] >= input[k ...
Codeforces/1370/E
# Binary Subsequence Rotation Naman has two binary strings s and t of length n (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert s into t using the following operation as few times as possible. In one operation, he can choose any subsequence of s and rotate it clockw...
[ { "input": { "stdin": "10\n1111100000\n1111100001\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "6\n010000\n000001\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "10\n1111100000\n0000011111\n" }, "output"...
{ "tags": [ "binary search", "constructive algorithms", "data structures", "greedy" ], "title": "Binary Subsequence Rotation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid fast() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n}\nvector<string> vec_splitter(string s) {\n s += ',';\n vector<string> res;\n while (!s.empty()) {\n res.push_back(s.substr(0, s.find(',')));\n s = s.substr(s.find(',') + 1);\n }\...
Codeforces/1393/A
# Rainbow Dash, Fluttershy and Chess Coloring One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash...
[ { "input": { "stdin": "2\n3\n4\n" }, "output": { "stdout": "2\n3\n" } }, { "input": { "stdin": "1\n10000001\n" }, "output": { "stdout": "5000001\n" } }, { "input": { "stdin": "1\n69\n" }, "output": { "stdout": "35\n" } }, ...
{ "tags": [ "greedy", "math" ], "title": "Rainbow Dash, Fluttershy and Chess Coloring" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int factorial(long long int n) {\n return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);\n}\nlong long int gcd(long long int a, long long int b) {\n if (a == 0) return b;\n return gcd(b % a, a);\n}\nlong long int lcm(long long int a, long long int...
Codeforces/1417/F
# Graph and Queries You are given an undirected graph consisting of n vertices and m edges. Initially there is a single integer written on every vertex: the vertex i has p_i written on it. All p_i are distinct integers from 1 to n. You have to process q queries of two types: * 1 v — among all vertices reachable fr...
[ { "input": { "stdin": "5 4 6\n1 2 5 4 3\n1 2\n2 3\n1 3\n4 5\n1 1\n2 1\n2 3\n1 1\n1 2\n1 2\n" }, "output": { "stdout": "5\n1\n2\n0\n" } }, { "input": { "stdin": "9 5 14\n7 1 4 8 5 2 6 9 3\n2 8\n1 6\n5 7\n1 3\n3 9\n1 5\n1 8\n1 5\n1 4\n1 8\n1 3\n1 5\n1 5\n1 7\n1 7\n2 3\n1 2\n1...
{ "tags": [ "data structures", "dsu", "graphs", "implementation", "trees" ], "title": "Graph and Queries" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = 1e9 + 7;\nconst int N = 5e5 + 5;\nconst int M = 7e5 + 5;\nint p[M], fa[M], id[M];\nint u[N], v[N], vis[N], op[N][2];\nvector<int> G[M];\nint fid(int x) { return fa[x] == x ? x : fa[x] = fid(fa[x]); }\nint in[M], out[M], cnt, pos[M];\nvoid dfs(i...
Codeforces/1434/D
# Roads and Ramen In the Land of Fire there are n villages and n-1 bidirectional road, and there is a path between any pair of villages by roads. There are only two types of roads: stone ones and sand ones. Since the Land of Fire is constantly renovating, every morning workers choose a single road and flip its type (s...
[ { "input": { "stdin": "5\n1 2 0\n1 3 0\n3 5 0\n3 4 0\n5\n3\n4\n1\n3\n4\n" }, "output": { "stdout": "3\n2\n3\n3\n2\n" } }, { "input": { "stdin": "5\n2 4 0\n5 2 0\n1 3 1\n1 2 1\n5\n3\n3\n4\n1\n1\n" }, "output": { "stdout": "2\n3\n2\n3\n2\n" } }, { "i...
{ "tags": [ "data structures", "trees" ], "title": "Roads and Ramen" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 5e5 + 9;\nint a[N], r[N];\nstruct ST {\n int t[4 * N][2], lazy[4 * N];\n ST() {\n memset(t, 0, sizeof t);\n memset(lazy, 0, sizeof lazy);\n }\n inline void push(int n, int b, int e) {\n if (lazy[n] == 0) return;\n swap(t[n][0], t[...
Codeforces/145/B
# Lucky Number 2 Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya loves long lucky numbers very much. He is interested in the minimum lucky ...
[ { "input": { "stdin": "4 7 3 1\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "2 2 1 1\n" }, "output": { "stdout": "4774" } }, { "input": { "stdin": "3 3 2 2\n" }, "output": { "stdout": "474774" } }, { "...
{ "tags": [ "constructive algorithms" ], "title": "Lucky Number 2" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = 1e9 + 7;\nint main() {\n int a, b, c, d;\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n if (abs(c - d) > 1) {\n puts(\"-1\");\n return 0;\n }\n if (c > d) {\n if (a - c < 0 || b - c < 0) {\n puts(\"-1\");\n return 0;\n }...
Codeforces/1485/D
# Multiples and Power Differences You are given a matrix a consisting of positive integers. It has n rows and m columns. Construct a matrix b consisting of positive integers. It should have the same size as a, and the following conditions should be met: * 1 ≤ b_{i,j} ≤ 10^6; * b_{i,j} is a multiple of a_{i,j};...
[ { "input": { "stdin": "2 3\n16 16 16\n16 16 16\n" }, "output": { "stdout": "\n16 32 48\n32 48 64\n" } }, { "input": { "stdin": "2 2\n1 2\n2 3\n" }, "output": { "stdout": "\n1 2\n2 3\n" } }, { "input": { "stdin": "2 2\n3 11\n12 8\n" }, ...
{ "tags": [ "constructive algorithms", "graphs", "math", "number theory" ], "title": "Multiples and Power Differences" }
{ "cpp": "#include <bits/stdc++.h>\n#include <cmath>\n#include <sstream>\n#include <vector>\n#include <stdio.h>\n#define ll long long\n#define fr(i,j,p) for(ll i=j; i<p ;i++)\n#define frr(i,j,p) for(ll i=j; i>=p ;i--)\n#define PI 3.14159265358979323846\n#define print(m) std...
Codeforces/150/B
# Quantity of Strings Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings ...
[ { "input": { "stdin": "1 1 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 2 4\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "784 1 1999\n" }, "output": { "stdout": "1\n" } }, { "inpu...
{ "tags": [ "combinatorics", "dfs and similar", "graphs", "math" ], "title": "Quantity of Strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint f[2010];\nint find(int x) {\n if (f[x] == x) return x;\n return f[x] = find(f[x]);\n}\nint n, m, k;\nint main() {\n cin >> n >> m >> k;\n for (int i = 1; i <= n; i++) f[i] = i;\n int cnt = n;\n for (int i = 1; i <= n; i++) {\n int l = i;\n int ...
Codeforces/1535/F
# String Distance Suppose you are given two strings a and b. You can apply the following operation any number of times: choose any contiguous substring of a or b, and sort the characters in it in non-descending order. Let f(a, b) the minimum number of operations you have to apply in order to make them equal (or f(a, b...
[ { "input": { "stdin": "4\nzzz\nbac\nabc\nacb\n" }, "output": { "stdout": "\n4015\n" } }, { "input": { "stdin": "2\na\nb\n" }, "output": { "stdout": "\n1337\n" } }, { "input": { "stdin": "1\na\n" }, "output": { "stdout": "0\n" ...
{ "tags": [ "binary search", "brute force", "data structures", "hashing", "implementation", "strings" ], "title": "String Distance" }
{ "cpp": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"unroll-loops\")\n#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n int n;\n cin >> n;\n if (n == 250...
Codeforces/162/I
# Truncatable primes A truncatable prime is a prime number which contains no zeros in decimal notation and all its suffixes are primes. 1 is considered to be not a prime. You are given a positive integer n. Figure out whether it is a truncatable prime. Input The only line of input contains an integer n (2 ≤ n ≤ 107...
[ { "input": { "stdin": "19\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "9137\n" }, "output": { "stdout": "YES\n" } } ]
{ "tags": [ "*special" ], "title": "Truncatable primes" }
{ "cpp": null, "java": null, "python": null }
Codeforces/182/E
# Wooden Fence Vasya has recently bought some land and decided to surround it with a wooden fence. He went to a company called "Wooden board" that produces wooden boards for fences. Vasya read in the catalog of products that the company has at its disposal n different types of wood. The company uses the i-th type of ...
[ { "input": { "stdin": "1 2\n2 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "2 3\n1 2\n2 3\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "6 6\n2 1\n3 2\n2 5\n3 3\n5 1\n2 1\n" }, "output": { "stdou...
{ "tags": [ "dp" ], "title": "Wooden Fence" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#pragma comment(linker, \"/STACK:200000000\")\nconst double EPS = 1E-9;\nconst int INF = 1000000000;\nconst long long INF64 = (long long)1E18;\nconst double PI = 3.1415926535897932384626433832795;\nconst int MOD = INF + 7;\nint a[1100], b[1100], z[3100][110][2...
Codeforces/207/A2
# Beaver's Calculator 1.0 The Smart Beaver from ABBYY has once again surprised us! He has developed a new calculating device, which he called the "Beaver's Calculator 1.0". It is very peculiar and it is planned to be used in a variety of scientific problems. To test it, the Smart Beaver invited n scientists, numbered...
[ { "input": { "stdin": "2\n2 1 1 1 10\n2 3 1 1 10\n" }, "output": { "stdout": "0\n1 1\n2 1\n3 2\n4 2\n" } }, { "input": { "stdin": "2\n3 10 2 3 1000\n3 100 1 999 1000\n" }, "output": { "stdout": "2\n10 1\n23 1\n49 1\n100 2\n99 2\n98 2\n" } }, { "inp...
{ "tags": [ "greedy" ], "title": "Beaver's Calculator 1.0" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\npair<pair<int, int>, int> ans[200005];\nint main() {\n int n, mx = 0, cur = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n int k, c, x, y, m, cnt = 0;\n scanf(\"%d%d%d%d%d\", &k, &c, &x, &y, &m);\n while (k--) {\n if (cur <= 200000)...
Codeforces/230/A
# Dragons Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initial...
[ { "input": { "stdin": "2 2\n1 99\n100 0\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "10 1\n100 100\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10 1\n10 10\n" }, "output": { "stdout": "NO\n" ...
{ "tags": [ "greedy", "sortings" ], "title": "Dragons" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read() {\n register int x = 0, t = 0;\n register char c = getchar();\n for (; c < '0' || c > '9'; t |= c == '-', c = getchar())\n ;\n for (; c >= '0' && c <= '9';\n x = (x << 3) + (x << 1) + (c ^ 48), c = getchar())\n ;\n return t ?...
Codeforces/255/E
# Furlo and Rublo and Game Furlo and Rublo play a game. The table has n piles of coins lying on it, the i-th pile has ai coins. Furlo and Rublo move in turns, Furlo moves first. In one move you are allowed to: * choose some pile, let's denote the current number of coins in it as x; * choose some integer y (0 ≤ y...
[ { "input": { "stdin": "10\n1 2 3 4 5 6 7 8 9 10\n" }, "output": { "stdout": "Furlo\n" } }, { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "Rublo\n" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "Rublo\n...
{ "tags": [ "games", "implementation", "math" ], "title": "Furlo and Rublo and Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n long long res = 0;\n for (int i = 0; i < n; i++) {\n long long x;\n cin >> x;\n if (x >= 4 && x <= 15) {\n res ^= 1;\n } else if (x >= 16 && x <= 81) {\n res ^= 2;\n } else if (x >= 6724 && x <= ...
Codeforces/279/E
# Beautiful Decomposition Valera considers a number beautiful, if it equals 2k or -2k for some integer k (k ≥ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible. Help V...
[ { "input": { "stdin": "1101101\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "10\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "111\n" }, "output": { "stdout": "2\n" } }, { "input": { ...
{ "tags": [ "dp", "games", "greedy", "number theory" ], "title": "Beautiful Decomposition" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1000005;\nchar s[N];\nint main() {\n scanf(\"%s\", s);\n int ans = 0;\n int i;\n for (i = strlen(s) - 1; i >= 0; --i) {\n if (s[i] == '0') continue;\n ans++;\n if (i == 0 || s[i - 1] == '0') continue;\n while (--i >= 0 && s[i] == ...
Codeforces/301/D
# Yaroslav and Divisors Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he has m queries: * Query number i is represented as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). * The answer to the query li, ri is the number of pairs of integers q, w (li ≤ q, w ≤ ri) such...
[ { "input": { "stdin": "1 1\n1\n1 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "10 9\n1 2 3 4 5 6 7 8 9 10\n1 10\n2 9\n3 8\n4 7\n5 6\n2 2\n9 10\n5 10\n4 10\n" }, "output": { "stdout": "27\n14\n8\n4\n2\n1\n2\n7\n9\n" } }, { "input":...
{ "tags": [ "data structures" ], "title": "Yaroslav and Divisors" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nsigned long long int bit[200000 + 5];\nsigned long long int read(int idx) {\n signed long long int sum = 0;\n if (idx == 0) return 0;\n while (idx > 0) {\n sum += bit[idx];\n idx -= (idx & -idx);\n }\n return sum;\n}\nvoid update(int idx, int val) {...
Codeforces/328/B
# Sheldon and Ice Pieces Do you remember how Kai constructed the word "eternity" using pieces of ice as components? Little Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number t. He realized that digits 6 and 9 are very similar, so he can rotat...
[ { "input": { "stdin": "42\n23454\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "169\n12118999\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5518\n98271085895851811183583522824259815685088253026112172543458311493...
{ "tags": [ "greedy" ], "title": "Sheldon and Ice Pieces" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint frc[20];\nint main() {\n string a, b;\n cin >> a >> b;\n for (auto &i : a) i -= '0';\n for (auto &i : b) frc[i -= '0']++;\n int r = 0, m = 1;\n while (m) {\n for (auto i : a) {\n if (frc[i] != 0)\n frc[i]--;\n else if (i == 6 && f...
Codeforces/34/C
# Page Numbers «Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces). Y...
[ { "input": { "stdin": "30,20,10\n" }, "output": { "stdout": "10,20,30" } }, { "input": { "stdin": "3,2,1\n" }, "output": { "stdout": "1-3" } }, { "input": { "stdin": "1,2,3,1,1,2,6,6,2\n" }, "output": { "stdout": "1-3,6" } ...
{ "tags": [ "expression parsing", "implementation", "sortings", "strings" ], "title": "Page Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> v;\nint main() {\n v.resize(10000, 0);\n int num;\n char coma;\n int n = 0;\n cin >> num;\n cin >> coma;\n v[num - 1] = 1;\n n++;\n while (!cin.eof()) {\n cin >> num;\n cin >> coma;\n v[num - 1] = 1;\n n++;\n }\n int size = 0...
Codeforces/373/D
# Counting Rectangles is Fun There is an n × m rectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on the i-th row and the j-th column as (i, j). Let's define a "rectangle" as four integers a, b, c, d (1 ≤ a ≤ c ≤ n; 1 ≤ b ≤ d ≤ m). Rectangle denotes a set of cells of t...
[ { "input": { "stdin": "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3\n" }, "output": { "stdout": "10\n1\n7\n34\n5\n" } }, { "input": { "stdin": "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7\n"...
{ "tags": [ "brute force", "divide and conquer", "dp" ], "title": "Counting Rectangles is Fun" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, q, a[77][77], sm[77][77], sum[77][77][77][77], x, y, xx, yy;\nchar str[77];\nint main() {\n scanf(\"%d%d%d\", &n, &m, &q);\n for (int i = 1; i <= n; i++) {\n scanf(\"%s\", str);\n for (int j = 1; j <= m; j++) {\n a[i][j] = str[j - 1] - '...
Codeforces/396/A
# On Number of Decompositions into Multipliers You are given an integer m as a product of integers a1, a2, ... an <image>. Your task is to find the number of distinct decompositions of number m into the product of n ordered positive integers. Decomposition into n products, given in the input, must also be considered ...
[ { "input": { "stdin": "1\n15\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3\n1 1 2\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "2\n5 7\n" }, "output": { "stdout": "4\n" } }, { "input...
{ "tags": [ "combinatorics", "math", "number theory" ], "title": "On Number of Decompositions into Multipliers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int p = 1000000007;\nint binomial(int n, int k, const int& factorial) {\n auto result = factorial;\n for (auto i = 1; i <= k; i++) result = 1LL * result * (n - i + 1) % p;\n return result;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n int n...
Codeforces/418/B
# Cunning Gena A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants are offered m problems on the contest. For each friend, Ge...
[ { "input": { "stdin": "2 2 1\n100 1 1\n2\n100 2 1\n1\n" }, "output": { "stdout": "202" } }, { "input": { "stdin": "1 2 1\n1 1 1\n1\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2\n" ...
{ "tags": [ "bitmasks", "dp", "greedy", "sortings" ], "title": "Cunning Gena" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = (1 << 20) + 10;\nlong long dp[maxn];\nstruct NN {\n int x, k, m;\n int ind;\n int sta;\n} nn[110];\nint cmp(NN a, NN b) { return a.k < b.k; }\nint a[110][110];\nint main() {\n int n, m, b, i, j, k;\n scanf(\"%d%d%d\", &n, &m, &b);\n for ...
Codeforces/444/E
# DZY Loves Planting DZY loves planting, and he enjoys solving tree problems. DZY has a weighted tree (connected undirected graph without cycles) containing n nodes (they are numbered from 1 to n). He defines the function g(x, y) (1 ≤ x, y ≤ n) as the longest edge in the shortest path between nodes x and y. Specially...
[ { "input": { "stdin": "4\n1 2 1\n2 3 2\n3 4 3\n1\n1\n1\n1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "4\n1 2 1\n2 3 2\n3 4 3\n4\n4\n4\n4\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "1\n1\n" }, "outpu...
{ "tags": [ "binary search", "dsu", "trees" ], "title": "DZY Loves Planting" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline long long read() {\n char ch = getchar();\n long long x = 0, f = 1;\n for (; ch > '9' || ch < '0'; ch = getchar())\n if (ch == '-') f = -1;\n for (; ch >= '0' && ch <= '9'; ch = getchar())\n x = (x << 1) + (x << 3) + ch - '0';\n return x * f;...
Codeforces/467/A
# George and Accommodation George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-...
[ { "input": { "stdin": "3\n1 1\n2 2\n3 3\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "3\n1 10\n0 10\n10 10\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "17\n68 69\n47 48\n30 31\n52 54\n41 43\n33 35\n38 40\n56 5...
{ "tags": [ "implementation" ], "title": "George and Accommodation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, i, j, r, arr[100][2];\n cin >> n;\n for (i = 0; i < n; i++) {\n for (j = 0; j < 2; j++) {\n cin >> arr[i][j];\n }\n }\n r = 0;\n for (i = 0; i < n; i++) {\n if (arr[i][1] - arr[i][0] >= 2) {\n r++;\n }\n }\n ...
Codeforces/48/D
# Permutations A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not. There are many tasks on permutations. Today you are going to solve one of them. Let’s imagine th...
[ { "input": { "stdin": "9\n1 2 3 1 2 1 4 2 5\n" }, "output": { "stdout": "3\n1 1 1 2 2 3 1 3 1 " } }, { "input": { "stdin": "4\n4 3 2 1\n" }, "output": { "stdout": "1\n1 1 1 1 " } }, { "input": { "stdin": "4\n1 2 2 3\n" }, "output": { ...
{ "tags": [ "greedy" ], "title": "Permutations" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int a[100010], k[100010], i, n;\n memset(k, 0, sizeof(int) * 100010);\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n k[a[i] - 1]++;\n }\n for (i = 0; i < 100010 - 1; i++) {\n if (k[i] < k[i + 1]) {\n printf(\"-1\");\n ...
Codeforces/513/F1
# Scaygerboss Cthulhu decided to catch Scaygerboss. Scaygerboss found it out and is trying to hide in a pack of his scaygers. Each scayger except Scaygerboss is either a male or a female. Scaygerboss's gender is "other". Scaygers are scattered on a two-dimensional map divided into cells. A scayger looks nerdy and lov...
[ { "input": { "stdin": "4 4 2 3\n....\n.###\n####\n####\n2 1 1\n2 1 2\n2 1 2\n2 1 2\n2 1 2\n1 1 2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2 4 2 2\n....\n.###\n2 1 1\n2 1 2\n2 1 2\n2 1 2\n2 1 2\n" }, "output": { "stdout": "-1\n" } }, ...
{ "tags": [ "flows" ], "title": "Scaygerboss" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\nT inline sqr(T x) {\n return x * x;\n}\nconst long double pi = 3.1415926535897932384626433832795;\nconst long double eps = 1e-8;\nconst int maxn = 1050;\nconst int inf = 1e9;\nstruct Edge {\n int to, cap, fl;\n Edge(int to, int cap, int ...
Codeforces/53/B
# Blog Photo One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the height / width quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2...
[ { "input": { "stdin": "2 1\n" }, "output": { "stdout": "1 1" } }, { "input": { "stdin": "2 2\n" }, "output": { "stdout": "2 2" } }, { "input": { "stdin": "5 5\n" }, "output": { "stdout": "5 4" } }, { "input": { ...
{ "tags": [ "binary search", "implementation" ], "title": "Blog Photo" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long w, h, cur = 2147483648;\nbool f(string s) {\n string a, b;\n a.substr(0, s.size() / 2);\n b.substr(s.size() / 2, s.size() / 2);\n if (a == b) {\n return 1;\n } else\n return 0;\n}\nint main() {\n cin >> h >> w;\n double wid = min(h, w);\...
Codeforces/567/D
# One-Dimensional Battle Ships Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a...
[ { "input": { "stdin": "11 3 3\n5\n4 8 6 1 11\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "5 1 3\n1\n3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 1 3\n2\n1 5\n" }, "output": { "stdout": "-1\n...
{ "tags": [ "binary search", "data structures", "greedy", "sortings" ], "title": "One-Dimensional Battle Ships" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint arr[100005];\nint main() {\n set<int> s;\n int n, k, a, m, i, x;\n cin >> n >> k >> a;\n cin >> m;\n s.insert(0);\n s.insert(n + 1);\n int cnt = (n + 1) / (a + 1);\n for (i = 1; i <= m; i++) {\n cin >> x;\n set<int>::iterator it = s.upper_bou...
Codeforces/588/F
# Duff in Mafia Duff is one if the heads of Mafia in her country, Andarz Gu. Andarz Gu has n cities (numbered from 1 to n) connected by m bidirectional roads (numbered by 1 to m). Each road has a destructing time, and a color. i-th road connects cities vi and ui and its color is ci and its destructing time is ti. Ma...
[ { "input": { "stdin": "5 7\n2 1 3 7\n3 1 1 6\n5 4 1 8\n4 5 1 1\n3 2 2 3\n4 5 2 5\n2 3 2 4\n" }, "output": { "stdout": "Yes\n3 2\n4 5 " } }, { "input": { "stdin": "3 5\n3 2 1 3\n1 3 1 1\n3 2 1 4\n1 3 2 2\n1 3 2 10\n" }, "output": { "stdout": "No" } }, {...
{ "tags": [ "2-sat", "binary search" ], "title": "Duff in Mafia" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX_BUF_SIZE = 16384;\nchar BUFOR[MAX_BUF_SIZE];\nint BUF_SIZE, BUF_POS;\nchar ZZZ;\nconst int MXN = 6 * 50010;\nconst int C = 262144;\nconst int INF = 1000000001;\nnamespace SAT2 {\nvector<int> G[MXN], GT[MXN];\nint pre[MXN], skl[MXN];\nvector<int> ...
Codeforces/610/C
# Harmony Analysis The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coo...
[ { "input": { "stdin": "2\n" }, "output": { "stdout": "++++\n+*+*\n++**\n+**+\n" } }, { "input": { "stdin": "2\n" }, "output": { "stdout": "++++\n+*+*\n++**\n+**+\n" } }, { "input": { "stdin": "4\n" }, "output": { "stdout": "++++...
{ "tags": [ "constructive algorithms" ], "title": "Harmony Analysis" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, k;\npair<int, int> d[3] = {{0, 1}, {1, 0}, {1, 1}};\nint b[550][550];\nvoid rec(int k) {\n if (k == 1) {\n b[0][0] = 1;\n return;\n }\n rec(k /= 2);\n for (int c = 0; c < 3; c++) {\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < ...
Codeforces/630/M
# Turn Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face. One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget — a rotating camera — come up to it to s...
[ { "input": { "stdin": "-60\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "60\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "6678504591813508\n" }, "output": { "stdout": "2\n" } }, { "inp...
{ "tags": [ "geometry", "math" ], "title": "Turn" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long n;\n cin >> n;\n n = -n;\n n %= 360;\n if (n < 0) n += 360;\n int best = min(n, 360 - n);\n int res = 0;\n for (int i = 0; i < 4; ++i) {\n int cur = n + 90 * i;\n cur %= 360;\n if (best > min(cur, 360 - cur)) {\n ...
Codeforces/659/E
# New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads. The President of Berland decided to make changes ...
[ { "input": { "stdin": "5 5\n2 1\n1 3\n2 3\n2 5\n4 3\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "4 3\n2 1\n1 3\n4 3\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "6 5\n1 2\n2 3\n4 5\n4 6\n5 6\n" }, "output"...
{ "tags": [ "data structures", "dfs and similar", "dsu", "graphs", "greedy" ], "title": "New Reform" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 10;\nint fa[N];\nbool flag[N];\nint find(int x) {\n int r = x;\n while (fa[r] != r) r = fa[r];\n int i = x, j;\n while (i != r) {\n j = fa[i];\n fa[i] = r;\n i = j;\n }\n return r;\n}\nint main() {\n ios::sync_with_stdio(fal...
Codeforces/682/B
# Alyona and Mex Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In partic...
[ { "input": { "stdin": "5\n1 3 3 3 6\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "2\n2 1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "4\n1 2 2 3\n" }, "output": { "stdout": "4\n" } }, { ...
{ "tags": [ "sortings" ], "title": "Alyona and Mex" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long a[100000];\n long long n, i;\n cin >> n;\n for (i = 0; i < n; i++) {\n cin >> a[i];\n }\n sort(a, a + n);\n a[0] = 1;\n for (i = 1; i <= n - 1; i++) {\n if (a[i] == a[i - 1] || a[i] == a[i - 1] + 1)\n continue;\n ...
Codeforces/705/A
# Hulk Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings. Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is ha...
[ { "input": { "stdin": "2\n" }, "output": { "stdout": "I hate that I love it\n" } }, { "input": { "stdin": "1\n" }, "output": { "stdout": "I hate it\n" } }, { "input": { "stdin": "3\n" }, "output": { "stdout": "I hate that I love...
{ "tags": [ "implementation" ], "title": "Hulk" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n for (int i = 0; i < n; i++) {\n if (i % 2 != 0)\n cout << \"I love \";\n else\n cout << \"I hate \";\n if (i + 1 < n) cout << \"that \";\n }\n cout << \"it\" << endl;\n return 0;\n}\n", "java": "im...
Codeforces/727/D
# T-shirts Distribution The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the numb...
[ { "input": { "stdin": "0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL\n" }, "output": { "stdout": "YES\nXL\nM\nXXL\n" } }, { "input": { "stdin": "1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "5 1 ...
{ "tags": [ "constructive algorithms", "flows", "greedy" ], "title": "T-shirts Distribution" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar ans[100005][7];\nchar shirts[6][7] = {\"S\", \"M\", \"L\", \"XL\", \"XXL\", \"XXXL\"};\nint GetId(char *s) {\n if (s[0] == 'S') return 0;\n if (s[0] == 'M') return 1;\n if (s[0] == 'L') return 2;\n if (s[1] == 'L') return 3;\n if (s[2] == 'L') return...
Codeforces/748/E
# Santa Claus and Tangerines Santa Claus has n tangerines, and the i-th of them consists of exactly ai slices. Santa Claus came to a school which has k pupils. Santa decided to treat them with tangerines. However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to div...
[ { "input": { "stdin": "3 2\n5 9 3\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "2 4\n12 14\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "2 3\n1 1\n" }, "output": { "stdout": "-1\n" } }, { ...
{ "tags": [ "binary search", "data structures", "greedy", "two pointers" ], "title": "Santa Claus and Tangerines" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline void out(int a) { printf(\"%d\\n\", a); }\ninline void out(int a, int b) { printf(\"%d %d\\n\", a, b); }\ninline void outf(double a) { printf(\"%3.lf\\n\", a); }\ninline void outf(double a, double b) { printf(\"%3.lf %3.lf\\n\", a, b); }\ninline void ba...
Codeforces/771/D
# Bear and Company Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible. Li...
[ { "input": { "stdin": "4\nVKVK\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "7\nVVKEVKK\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "5\nLIMAK\n" }, "output": { "stdout": "0\n" } }, { ...
{ "tags": [ "dp" ], "title": "Bear and Company" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 80;\nint f[N][N][N][2], s[N][3];\nint kn, vn, xn, pv[N], pk[N], px[N];\nchar ch[N];\nint n;\nvoid upd(int &x, int y) { x = min(x, y); }\nint get(int d, int f, int v, int k, int x) {\n int tot = 0, cnt[3];\n cnt[0] = max(s[d][0] - v, 0);\n cnt[...
Codeforces/796/D
# Police Stations Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own. Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each ...
[ { "input": { "stdin": "6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6\n" }, "output": { "stdout": "2\n4 5 " } }, { "input": { "stdin": "6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6\n" }, "output": { "stdout": "1\n3 " } }, { "input": { "stdin": "6 2 4\n1 6\n1 2...
{ "tags": [ "constructive algorithms", "dfs and similar", "dp", "graphs", "shortest paths", "trees" ], "title": "Police Stations" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma warning(disable : 4996)\n#pragma comment(linker, \"/STACK:1048576\")\nusing namespace std;\nint IT_MAX = 1 << 17;\nconst long long MOD = 1000000007;\nconst int INF = 0x3f3f3f3f;\nconst long long LL_INF = 1234567890123456789ll;\nconst double PI = acos(-1);\nconst double ERR ...
Codeforces/816/E
# Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some groceries. <image> She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars. The supermarket sells n goods. The i-th good can be bought for ...
[ { "input": { "stdin": "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "9 7\n3 1\n...
{ "tags": [ "brute force", "dp", "trees" ], "title": "Karen and Supermarket" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid fre() {\n freopen(\"c://test//input.in\", \"r\", stdin);\n freopen(\"c://test//output.out\", \"w\", stdout);\n}\ntemplate <class T1, class T2>\ninline void gmax(T1 &a, T2 b) {\n if (b > a) a = b;\n}\ntemplate <class T1, class T2>\ninline void gmin(T1 &...
Codeforces/841/E
# On the Bench A year ago on the bench in public park Leha found an array of n numbers. Leha believes that permutation p is right if for all 1 ≤ i < n condition, that api·api + 1 is not perfect square, holds. Leha wants to find number of right permutations modulo 109 + 7. Input First line of input data contains sing...
[ { "input": { "stdin": "7\n5 2 4 2 4 1 1\n" }, "output": { "stdout": "144" } }, { "input": { "stdin": "3\n1 2 4\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "50\n297 787 34 268 439 629 600 398 425 833 721 908 830 636 64 509 420 6...
{ "tags": [ "combinatorics", "dp" ], "title": "On the Bench" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = (int)5e5 + 7;\nconst int inf = (int)1e9 + 7;\nint n;\nint a[303], f[303], cnt[303], sz;\nint c[303][303];\nint d[303][303];\nint ff[303];\nint main() {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", a + i);\n for (i...
Codeforces/862/F
# Mahmoud and Ehab and the final stage Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it...
[ { "input": { "stdin": "5 9\nmahmoud mahmoudbadawy drmahmoud drevil mahmoud\n1 1 5\n1 1 2\n1 2 3\n2 3 mahmoud\n2 4 mahmoud\n2 2 mahmouu\n1 1 5\n1 2 3\n1 1 1\n" }, "output": { "stdout": "14\n14\n13\n30\n12\n7\n" } }, { "input": { "stdin": "10 20\nvgxgp vgxgpuamkx vgxgpua vgxg...
{ "tags": [ "data structures", "strings" ], "title": "Mahmoud and Ehab and the final stage" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint N, Q;\nstring S[100005];\nchar Str[100005];\nint X[100005];\nint Left[45][400005], Right[45][400005], Arb[45][400005];\nint Arb2[400005];\nconst int limit = 40;\nint Right2, ans;\nset<int> P;\nint Next[100005], Prev[100005];\nint Aux[100005], Stack[100005]...
Codeforces/888/D
# Almost Identity Permutations A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array. Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i. Your task is to count the number of almost identit...
[ { "input": { "stdin": "4 2\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "4 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 4\n" }, "output": { "stdout": "76\n" } }, { "input": { ...
{ "tags": [ "combinatorics", "dp", "math" ], "title": "Almost Identity Permutations" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long N = 1e3 + 5;\nlong long C[N][5], dif_fact[5];\nvoid calc() {\n for (long long i = 0; i < N; i++) {\n for (long long j = 0; j <= min(i, 4LL); j++) {\n if (j == 0 || j == i) {\n C[i][j] = 1;\n } else {\n C[i][j] = C[i ...
Codeforces/911/B
# Two Cakes It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces. Ivan knows that there will be n people at the celebration (including himself), so Ivan h...
[ { "input": { "stdin": "4 7 10\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "5 2 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 2 8\n" }, "output": { "stdout": "2\n" } }, { "input": ...
{ "tags": [ "binary search", "brute force", "implementation" ], "title": "Two Cakes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline void boost() {\n ios_base::sync_with_stdio();\n cin.tie(0);\n cout.tie(0);\n}\nconst long long maxn = 1e5 + 123;\nconst long long inf = 1e9 + 123;\nconst long long mod = 1e9 + 7;\nconst double pi = acos(-1);\nint main() {\n boost();\n int n, a, b;\...
Codeforces/932/G
# Palindrome Partition Given a string s, find the number of ways to split s to substrings such that if there are k substrings (p1, p2, p3, ..., pk) in partition, then pi = pk - i + 1 for all i (1 ≤ i ≤ k) and k is even. Since the number of ways can be large, print it modulo 109 + 7. Input The only line of input con...
[ { "input": { "stdin": "abcdcdab\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "abbababababbab\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "bacbccacaabbcbbbbaccbbbbacacaccbabaabccbaababccacacabbbbccabbbbcbbaacac...
{ "tags": [ "dp", "string suffix structures", "strings" ], "title": "Palindrome Partition" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read() {\n char ch = getchar();\n int w = 1, c = 0;\n for (; !isdigit(ch); ch = getchar())\n if (ch == '-') w = -1;\n for (; isdigit(ch); ch = getchar()) c = (c << 1) + (c << 3) + (ch ^ 48);\n return w * c;\n}\nconst int M = 1e6 + 10, mod = ...
Codeforces/95/B
# Lucky Numbers Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Lucky number is super lucky if it's decimal representation contains equal amount of d...
[ { "input": { "stdin": "47\n" }, "output": { "stdout": "47\n" } }, { "input": { "stdin": "4500\n" }, "output": { "stdout": "4747\n" } }, { "input": { "stdin": "73\n" }, "output": { "stdout": "74\n" } }, { "input": { ...
{ "tags": [ "dp", "greedy" ], "title": "Lucky Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar s[100005];\nint n;\nint main() {\n while (~scanf(\"%s\", s)) {\n n = strlen(s);\n if (n & 1) {\n n = (n + 1) / 2;\n for (int i = 0; i < n; i++) putchar('4');\n for (int i = 0; i < n; i++) putchar('7');\n puts(\"\");\n conti...
Codeforces/986/F
# Oppa Funcan Style Remastered Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit l...
[ { "input": { "stdin": "3\n7 7\n3 8\n5 6\n" }, "output": { "stdout": "YES\nNO\nYES\n" } }, { "input": { "stdin": "4\n1 1\n2 1\n1000000000 1\n1000000000000000000 1\n" }, "output": { "stdout": "NO\nNO\nNO\nNO\n" } }, { "input": { "stdin": "3\n7 ...
{ "tags": [ "graphs", "math", "number theory", "shortest paths" ], "title": "Oppa Funcan Style Remastered" }
{ "cpp": "#include <bits/stdc++.h>\nusing std ::pair;\nusing std ::priority_queue;\nusing std ::sort;\ntemplate <typename T>\nT max(T x, T y) {\n return (x > y) ? x : y;\n}\ntemplate <typename T>\nT min(T x, T y) {\n return (x < y) ? x : y;\n}\ntemplate <typename T>\nbool chkmax(T &x, T y) {\n return (x >= y) ? 0 ...
HackerEarth/akbar-and-birbal-1
This is the time of Akbar and Birbal. One day , in the state there happened some riots and murders .Akbar found some N people guilty in this matter.He called Birbal to punish them .Birbal gave them a unique punishment .He ordered the first person(guilty) to dig all the N spaces indicated on the ground.Then he called th...
[ { "input": { "stdin": "100\n1804246908\n846901785\n1681651770\n1714595508\n1957703548\n424217739\n719858557\n1649719876\n596492227\n1189606931\n1025170345\n1350453279\n783340703\n1102486856\n2044852544\n1967469571\n1365143593\n1540344180\n304071735\n1303419634\n34999296\n521572531\n294685402\n1726914874\n...
{ "tags": [], "title": "akbar-and-birbal-1" }
{ "cpp": null, "java": null, "python": "import math\nfor _ in xrange(input()):\n\tn = input()\n\tprint (n - int(math.sqrt(n)))" }
HackerEarth/building-network
There are N routers. The cost of the wires required in connecting two routers, i and j, is R[i]+R[j] where R[i] is the number of slots in the i-th router and i!=j. You need to build a network, such that there is a path between every pair of routers. Find the minimum cost of building such a network.The connection betwee...
[ { "input": { "stdin": "2\n2\n1 2\n3\n1 1 1\n\nSAMPLE" }, "output": { "stdout": "3\n4" } }, { "input": { "stdin": "5\n42\n18468 6335 26501 19170 15725 11479 29359 26963 24465 5706 28146 23282 16828 9962 492 2996 11943 4828 5437 32392 14605 3903 154 293 12383 17422 18717 1971...
{ "tags": [], "title": "building-network" }
{ "cpp": null, "java": null, "python": "t=input()\nwhile t>0:\n n=input()\n arr=[]\n arr=map(int,raw_input().split())\n brr=sorted(arr)\n l=len(brr)\n su=0\n for i in range(1,l):\n a=brr[0]+brr[i]\n su=su+a\n print su\n t=t-1" }
HackerEarth/dawood-the-mathematician
Dr.Dawood is another mathematician who is more geekier than Osama (that's why, ++). Dawood deals with more complex problems than Osama usually deals with. He also recruits people to be his sub-ordinates. He usually gives them "trivial" problems as test for them to solve and earn their position to be his sub-ordinate. ...
[ { "input": { "stdin": "4\n3\n1\n2\n5\n\nSAMPLE" }, "output": { "stdout": "2\n2\n4\n2" } } ]
{ "tags": [], "title": "dawood-the-mathematician" }
{ "cpp": null, "java": null, "python": "for i in range(0,int(input())):\n f =0\n ans = 0\n x = int(input())\n while x%2 == 0:\n f = f+1\n x= x/2\n ans = pow(2,(f+1))\n print(ans)" }
HackerEarth/game-of-divisors
Two players are playing the following game with a positive integer N. On his turn a player has to write down on paper some integer between 1 and N. But it's forbidden to write an integer which is a divisor of some number already written on paper. For example N = 8 and numbers already written are: 6, 4, 7. In this case ...
[ { "input": { "stdin": "1\n3\n\nSAMPLE" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5\n6\n7\n8\n9\n10" }, "output": { "stdout": "1\n1\n1\n1\n1\n" } }, { "input": { "stdin": "1\n9\n\nESMNOD" }, "output": { "stdout":...
{ "tags": [], "title": "game-of-divisors" }
{ "cpp": null, "java": null, "python": "def winner(N):\n\tls=[1]\n\tfor num in range(2, N + 1):\n\t\tif num > 1:\n\t\t\tfor i in range(2,num):\n\t\t\t\tif (num % i) == 0:\n\t\t\t\t\tbreak\n\t\telse:\n\t\t\tls.append(num)\n\tif len(ls)%2==0:\n\t\treturn '2'\n\telse:\n\t\treturn '1'\nfor i in xrange(int(raw_input()...
HackerEarth/karan-and-strings
After the Tatvik Hiring Challenge, Karan's NLP professor has given him another homework. Since Manhattan Associates Hiring Challenge is round the corner and Karan is busy preparing for it, he turns to you for help. Given a string, replace all the consecutively occurring characters by a single, same character. Input: ...
[ { "input": { "stdin": "1\naabbcc\n\nSAMPLE" }, "output": { "stdout": "abc\n" } }, { "input": { "stdin": "10\naabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nabcdefghijklmnopqrstuvwxyz\n...............................aaaaaaaa...
{ "tags": [], "title": "karan-and-strings" }
{ "cpp": null, "java": null, "python": "T = int(raw_input())\n\nif T in range(1, 101):\n\tfor i in range(T):\n\t\tstring = raw_input()\n\t\tif len(string) in range(1, 10**6):\n\t\t\tnewString = ''\n\t\t\tfor s in string:\n\t\t\t\tif newString:\n\t\t\t\t\tif not newString[-1] == s:\n\t\t\t\t\t\tnewString += s\n\t\...
HackerEarth/mittal-wants-to-go-to-play
Mittal lives in the Niti Colony. The colony has N houses numbered from 1 to N. There are M bidirectional roads in the colony for travelling between houses. There might be multiple roads between two houses. Mittal lives in the house with index 1. He has friends in all houses of the colony. He is always wanting to visi...
[ { "input": { "stdin": "1\n5 5\n1 2 2\n2 3 4\n3 4 5\n4 5 1\n1 4 7\n3\n4 20\n5 21\n3 5\n\nSAMPLE" }, "output": { "stdout": "6\n5\n0" } }, { "input": { "stdin": "2\n2 2\n1 2 10\n1 2 2\n4\n2 45\n2 30\n2 11\n2 13\n3 6\n1 2 2\n2 3 4\n2 3 10\n2 3 4\n1 2 5\n2 3 6\n5\n3 20\n3 11\n3 ...
{ "tags": [], "title": "mittal-wants-to-go-to-play" }
{ "cpp": null, "java": null, "python": "from collections import defaultdict\nimport sys\ninf = sys.maxint\n\n\n\nclass Graph():\n\n def __init__(self, connections=[], directed=False,weighted=False):\n self.status_dict = defaultdict(lambda : 1)\n self.graph_dict = defaultdict(list)\n ...
HackerEarth/panda-and-combination
Panda loves solving problems which are deemed impossible by his fellow classmates. The current problem which he is working on is to express a number N as sum of powers of number X (Not necessarily distinct) such that the number of powers of number X used should be minimum. Note: The powers of a number can be 0, 1,...
[ { "input": { "stdin": "3\n4 4\n5 3\n6 1\n\nSAMPLE" }, "output": { "stdout": "1\n3\n6\n" } }, { "input": { "stdin": "3\n7 5\n0 1\n6 1\n\nSAMPKE" }, "output": { "stdout": "3\n0\n6\n" } }, { "input": { "stdin": "3\n2 1\n0 1\n3 1\n\nSAMPKE" }...
{ "tags": [], "title": "panda-and-combination" }
{ "cpp": null, "java": null, "python": "def power(a,n):\n\tr =1\n\tv=a\n\tp=n\n\twhile p >0:\n\t\tif p&1:\n\t\t\tr =r*v \n\t\t\tr= r\n\t\tv = v * v\n\t\tv=v\n\t\tp = p/2\n\treturn r\nt=input()\nwhile t:\n\tt-=1\n\ta,b=map(int,raw_input().split())\n\tnew=[]\n\ti=0\n\tif b>1:\n\t\twhile b**i<=a:\n\t\t\tm=power(b,i)...
HackerEarth/rod-cutting-problem-7
A young mischievous boy Harsh, got into a trouble when his mechanical workshop teacher told him to cut Iron rods. The rod cutting algorithm is as follows: Step 1. If the rod can be divided into two equal parts, cut it and choose any one of them. Step 2. Else cut the rod into two parts having non-zero integral ...
[ { "input": { "stdin": "2\n3\n2\n\nSAMPLE" }, "output": { "stdout": "1\n0\n" } }, { "input": { "stdin": "10\n977\n543\n252\n979\n348\n889\n876\n55\n938\n610" }, "output": { "stdout": "8\n8\n6\n8\n7\n8\n8\n4\n8\n8\n" } }, { "input": { "stdin": ...
{ "tags": [], "title": "rod-cutting-problem-7" }
{ "cpp": null, "java": null, "python": "def count_special(N):\n\tcount = 0\n\tprev = 0\n\tcurr = 3\n\twhile(curr <= N):\n\t\tprev = curr\n\t\tcurr = 2*prev + 1\n\t\tcount += 1\n\treturn count\n\nT = int(raw_input())\nfor t in xrange(T):\n\tN = int(raw_input())\n\tcount = count_special(N)\n\tprint count" }
HackerEarth/steal-the-show
It’s the company's 3^rd anniversary, and everyone is super-excited about it. There is a tradition in the company, that the interns decide the seating arrangement of all the members. The requirements of this order are: The CEO of the company must sit in the middle. (In case of even number of team members, he can sit on ...
[ { "input": { "stdin": "13 17 11\n2\n12 10\n\nSAMPLE" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "80 82 81\n3\n50 90 65" }, "output": { "stdout": "79\n" } }, { "input": { "stdin": "13 9 2\n2\n12 3\n\nMASPME" }, "output"...
{ "tags": [], "title": "steal-the-show" }
{ "cpp": null, "java": null, "python": "#mod = 10**9 + 7\n#test = int(raw_input())\n#for i in range(test):\nceo , coo , cto = [int(x) for x in raw_input().split()]\nn = int( raw_input() )\narr = [int(x) for x in raw_input().split()]\narr.sort(reverse = True)\nres = [0 for i in range(n+3)]\nl = len(res)\nres[0] = ...