id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
HackerEarth/just-find-the-next-number
You are given an integer n find its next greater or equal number whose binary representation must not contain consecutive ones. For eg. given n=6 whose binary is 110 and the next number with no consecutive ones is 8 whose binary is 1000. INPUT First line of input contains t, the total number of test cases. Then t l...
[ { "input": { "stdin": "2\n6\n2\n\nSAMPLE" }, "output": { "stdout": "8\n2\n" } }, { "input": { "stdin": "10\n71\n8497\n6364\n6530\n9199\n5754\n1508\n9388\n6992\n4494" }, "output": { "stdout": "72\n8512\n8192\n8192\n9216\n8192\n2048\n9472\n8192\n4608\n" } ...
{ "tags": [], "title": "just-find-the-next-number" }
{ "cpp": null, "java": null, "python": "for case in range(int(raw_input())):\n\tn=int(raw_input())\n\twhile True:\n\t\ts=str(bin(n)[2:])\n\t\tif \"11\" not in s:\n\t\t\tbreak\n\t\tn=n+1\n\tprint n" }
HackerEarth/minimum-coins
Ram has to pay Shyam R rupees and but the coins which he has got is of limited denomination .Assuming he has a limited knowledge of the mathematics and has unlimited number of coins of each denomination.Write a code to help Ram to pay the money using minimum number of coins .Suppose Ram has coins of {1,2} denomination ...
[ { "input": { "stdin": "24\n{1,7,3}\n\nSAMPLE" }, "output": { "stdout": "Rupee 1 coin 0\nRupee 3 coin 1\nRupee 7 coin 3" } }, { "input": { "stdin": "9\n{10}" }, "output": { "stdout": "ERROR" } }, { "input": { "stdin": "-19\n{10}" }, "o...
{ "tags": [], "title": "minimum-coins" }
{ "cpp": null, "java": null, "python": "def min_coins(money, denoms):\n # money : The money which is needed to get minimum coins\n # denoms : Available denominations\n denoms.sort()\n \n min = denoms[0] * money\n coin_list = {}\n \n for j in range(len(denoms)):\n temp = money\n use...
HackerEarth/palindrome-count-1
Given a string S, count the number of non empty sub strings that are palindromes. A sub string is any continuous sequence of characters in the string. A string is said to be palindrome, if the reverse of the string is same as itself. Two sub strings are different if they occur at different positions in S Input Input c...
[ { "input": { "stdin": "dskjkd\n\nSAMPLE" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "wqc" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "mygrlbiadhzlabefvjtghfrtydurkpmoveczbqzzlenezq" }, "output": { "...
{ "tags": [], "title": "palindrome-count-1" }
{ "cpp": null, "java": null, "python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\ns=raw_input()\ncount=0\nfor i in xrange(1,len(s)):\n\tfor j in xrange(0,len(s)-i+1):\n\t\ta=s[j:j+i]\n\t\tb=s[j:j+i][::-1]\n\t\tif a...
HackerEarth/rhezo-and-prime-problems
Rhezo and his friend Vanya love problem solving. They have a problem set containing N problems, with points assigned to each. Rhezo wants to solve problems in such a way that he gets the maximum number of points. Rhezo has a weird habit of solving only prime number of consecutive problems, that is, if he solves X conse...
[ { "input": { "stdin": "4\n8 1 3 7\n\nSAMPLE" }, "output": { "stdout": "12" } }, { "input": { "stdin": "4\n-2 0 0 -1\n\nNLQEAR" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n-2 0 -1 -1\n\nRAEQLN" }, "output": { "s...
{ "tags": [], "title": "rhezo-and-prime-problems" }
{ "cpp": null, "java": null, "python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\nimport sys\n \ndef readline():\n return sys.stdin.readline().strip()\ndef readInts(sep=None):\n return map(int,readline().spli...
HackerEarth/special-sum-3
Special Sum of number N is defined as follows: def foo(n): { ret = 0 for i = 1 to n: { if gcd(n,i) is 1: ret += 1 } return ret } def SpecialSum(N): { ret=0 for i = 1 to N: { if i divides N: ret += foo(i) } return ret } Given a N print SpecialSum(N). Input:...
[ { "input": { "stdin": "1\n5\n\nSAMPLE" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "1000\n84\n87\n78\n16\n94\n36\n87\n93\n50\n22\n63\n28\n91\n60\n64\n27\n41\n27\n73\n37\n12\n69\n68\n30\n83\n31\n63\n24\n68\n36\n30\n3\n23\n59\n70\n68\n94\n57\n12\n43\n30\n74\n22\...
{ "tags": [], "title": "special-sum-3" }
{ "cpp": null, "java": null, "python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\n\n\nt = int(raw_input())\nwhile(t):\n\tn = int(raw_input())\n\tprint n\n\tt-=1" }
HackerEarth/trick-with-the-cards
Mr. X is performing a trick with the cards. He has N cards, lets name them 1.....N, on a round table. So card 1 is in between 2nd card and Nth card. Initially all cards are upside down. His trick involves making all cards face up. His trick is whenever he taps on a card, it flips (if card was originally upside down,...
[ { "input": { "stdin": "2\n2\n3\n\nSAMPLE" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "2\n1\n16\n\nFAPMMS" }, "output": { "stdout": "1\n16\n" } }, { "input": { "stdin": "2\n19\n2\n\nENPM@T" }, "output": { "stdou...
{ "tags": [], "title": "trick-with-the-cards" }
{ "cpp": null, "java": null, "python": "for _ in range(int(raw_input())):\n\tnumber = int(raw_input())\n\tif number == 0:\n\t\tprint 0\n\telif number < 3:\n\t\tprint 1\n\telif number % 3 == 0:\n\t\tprint number / 3\n\telse:\n\t\tprint number" }
p02578 AtCoder Beginner Contest 176 - Step
N persons are standing in a row. The height of the i-th person from the front is A_i. We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person: Condition: Nobody in front of the person is taller than the person. Here, the height of a ...
[ { "input": { "stdin": "5\n3 3 3 3 3" }, "output": { "stdout": "0" } }, { "input": { "stdin": "5\n2 1 5 4 3" }, "output": { "stdout": "4" } }, { "input": { "stdin": "5\n0 -1 1 19 2" }, "output": { "stdout": "18\n" } }, { ...
{ "tags": [], "title": "p02578 AtCoder Beginner Contest 176 - Step" }
{ "cpp": "#include <iostream>\n\nusing namespace std;\n\nint main(){\n\tlong long n = 0, count = 0, last = 0;\n\tcin >> n;\n\tfor(int i = 0;i < n; ++i){\n\t\tint x;\n\t\tcin >> x;\n\t\tif(x >= last){\n\t\t\tlast = x;\n\t\t\t}\n\t\telse{\n\t\t\tcount += (last - x);\n\t\t\t}\n\t\t}\n\tcout << count;\n\treturn 0;\n\t}\n...
p02709 AtCoder Beginner Contest 163 - Active Infants
There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i. You can rearrange these children just one time in any order you like. When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child...
[ { "input": { "stdin": "4\n1 3 4 2" }, "output": { "stdout": "20" } }, { "input": { "stdin": "6\n8 6 9 1 2 1" }, "output": { "stdout": "85" } }, { "input": { "stdin": "6\n5 5 6 1 1 1" }, "output": { "stdout": "58" } }, { ...
{ "tags": [], "title": "p02709 AtCoder Beginner Contest 163 - Active Infants" }
{ "cpp": "#include <bits/stdc++.h>\n#define int long long\n#define f first\n#define s second\nusing namespace std;\nconst int N=2009,inf=1e18;\npair <int,int> ar[N];\nint n,dp[N][N];\nint f(int p,int x){\n if(p<x or x<0)return -inf;\n if(p==0)return 0;\n if(dp[p][x]!=-1)return dp[p][x];\n int r=max(ar[p]....
p02838 AtCoder Beginner Contest 147 - Xor Sum 4
We have N integers. The i-th integer is A_i. Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7). What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if eith...
[ { "input": { "stdin": "3\n1 2 3" }, "output": { "stdout": "6" } }, { "input": { "stdin": "10\n3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820" }, "output": { "stdout": "103715602" } }, { "input": { "stdin": "10\n3 1 4 1 5 9 2...
{ "tags": [], "title": "p02838 AtCoder Beginner Contest 147 - Xor Sum 4" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll MOD = 1e9+7;\n\nint main(){\n\tll n; cin >> n;\n\tvector<ll> a(n);\n\tfor(int i = 0; i < n; i++) cin >> a[i];\n\tll ans = 0;\n\tfor(ll digit = 0; digit <= 60; digit++){\n\t\tll c0 = 0, c1 = 0;\n\t\tfor(int i = 0; i < n; i++){\n\...
p02975 AtCoder Grand Contest 035 - XOR Circle
Snuke has N hats. The i-th hat has an integer a_i written on it. There are N camels standing in a circle. Snuke will put one of his hats on each of these camels. If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print `Yes`; otherwise, print `No...
[ { "input": { "stdin": "3\n1 2 3" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "4\n1 2 4 8" }, "output": { "stdout": "No" } }, { "input": { "stdin": "4\n0 -1 0 2" }, "output": { "stdout": "No\n" } }, { "i...
{ "tags": [], "title": "p02975 AtCoder Grand Contest 035 - XOR Circle" }
{ "cpp": "#include <iostream>\nusing namespace std;\n\nint main(){\n int N;\n cin >> N;\n int a, n=0;\n for(int i=0; i<N; i++){\n cin >> a; n^=a;\n }\n cout << ((n==0) ? \"Yes\" : \"No\") << endl;\n}", "java": "import java.util.*;\n\npublic class Main {\n\n public static void main(String[] args) {\n\n ...
p03111 AtCoder Beginner Contest 119 - Synthetic Kadomatsu
You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively. Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number: * Extension Magic: Consumes 1 MP (magic point)....
[ { "input": { "stdin": "8 1000 800 100\n300\n333\n400\n444\n500\n555\n600\n666" }, "output": { "stdout": "243" } }, { "input": { "stdin": "5 100 90 80\n98\n40\n30\n21\n80" }, "output": { "stdout": "23" } }, { "input": { "stdin": "8 100 90 80\n...
{ "tags": [], "title": "p03111 AtCoder Beginner Contest 119 - Synthetic Kadomatsu" }
{ "cpp": "#include <iostream>\n#include <stack>\n#include <cstdio>\n#include <queue>\n#include <algorithm>\n#include <numeric>\n#include <vector>\nusing namespace std;\n#define endl \"\\n\"\ntypedef long long Int;\nInt N,A,B,C,l[8];\n\nInt dfs(Int cur,Int a,Int b,Int c){\n if(cur==N){\n if(min({a,b,c})>0)re...
p03258 AtCoder Grand Contest 027 - ABBreviate
There is a string s consisting of `a` and `b`. Snuke can perform the following two kinds of operation any number of times in any order: * Choose an occurrence of `aa` as a substring, and replace it with `b`. * Choose an occurrence of `bb` as a substring, and replace it with `a`. How many strings s can be obtained b...
[ { "input": { "stdin": "babbabaaba" }, "output": { "stdout": "35" } }, { "input": { "stdin": "ababababa" }, "output": { "stdout": "1" } }, { "input": { "stdin": "aaaa" }, "output": { "stdout": "6" } }, { "input": { ...
{ "tags": [], "title": "p03258 AtCoder Grand Contest 027 - ABBreviate" }
{ "cpp": "#include<iostream>\n#include<cstring>\n#include<cstdio>\n#include<cmath>\nusing namespace std;\ntypedef long long LL;\nconst int N = 1e5+10 , mod = 1e9+7;\ninline int read()\n{\n\tregister int x = 0 , f = 0; register char c = getchar();\n\twhile(c < '0' || c > '9') f |= c == '-' , c = getchar();\n\twhile(c ...
p03414 AtCoder Regular Contest 092 - Two Faced Edges
You are given a directed graph with N vertices and M edges. The vertices are numbered 1, 2, ..., N, and the edges are numbered 1, 2, ..., M. Edge i points from Vertex a_i to Vertex b_i. For each edge, determine whether the reversion of that edge would change the number of the strongly connected components in the graph...
[ { "input": { "stdin": "2 2\n1 2\n2 1" }, "output": { "stdout": "diff\ndiff" } }, { "input": { "stdin": "5 9\n3 2\n3 1\n4 1\n4 2\n3 5\n5 3\n3 4\n1 2\n2 5" }, "output": { "stdout": "same\nsame\nsame\nsame\nsame\ndiff\ndiff\ndiff\ndiff" } }, { "input"...
{ "tags": [], "title": "p03414 AtCoder Regular Contest 092 - Two Faced Edges" }
{ "cpp": "#include<cmath>\n#include<cstdio>\n#include<cstring>\n#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<bitset>\nusing namespace std;\nconst int MAXN=1005,MAXM=200005;\nint n,m;\nint ex[MAXM],ey[MAXM];\nvector<int> ver[MAXN];\nbool ok[MAXN][MAXN];\nvoid dfs(int u,bool tag[]) {\n\ttag[u]=1;...
p03574 AtCoder Beginner Contest 075 - Minesweeper
You are given an H × W grid. The squares in the grid are described by H strings, S_1,...,S_H. The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W). `.` stands for an empty square, and `#` stands for a square containin...
[ { "input": { "stdin": "3 5\n.....\n.#.#.\n....." }, "output": { "stdout": "11211\n1#2#1\n11211" } }, { "input": { "stdin": "3 5" }, "output": { "stdout": "" } }, { "input": { "stdin": "6 6\n.\n.#.##\n.#\n.#..#.\n.##..\n.#..." }, "outp...
{ "tags": [], "title": "p03574 AtCoder Beginner Contest 075 - Minesweeper" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nconst int dr[]={-1,-1,-1,0,0,1,1,1};\nconst int dc[]={-1,0,1,-1,1,-1,0,1};\nint h,w;\nchar m[55][55];\nint count(int x,int y){\n\tint sum=0,r,c;\n\tfor(int i=0;i<8;i++){\n\t\tr=x+dr[i];\n\t\tc=y+dc[i];\n\t\tif(r>=0&&r<h&&c>=0&&c<w&&m[r][c]=='#') sum++;\n\t}\n\t...
p03729 AtCoder Beginner Contest 060 - Shiritori
You are given three strings A, B and C. Check whether they form a word chain. More formally, determine whether both of the following are true: * The last character in A and the initial character in B are the same. * The last character in B and the initial character in C are the same. If both are true, print `YES`....
[ { "input": { "stdin": "a a a" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "rng gorilla apple" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "aaaaaaaaab aaaaaaaaaa aaaaaaaaab" }, "output": { "stdout": "N...
{ "tags": [], "title": "p03729 AtCoder Beginner Contest 060 - Shiritori" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n string a, b, c;\n cin >> a >> b >> c;\n cout << ((a[a.size()-1]==b[0] && b[b.size()-1]==c[0])?\"YES\":\"NO\") << endl;\n}\n", "java": "\nimport java.util.Scanner;\npublic class Main {\n\n\tpublic static void main(String[] args) {\n\t\t// ...
p03893 CODE FESTIVAL 2016 Relay (Parallel) - Trichotomy
We have a cord whose length is a positive integer. We will perform the following condition until the length of the cord becomes at most 2: * Operation: Cut the rope at two positions to obtain three cords, each with a length of a positive integer. Among these, discard one with the longest length and one with the shorte...
[ { "input": { "stdin": "2" }, "output": { "stdout": "14" } }, { "input": { "stdin": "52" }, "output": { "stdout": "18014398509481982\n" } }, { "input": { "stdin": "46" }, "output": { "stdout": "281474976710654\n" } }, { ...
{ "tags": [], "title": "p03893 CODE FESTIVAL 2016 Relay (Parallel) - Trichotomy" }
{ "cpp": "#include<bits/stdc++.h> \nusing namespace std; \nint a[100+1][100+1],dp[100+1][100+1];\nint main(){\n int n;\n long long f[100];\n f[0]=2;\n cin>>n;\n for(int i=1;i<=n;i++){\n f[i]=f[i-1]*2+2;\n }\n cout<<f[n]<<endl;\n return 0;\n}", "java": "import java.io.IOException; \ni...
p04052 AtCoder Grand Contest 001 - Wide Swap
You are given a permutation P_1 ... P_N of the set {1, 2, ..., N}. You can apply the following operation to this permutation, any number of times (possibly zero): * Choose two indices i,j (1 ≦ i < j ≦ N), such that j - i ≧ K and |P_i - P_j| = 1. Then, swap the values of P_i and P_j. Among all permutations that can...
[ { "input": { "stdin": "8 3\n4 5 7 8 3 1 2 6" }, "output": { "stdout": "1\n2\n6\n7\n5\n3\n4\n8" } }, { "input": { "stdin": "5 1\n5 4 3 2 1" }, "output": { "stdout": "1\n2\n3\n4\n5" } }, { "input": { "stdin": "4 2\n4 2 3 1" }, "output":...
{ "tags": [], "title": "p04052 AtCoder Grand Contest 001 - Wide Swap" }
{ "cpp": "#include<bits/stdc++.h>\n#define N 505000\nusing namespace std;\n\nint n,k,a[N],L[N],R[N],mn[N],an[N],pos[N],sz=500,tot,ans[N];\nint now,res,p;\n\nvoid RE(int w){\n\tnow=res=1e8;\n\tfor (int i=L[w];i<=R[w];++i){\n\t\tif (now-a[i]>=k) res=a[i],p=i;\n\t\tnow=min(now,a[i]);\n\t}\n\tmn[w]=now, an[w]=res, pos[w]...
AIZU/p00131
# Doctor's Strange Particles Dr .: Peter. I did. Peter: See you again? What kind of silly invention is this time? Dr .: You invented the detector for that phantom elementary particle axion. Peter: Speaking of Axion, researchers such as the European Organization for Nuclear Research (CERN) are chasing with a bloody ...
[ { "input": { "stdin": "1\n0 1 0 0 0 0 0 0 0 0\n1 1 1 0 0 0 0 0 0 0\n0 1 0 0 0 0 0 0 0 0\n0 0 0 0 1 1 0 0 0 0\n0 0 0 1 0 0 1 0 0 0\n0 0 0 0 1 1 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 1 0\n0 0 0 0 0 0 0 1 1 1\n0 0 0 0 0 0 0 0 1 0" }, "output": { "stdout": "0 0 0 0 0 0 0 0 0 0\n0 1 0 0 0...
{ "tags": [], "title": "Doctor's Strange Particles" }
{ "cpp": "#include <bits/stdc++.h>\n \nusing namespace std;\n \nconst int dx[5] = {-1, 0, 0, 0, 1};\nconst int dy[5] = {0, -1, 0, 1, 0};\n \nint arr[10][10];\nint ans[10][10], flip[10][10];\n \nint get(int x, int y)\n{\n int c = arr[x][y];\n for (int d = 0; d < 5; d++) {\n\tint x2 = x + dx[d], y2 = y + dy[d];\n...
AIZU/p00264
# East Wind I decided to move and decided to leave this place. There is nothing wrong with this land itself, but there is only one thing to worry about. It's a plum tree planted in the garden. I was looking forward to this plum blooming every year. After leaving here, the fun of spring will be reduced by one. Wouldn't...
[ { "input": { "stdin": "6 3\n2 1\n1 2\n5 2\n1 3\n1 5\n-2 3\n1 1 1 90 30 45\n3 -4\n-3 0\n2 -2\n45 6\n90 6\n135 6\n2 1\n1 3\n5 2\n0 1 1 90 30 45\n-3 0\n2 -2\n45 6\n0 0" }, "output": { "stdout": "5 6\nNA" } }, { "input": { "stdin": "6 3\n2 1\n1 2\n5 2\n1 2\n1 5\n-2 3\n1 1 1 90 ...
{ "tags": [], "title": "East Wind" }
{ "cpp": "#include <iostream>\n#include <cstdio>\n#include <iomanip>\n#include <vector>\n#include <map>\n#include <set>\n#include <queue>\n#include <bitset>\n#include <stack>\n#include <utility>\n#include <numeric>\n#include <algorithm>\n#include <functional>\n#include <cctype>\n#include <complex>\n#include <string>\...
AIZU/p00451
# Common Sub-String problem Given two strings, find the longest of the strings contained in both strings and write a program that answers that length. Here, the string s included in the string t means that s appears consecutively in t. An empty string, that is, a string of length 0, is included in any string. For ex...
[ { "input": { "stdin": "None" }, "output": { "stdout": "None" } }, { "input": { "stdin": "ABRACADABRA\nECADADABRBCRDARA\nUPWJCIRUCAXIIRGL\nSBQNYBSBZDFNEV" }, "output": { "stdout": "5\n0" } }, { "input": { "stdin": "ABRACADAARA\nECADADABRBCRDAR...
{ "tags": [], "title": "Common Sub-String" }
{ "cpp": "#include <cstdio>\n#include <cstring>\n#include <algorithm>\nusing namespace std;\nchar str1[4001], str2[4001];\nint main() {\n while (scanf(\"%s%s\",&str1,&str2)!=EOF) {\n int res=0;\n for (int off=-1*(int)strlen(str1); off<=(int)strlen(str2); off++) {\n int len=0;\n ...
AIZU/p00642
# Ben Toh As usual, those who called wolves get together on 8 p.m. at the supermarket. The thing they want is only one, a box lunch that is labeled half price. Scrambling for a few discounted box lunch, they fiercely fight every day. And those who are blessed by hunger and appetite the best can acquire the box lunch, ...
[ { "input": { "stdin": "1\n2\n3\n0" }, "output": { "stdout": "1.00000000\n1.50000000\n2.12500000" } }, { "input": { "stdin": "1\n0\n1\n1" }, "output": { "stdout": "1.000000000000\n" } }, { "input": { "stdin": "1\n2\n0\n-1" }, "output":...
{ "tags": [], "title": "Ben Toh" }
{ "cpp": "#include<stdio.h>\ndouble dp[2][100];\nint main(){\n\twhile(1){\n\tint n;\n\tscanf(\"%d\",&n);\n\tif(n==0)return 0;\n\tint i,j;\n\tdouble ans=0.0f;\n\tfor(j=0;j<100;j++)dp[0][j]=0.0f;\n\tdp[0][1]=1.0f;\n\tfor(i=0;i<n-1;i++){\n\t\tfor(j=0;j<100;j++)dp[(i+1)%2][j]=0.0f;\n\t\tdp[(i+1)%2][1]+=dp[i%2][0];\n\t\tf...
AIZU/p00786
# BUT We Need a Diagram Consider a data structure called BUT (Binary and/or Unary Tree). A BUT is defined inductively as follows: * Let l be a letter of the English alphabet, either lowercase or uppercase (n the sequel, we say simply "a letter"). Then, the object that consists only of l, designating l as its label, i...
[ { "input": { "stdin": "a(A,b(B,C));\nx( y( y( z(z), v( s, t ) ) ), u ) ;\n\na( b( c,\n d(\n e(f),\n g\n )\n ),\n h( i(\n j(\n k(k,k),\n l(l)\n ),\n m(m)\n )\n )\n );\n\na(B(C),d(e(f(g(h(i(j,k),l),m),n),o),p))\n." }...
{ "tags": [], "title": "BUT We Need a Diagram" }
{ "cpp": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#include <map>\n#include <valarray>\n#include <bitset>\n#include <stack>\n#i...
AIZU/p00918
# Dragon's Cruller Dragon's Cruller is a sliding puzzle on a torus. The torus surface is partitioned into nine squares as shown in its development in Figure E.1. Here, two squares with one of the sides on the development labeled with the same letter are adjacent to each other, actually sharing the side. Figure E.2 sho...
[ { "input": { "stdin": "4 9\n6 3 0\n8 1 2\n4 5 7\n6 3 0\n8 1 2\n4 5 7\n31 31\n4 3 6\n0 1 5\n8 2 7\n0 3 6\n4 1 5\n8 2 7\n92 4\n1 5 3\n4 0 7\n8 2 6\n1 5 0\n4 7 3\n8 2 6\n12 28\n3 4 5\n0 2 6\n7 1 8\n5 7 1\n8 6 2\n0 3 4\n0 0" }, "output": { "stdout": "0\n31\n96\n312" } }, { "input": {...
{ "tags": [], "title": "Dragon's Cruller" }
{ "cpp": "/*\n * Author: Gatevin\n * Created Time: 2015/10/31 13:46:57\n * File Name: Sakura_Chiyo.cpp\n */\n#include<iostream>\n#include<sstream>\n#include<fstream>\n#include<vector>\n#include<list>\n#include<deque>\n#include<queue>\n#include<stack>\n#include<map>\n#include<set>\n#include<bitset>\n#include<algorith...
AIZU/p01051
# Squid Ink Problem In recent years, turf wars have frequently occurred among squids. It is a recent squid fighting style that multiple squids form a team and fight with their own squid ink as a weapon. There is still a turf war, and the battlefield is represented by an R × C grid. The squid Gesota, who is participa...
[ { "input": { "stdin": "4 5\nS#...\n.#.#.\n.#.#.\n...#G" }, "output": { "stdout": "23" } }, { "input": { "stdin": "5 5\nSxxxx\nxxxxx\nxxxxx\nxxxxx\nxxxxG" }, "output": { "stdout": "15" } }, { "input": { "stdin": "4 5\nS#ooo\no#o#o\no#o#o\nooo#...
{ "tags": [], "title": "Squid Ink" }
{ "cpp": "#include <iostream>\n#include <string>\n#include <queue>\n#define N 30\nusing namespace std;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\nint dijkstra();\nint R,C,sy,sx,gy,gx,d[N][N];\nstring s[N];\n\nint main(){\n cin>>R>>C;\n for(int i=0;i<R;i++) cin>>s[i];\n for(int i=0;i<R;i++)\n for(int j=...
AIZU/p01183
# Tetrahedra Peter P. Pepper is facing a difficulty. After fierce battles with the Principality of Croode, the Aaronbarc Kingdom, for which he serves, had the last laugh in the end. Peter had done great service in the war, and the king decided to give him a big reward. But alas, the mean king gave him a hard question...
[ { "input": { "stdin": "7 1 2 2 2 2 2 2\n0" }, "output": { "stdout": "0.942809" } }, { "input": { "stdin": "7 2 4 2 2 2 1 1\n0" }, "output": { "stdout": "0.311804782\n" } }, { "input": { "stdin": "7 1 2 2 2 3 2 0\n0" }, "output": { ...
{ "tags": [], "title": "Tetrahedra" }
{ "cpp": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#inclu...
AIZU/p01320
# Magical Island 2 It was an era when magic still existed as a matter of course. A clan of magicians lived on a square-shaped island created by magic. At one point, a crisis came to this island. The empire has developed an intercontinental ballistic missile and aimed it at this island. The magic of this world can be ...
[ { "input": { "stdin": "4 4 1\n1 0\n0 1\n-1 0\n0 -1\n5 6 2\n1 1\n4 4\n2 -4\n-4 2\n1 5\n0 0 0" }, "output": { "stdout": "1.000000000\n6.797434948" } }, { "input": { "stdin": "4 4 1\n0 0\n0 1\n-1 -1\n0 -1\n5 6 2\n1 1\n4 8\n2 -4\n-4 2\n1 5\n0 -1 0" }, "output": { ...
{ "tags": [], "title": "Magical Island 2" }
{ "cpp": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include...
AIZU/p01488
# TransferTrain Example Input 2 10 Warsaw Petersburg 3 Kiev Moscow Petersburg 150 120 3 Moscow Minsk Warsaw 100 150 Output 380 1
[ { "input": { "stdin": "2 10\nWarsaw Petersburg\n3\nKiev Moscow Petersburg\n150 120\n3\nMoscow Minsk Warsaw\n100 150" }, "output": { "stdout": "380 1" } }, { "input": { "stdin": "1 10\nWarsaw setgrPbtre\n3\nKiev cosMow Petersburg\n66 3\n6\nMoscow ksnjM Warsaw\n100 14" },...
{ "tags": [], "title": "TransferTrain" }
{ "cpp": "// g++ -std=c++11 a.cpp\n#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<unordered_map>\n#include<utility>\n#include<cmath>\n#include<random>\n#include<cstring>\n#include<queue>\n#include<stack>\n#include<bitset>\n#include<cstdio>\n#inclu...
AIZU/p01650
# Stack Maze Problem Statement There is a maze which can be described as a W \times H grid. The upper-left cell is denoted as (1, 1), and the lower-right cell is (W, H). You are now at the cell (1, 1) and have to go to the cell (W, H). However, you can only move to the right adjacent cell or to the lower adjacent cel...
[ { "input": { "stdin": "3 3\nac#\nb#C\n.BA\n3 3\naaZ\na#Z\naZZ\n3 3\n..#\n.#.\n#..\n1 50\nabcdefghijklmnopqrstuvwxyYXWVUTSRQPONMLKJIHGFEDCBA\n1 50\naAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY\n1 50\nabcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY\n1 50\naaaaaaaaaabbbbbbbbbbcccccCCCCCBBBBBBBBBBAAA...
{ "tags": [], "title": "Stack Maze" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = int;\nusing PII = pair<ll, ll>;\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) x.begin(), x.end()\ntemplate<typename T> void chmin(T &a, const T &b) { a = min(a, b); }\ntemplate<typename T> v...
AIZU/p01801
# Wall Making Game Example Input 2 2 .. .. Output Second
[ { "input": { "stdin": "2 2\n..\n.." }, "output": { "stdout": "Second" } }, { "input": { "stdin": "2 -2\n//\n./" }, "output": { "stdout": "Second\n" } }, { "input": { "stdin": "3 0\n/0\n.." }, "output": { "stdout": "Second\n" ...
{ "tags": [], "title": "Wall Making Game" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i,a,b) for(int i=a;i<(int)b;i++)\n#define rep(i,n) REP(i,0,n)\n#define all(c) (c).begin(), (c).end()\n#define zero(a) memset(a, 0, sizeof a)\n#define minus(a) memset(a, -1, sizeof a)\n#define watch(a) { cout << #a << \" = \" << a << endl; }\nte...
AIZU/p01935
# Protect from the enemy attack problem There are $ V $ islands, numbered $ 0, 1, ..., V-1 $, respectively. There are $ E $ bridges, numbered $ 0, 1, ..., E-1 $, respectively. The $ i $ th bridge spans island $ s_i $ and island $ t_i $ and is $ c_i $ wide. The AOR Ika-chan Corps (commonly known as the Squid Corps), ...
[ { "input": { "stdin": "4 4\n0 1 3\n0 2 4\n1 3 1\n2 3 5" }, "output": { "stdout": "4" } }, { "input": { "stdin": "5 8\n1 2 6\n1 0 4\n2 0 4\n1 4 5" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "2 15\n0 1 0\n0 1 4\n2 3 0\n0 3 9" ...
{ "tags": [], "title": "Protect from the enemy attack" }
{ "cpp": "#include<iostream>\n#include<cmath>\n#include<queue>\n#include<cstdio>\n#include<cstdlib>\n#include<cassert>\n#include<cstring>\n#include<climits>\n#include<sstream>\n#include<deque>\n#include<vector>\n#include<algorithm>\n#include<set>\n#include<map>\n#include<bitset>\n#define REP(i,s,n) for(int i=s;i<n;i+...
AIZU/p02074
# N-by-M grid calculation Problem Statement Have you experienced $10$-by-$10$ grid calculation? It's a mathematical exercise common in Japan. In this problem, we consider the generalization of the exercise, $N$-by-$M$ grid calculation. In this exercise, you are given an $N$-by-$M$ grid (i.e. a grid with $N$ rows and...
[ { "input": { "stdin": "" }, "output": { "stdout": "" } } ]
{ "tags": [], "title": "N-by-M grid calculation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define eps 1e-7\n#define INF 1000000000\n//#define mod 998244353\n#define fi first\n#define sc second...
AIZU/p02216
# Array Game Problem statement There is a positive integer sequence $ a_1, a_2, \ ldots, a_N $ of length $ N $. Consider the following game, which uses this sequence and is played by $ 2 $ players on the play and the play. * Alternately select one of the following operations for the first move and the second move. ...
[ { "input": { "stdin": "2\n1 2" }, "output": { "stdout": "First" } }, { "input": { "stdin": "2\n4 -2" }, "output": { "stdout": "Second\n" } }, { "input": { "stdin": "2\n19 0" }, "output": { "stdout": "First\n" } }, { ...
{ "tags": [], "title": "Array Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n\tint N;\n\tcin >> N;\n\tvector<int> A(N);\n\tfor (auto& x : A) cin >> x;\n\tif (N % 2 == 1) {\n\t\tlong long sum = 0;\n\t\tfor (auto x : A) sum += x;\n\t\tcout << (sum % 2 == 1 ? \"First\" : \"Second\") << endl;\n\t\treturn 0;\n\t}\n\tauto m = *...
AIZU/p02369
# Cycle Detection for a Directed Graph Find a cycle in a directed graph G(V, E). Constraints * 1 ≤ |V| ≤ 100 * 0 ≤ |E| ≤ 1,000 * si ≠ ti Input A directed graph G is given in the following format: |V| |E| s0 t0 s1 t1 : s|E|-1 t|E|-1 |V| is the number of nodes and |E| is the number of edges in the graph. The gra...
[ { "input": { "stdin": "3 3\n0 1\n0 2\n1 2" }, "output": { "stdout": "0" } }, { "input": { "stdin": "3 3\n0 1\n1 2\n2 0" }, "output": { "stdout": "1" } }, { "input": { "stdin": "8 0\n0 2\n2 1\n2 0" }, "output": { "stdout": "0\n" ...
{ "tags": [], "title": "Cycle Detection for a Directed Graph" }
{ "cpp": "#include<bits/stdc++.h>\n#define range(i,a,b) for(int i = (a); i < (b); i++)\n#define rep(i,b) for(int i = 0; i < (b); i++)\n#define all(a) (a).begin(), (a).end()\n#define show(x) cerr << #x << \" = \" << (x) << endl;\n#define debug(x) cerr << #x << \" = \" << (x) << \" (L\" << __LINE__ << \")\" << \" \" <...
CodeChef/aba15f
Problem description. “Murphy’s Law doesn’t meant that something bad will happen. It means that whatever can happen, will happen.” —Cooper While traveling across space-time,the data sent by NASA to "The Endurance" spaceship is sent in the format of, For...
[ { "input": { "stdin": "5\n100\n23\n4\n1000\n5" }, "output": { "stdout": "68\n21\n4\n320\n5" } } ]
{ "tags": [], "title": "aba15f" }
{ "cpp": null, "java": null, "python": "def toBin(n):\n\tbinary = []\n\twhile n!=0:\n\t\tbit = str(n%2)\n\t\tbinary.append(bit)\n\t\tn = n/2\n\tbinary.reverse()\n\treturn binary\n\n\ndef main():\n\ttc = int(raw_input())\n\twhile tc>0:\n\t\tn = int(raw_input())\n\t\tbinary = toBin(n)\n \"\"\"for x i...
CodeChef/chefch
Chef had a hard day and want to play little bit. The game is called "Chain". Chef has the sequence of symbols. Each symbol is either '-' or '+'. The sequence is called Chain if each two neighboring symbols of sequence are either '-+' or '+-'. For example sequence '-+-+-+' is a Chain but sequence '-+-+--+' is not. H...
[ { "input": { "stdin": "2\n---+-+-+++\n-------" }, "output": { "stdout": "2\n3\n" } }, { "input": { "stdin": "2\n----+++++-\n-------" }, "output": { "stdout": "4\n3\n" } }, { "input": { "stdin": "2\n+-+-+-+-+-\n-------" }, "output": { ...
{ "tags": [], "title": "chefch" }
{ "cpp": null, "java": null, "python": "t=int(raw_input())\nfor i in xrange(t):\n a=raw_input()\n count1=0\n count2=0\n for j in xrange(len(a)):\n if j%2==0:\n if a[j]=='-':\n count2+=1\n else:\n count1+=1\n else:\n if a[j]==...
CodeChef/directi
Chef recently printed directions from his home to a hot new restaurant across the town, but forgot to print the directions to get back home. Help Chef to transform the directions to get home from the restaurant. A set of directions consists of several instructions. The first instruction is of the form "Begin on XXX", i...
[ { "input": { "stdin": "2\n4\nBegin on Road A\nRight on Road B\nRight on Road C\nLeft on Road D\n6\nBegin on Old Madras Road\nLeft on Domlur Flyover\nLeft on 100 Feet Road\nRight on Sarjapur Road\nRight on Hosur Road\nRight on Ganapathi Temple Road" }, "output": { "stdout": "Begin on Road D\n...
{ "tags": [], "title": "directi" }
{ "cpp": null, "java": null, "python": "t=input()\nwhile(t>0):\n\tt-=1\n\tn=input()\n\ta=[0]*n;c=[0]*n;i=0\n\twhile(i<n):\n\t\tb=raw_input().split()\n\t\ta[i]=b[0];c[i]=b[2:]\n\t\ti+=1\n\tprint \"Begin on\",\n\tfor j in c[n-1]:\n\t\tprint j,\n\tprint\n\ti=n-2\n\twhile(i>=0):\n\t\tif(a[i+1]=='Left'):\n\t\t\tprint ...
CodeChef/insomb1
Modern cryptosystems rely heavily on our inability to factor large integers quickly as the basis for their security. They need a quick and easy way to generate and test for primes. Very often, the primes generated are very large numbers. You need to implement ways to test the primality of very large numbers. Input Lin...
[ { "input": { "stdin": "2760727302517" }, "output": { "stdout": "PRIME\n\n" } }, { "input": { "stdin": "2012712727145" }, "output": { "stdout": "COMPOSITE\n" } }, { "input": { "stdin": "1067330004295" }, "output": { "stdout": "CO...
{ "tags": [], "title": "insomb1" }
{ "cpp": null, "java": null, "python": "\"\"\" AUTHOR : PVKCSE\n STUDENTAT : ACCET,KARAIKUDI\n TASK : PRIMALITY TEST-CODECHEF PEER \"\"\"\n\nimport sys,math\n\nPrime_Nos=[]\n\ndef Gen_Primes() :\n for i in range(2,1032) :\n count=0\n for j in range(1,i) :\n ...
CodeChef/nfeb4
Buffalo Marketing Gopal wants to make some money from buffalos, but in a quite a different way. He decided that his future lay in speculating on buffalos. In the market in his village, buffalos were bought and sold everyday. The price fluctuated over the year, but on any single day the price was always the same. ...
[ { "input": { "stdin": "2\n3\n1 2 90\n4\n1 3 1 2" }, "output": { "stdout": "177\n 3" } } ]
{ "tags": [], "title": "nfeb4" }
{ "cpp": null, "java": null, "python": "t=int(input())\nwhile(t>0):\n\tn=int(input())\n\ta=map(int,raw_input().split(\" \"))\n\ti=n-1\n\tm=a[i]\n\tans=0\n\ti=n-2\n\twhile(i>=0):\n\t\tif(m>a[i]):\n\t\t\tans+=m-a[i]\n\t\telif(m<a[i]):\n\t\t\tm=a[i]\n\t\ti-=1\n\tprint ans\n\t\t\t\n\tt-=1" }
CodeChef/scores
Mrityunjay is a high-school teacher, currently coaching students for JEE (Joint Entrance Exam). Mrityunjay every year gives prizes to the toppers of JEE in India. A lot of students give this exam in India and it is difficult for him to manually go through results and select two top students from them. So he asked for y...
[ { "input": { "stdin": "3 \n30 30 20" }, "output": { "stdout": "30 30" } }, { "input": { "stdin": "3 \n12 20 12" }, "output": { "stdout": "20 12" } }, { "input": { "stdin": "3 \n-1 2 50" }, "output": { "stdout": "50 2\n" } ...
{ "tags": [], "title": "scores" }
{ "cpp": null, "java": null, "python": "N = int(raw_input())\narr = map(int,raw_input().strip().split(' '))\nc = []\nfor i in range(2):\n\tm = max(arr)\n\tc.append(m)\n\tarr.remove(m)\nprint ' '.join(str(t) for t in c)" }
Codeforces/1000/C
# Covered Points Count You are given n segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide. Your task is the following: for every k ∈ [1..n], calculate the number...
[ { "input": { "stdin": "3\n0 3\n1 3\n3 8\n" }, "output": { "stdout": "6 2 1\n" } }, { "input": { "stdin": "3\n1 3\n2 4\n5 7\n" }, "output": { "stdout": "5 2 0\n" } }, { "input": { "stdin": "1\n0 1000000000000000000\n" }, "output": { ...
{ "tags": [ "data structures", "implementation", "sortings" ], "title": "Covered Points Count" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 2e5 + 10;\nconst int p = 1;\nint cnt, num, n;\nlong long ans[maxn];\nnamespace Tzh {\nstruct node {\n long long pos;\n int type;\n} g[maxn << 1];\nbool cm(node a, node b) { return a.pos < b.pos; }\nlong long qpow(long long a, long long b) {\...
Codeforces/1025/D
# Recovering BST Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time...
[ { "input": { "stdin": "9\n4 8 10 12 15 18 33 44 81\n" }, "output": { "stdout": "Yes\n" } }, { "input": { "stdin": "6\n3 6 9 18 36 108\n" }, "output": { "stdout": "Yes\n" } }, { "input": { "stdin": "2\n7 17\n" }, "output": { "std...
{ "tags": [ "brute force", "dp", "math", "number theory", "trees" ], "title": "Recovering BST" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, ok[777][777], a[777];\nint l[777][777], r[777][777];\nint gcd(int a, int b) { return b ? gcd(b, a % b) : a; }\nint main() {\n memset(ok, 0, sizeof(ok));\n cin >> n;\n for (int i = 1; i <= n; i++) {\n cin >> a[i];\n l[i][i] = r[i][i] = 1;\n }\n...
Codeforces/1045/F
# Shady Lady Ani and Borna are playing a short game on a two-variable polynomial. It's a special kind of a polynomial: the monomials are fixed, but all of its coefficients are fill-in-the-blanks dashes, e.g. $$$ \\_ xy + \\_ x^4 y^7 + \\_ x^8 y^3 + … $$$ Borna will fill in the blanks with positive integers. He wants ...
[ { "input": { "stdin": "3\n1 1\n2 0\n0 2\n" }, "output": { "stdout": "Ani\n" } }, { "input": { "stdin": "4\n0 0\n0 1\n0 2\n0 8\n" }, "output": { "stdout": "Borna\n" } }, { "input": { "stdin": "2\n2 0\n0 0\n" }, "output": { "stdou...
{ "tags": [ "geometry", "math" ], "title": "Shady Lady" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 2e5 + 5;\ntemplate <typename T>\nvoid chkmax(T &x, T y) {\n x = max(x, y);\n}\ntemplate <typename T>\nvoid chkmin(T &x, T y) {\n x = min(x, y);\n}\ntemplate <typename T>\nvoid read(T &x) {\n x = 0;\n int f = 1;\n char c = getchar();\n fo...
Codeforces/1068/F
# Knights Ivan places knights on infinite chessboard. Initially there are n knights. If there is free cell which is under attack of at least 4 knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that positio...
[ { "input": { "stdin": "7\n" }, "output": { "stdout": "0 0\n1 0\n1 3\n2 0\n3 0\n3 3\n4 0\n" } }, { "input": { "stdin": "4\n" }, "output": { "stdout": "0 0\n1 0\n1 3\n2 0\n" } }, { "input": { "stdin": "523\n" }, "output": { "stdou...
{ "tags": [ "constructive algorithms" ], "title": "Knights" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int dell[8][2] = {{1, 2}, {1, -2}, {2, 1}, {2, -1},\n {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};\nlong long mod = 998244353;\nconst long long inf = (1LL << 31) - 1;\nconst int maxn = 1e5 + 7;\nconst int maxm = 1e6 + 7;\nconst double...
Codeforces/1090/J
# Two Prefixes Misha didn't do his math homework for today's lesson once again. As a punishment, his teacher Dr. Andrew decided to give him a hard, but very useless task. Dr. Andrew has written two strings s and t of lowercase English letters at the blackboard. He reminded Misha that prefix of a string is a string fo...
[ { "input": { "stdin": "aaaaa\naaaa\n" }, "output": { "stdout": " 8\n" } }, { "input": { "stdin": "aba\naa\n" }, "output": { "stdout": " ...
{ "tags": [ "strings" ], "title": "Two Prefixes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\ninline int size(const T& t) {\n return t.size();\n}\nnamespace sam {\nconst int maxN = 200010;\nconst int blank = 0;\nconst int null = -1;\narray<int, 26> go[maxN];\nint len[maxN], cnt[maxN];\nint idx[maxN], fail[maxN];\nint n, m;\nint all...
Codeforces/110/B
# Lucky String Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya recently learned to determine whether a string of lowercase Latin letters is l...
[ { "input": { "stdin": "3\n" }, "output": { "stdout": "abc" } }, { "input": { "stdin": "5\n" }, "output": { "stdout": "abcda" } }, { "input": { "stdin": "1\n" }, "output": { "stdout": "a" } }, { "input": { "stdi...
{ "tags": [ "constructive algorithms", "strings" ], "title": "Lucky String" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int t;\n t = 1;\n while (t--) {\n int n;\n cin >> n;\n string s = \"\";\n for (int i = 1; i <= n / 4; i++) s = s + \"abcd\";\n if (n % 4 == 1) s = s + \"a\";\n if (n % 4 == 2) s = s + \"ab\";\n if (n % 4 == 3) s = s + \"a...
Codeforces/1139/E
# Maximize Mex There are n students and m clubs in a college. The clubs are numbered from 1 to m. Each student has a potential p_i and is a member of the club with index c_i. Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next d days. There is a...
[ { "input": { "stdin": "5 5\n0 1 2 4 5\n1 2 3 4 5\n4\n2\n3\n5\n4\n" }, "output": { "stdout": "1\n1\n1\n1\n" } }, { "input": { "stdin": "5 3\n0 1 2 2 0\n1 2 2 3 2\n5\n3\n2\n4\n5\n1\n" }, "output": { "stdout": "3\n1\n1\n1\n0\n" } }, { "input": { ...
{ "tags": [ "flows", "graph matchings", "graphs" ], "title": "Maximize Mex" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read();\nconst int maxn = 5000 + 100;\nint n, m, d, c, cnt;\nint he[maxn], ne[maxn], to[maxn];\nint p[maxn], in[maxn], out[maxn], match[maxn], ans[maxn], vis[maxn];\nbool away[maxn];\nvoid Add_Edge(int x, int y) {\n ne[++c] = he[x];\n he[x] = c;\n...
Codeforces/1157/E
# Minimum Array You are given two arrays a and b, both of length n. All elements of both arrays are from 0 to n-1. You can reorder elements of the array b (if you want, you may leave the order of elements as it is). After that, let array c be the array of length n, the i-th element of this array is c_i = (a_i + b_i) ...
[ { "input": { "stdin": "4\n0 1 2 1\n3 2 1 1\n" }, "output": { "stdout": "1 0 0 2 " } }, { "input": { "stdin": "7\n2 5 1 5 3 4 3\n2 4 3 5 6 5 1\n" }, "output": { "stdout": "0 0 0 1 0 2 4 " } }, { "input": { "stdin": "1\n0\n0\n" }, "outp...
{ "tags": [ "binary search", "data structures", "greedy" ], "title": "Minimum Array" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n map<int, int> mp;\n vector<int> vc, ans;\n for (int i = 0; i < n; i++) {\n int x;\n cin >> x;\n vc.push_back(x);\n }\n for (int i = 0; i < n; i++) {\n int x;\n cin >> x;\n mp[x]++;\n }\n for (int i...
Codeforces/117/B
# Very Interesting Game In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string s1, consisting of exactly nine digits and representing a number that does not exceed a. After that second player looks at s1 and writes a string s2, consisting of exactly n...
[ { "input": { "stdin": "4 0 9\n" }, "output": { "stdout": "1 000000001\n" } }, { "input": { "stdin": "1 10 7\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1 2 11\n" }, "output": { "stdout": "2\n" } }, { ...
{ "tags": [ "brute force", "number theory" ], "title": "Very Interesting Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a, b, m;\nint mul = 1e9;\nint gcd(int a, int b) {\n if (a == 0 || b == 0) {\n return a + b;\n }\n int r = a % b;\n while (r) {\n a = b;\n b = r;\n r = a % b;\n }\n return b;\n}\nint inverse(int a, int b) {\n long long x0 = 1, x1 = 0, y0 ...
Codeforces/1198/C
# Matching vs Independent Set You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with ...
[ { "input": { "stdin": "4\n1 2\n1 3\n1 2\n1 2\n1 3\n1 2\n2 5\n1 2\n3 1\n1 4\n5 1\n1 6\n2 15\n1 2\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n4 5\n4 6\n5 6\n" }, "output": { "stdout": "Matching\n1 \nMatching\n1 \nIndSet\n3 4 \nMatching\n1 10 \n" } }, { "input": { ...
{ "tags": [ "constructive algorithms", "graphs", "greedy", "sortings" ], "title": "Matching vs Independent Set" }
{ "cpp": "#include <bits/stdc++.h>\nconst int N = 500005;\nint n, m, a[N];\nbool vis[N];\nint main() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n scanf(\"%d%d\", &n, &m);\n for (int i = 1; i <= n * 3; i++) vis[i] = 0;\n int top = 0;\n for (int i = 1; i <= m; i++) {\n int x, y;\n scanf(\"...
Codeforces/1215/B
# The Number of Products You are given a sequence a_1, a_2, ..., a_n consisting of n non-zero integers (i.e. a_i ≠ 0). You have to calculate two following values: 1. the number of pairs of indices (l, r) (l ≤ r) such that a_l ⋅ a_{l + 1} ... a_{r - 1} ⋅ a_r is negative; 2. the number of pairs of indices (l, r)...
[ { "input": { "stdin": "10\n4 2 -4 3 1 2 -4 3 2 3\n" }, "output": { "stdout": "28 27\n" } }, { "input": { "stdin": "5\n-1 -2 -3 -4 -5\n" }, "output": { "stdout": "9 6\n" } }, { "input": { "stdin": "5\n5 -3 3 -1 1\n" }, "output": { ...
{ "tags": [ "combinatorics", "dp", "implementation" ], "title": "The Number of Products" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long n;\n cin >> n;\n int arr[n];\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n long long balance = 0, oddcount = 0, evencount = 0;\n long long anspos = 0;\n for (int i = 0; i < n; i++) {\n if (balance % 2 == 0)\n ...
Codeforces/1238/D
# AB-string The string t_1t_2 ... t_k is good if each letter of this string belongs to at least one palindrome of length greater than 1. A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not. Here a...
[ { "input": { "stdin": "5\nAABBB\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "3\nAAA\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "7\nAAABABB\n" }, "output": { "stdout": "15\n" } }, { ...
{ "tags": [ "binary search", "combinatorics", "dp", "strings" ], "title": "AB-string" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL), cout.tie(NULL);\n long long n, i, j, k;\n cin >> n;\n string a;\n cin >> a;\n for (i = 1; i < n; i++) {\n if (a[i - 1] != a[i]) break;\n }\n k = 0;\n for (; i < n - 1; i++) {\n i...
Codeforces/1256/E
# Yet Another Division Into Teams There are n students at your university. The programming skill of the i-th student is a_i. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has 2 ⋅ 10^5 students ready for the finals! Each team...
[ { "input": { "stdin": "5\n1 1 3 4 2\n" }, "output": { "stdout": "3 1\n1 1 1 1 1 " } }, { "input": { "stdin": "6\n1 5 12 13 2 15\n" }, "output": { "stdout": "7 2\n2 2 1 1 2 1\n" } }, { "input": { "stdin": "10\n1 2 5 129 185 581 1041 1909 1580 ...
{ "tags": [ "dp", "greedy", "sortings" ], "title": "Yet Another Division Into Teams" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing LL = long long;\nconst int N = 200005;\nLL f[N];\nint p[N];\nint res[N];\nstruct Node {\n LL val;\n int id;\n bool operator<(const Node& rhs) const {\n if (val == rhs.val) return id < rhs.id;\n return val < rhs.val;\n }\n} a[N];\nint main() {\n...
Codeforces/127/E
# E-reader Display After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided...
[ { "input": { "stdin": "5\n01110\n10010\n10001\n10011\n11110\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "3\n000\n000\n000\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "10\n1111100000\n0010100000\n0110111000\n0...
{ "tags": [ "constructive algorithms", "greedy" ], "title": "E-reader Display" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 2e3 + 50;\nconst int maxm = 1e6 + 50;\nconst double eps = 1e-10;\nconst int max_index = 62;\nconst int inf = 0x3f3f3f3f;\nconst int MOD = 1e9 + 7;\ninline int read() {\n char c = getchar();\n while (!isdigit(c)) c = getchar();\n int x = 0;\...
Codeforces/12/D
# Ball N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, sh...
[ { "input": { "stdin": "3\n1 4 2\n4 3 2\n2 5 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5\n5 4 0 2 5\n8 3 1 0 10\n4 5 0 0 5\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "10\n10 19 4 1 11 6 1 20 11 13\n2 7 1...
{ "tags": [ "data structures", "sortings" ], "title": "Ball" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N(5e5 + 9);\nint T, n, m, ans;\nint c[N];\nmap<int, int> M;\nstruct A {\n int x, y, z;\n} a[N];\nbool cmp1(A &a, A &b) { return a.x > b.x; }\nvoid add(int id, int val) {\n for (int i = id; i > 0; i -= (i & -i)) {\n c[i] = max(c[i], val);\n }\n}...
Codeforces/1323/F
# Reality Show A popular reality show is recruiting a new cast for the third season! n candidates numbered from 1 to n have been interviewed. The candidate i has aggressiveness level l_i, and recruiting this candidate will cost the show s_i roubles. The show host reviewes applications of all candidates from i=1 to i=...
[ { "input": { "stdin": "2 2\n1 2\n0 0\n2 1 -100 -100\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5 4\n4 3 2 1 1\n0 2 6 7 4\n12 12 12 6 -3 -5 3 10 -4\n" }, "output": { "stdout": "62\n" } }, { "input": { "stdin": "5 4\n4 3 1 2 ...
{ "tags": [ "bitmasks", "dp" ], "title": "Reality Show" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid add(int& a, int b, int mod) { a = (a + b) % mod; }\nvoid sub(int& a, int b, int mod) { a = ((a - b) % mod + mod) % mod; }\nvoid mul(int& a, int b, int mod) { a = (a * 1ll * b) % mod; }\ntemplate <class T>\nbool umin(T& a, const T& b) {\n return a > b ? a...
Codeforces/1342/E
# Placing Rooks Calculate the number of ways to place n rooks on n × n chessboard so that both following conditions are met: * each empty cell is under attack; * exactly k pairs of rooks attack each other. An empty cell is under attack if there is at least one rook in the same row or at least one rook in the...
[ { "input": { "stdin": "4 0\n" }, "output": { "stdout": "24\n" } }, { "input": { "stdin": "3 2\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1337 42\n" }, "output": { "stdout": "807905441\n" } }, { "in...
{ "tags": [ "combinatorics", "fft", "math" ], "title": "Placing Rooks" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 2e5 + 5, md = 998244353;\nlong long fac[N], ifac[N];\nlong long power(long long base, long long pow) {\n base %= md;\n long long res = 1;\n while (pow > 0) {\n if (pow & 1) {\n (res *= base) %= md;\n }\n (base *= base) %= md;\n ...
Codeforces/1364/E
# X-OR This is an interactive problem! Ehab has a hidden permutation p of length n consisting of the elements from 0 to n-1. You, for some reason, want to figure out the permutation. To do that, you can give Ehab 2 different indices i and j, and he'll reply with (p_i|p_j) where | is the [bitwise-or](https://en.wikipe...
[ { "input": { "stdin": "3\n1\n3\n2" }, "output": { "stdout": "? 1 2\n? 1 3\n? 2 3\n! 1 0 2\n" } }, { "input": { "stdin": "128\n49 64 93 66 79 101 57 76 30 25 31 104 39 15 24 51 83 117 73 41 46 77 50 16 61 32 112 95 100 68 52 116 91 40 54 125 113 89 10 84 106 23 12 3 75 36 4 ...
{ "tags": [ "bitmasks", "constructive algorithms", "divide and conquer", "interactive", "probabilities" ], "title": "X-OR" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nmap<pair<int, int>, int> m;\nint q(int a, int b) {\n if (m.find({a, b}) != m.end()) return m[{a, b}];\n int x;\n cout << \"? \" << a + 1 << ' ' << b + 1 << endl;\n cin >> x;\n m[{a, b}] = x;\n m[{b, a}] = x;\n return x;\n}\nint32_t main() {\n c...
Codeforces/1384/F
# Rearrange Koa the Koala has a matrix A of n rows and m columns. Elements of this matrix are distinct integers from 1 to n ⋅ m (each number from 1 to n ⋅ m appears exactly once in the matrix). For any matrix M of n rows and m columns let's define the following: * The i-th row of M is defined as R_i(M) = [ M_{i1},...
[ { "input": { "stdin": "3 4\n12 10 8 6\n3 4 5 7\n2 11 9 1\n" }, "output": { "stdout": "12 8 6 1 \n10 11 9 2 \n3 4 5 7 \n" } }, { "input": { "stdin": "3 3\n3 5 6\n1 7 9\n4 8 2\n" }, "output": { "stdout": "9 5 1 \n7 8 2 \n3 6 4 \n" } }, { "input": { ...
{ "tags": [ "brute force", "constructive algorithms", "graphs", "greedy", "sortings" ], "title": "Rearrange" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = 998244353;\nint n, m, k, t;\nstring s;\nvector<int> a(1000005), b(1000005);\nvector<vector<int>> mat(1005, vector<int>(1005)), res(1005, vector<int>(1005));\nint main() {\n cin >> n >> m;\n for (int i = 0; i < n; i++)\n for (int j = 0; j <...
Codeforces/1406/C
# Link Cut Centroids Fishing Prince loves trees, and he especially loves trees with only one centroid. The tree is a connected graph without cycles. A vertex is a centroid of a tree only when you cut this vertex (remove it and remove all edges from this vertex), the size of the largest connected component of the rema...
[ { "input": { "stdin": "2\n5\n1 2\n1 3\n2 4\n2 5\n6\n1 2\n1 3\n1 4\n2 5\n2 6\n" }, "output": { "stdout": "1 2\n1 2\n2 5\n1 5\n" } }, { "input": { "stdin": "15\n4\n1 2\n2 3\n3 4\n4\n1 2\n2 4\n3 4\n4\n1 2\n1 3\n1 4\n4\n1 3\n2 3\n4 3\n4\n1 4\n3 4\n2 3\n4\n4 1\n3 2\n2 1\n5\n1 2\...
{ "tags": [ "constructive algorithms", "dfs and similar", "graphs", "trees" ], "title": "Link Cut Centroids" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct s1 {\n int t, next;\n} e[200003];\nint t, n, x, y, et, ef[100002], f[100002], fmx, tar;\nvoid addedge(int s, int t) {\n e[++et].next = ef[s];\n ef[s] = et;\n e[et].t = t;\n}\nint dfs(int id, int pa) {\n int siz = 1;\n for (int i = ef[id]; i; i = e...
Codeforces/1427/D
# Unshuffling a Deck You are given a deck of n cards numbered from 1 to n (not necessarily in this order in the deck). You have to sort the deck by repeating the following operation. * Choose 2 ≤ k ≤ n and split the deck in k nonempty contiguous parts D_1, D_2,..., D_k (D_1 contains the first |D_1| cards of the de...
[ { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "6\n6 5 4 3 2 1\n" }, "output": { "stdout": "4\n3 1 1 4\n3 1 1 4\n3 1 1 4\n3 2 2 2\n" } }, { "input": { "stdin": "4\n3 1 2 4\n" }, "output": { ...
{ "tags": [ "constructive algorithms", "implementation" ], "title": "Unshuffling a Deck" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nvector<int> cards;\nvector<vector<int>> answer;\nbool sorted = true;\nvoid In() {\n cin >> n;\n cards.resize(n);\n for (int i = 0; i < n; i++) cin >> cards[i];\n}\nvoid Apply(vector<int> operations) {\n vector<vector<int>> values;\n int curr = 0;\...
Codeforces/1450/D
# Rating Compression On the competitive programming platform CodeCook, every person has a rating graph described by an array of integers a of length n. You are now updating the infrastructure, so you've created a program to compress these graphs. The program works as follows. Given an integer parameter k, the program...
[ { "input": { "stdin": "5\n5\n1 5 3 4 2\n4\n1 3 2 1\n5\n1 3 3 3 2\n10\n1 2 3 4 5 6 7 8 9 10\n3\n3 3 2\n" }, "output": { "stdout": "\n10111\n0001\n00111\n1111111111\n000\n" } }, { "input": { "stdin": "5\n5\n1 5 4 2 2\n4\n1 3 2 1\n5\n1 3 3 3 2\n10\n1 2 1 4 5 2 7 8 9 10\n3\n3 3...
{ "tags": [ "binary search", "data structures", "greedy", "implementation", "two pointers" ], "title": "Rating Compression" }
{ "cpp": "#include<bits/stdc++.h>\n#include<algorithm>\n\n#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);\n#define ll long long\n#define ull unsigned long long\n#define mp make_pair\n#define ff first\n#define ss second\n#define mloop(i) for(auto i=m.begin();i!=m.end();i++)\n#define pb push_back\n#defin...
Codeforces/1474/B
# Different Divisors Positive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8. We gave you an integer d and asked you to find the smallest positive integer a, such that * a has at least 4 divisors; * differe...
[ { "input": { "stdin": "2\n1\n2\n" }, "output": { "stdout": "\n6\n15\n" } }, { "input": { "stdin": "2\n15\n13\n" }, "output": { "stdout": "629\n527\n" } }, { "input": { "stdin": "2\n22\n19\n" }, "output": { "stdout": "1081\n989\n...
{ "tags": [ "binary search", "constructive algorithms", "greedy", "math", "number theory" ], "title": "Different Divisors" }
{ "cpp": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define MOD 1000000007\n#define ull unsigned long long\n#define mp make_pair\n#define ll long long\n#define pb push_back\n#define MAX 100005\n\nint t,n;\n\nbool isprime(int n) {\n\tif ( n < 2) return false;\n\tfor ( int i = 2; i < n; i++) {\n\t\tif ( n % i ...
Codeforces/149/D
# Coloring Brackets Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it. You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is ...
[ { "input": { "stdin": "(())\n" }, "output": { "stdout": "12" } }, { "input": { "stdin": "()\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "(()())\n" }, "output": { "stdout": "40" } }, { "input": { ...
{ "tags": [ "dp" ], "title": "Coloring Brackets" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int M = (int)7e2;\nconst int mod = (int)1e9 + 7;\nchar s[M + 5];\nint tail[M + 5];\nint st[M + 5], top;\nlong long dp[M + 5][M + 5][3][3];\nvoid init() {\n int n = strlen(s + 1);\n for (int i = 1; i <= n; ++i) {\n if (s[i] == '(')\n st[++top] =...
Codeforces/1523/D
# Love-Hate <image> William is hosting a party for n of his trader friends. They started a discussion on various currencies they trade, but there's an issue: not all of his trader friends like every currency. They like some currencies, but not others. For each William's friend i it is known whether he likes currency...
[ { "input": { "stdin": "3 4 3\n1000\n0110\n1001\n" }, "output": { "stdout": "\n1000\n" } }, { "input": { "stdin": "5 5 4\n11001\n10101\n10010\n01110\n11011\n" }, "output": { "stdout": "\n10001\n" } }, { "input": { "stdin": "2 30 15\n1110100000...
{ "tags": [ "bitmasks", "brute force", "dp", "probabilities" ], "title": "Love-Hate" }
{ "cpp": "/*\n * author: ADMathNoob\n * created: 05/30/21 10:33:09\n * problem: https://codeforces.com/contest/1523/problem/D\n */\n\n/*\ng++ 1523D.cpp -std=c++11 -D_DEBUG -D_GLIBCXX_DEBUG -Wl,--stack=268435456 -Wall -Wextra -Wfatal-errors -Wshadow -ggdb && gdb a\n\ng++ 1523D.cpp -std=c++11 -D_DEBUG -D_GLIBCXX_DEBUG...
Codeforces/155/C
# Hometask Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally f...
[ { "input": { "stdin": "ababa\n1\nab\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "codeforces\n2\ndo\ncs\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\n4\nli\nqh\...
{ "tags": [ "greedy" ], "title": "Hometask" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string str;\n cin >> str;\n long long k;\n cin >> k;\n pair<char, char> arr[k];\n for (long long i = 0; i < k; i++) {\n char a, b;\n cin >> a >> b;\n arr[i] = make_pair(min(a, b), max(a, b));\n }\n long long len = str.length();\...
Codeforces/177/D1
# Encrypting Messages The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help. A message is a sequence of n integers a1, a2, ..., an. Encryption uses a key which is a...
[ { "input": { "stdin": "3 1 5\n1 2 3\n4\n" }, "output": { "stdout": "0 1 2 \n" } }, { "input": { "stdin": "4 3 2\n1 1 1 1\n1 1 1\n" }, "output": { "stdout": "0 1 1 0 \n" } }, { "input": { "stdin": "80 6 99\n48 97 9 77 73 21 86 78 48 5 71 16 42...
{ "tags": [ "brute force" ], "title": "Encrypting Messages" }
{ "cpp": "#include <bits/stdc++.h>\nconst int MAXN = 1e5 + 10;\nint a[MAXN];\nint b[MAXN];\nint N, M, mod;\nvoid read() {\n int i;\n scanf(\" %d %d %d\", &N, &M, &mod);\n for (i = 1; i <= N; i++) scanf(\" %d\", &a[i]);\n for (i = 1; i <= M; i++) scanf(\" %d\", &b[i]);\n}\nint main() {\n read();\n int i, j, k;\n...
Codeforces/198/D
# Cube Snake You've got an n × n × n cube, split into unit cubes. Your task is to number all unit cubes in this cube with positive integers from 1 to n3 so that: * each number was used as a cube's number exactly once; * for each 1 ≤ i < n3, unit cubes with numbers i and i + 1 were neighbouring (that is, shared ...
[ { "input": { "stdin": "3\n" }, "output": { "stdout": "1 8 9\n2 7 10\n27 26 25\n\n4 5 12\n3 6 11\n22 23 24\n\n15 14 13\n16 17 18\n21 20 19\n" } }, { "input": { "stdin": "24\n" }, "output": { "stdout": "1 11662 11707 11708 11753 11754 11799 11800 11845 11846 118...
{ "tags": [ "constructive algorithms" ], "title": "Cube Snake" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T, typename U>\ninline void smin(T &a, const U &b) {\n if (a > b) a = b;\n}\ntemplate <typename T, typename U>\ninline void smax(T &a, const U &b) {\n if (a < b) a = b;\n}\nstruct point {\n int x, y, z;\n point(int x = 0, int y = 0, int ...
Codeforces/221/E
# Little Elephant and Shifts The Little Elephant has two permutations a and b of length n, consisting of numbers from 1 to n, inclusive. Let's denote the i-th (1 ≤ i ≤ n) element of the permutation a as ai, the j-th (1 ≤ j ≤ n) element of the permutation b — as bj. The distance between permutations a and b is the min...
[ { "input": { "stdin": "4\n2 1 3 4\n3 4 2 1\n" }, "output": { "stdout": "2\n1\n0\n1\n" } }, { "input": { "stdin": "2\n1 2\n2 1\n" }, "output": { "stdout": "1\n0\n" } }, { "input": { "stdin": "74\n52 59 25 35 69 1 54 20 26 12 53 44 24 51 66 16 ...
{ "tags": [ "data structures" ], "title": "Little Elephant and Shifts" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 100010;\nconst int INF = 1 << 29;\nint a[N], b[N], x[N], y[N];\nmap<int, int> m;\nint query(int x) {\n int ans = INF;\n __typeof((m).begin()) it = m.lower_bound(x);\n if (it != m.end()) ans = it->first - x;\n if (it != m.begin()) --it, ans = ...
Codeforces/245/F
# Log Stream Analysis You've got a list of program warning logs. Each record of a log stream is a string in this format: "2012-MM-DD HH:MM:SS:MESSAGE" (without the quotes). String "MESSAGE" consists of spaces, uppercase and lowercase English letters and characters "!", ".", ",", "?". String "2012-MM-DD" determines...
[ { "input": { "stdin": "60 3\n2012-03-16 16:15:25: Disk size is\n2012-03-16 16:15:25: Network failute\n2012-03-16 16:16:29: Cant write varlog\n2012-03-16 16:16:42: Unable to start process\n2012-03-16 16:16:43: Disk size is too small\n2012-03-16 16:16:53: Timeout detected\n" }, "output": { "st...
{ "tags": [ "binary search", "brute force", "implementation", "strings" ], "title": "Log Stream Analysis" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long double PI = 3.1415926535897932384626433832795;\nconst long double EPS = 1e-11;\nint n, m;\nint ye, mo, da, ho, mi, se;\nint a[100100];\nint head = 0, tail = 0;\nstring s;\nint getsec() {\n int sec = se;\n sec += 60 * mi;\n sec += 60 * 60 * ho;\n ...
Codeforces/270/D
# Greenhouse Effect Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length. Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite li...
[ { "input": { "stdin": "3 3\n1 5.0\n2 5.5\n3 6.0\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125\n" }, "output": { "stdout": "2\n" } }, { "input": { ...
{ "tags": [ "dp" ], "title": "Greenhouse Effect" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\npair<int, double> a[5002];\nint dp[5002], n, m;\nint main() {\n cin >> n >> m;\n for (int i = 0; i < n; i++) {\n cin >> a[i].first >> a[i].second;\n dp[i] = 1;\n }\n int maxv = 1;\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < i; j++)\n ...
Codeforces/294/A
# Shaass and Oskols Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed...
[ { "input": { "stdin": "5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6\n" }, "output": { "stdout": "0\n12\n5\n0\n16\n" } }, { "input": { "stdin": "3\n2 4 1\n1\n2 2\n" }, "output": { "stdout": "3\n0\n3\n" } }, { "input": { "stdin": "1\n100\n1...
{ "tags": [ "implementation", "math" ], "title": "Shaass and Oskols" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxint = -1u >> 2;\nconst double eps = 1e-6;\nint main(int argc, char const *argv[]) {\n int n;\n cin >> n;\n int a[101];\n for (int i = 0; i < n; ++i) {\n cin >> a[i];\n }\n int m;\n cin >> m;\n for (int i = 0; i < m; ++i) {\n int x, y...
Codeforces/317/D
# Game with Powers Vasya and Petya wrote down all integers from 1 to n to play the "powers" game (n can be quite large; however, Vasya and Petya are not confused by this fact). Players choose numbers in turn (Vasya chooses first). If some number x is chosen at the current turn, it is forbidden to choose x or all of i...
[ { "input": { "stdin": "8\n" }, "output": { "stdout": "Petya\n" } }, { "input": { "stdin": "1\n" }, "output": { "stdout": "Vasya\n" } }, { "input": { "stdin": "2\n" }, "output": { "stdout": "Petya\n" } }, { "input": {...
{ "tags": [ "dp", "games" ], "title": "Game with Powers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nmap<long long, int> visit;\nint arr[100100];\nvector<int> vec;\nint grandi[] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5,\n 9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12};\nlong long masks[35];\nint main() {\n for (int i = 1; i <= 30; i++...
Codeforces/341/D
# Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this problem asks you for. You are given a matrix a with n rows and n columns. Initially, all values of the matrix are zeros. Both rows and columns are 1-based, that is rows are numbered 1, 2, ..., n and columns are numbered 1, 2,...
[ { "input": { "stdin": "3 5\n2 1 1 2 2 1\n2 1 3 2 3 2\n2 3 1 3 3 3\n1 2 2 3 3\n1 2 2 3 2\n" }, "output": { "stdout": "3\n2\n" } }, { "input": { "stdin": "3 5\n2 1 1 2 2 0\n2 1 6 3 6 4\n2 1 1 5 3 3\n1 1 4 1 3\n1 3 2 3 3\n" }, "output": { "stdout": "0\n0\n" }...
{ "tags": [ "data structures" ], "title": "Iahub and Xors" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(3, \"inline\", \"Ofast\")\nusing namespace std;\nint n, m;\nlong long t[4][1010][1010];\nint lowbit(int x) { return x & (-x); }\nint opt(int x, int y) {\n int ans;\n if (x & 1)\n ans = 0;\n else\n ans = 2;\n if (y & 1)\n ans += 0;\n else\n ans ...
Codeforces/365/A
# Good Number Let's call a number k-good if it contains all digits not exceeding k (0, ..., k). You've got a number k and an array a containing n numbers. Find out how many k-good numbers are in a (count each number every time it occurs in array a). Input The first line contains integers n and k (1 ≤ n ≤ 100, 0 ≤ k ...
[ { "input": { "stdin": "2 1\n1\n10\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "10 6\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n" }, "output": { "stdout": "10\n" } }, { "input": { ...
{ "tags": [ "implementation" ], "title": "Good Number" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, k, ans = 0;\n cin >> n >> k;\n set<int> sett;\n for (int i = 0; i < n; i++) {\n int a;\n cin >> a;\n sett.clear();\n while (a != 0) {\n if (a % 10 <= k) sett.insert(a % 10);\n a /= 10;\n }\n if (sett.size()...
Codeforces/388/E
# Fox and Meteor Shower There is a meteor shower on the sky and there are n meteors. The sky can be viewed as a 2D Euclid Plane and the meteor is point on this plane. Fox Ciel looks at the sky. She finds out that the orbit of each meteor is a straight line, and each meteor has a constant velocity. Now Ciel wants to ...
[ { "input": { "stdin": "4\n0 0 0 1 0 1\n0 0 1 1 1 1\n0 1 1 1 1 0\n0 1 0 1 0 0\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1\n0 0 0 1 0 0\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3\n-1 -1 0 3 3 0\n0 2 -1 -...
{ "tags": [ "geometry" ], "title": "Fox and Meteor Shower" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint N;\nint t1[1010], x11[1010], y11[1010], t2[1010], x22[1010], y22[1010];\ndouble x00[1010], y00[1010], vx[1010], vy[1010];\nbool meet[1010][1010];\ndouble t[1010][1010];\nint most_freq(vector<double> &v) {\n int N = v.size();\n int i, j, ans = 0;\n sort(...
Codeforces/409/C
# Magnum Opus Salve, mi amice. Et tu quidem de lapis philosophorum. Barba non facit philosophum. Labor omnia vincit. Non potest creatio ex nihilo. Necesse est partibus. Rp: I Aqua Fortis I Aqua Regia II Amalgama VII Minium IV Vitriol Misce in vitro et æstus, et nil admirari. Festina lente, et nulla tenaci invi...
[ { "input": { "stdin": "2 4 6 8 10\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "18 13 91 64 22\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "31 38 47 26 13\n" }, "output": { "stdout": "3\n" } ...
{ "tags": [ "*special" ], "title": "Magnum Opus" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int i, min, a[5], b[5] = {1, 1, 2, 7, 4};\n for (i = 0; i < 5; i++) scanf(\"%d\", &a[i]);\n min = a[0] / b[0];\n for (i = 1; i < 5; i++)\n if (min > a[i] / b[i]) min = a[i] / b[i];\n printf(\"%d\\n\", min);\n return 0;\n}\n", "java": "import java.io.Buffer...
Codeforces/436/D
# Pudding Monsters Have you ever played Pudding Monsters? In this task, a simplified one-dimensional model of this game is used. <image> Imagine an infinite checkered stripe, the cells of which are numbered sequentially with integers. Some cells of the strip have monsters, other cells of the strip are empty. All mon...
[ { "input": { "stdin": "4 2\n1 8 4 5\n7 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 2\n1 3 5\n2 4\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "4 2\n1 3 4 6\n2 5\n" }, "output": { "stdout": "...
{ "tags": [ "dp" ], "title": "Pudding Monsters" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T1, class T2>\nostream& operator<<(ostream& out, pair<T1, T2> pair) {\n return out << \"(\" << pair.first << \", \" << pair.second << \")\";\n}\nint special_cell_pos[2005];\nint N, M;\nint in[100005];\nint left_monster[100005];\nint right_mons...
Codeforces/459/E
# Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are given a weighted directed graph with n vertices and m edges. You need...
[ { "input": { "stdin": "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "3 3\n1 2 1\n2 3 1\n3 1 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 3\n1 2 1\n2 3 2...
{ "tags": [ "dp", "sortings" ], "title": "Pashmak and Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct canh {\n int v, u, gt;\n};\ncanh a[400005];\nint n, m;\nint d[400005];\nint F[400005];\nvector<int> dd[400005];\nvoid nhap() {\n cin >> n >> m;\n for (int i = 1; i <= m; i++) cin >> a[i].u >> a[i].v >> a[i].gt;\n}\nbool ss(canh p, canh q) { return p....
Codeforces/480/D
# Parcels Jaroslav owns a small courier service. He has recently got and introduced a new system of processing parcels. Each parcel is a box, the box has its weight and strength. The system works as follows. It originally has an empty platform where you can put boxes by the following rules: * If the platform is em...
[ { "input": { "stdin": "3 2\n0 1 1 1 1\n1 2 1 1 1\n0 2 1 1 1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "5 5\n0 6 1 2 1\n1 2 1 1 1\n1 3 1 1 1\n3 6 2 1 2\n4 5 1 1 1\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": ...
{ "tags": [ "dp", "graphs" ], "title": "Parcels" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MaxV = 500 * 2 + 10;\nint f[MaxV][MaxV], g[MaxV][MaxV];\nbool vis[MaxV][MaxV], inq[MaxV];\nint head[MaxV], nxt[MaxV], to[MaxV], tid[MaxV], tsd[MaxV], twd[MaxV], tvd[MaxV],\n in, out, si, vi, wi, n, S, top;\nvoid addedge(int fr, int tt, int ww, int...
Codeforces/505/D
# Mr. Kitayuta's Technology Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n. Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two...
[ { "input": { "stdin": "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "7 13\n6 1\n7 2\n3 7\n6 5\n3 6\n7 4\n...
{ "tags": [ "dfs and similar" ], "title": "Mr. Kitayuta's Technology" }
{ "cpp": "#include <bits/stdc++.h>\nint lg(long long a) { return 64 - __builtin_clzll(a) - 1; }\nlong long highpow(long long a) { return 1LL << (long long)lg(a); }\nusing namespace std;\nconst long long LINF = 4e18;\nconst int mxN = 2e5 + 10, INF = 2e9, mod = (1 ? 1e9 + 7 : 998244353);\nlong long n, m, d, G, q, sz[mx...
Codeforces/529/C
# Rooks and Rectangles Polycarpus has a chessboard of size n × m, where k rooks are placed. Polycarpus hasn't yet invented the rules of the game he will play. However, he has already allocated q rectangular areas of special strategic importance on the board, they must be protected well. According to Polycarpus, a rect...
[ { "input": { "stdin": "4 3 3 3\n1 1\n3 2\n2 3\n2 3 2 3\n2 1 3 3\n1 2 2 3\n" }, "output": { "stdout": "YES\nYES\nNO\n" } }, { "input": { "stdin": "1 1 1 1\n1 1\n1 1 1 1\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "10 11 12 2...
{ "tags": [ "data structures", "sortings" ], "title": "Rooks and Rectangles" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 1e9, mod = 1e9 + 7;\nconst int N = 2e5 + 5;\nint aa, bb, ST[4 * N], n, m, i, j, k, ans[N];\npair<int, int> a[N];\npair<pair<int, int>, pair<int, int> > q[N];\nvector<pair<pair<int, int>, pair<int, int> > > qu[N];\nvector<int> v[N], v2[N];\nint ...
Codeforces/554/E
# Love Triangles There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters. The characters are labeled from 1 to n. Every pair of two characters can either mutually love each other or mutually ha...
[ { "input": { "stdin": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "3 0\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1\n" }, "output": { ...
{ "tags": [ "dfs and similar", "dsu", "graphs" ], "title": "Love Triangles" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma comment(linker, \"/STACK:1000000000\")\nusing namespace std;\nconst int maxn = (int)1e5 + 10;\nint p[maxn];\nint xorrrr[maxn];\nint d[maxn];\nvector<int> ed[maxn];\nint get_parent(int v) {\n if (v == p[v]) {\n return v;\n }\n int par = get_parent(p[v]);\n d[v] += d[p...
Codeforces/580/D
# Kefa and Dishes When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the i-th dish gives him ai...
[ { "input": { "stdin": "2 2 1\n1 1\n2 1 1\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "4 3 2\n1 2 3 4\n2 1 5\n3 4 2\n" }, "output": { "stdout": "12" } }, { "input": { "stdin": "10 5 5\n45 45 12 67 32 6 125 33 89 100\n6 3 78\n1 2...
{ "tags": [ "bitmasks", "dp" ], "title": "Kefa and Dishes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long int INF = 1LL << 60;\nconst int MAXN = 20;\nint n, m, k;\nlong long int num[MAXN];\nlong long int mat[MAXN][MAXN];\nlong long int memo[262154][MAXN];\nint main() {\n int a, b;\n long long int c;\n scanf(\"%d %d %d\", &n, &k, &m);\n for (int...
Codeforces/602/C
# The Two Routes In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a differen...
[ { "input": { "stdin": "5 5\n4 2\n3 5\n4 5\n5 1\n1 2\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "4 2\n1 3\n3 4\n" }, "ou...
{ "tags": [ "graphs", "shortest paths" ], "title": "The Two Routes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint ma[410][410], n, m;\nint ma2[410][410];\nint f[410][410], vis1[410], vis2[410];\nint main() {\n scanf(\"%d %d\", &n, &m);\n for (int i = 1; i <= m; i++) {\n int x, y;\n scanf(\"%d %d\", &x, &y);\n ma[x][y] = ma[y][x] = 1;\n }\n if (ma[1][n] ==...
Codeforces/625/A
# Guest From the Past Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated. Kolya is hungry, so he went to the nearest milk shop. In 2084 you ...
[ { "input": { "stdin": "10\n11\n9\n8\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "10\n5\n6\n1\n" }, "output": { "stdout": " 2\n" } }, { "input": { "stdin": "10\n5\n...
{ "tags": [ "implementation", "math" ], "title": "Guest From the Past" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = (int)1e9, maxn = (int)1e5 + 5;\nconst double eps = (double)1e-8;\nconst int mod = (int)1000000007;\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n long long n, a, b, c, o, o1, x;\n cin >> n >> a >> b >> c;\n o...
Codeforces/64/C
# Table The integer numbers from 1 to nm was put into rectangular table having n rows and m columns. The numbers was put from left to right, from top to bottom, i.e. the first row contains integers 1, 2, ..., m, the second — m + 1, m + 2, ..., 2 * m and so on. After it these numbers was written on the paper in anothe...
[ { "input": { "stdin": "3 4 11\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "20000 10000 200000000\n" }, "output": { "stdout": "200000000\n" } } ]
{ "tags": [ "*special", "greedy", "implementation", "math" ], "title": "Table" }
{ "cpp": null, "java": null, "python": null }
Codeforces/673/C
# Bear and Colors Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti. For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color o...
[ { "input": { "stdin": "3\n1 1 1\n" }, "output": { "stdout": "6 0 0\n" } }, { "input": { "stdin": "4\n1 2 1 2\n" }, "output": { "stdout": "7 3 0 0\n" } }, { "input": { "stdin": "10\n9 1 5 2 9 2 9 2 1 1\n" }, "output": { "stdout":...
{ "tags": [ "implementation" ], "title": "Bear and Colors" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD = 1e9 + 7;\nconst long long mod = 1e9 + 7;\nvoid sout() { cout << \"\\n\"; }\ntemplate <typename T, typename... Types>\nvoid sout(T var1, Types... var2) {\n cout << var1 << \" \";\n sout(var2...);\n}\nlong long n, m;\nlong long temp;\nlon...
Codeforces/698/D
# Limak and Shooting Points Bearland is a dangerous place. Limak can’t travel on foot. Instead, he has k magic teleportation stones. Each stone can be used at most once. The i-th stone allows to teleport to a point (axi, ayi). Limak can use stones in any order. There are n monsters in Bearland. The i-th of them stand...
[ { "input": { "stdin": "3 8\n10 20\n0 0\n20 40\n300 600\n30 60\n170 340\n50 100\n28 56\n90 180\n-4 -8\n-1 -2\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "2 4\n-2 -1\n4 5\n4 2\n2 1\n4 -1\n1 -1\n" }, "output": { "stdout": "3\n" } }, { ...
{ "tags": [ "brute force", "geometry", "math" ], "title": "Limak and Shooting Points" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int oo = 0x3f3f3f3f;\nconst double eps = 1e-9;\nconst int maxn = 2000;\nlong long cross(complex<long long> a, complex<long long> b) {\n return imag(conj(a) * b);\n}\ncomplex<long long> stone[7];\ncomplex<long long> monster[maxn];\ncomplex<long long> cen...
Codeforces/719/E
# Sasha and Array Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1. 1 l r x — increase all integers on the segment from l to r by values x; 2. 2 l r — find <image>, where f(x) is the x-th Fibonacci number. As this number may be large, you only ...
[ { "input": { "stdin": "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5\n" }, "output": { "stdout": "5\n7\n9\n" } }, { "input": { "stdin": "2 3\n1 3\n2 1 1\n1 1 2 3\n1 1 2 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "9 4\n2 1 2...
{ "tags": [ "data structures", "math", "matrices" ], "title": "Sasha and Array" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MOD = 1e9 + 7;\nvoid add(int &x, int y) {\n x += y;\n if (x >= MOD) x -= MOD;\n}\nint _add(int x, int y) {\n x += y;\n if (x >= MOD) x -= MOD;\n return x;\n}\nint mul(int a, int b) { return (a * 1LL * b) % MOD; }\nstruct Matrix {\n int n = 2, m...
Codeforces/740/B
# Alyona and flowers Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative. Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona ...
[ { "input": { "stdin": "4 3\n1 2 3 4\n1 3\n2 4\n1 1\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "5 4\n1 -2 1 3 -4\n1 2\n4 5\n3 4\n1 4\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "2 2\n-1 -2\n1 1\n1 2\n" }...
{ "tags": [ "constructive algorithms" ], "title": "Alyona and flowers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m;\n int a[105];\n cin >> n >> m;\n for (int i = 1; i <= n; i++) cin >> a[i];\n int sum1 = 0;\n while (m--) {\n int s, e;\n cin >> s >> e;\n int sum = 0;\n for (int i = s; i <= e; i++) sum += a[i];\n if (sum > 0) sum1...
Codeforces/764/A
# Taymyr is calling you Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, i.e. in minutes m, 2m, 3m and so on. The day is z minute...
[ { "input": { "stdin": "1 1 10\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "2 3 9\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1 2 5\n" }, "output": { "stdout": "2\n" } }, { "input":...
{ "tags": [ "brute force", "implementation", "math" ], "title": "Taymyr is calling you" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m, z;\n cin >> n >> m >> z;\n int ans = 0;\n int nX = n;\n int mX = m;\n int nPrev = 0, mPrev = 0;\n for (int i = 0; i < z && (nX <= z && mX <= z); ++i) {\n if (nX == nPrev && mX == mPrev) {\n } else if ((nX == mX)) {\n ...
Codeforces/787/C
# Berzerk Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer. In this game there are n objects numbered from 1 to n arranged in a circle (in clockwise order). Object number 1 is a black hole ...
[ { "input": { "stdin": "8\n4 6 2 3 4\n2 3 6\n" }, "output": { "stdout": "Win Win Win Win Win Win Win \nLose Win Lose Lose Win Lose Lose \n" } }, { "input": { "stdin": "5\n2 3 2\n3 1 2 3\n" }, "output": { "stdout": "Lose Win Win Loop \nLoop Win Win Win \n" }...
{ "tags": [ "dfs and similar", "dp", "games" ], "title": "Berzerk" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int Win = 3, Loop = 2, Lose = 1;\nint n, dp[7001][3], ne[3], a[7001][3], k[3], sum[7001][3];\nbool b[7001][3];\nint max(int x, int y) { return x > y ? x : y; }\nvoid dfs(int x, int y) {\n if (b[x][y]) return;\n b[x][y] = 1;\n for (int i = 1; i <= k[ne...
Codeforces/808/G
# Anthem of Berland Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem. Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many ...
[ { "input": { "stdin": "glo?yto?e??an?\nor\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "??c?????\nabcab\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "winlose???winl???w??\nwin\n" }, "output": { "stdou...
{ "tags": [ "dp", "strings" ], "title": "Anthem of Berland" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstring S, T;\nint s, t;\nint bk[3505], nxt[3505][26];\nvoid BK() {\n bk[1] = 0;\n for (int i = (1); i <= (t - 1); i += (1)) {\n int f = bk[i];\n while (f && T[f] != T[i]) f = bk[f];\n if (T[f] == T[i]) f++;\n bk[i + 1] = f;\n }\n}\nvector<vector...
Codeforces/833/C
# Ever-Hungry Krakozyabra <image> Recently, a wild Krakozyabra appeared at Jelly Castle. It is, truth to be said, always eager to have something for dinner. Its favorite meal is natural numbers (typically served with honey sauce), or, to be more precise, the zeros in their corresponding decimal representations. As f...
[ { "input": { "stdin": "1 10\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "40 57\n" }, "output": { "stdout": "17\n" } }, { "input": { "stdin": "157 165\n" }, "output": { "stdout": "9\n" } }, { "input":...
{ "tags": [ "brute force", "combinatorics", "greedy", "math" ], "title": "Ever-Hungry Krakozyabra" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long mod = 1000000007;\nlong long dx(long long x, int n) {\n long long res = 1;\n for (int i = 0; i < n; i++) res *= x;\n return res;\n}\nstd::vector<int> ban(10, 0);\nvector<std::vector<long long> > cnk(27, std::vector<long long>(27, 0));\nlong ...
Codeforces/854/A
# Fraction Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction <image> is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have p...
[ { "input": { "stdin": "12\n" }, "output": { "stdout": "5 7\n" } }, { "input": { "stdin": "4\n" }, "output": { "stdout": "1 3\n" } }, { "input": { "stdin": "3\n" }, "output": { "stdout": "1 2\n" } }, { "input": { ...
{ "tags": [ "brute force", "constructive algorithms", "math" ], "title": "Fraction" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int number;\n cin >> number;\n if (number % 2 == 0) {\n number /= 2;\n if (number % 2 != 0) {\n cout << number - 2 << \" \" << number + 2 << endl;\n } else {\n cout << number - 1 << \" \" << number + 1 << endl;\n }\n ...
Codeforces/878/D
# Magic Breeding Nikita and Sasha play a computer game where you have to breed some magical creatures. Initially, they have k creatures numbered from 1 to k. Creatures have n different characteristics. Sasha has a spell that allows to create a new creature from two given creatures. Each of its characteristics will be...
[ { "input": { "stdin": "2 2 4\n1 2\n2 1\n1 1 2\n2 1 2\n3 3 1\n3 4 2\n" }, "output": { "stdout": "2\n1\n" } }, { "input": { "stdin": "5 3 8\n1 2 3 4 5\n5 1 2 3 4\n4 5 1 2 3\n1 1 2\n1 2 3\n2 4 5\n3 6 1\n3 6 2\n3 6 3\n3 6 4\n3 6 5\n" }, "output": { "stdout": "5\n2...
{ "tags": [ "bitmasks" ], "title": "Magic Breeding" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, K, Q, i, j, k, l, tp, x, y, z, t;\nint a[13][100001];\nbitset<4096> b[200001];\nint p[13];\nint ans;\nstruct type {\n int x, id;\n} c[13];\nbool cmp(type a, type b) { return a.x < b.x; }\nint main() {\n p[0] = 1;\n for (i = 1; i <= 12; i++) p[i] = p[...
Codeforces/902/A
# Visiting a Friend Pig is visiting a friend. Pig's house is located at point 0, and his friend's house is located at point m on an axis. Pig can use teleports to move along the axis. To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport th...
[ { "input": { "stdin": "3 5\n0 2\n2 4\n3 5\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "3 7\n0 4\n2 5\n6 7\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "50 10\n0 2\n0 2\n0 6\n1 9\n1 3\n1 2\n1 6\n1 1\n1 1\n2 ...
{ "tags": [ "greedy", "implementation" ], "title": "Visiting a Friend" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int i, j, n, m, x, y;\n int a[105], b[105];\n while (cin >> n >> m) {\n int p = 1;\n int mp[105];\n memset(mp, 0, sizeof(mp));\n for (i = 0; i < n; i++) {\n cin >> a[i] >> b[i];\n if (a[0] == 0) mp[0] = 1;\n if (mp[...
Codeforces/924/C
# Riverside Curio Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is c...
[ { "input": { "stdin": "5\n0 1 1 2 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "6\n0 1 0 3 0 2\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "5\n0 1 2 1 2\n" }, "output": { "stdout": "1\n" } ...
{ "tags": [ "data structures", "dp", "greedy" ], "title": "Riverside Curio" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int NMAX = 1e5 + 10;\nint n, v[NMAX], dp[NMAX];\nint main() {\n int i, j, local = 0;\n cin >> n;\n for (i = 1; i <= n; i++) cin >> v[i];\n for (i = n; i >= 1; i--) {\n local = max(0, local - 1);\n local = max(local, v[i] + 1);\n dp[i] = loca...
Codeforces/952/E
# Cheese Board Not to be confused with [chessboard](https://en.wikipedia.org/wiki/Chessboard). <image> Input The first line of input contains a single integer N (1 ≤ N ≤ 100) — the number of cheeses you have. The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name o...
[ { "input": { "stdin": "6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\ns...
{ "tags": [], "title": "Cheese Board" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\npair<long long, long long> gget(int p) {\n int a = 0, b = 0;\n for (int i = (0); i < (p); i++)\n for (int j = (0); j < (p); j++)\n if ((i + j) % 2 == 0)\n a++;\n else\n b++;\n return pair<long long, long long>(min(a, b), max(a, b)...
Codeforces/97/B
# Superset A set of points on a plane is called good, if for any two points at least one of the three conditions is true: * those two points lie on same horizontal line; * those two points lie on same vertical line; * the rectangle, with corners in these two points, contains inside or on its borders at least ...
[ { "input": { "stdin": "2\n1 1\n2 2\n" }, "output": { "stdout": "3\n1 1\n1 2\n2 2" } }, { "input": { "stdin": "2\n0 1\n1 0\n" }, "output": { "stdout": "3\n0 0\n0 1\n1 0" } }, { "input": { "stdin": "4\n-10 1\n-2 -5\n-1 -2\n4 -3\n" }, "o...
{ "tags": [ "constructive algorithms", "divide and conquer" ], "title": "Superset" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX_N = 1e4 + 7;\npair<int, int> p[MAX_N];\nset<pair<int, int> > ans;\nvoid daq(int l, int r) {\n if (r - l == 1) {\n return;\n }\n int mid = (r + l) / 2;\n daq(l, mid);\n daq(mid, r);\n int x = p[mid].first;\n for (int i = l; i < r; ++i) {...