id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
AIZU/p01974
# Pigeonhole principle problem Given $ N $ different natural numbers $ a_i $. I decided to make a pair by choosing a different natural number from the given natural numbers. Output one pair that can be created with a value difference that is a multiple of $ N -1 $. It should be noted that such a pair always exists. ...
[ { "input": { "stdin": "5\n1 2 4 7 10" }, "output": { "stdout": "2 10" } }, { "input": { "stdin": "5\n2 2 22 7 24" }, "output": { "stdout": "2 2\n" } }, { "input": { "stdin": "5\n2 0 3 5 9" }, "output": { "stdout": "5 9\n" } ...
{ "tags": [], "title": "Pigeonhole principle" }
{ "cpp": "/* -*- coding: utf-8 -*-\n *\n * 2874.cc: Pigeonhole principle\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n#include<queue>\n#include<deque>\n#include<a...
AIZU/p02120
# Cluster Network Problem A large-scale cluster † type supercomputer (commonly known as "SAKURA") cluster at Maze University consists of $ N $ computers (nodes) and $ M $ physical communication channels that can communicate with each other. .. In addition, each node is assigned an identification number from 1 to $ N ...
[ { "input": { "stdin": "9 10\n1 2 3 4 5 6 7 8 9\n1 2\n2 3\n2 4\n3 4\n4 5\n4 6\n2 7\n7 8\n7 9\n9 1" }, "output": { "stdout": "44\n25\n42\n30\n40\n39\n30\n37\n36" } }, { "input": { "stdin": "2 1\n1 2\n1 2" }, "output": { "stdout": "2\n1" } }, { "input...
{ "tags": [], "title": "Cluster Network" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nint const MAX_V = 100001;\nint const MAX_E = 200001;\nint const INF = 1<<29;\n\nvector<int> G[MAX_V];\nint ord[MAX_V], low[MAX_V];\n\nvector<int> tree[MAX_V];\nll size[MAX_V];\nll A[MAX_V];\n\nvoid dfs(int pos){\n size[pos]=A[pos];\...
AIZU/p02260
# Selection Sort Write a program of the Selection Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode: SelectionSort(A) 1 for i = 0 to A.length-1 2 mini = i 3 for j = i to A.length-1 4 if A[j] < A[mini] 5 mini = j 6 swa...
[ { "input": { "stdin": "6\n5 2 4 6 1 3" }, "output": { "stdout": "1 2 3 4 5 6\n3" } }, { "input": { "stdin": "6\n5 6 4 2 1 3" }, "output": { "stdout": "1 2 3 4 5 6\n4" } }, { "input": { "stdin": "6\n5 3 2 5 -1 3" }, "output": { "...
{ "tags": [], "title": "Selection Sort" }
{ "cpp": "#include<iostream>\n#include<algorithm>\nusing namespace std;\n\n\nint main(){\nint minj,cnt=0,N,A[100];\ncin >> N ;\n\nfor(int i=0;i<N;i++)cin >> A[i];\n\nfor(int i=0;i<N;i++){\n minj = i;\n for(int j=i;j<N;j++){\n \tif (A[j] < A[minj]) minj = j;\n }\n if(minj!=i){\n swap(A[minj],A[...
AIZU/p02408
# Finding Missing Cards Taro is going to play a card game. However, now he has only n cards, even though there should be 52 cards (he has no Jokers). The 52 cards include 13 ranks of each of the four suits: spade, heart, club and diamond. Note 解説 Input In the first line, the number of cards n (n ≤ 52) is given....
[ { "input": { "stdin": "47\nS 10\nS 11\nS 12\nS 13\nH 1\nH 2\nS 6\nS 7\nS 8\nS 9\nH 6\nH 8\nH 9\nH 10\nH 11\nH 4\nH 5\nS 2\nS 3\nS 4\nS 5\nH 12\nH 13\nC 1\nC 2\nD 1\nD 2\nD 3\nD 4\nD 5\nD 6\nD 7\nC 3\nC 4\nC 5\nC 6\nC 7\nC 8\nC 9\nC 10\nC 11\nC 13\nD 9\nD 10\nD 11\nD 12\nD 13" }, "output": { ...
{ "tags": [], "title": "Finding Missing Cards" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n char mark[]={'S','H','C','D'},c;\n int d[4][13]={0},n,m;\n cin>>n;\n for(int i=1;i<=n;++i){\n cin>>c>>m;\n switch(c){\n case 'S':\n d[0][m-1]=1;\n break;\n case 'H':\n d[1][m-1]=1;\n break;\n case 'C':\n ...
CodeChef/cdva1502
Your task is very simple. Given K numbers A1, A2, ..., AK. You need to find f(N) mod max(A1, A2, ..., AK) . f(N)=N! Input First line contains single integer T denoting the number of test cases.. First line of each test case contains two integers N and K. Next line contains K integers Ak Output For each test case,...
[ { "input": { "stdin": "1\n5 3\n200 6 9" }, "output": { "stdout": "120" } }, { "input": { "stdin": "1\n24 1\n562 8 5" }, "output": { "stdout": "332\n" } }, { "input": { "stdin": "1\n32 -1\n30 0 1" }, "output": { "stdout": "0\n" ...
{ "tags": [], "title": "cdva1502" }
{ "cpp": null, "java": null, "python": "for ii in range(input()):\n import math\n ak=map(int,raw_input().split())\n n=ak[0]\n aks=map(int,raw_input().split())\n a=max(aks)\n k=math.factorial(n)\n ans= k%a\n print ans" }
CodeChef/dbyz15t
Problem Desrcription Shil likes to play with trees a lot.He is playing with an undirected tree containing N nodes.He wants to find out total number of unordered triplets (A,B,C) such that A is not connected to B, B is not connected to C and A is not connected to C by a direct edge (No two of A, B, C are mutually conne...
[ { "input": { "stdin": "5\n1 2\n1 3\n3 4\n2 5" }, "output": { "stdout": "1" } } ]
{ "tags": [], "title": "dbyz15t" }
{ "cpp": null, "java": null, "python": "N=int(input())\nD=[0]*N\nfor i in range(N-1):\n a,b=map(int,raw_input().split())\n a-=1;b-=1;\n D[a]+=1\n D[b]+=1\nans=N*(N-1)*(N-2)\nans/=6\nans-=(N-1)*(N-2)\nfor i in D:\n if i>=2:\n ans+=(i*(i-1))/2\nprint ans" }
CodeChef/ig01
This is a simple game you must have played around with during your school days, calculating FLAMES of you and your crush! Given the names of two people, cancel out the common letters (repeated occurrence of a letter is treated separately, so 2A's in one name and one A in the other would cancel one A in each name), coun...
[ { "input": { "stdin": "2\nSHILPA\nAAMIR\nMATT\nDENISE" }, "output": { "stdout": "ENEMIES\nLOVE" } } ]
{ "tags": [], "title": "ig01" }
{ "cpp": null, "java": null, "python": "testcase = raw_input();\ntc = int(testcase)\n\nwhile (tc > 0):\n\t\n\tflames = \"FLAMES\"\n\t#flames[0] = 'X'\n\t\n\tname1=list((raw_input()).replace(' ',''))\n\tname2=list((raw_input()).replace(' ',''))\n\t\n\t\n\t#ht = {}\n\t\n\tcount =0\n\tcount1 = 0\n\t\n\ti=0\n\twhile...
CodeChef/muffins3
Now that Chef has finished baking and frosting his cupcakes, it's time to package them. Chef has N cupcakes, and needs to decide how many cupcakes to place in each package. Each package must contain the same number of cupcakes. Chef will choose an integer A between 1 and N, inclusive, and place exactly A cupcakes into ...
[ { "input": { "stdin": "2\n2\n5" }, "output": { "stdout": "2\n3\n" } }, { "input": { "stdin": "2\n12\n8" }, "output": { "stdout": "7\n5\n" } }, { "input": { "stdin": "2\n4\n14" }, "output": { "stdout": "3\n8\n" } }, { ...
{ "tags": [], "title": "muffins3" }
{ "cpp": null, "java": null, "python": "t = input()\nwhile(t>0):\n\tt-=1\n\tn = input()\n\tif( n<=2 ):\n\t\tprint n\n\telse:\n\t\tif( n%2==0 ):\n\t\t\tprint n/2+1\n\t\telse:\n\t\t\tprint (n+1)/2" }
CodeChef/rrcode
You are given a simple code of a function and you would like to know what it will return. F(N, K, Answer, Operator, A[N]) returns int; begin for iK do for jN do AnswerAnswer operator Aj) return Answer end Here N, K, Answer and the value returned by the function F are integers;...
[ { "input": { "stdin": "3\n3 1 0\n1 2 3\nXOR\n3 1 0\n1 2 3\nAND\n3 1 0\n1 2 3\nOR" }, "output": { "stdout": "0\n0\n3\n" } }, { "input": { "stdin": "3\n3 2 0\n1 2 4\nXOR\n3 0 1\n1 2 2\nAND\n3 1 0\n1 2 2\nOR" }, "output": { "stdout": "0\n1\n3\n" } }, { ...
{ "tags": [], "title": "rrcode" }
{ "cpp": null, "java": null, "python": "f={'XOR': lambda x,y:x^y, 'AND': lambda x,y:x&y, 'OR': lambda x,y:x|y}\nfor _ in range(int(raw_input())):\n n,k,answer=map(int, raw_input().split())\n if k:\n s=map(int, raw_input().split())\n op=raw_input().strip()\n if (answer!=0 or op!=\"AND\")...
CodeChef/walk
Chef and his girlfriend are going to have a promenade. They are walking along the straight road which consists of segments placed one by one. Before walking Chef and his girlfriend stay at the beginning of the first segment, they want to achieve the end of the last segment. There are few problems: At the beginning ...
[ { "input": { "stdin": "2\n5\n6 5 4 3 2\n5\n3 4 3 1 1" }, "output": { "stdout": "6\n5\n" } }, { "input": { "stdin": "2\n5\n6 15 1 4 -4\n5\n12 4 28 0 1" }, "output": { "stdout": "16\n30\n" } }, { "input": { "stdin": "2\n5\n6 15 1 4 -3\n5\n12 4 ...
{ "tags": [], "title": "walk" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\n\ndef walk(L):\n return max([L[x] + x for x in range(len(L))])\n\nfor x in range(t):\n raw_input()\n print walk([int(x) for x in raw_input().split()])" }
Codeforces/101/C
# Vectors At a geometry lesson Gerald was given a task: to get vector B out of vector A. Besides, the teacher permitted him to perform the following operations with vector А: * Turn the vector by 90 degrees clockwise. * Add to the vector a certain vector C. Operations could be performed in any order any number...
[ { "input": { "stdin": "0 0\n1 1\n1 1\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "0 0\n1 1\n0 1\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "0 0\n1 1\n2 2\n" }, "output": { "stdout": "NO\n" ...
{ "tags": [ "implementation", "math" ], "title": "Vectors" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int x2, y2, x3, y3;\nbool check(long long int x, long long int y) {\n long long int xx = x3 * x3 + y3 * y3, tx = x2 + x, ty = y2 + y;\n if (xx == 0) return x == x2 && y == y2;\n long long int ret1 = tx * x3 + ty * y3, ret2 = tx * y3 - ty * x3;\n ...
Codeforces/1043/A
# Elections Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of them. So Awruk knows that if a person gives a_i votes for Elodreip, than he will get exactly k - a_i...
[ { "input": { "stdin": "5\n2 2 3 2 2\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "5\n1 1 1 5 1\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "3\n1 2 6\n" }, "output": { "stdout": "7\n" } }, ...
{ "tags": [ "implementation", "math" ], "title": "Elections" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nint main() {\n ios::sync_with_stdio(0), cin.tie(0);\n int n;\n cin >> n;\n vector<int> arr(n);\n for (int i = 0; i < n; ++i) cin >> arr[i];\n int sum = accumulate((arr).begin(), (arr).end(), 0);\n for (int i = *max_element((arr).b...
Codeforces/1065/F
# Up and Down the Tree You are given a tree with n vertices; its root is vertex 1. Also there is a token, initially placed in the root. You can move the token to other vertices. Let's assume current vertex of token is v, then you make any of the following two possible moves: * move down to any leaf in subtree of v...
[ { "input": { "stdin": "8 2\n1 1 2 3 4 5 5\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "7 1\n1 1 3 3 4 4\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "30 2\n26 11 22 16 14 29 16 14 8 23 21 22 11 24 15 23 22 1 11 20...
{ "tags": [ "dfs and similar", "dp", "trees" ], "title": "Up and Down the Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long double pi = acos(-1.0);\nconst long double log23 = 1.58496250072115618145373894394781;\nconst long double eps = 1e-8;\nconst long long INF = 1e18 + 239;\nconst long long prost = 239;\nconst int two = 2;\nconst int th = 3;\nconst long long MOD = 9982...
Codeforces/1088/D
# Ehab and another another xor problem This is an interactive problem! Ehab plays a game with Laggy. Ehab has 2 hidden integers (a,b). Laggy can ask a pair of integers (c,d) and Ehab will reply with: * 1 if a ⊕ c>b ⊕ d. * 0 if a ⊕ c=b ⊕ d. * -1 if a ⊕ c<b ⊕ d. Operation a ⊕ b is the [bitwise-xor operatio...
[ { "input": { "stdin": "1\n-1\n0" }, "output": { "stdout": "? 0 0\n? 536870912 0\n? 0 536870912\n? 805306368 536870912\n? 536870912 805306368\n? 939524096 536870912\n? 805306368 671088640\n? 872415232 671088640\n? 805306368 738197504\n? 838860800 738197504\n? 805306368 771751936\n? 822083584 ...
{ "tags": [ "bitmasks", "constructive algorithms", "implementation", "interactive" ], "title": "Ehab and another another xor problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long N = 100100, SQ = 330, LG = 25, inf = 1e17;\nlong long n, m, t, k, x, y, z, w, a, b;\nchar c = '?';\nvoid get() {\n cout << endl;\n cin >> x;\n if (x == -2) exit(0);\n}\nvoid solve(int k, int bit) {\n if (bit == -1) return;\n long long ax =...
Codeforces/1107/D
# Compression You are given a binary matrix A of size n × n. Let's denote an x-compression of the given matrix as a matrix B of size n/x × n/x such that for every i ∈ [1, n], j ∈ [1, n] the condition A[i][j] = B[⌈ i/x ⌉][⌈ j/x ⌉] is met. Obviously, x-compression is possible only if x divides n, but this condition is ...
[ { "input": { "stdin": "8\nE7\nE7\nE7\n00\n00\nE7\nE7\nE7\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4\n7\nF\nF\nF\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "8\nFF\nFF\n00\n00\nFF\nFF\n00\n00\n" }, "ou...
{ "tags": [ "dp", "implementation", "math", "number theory" ], "title": "Compression" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool isPrime(long long n) {\n if (n <= 1) return false;\n if (n <= 3) return true;\n if (n % 2 == 0 || n % 3 == 0) return false;\n for (long long i = 5; i * i <= n; i = i + 6)\n if (n % i == 0 || n % (i + 2) == 0) return false;\n return true;\n}\nvecto...
Codeforces/1136/D
# Nastya Is Buying Lunch At the big break Nastya came to the school dining room. There are n pupils in the school, numbered from 1 to n. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, ...
[ { "input": { "stdin": "5 2\n3 1 5 4 2\n5 2\n5 4\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 3\n3 1 2\n1 2\n3 1\n3 2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2 1\n1 2\n1 2\n" }, "output": { ...
{ "tags": [ "greedy" ], "title": "Nastya Is Buying Lunch" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 3e5 + 5;\nconst int INF = 1e9;\nconst long long mod = 1e9 + 7;\nvector<int> v[N];\nbool memo[N];\nint cnt[N], ans, arr[N], n, m;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n cin >> n >> m;\n for (int...
Codeforces/1155/A
# Reverse a Substring You are given a string s consisting of n lowercase Latin letters. Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position 3 and ends in position 6), but "aa" or "d" aren't substrings of this string. So the substring ...
[ { "input": { "stdin": "7\nabacaba\n" }, "output": { "stdout": "YES\n2 3\n" } }, { "input": { "stdin": "6\naabcfg\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "6\nbabcdc\n" }, "output": { "stdout": "YES\n1 2\n" ...
{ "tags": [ "implementation", "sortings", "strings" ], "title": "Reverse a Substring" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n int a, b;\n bool flag = 0;\n string s;\n cin >> n;\n cin >> s;\n for (int i = 0; i < n - 1; i++) {\n if (s[i] > s[i + 1]) {\n a = i;\n b = i + 1;\n flag = 1;\n break;\n }\n }\n if (flag == 0)\n cout...
Codeforces/1176/F
# Destroy it! You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game. The boss battle consists of n turns. During each turn, you will get several cards. Each card has two parameters: its cost c_i and damage d_i. You may play some of your cards dur...
[ { "input": { "stdin": "5\n3\n1 6\n1 7\n1 5\n2\n1 4\n1 3\n3\n1 10\n3 5\n2 3\n3\n1 15\n2 4\n1 10\n1\n1 100\n" }, "output": { "stdout": "263\n" } }, { "input": { "stdin": "5\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 1\n1 1\n1 1\n3\n1 100\n1 1\n1 1\n" }, ...
{ "tags": [ "dp", "implementation", "sortings" ], "title": "Destroy it!" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool isrange(int second, int first, int n, int m) {\n if (0 <= second && second < n && 0 <= first && first < m) return true;\n return false;\n}\nint dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},\n ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -...
Codeforces/1195/D2
# Submarine in the Rybinsk Sea (hard edition) This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers a_1, a_2, ..., a_n. A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom ...
[ { "input": { "stdin": "3\n12 3 45\n" }, "output": { "stdout": "12330\n" } }, { "input": { "stdin": "2\n123 456\n" }, "output": { "stdout": "1115598\n" } }, { "input": { "stdin": "20\n76 86 70 7 16 24 10 62 26 29 40 65 55 49 34 55 92 47 43 100...
{ "tags": [ "combinatorics", "math", "number theory" ], "title": "Submarine in the Rybinsk Sea (hard edition)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst double eps = 1e-6;\nconst int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};\nconst int maxn = 1e5 + 5;\nconst long long mo = 998244353;\nlong long n;\nlong long w[maxn];\nlong long C[55];\nlong long cnt[maxn];\nlong long table[5000];\nint main() {\n scanf(\"%...
Codeforces/1211/I
# Unusual Graph Ivan on his birthday was presented with array of non-negative integers a_1, a_2, …, a_n. He immediately noted that all a_i satisfy the condition 0 ≤ a_i ≤ 15. Ivan likes graph theory very much, so he decided to transform his sequence to the graph. There will be n vertices in his graph, and vertices u...
[ { "input": { "stdin": "4 4\n1 2\n2 3\n3 4\n4 1\n" }, "output": { "stdout": "\n0 1 0 1 \n" } }, { "input": { "stdin": "3 0\n" }, "output": { "stdout": "\n0 0 0 \n" } } ]
{ "tags": [ "*special", "graphs" ], "title": "Unusual Graph" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1236/A
# Stones Alice is playing with some stones. Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones. Each time she can do one of two operations: 1. take one stone from the first heap and two stones from the sec...
[ { "input": { "stdin": "3\n3 4 5\n1 0 5\n5 3 2\n" }, "output": { "stdout": "9\n0\n6\n" } }, { "input": { "stdin": "20\n9 4 8\n10 6 7\n4 6 0\n7 7 6\n3 3 10\n4 2 1\n4 4 0\n2 0 0\n8 8 7\n3 1 7\n3 10 7\n1 7 3\n7 9 1\n1 6 9\n0 9 5\n4 0 0\n2 10 0\n4 8 5\n10 0 1\n8 1 1\n" }, ...
{ "tags": [ "brute force", "greedy", "math" ], "title": "Stones" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int T, a, b, c;\n cin >> T;\n for (int t = 0; t < T; t++) {\n cin >> a >> b >> c;\n cout << (min(c / 2, b) * 3) + min(a, (b - min(c / 2, b)) / 2) * 3 << \"\\n\";\n }\n}\n", "java": "import java.io.OutputStream;\nimport java.io.IOExce...
Codeforces/1253/F
# Cheap Robot You're given a simple, undirected, connected, weighted graph with n nodes and m edges. Nodes are numbered from 1 to n. There are exactly k centrals (recharge points), which are nodes 1, 2, …, k. We consider a robot moving into this graph, with a battery of capacity c, not fixed by the constructor yet. ...
[ { "input": { "stdin": "10 9 3 1\n10 9 11\n9 2 37\n2 4 4\n4 1 8\n1 5 2\n5 7 3\n7 3 2\n3 8 4\n8 6 13\n2 3\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "9 11 3 2\n1 3 99\n1 4 5\n4 5 3\n5 6 3\n6 4 11\n6 7 21\n7 2 6\n7 8 4\n8 9 3\n9 2 57\n9 3 2\n3 1\n2 3\n" ...
{ "tags": [ "binary search", "dsu", "graphs", "shortest paths", "trees" ], "title": "Cheap Robot" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int NR = 1e6 + 10;\nvoid Min(long long& x, long long y) { x = min(x, y); }\nvoid Max(long long& x, long long y) { x = max(x, y); }\nint read() {\n int x = 0, f = 1;\n char ch = getchar();\n while (ch < '0' || ch > '9') {\n if (ch == '-') f = -1;\n ...
Codeforces/1277/E
# Two Fairs There are n cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from 1 to n. Two fairs are currently taking place in Berland — they are held in two different cities a and b (1 ≤...
[ { "input": { "stdin": "3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n3 4\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n" }, "output": { "stdout": "4\n0\n1\n" } }, { "input": { "stdin": "3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 4\n1 2\n2 3\n3 1\n4 2\n4 2...
{ "tags": [ "combinatorics", "dfs and similar", "dsu", "graphs" ], "title": "Two Fairs" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 2e5 + 10;\nint q, num;\nvector<int> v[maxn];\nbool vis[maxn];\nvoid dfs(int x) {\n for (int i = 0; i < v[x].size(); i++) {\n if (!vis[v[x][i]]) {\n num++;\n vis[v[x][i]] = 1;\n dfs(v[x][i]);\n }\n }\n}\nint main() {\n c...
Codeforces/1297/E
# Modernization of Treeland Treeland consists of n cities and n-1 two-way roads connecting pairs of cities. From every city, you can reach every other city moving only by the roads. You are right, the system of cities and roads in this country forms an undirected tree. The government has announced a program for the m...
[ { "input": { "stdin": "4\n10 4\n4 5\n5 2\n2 1\n1 3\n1 9\n9 10\n2 7\n7 8\n5 6\n4 3\n1 2\n2 3\n3 4\n5 3\n1 2\n1 3\n1 4\n1 5\n4 1\n1 2\n2 4\n2 3\n" }, "output": { "stdout": "\nYes\n9\n1 2 4 5 6 7 8 9 10 \nNo\nYes\n4\n1 3 4 5 \nYes\n1\n4 \n" } } ]
{ "tags": [ "*special", "dfs and similar", "trees" ], "title": "Modernization of Treeland" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1320/F
# Blocks and Sensors Polycarp plays a well-known computer game (we won't mention its name). Every object in this game consists of three-dimensional blocks — axis-aligned cubes of size 1 × 1 × 1. These blocks are unaffected by gravity, so they can float in the air without support. The blocks are placed in cells of size...
[ { "input": { "stdin": "1 1 1\n\n0\n\n0\n\n1337\n\n0\n\n0\n\n0\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "1 1 1\n\n1337\n\n1337\n\n1337\n\n1337\n\n1337\n\n1337\n" }, "output": { "stdout": "1337 " } }, { "input": { "stdin": ...
{ "tags": [ "brute force" ], "title": "Blocks and Sensors" }
{ "cpp": "#include <bits/stdc++.h>\nint read() {\n static int c, x;\n while ((c = getchar()) < 48) {\n }\n x = c & 15;\n while ((c = getchar()) >= 48) x = x * 10 + (c & 15);\n return x;\n}\nconst int dx[6] = {1, -1, 0, 0, 0, 0};\nconst int dy[6] = {0, 0, 1, -1, 0, 0};\nconst int dz[6] = {0, 0, 0, 0, 1, -1};\nbo...
Codeforces/133/D
# Piet Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules. The program will be a rectangular image consistin...
[ { "input": { "stdin": "5 9\n10345\n23456\n34567\n45678\n56789\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "2 10\n12\n43\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "3 12\n1423\n6624\n6625\n" }, "output": ...
{ "tags": [ "implementation" ], "title": "Piet" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint read() {\n int n;\n char c;\n bool neg;\n n = 0;\n neg = false;\n for (c = getchar(); !(c >= '0' && c <= '9') && c != '-'; c = getchar())\n ;\n if (c == '-') {\n neg = true;\n c = getchar();\n }\n while ((c >= '0' && c <= '9')) {\n n =...
Codeforces/1361/E
# James and the Chase James Bond has a new plan for catching his enemy. There are some cities and directed roads between them, such that it is possible to travel between any two cities using these roads. When the enemy appears in some city, Bond knows her next destination but has no idea which path she will choose to ...
[ { "input": { "stdin": "4\n3 3\n1 2\n2 3\n3 1\n3 6\n1 2\n2 1\n2 3\n3 2\n1 3\n3 1\n7 10\n1 2\n2 3\n3 1\n1 4\n4 5\n5 1\n4 6\n6 7\n7 4\n6 1\n6 8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 1\n6 2\n5 1\n" }, "output": { "stdout": "1 2 3 \n-1\n1 2 3 5 \n-1\n" } }, { "input": { "stdin": "4\n3 3\n...
{ "tags": [ "dfs and similar", "graphs", "probabilities", "trees" ], "title": "James and the Chase" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T>\ninline void read(T &n) {\n T w = 1;\n n = 0;\n char ch = getchar();\n while (!isdigit(ch) && ch != EOF) {\n if (ch == '-') w = -1;\n ch = getchar();\n }\n while (isdigit(ch) && ch != EOF) {\n n = (n << 3) + (n << 1) + (ch ...
Codeforces/1382/A
# Common Subsequence You are given two arrays of integers a_1,…,a_n and b_1,…,b_m. Your task is to find a non-empty array c_1,…,c_k that is a subsequence of a_1,…,a_n, and also a subsequence of b_1,…,b_m. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the small...
[ { "input": { "stdin": "5\n4 5\n10 8 6 4\n1 2 3 4 5\n1 1\n3\n3\n1 1\n3\n2\n5 3\n1000 2 2 2 3\n3 1 5\n5 5\n1 2 3 4 5\n1 2 3 4 5\n" }, "output": { "stdout": "YES\n1 4\nYES\n1 3\nNO\nYES\n1 3\nYES\n1 1\n" } }, { "input": { "stdin": "1\n2 2\n1 1\n1 2\n" }, "output": { ...
{ "tags": [ "brute force" ], "title": "Common Subsequence" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mx = 1e5 + 10;\nconst double PI = cos(-1.0);\nmap<int, int> a, b;\nint n, m, t;\nint main() {\n cin >> t;\n while (t--) {\n int x;\n cin >> n >> m;\n a.clear();\n b.clear();\n for (int i = 1; i <= n; i++) {\n cin >> x;\n a[...
Codeforces/1402/A
# Fancy Fence Everybody knows that Balázs has the fanciest fence in the whole town. It's built up from N fancy sections. The sections are rectangles standing closely next to each other on the ground. The ith section has integer height h_i and integer width w_i. We are looking for fancy rectangles on this fancy fence. ...
[ { "input": { "stdin": "2\n1 2\n1 2\n" }, "output": { "stdout": "\n12\n" } } ]
{ "tags": [ "*special", "data structures", "dsu", "implementation", "math", "sortings" ], "title": "Fancy Fence" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1424/N
# BubbleSquare Tokens BubbleSquare social network is celebrating 13^{th} anniversary and it is rewarding its members with special edition BubbleSquare tokens. Every member receives one personal token. Also, two additional tokens are awarded to each member for every friend they have on the network. Yet, there is a twis...
[ { "input": { "stdin": "2 1\n1 2\n" }, "output": { "stdout": "1\n1 \n1 2 0\n" } }, { "input": { "stdin": "3 3\n1 2\n1 3\n2 3\n" }, "output": { "stdout": "0\n\n1 2 0\n1 3 2\n2 3 1\n" } }, { "input": { "stdin": "8 0\n0 1\n" }, "output": ...
{ "tags": [], "title": "BubbleSquare Tokens" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1000005;\nint n, m, x[N], y[N], w[N], s[N], v[N], a[N];\nvector<pair<int, int>> e[N];\nvector<int> r;\nint main() {\n scanf(\"%d%d\", &n, &m);\n for (int i = 1; i <= m; i++) {\n scanf(\"%d%d\", &x[i], &y[i]);\n w[i] = 1, s[x[i]]++, s[y[i]...
Codeforces/1446/D2
# Frequency Problem (Hard Version) This is the hard version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest ...
[ { "input": { "stdin": "7\n1 1 2 2 3 3 3\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "10\n1 1 1 5 4 1 3 1 2 2\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "0\n" ...
{ "tags": [ "data structures", "greedy", "two pointers" ], "title": "Frequency Problem (Hard Version)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename Arg1>\nvoid prn(Arg1&& arg1) {\n cout << arg1 << \"\\n\";\n}\ntemplate <typename Arg1, typename... Args>\nvoid prn(Arg1&& arg1, Args&&... args) {\n cout << arg1 << \" \";\n prn(args...);\n}\ntemplate <typename Arg1>\nvoid prs(Arg1&& arg1)...
Codeforces/1470/E
# Strange Permutation Alice had a permutation p_1, p_2, …, p_n. Unfortunately, the permutation looked very boring, so she decided to change it and choose some non-overlapping subranges of this permutation and reverse them. The cost of reversing a single subrange [l, r] (elements from position l to position r, inclusiv...
[ { "input": { "stdin": "2\n3 1 9\n1 2 3\n1 1\n2 1\n3 1\n1 2\n2 2\n3 2\n1 3\n2 3\n3 3\n6 4 4\n6 5 4 3 1 2\n1 1\n3 14\n1 59\n2 6\n" }, "output": { "stdout": "\n1\n2\n3\n1\n3\n2\n2\n1\n3\n1\n4\n-1\n5\n" } }, { "input": { "stdin": "1\n12 4 2\n1 2 3 4 5 6 7 8 9 10 11 12\n2 20\n2 ...
{ "tags": [ "binary search", "combinatorics", "data structures", "dp", "graphs", "implementation", "two pointers" ], "title": "Strange Permutation" }
{ "cpp": "#include<bits/stdc++.h>\n#define ci const int&\n#define TOT(tc,ql,qr) ((qr>=0?s[qr][tc]:0)-(ql>0?s[ql-1][tc]:0))\nusing namespace std;\nstruct elem{\n\tint l,r;\n\tlong long num;\n};\nint T,n,C,Q,p[30010],li[30010][5],ri[30010][5],l,r,mid,qi,nc,ni,opl[5],opr[5],sz;\nlong long qj,s[120010][5];\nvector<elem>L...
Codeforces/1497/D
# Genius Please note the non-standard memory limit. There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i. After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ chan...
[ { "input": { "stdin": "5\n4\n1 2 3 4\n5 10 15 20\n4\n1 2 1 2\n5 10 15 20\n4\n2 2 4 1\n2 8 19 1\n2\n1 1\n6 9\n1\n1\n666\n" }, "output": { "stdout": "\n35\n30\n42\n0\n0\n" } }, { "input": { "stdin": "5\n4\n1 3 1 7\n5 23 28 16\n4\n0 1 1 2\n5 10 19 31\n4\n4 2 6 1\n0 14 8 0\n2\n...
{ "tags": [ "bitmasks", "dp", "graphs", "number theory" ], "title": "Genius" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main(){\n ios_base::sync_with_stdio(0);\n cin.tie();\n cout.tie();\n\n int tt;\n cin>>tt;\n while(tt--){\n long long n;\n cin>>n;\n vector <long long> t(n),s(n);\n for(long long i=0;i<n;i++) cin>>t[i];\n ...
Codeforces/151/C
# Win or Freeze You can't possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer q. During a move a player should write any integer number that is a non-trivial divisor of the last written number. Then he should run thi...
[ { "input": { "stdin": "1\n" }, "output": { "stdout": "1\n0" } }, { "input": { "stdin": "6\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "30\n" }, "output": { "stdout": "1\n6" } }, { "input": { "std...
{ "tags": [ "games", "math", "number theory" ], "title": "Win or Freeze" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long least_prime_factor(long long x) {\n long long lim = sqrt(x);\n for (int i = int(2); i <= int(lim); i++) {\n if (x % i == 0) return i;\n }\n return x;\n}\nvector<long long> prime_factorise(long long x) {\n vector<long long> ans;\n while (x > ...
Codeforces/1547/C
# Pair Programming Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming. It's known that they have worked together on the same file for n + m minutes. Every minute exactly one of them made one change to the file. Before they started, there were already k lines writte...
[ { "input": { "stdin": "5\n\n3 2 2\n2 0\n0 5\n\n4 3 2\n2 0 5\n0 6\n\n0 2 2\n1 0\n2 3\n\n5 4 4\n6 0 8 0\n0 7 0 9\n\n5 4 1\n8 7 8 0\n0\n" }, "output": { "stdout": "0 2 0 5\n0 2 0 5 6\n-1\n0 6 0 7 0 8 0 9\n-1\n" } }, { "input": { "stdin": "5\n\n3 2 2\n2 0\n0 5\n\n4 3 2\n2 0 5\n...
{ "tags": [ "greedy", "two pointers" ], "title": "Pair Programming" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pint = pair<int, int>;\nusing pll = pair<ll, ll>;\n\nint main(){\n int t; cin >> t;\n while(t--){\n int k, n, m; cin >> k >> n >> m;\n queue<int> a, b;\n int size = k;\n for...
Codeforces/174/E
# Ancient Berland Hieroglyphs Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them). Polycarpus...
[ { "input": { "stdin": "5 4\n1 2 3 4 5\n1 3 5 6\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3 3\n1 2 3\n3 2 1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "4 6\n1 3 5 2\n1 2 3 4 5 6\n" }, "output": { ...
{ "tags": [ "two pointers" ], "title": "Ancient Berland Hieroglyphs" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma comment(linker, \"/STACK:64000000\")\nusing namespace std;\nconst int INF = (int)1E9;\nconst long long INF64 = (long long)1E18;\nconst long double EPS = 1E-9;\nconst long double PI = 3.1415926535897932384626433832795;\nconst int MAXN = 2001000;\nint n1, n2, a1[MAXN], a2[MAX...
Codeforces/195/A
# Let's Watch Football Valeric and Valerko missed the last Euro football game, so they decided to watch the game's key moments on the Net. They want to start watching as soon as possible but the connection speed is too low. If they turn on the video right now, it will "hang up" as the size of data to watch per second ...
[ { "input": { "stdin": "10 3 2\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "13 12 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "4 1 1\n" }, "output": { "stdout": "3\n" } }, { "input"...
{ "tags": [ "binary search", "brute force", "math" ], "title": "Let's Watch Football" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a, b, c;\nint main() {\n scanf(\"%d%d%d\", &a, &b, &c);\n printf(\"%d\\n\", (a * c + b - 1) / b - c);\n return 0;\n}\n", "java": "import java.io.*;\nimport java.util.*;\n\n\npublic class A {\n public static void main(String[] args) throws Exception...
Codeforces/219/A
# k-String A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string. You are given ...
[ { "input": { "stdin": "2\naazz\n" }, "output": { "stdout": "azaz" } }, { "input": { "stdin": "3\nabcabcabz\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "2\naaab\n" }, "output": { "stdout": "-1\n" } }, { ...
{ "tags": [ "implementation", "strings" ], "title": "k-String" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int k;\n while (cin >> k) {\n string s;\n cin >> s;\n sort(s.begin(), s.end());\n string ans;\n int cnt = 1, n = s.size();\n for (int i = 0; i < n; ++i) {\n if (i == n - 1 || s[i] != s[i + 1]) {\n if (cnt % k != 0...
Codeforces/242/C
# King's Path The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is...
[ { "input": { "stdin": "3 4 3 10\n3\n3 1 4\n4 5 9\n3 10 10\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1 1 2 10\n2\n1 1 3\n2 6 10\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 7 6 11\n3\n5 3 8\n6 7 11\n5 2 ...
{ "tags": [ "dfs and similar", "graphs", "hashing", "shortest paths" ], "title": "King's Path" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n pair<int, int> start, target;\n scanf(\"%d %d %d %d\", &start.first, &start.second, &target.first,\n &target.second);\n int n;\n set<pair<int, int> > mat;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n int a, b, c;\n s...
Codeforces/268/A
# Games Manao works on a sports TV. He's spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team ...
[ { "input": { "stdin": "2\n1 2\n1 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n100 42\n42 100\n5 42\n100 5\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "3\n1 2\n2 4\n3 4\n" }, "output": { "st...
{ "tags": [ "brute force" ], "title": "Games" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n int ans = 0;\n vector<pair<int, int> > v(n);\n for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n if (v[i].first == v[j].second) ans++;\n...
Codeforces/290/D
# Orange <image> Input The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase. The second line of the input is an integer between 0 and 26, inclusive. Output Output the required string. Examples Input Ap...
[ { "input": { "stdin": "AprilFool\n14\n" }, "output": { "stdout": "AprILFooL" } }, { "input": { "stdin": "qH\n2\n" }, "output": { "stdout": "qh" } }, { "input": { "stdin": "nifzlTLaeWxTD\n0\n" }, "output": { "stdout": "nifzltlaew...
{ "tags": [ "*special", "implementation" ], "title": "Orange" }
{ "cpp": "#include <bits/stdc++.h>\nchar a[100];\nint main() {\n int i, n, m;\n scanf(\"%s%d\", a, &m);\n n = strlen(a);\n for (i = 0; i < n; i++)\n if (a[i] < 'a') a[i] += 'a' - 'A';\n for (i = 0; i < n; i++) {\n if (a[i] < m + 97) {\n a[i] -= 'a' - 'A';\n }\n }\n puts(a);\n}\n", "java": "impo...
Codeforces/316/B2
# EKG In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another. (Cultural note: standing in huge and disorganized queues for hours is a ...
[ { "input": { "stdin": "6 2\n2 3 0 5 6 0\n" }, "output": { "stdout": "2\n5\n" } }, { "input": { "stdin": "6 2\n0 0 1 0 4 5\n" }, "output": { "stdout": "1\n3\n4\n6\n" } }, { "input": { "stdin": "6 1\n2 0 4 0 6 0\n" }, "output": { ...
{ "tags": [ "dfs and similar", "dp" ], "title": "EKG" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool table[1010];\nint mae[1010], ato[1010];\nint n, x;\nvoid init() {}\nvoid input() {\n for (int i = (0); i < (1010); i++) table[i] = false;\n table[0] = true;\n cin >> n >> x;\n for (int i = (0); i < (1010); i++) mae[i] = ato[i] = 0;\n for (int i = (0)...
Codeforces/339/A
# Helpful Maths Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation. The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that is...
[ { "input": { "stdin": "2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3+2+1\n" }, "output": { "stdout": "1+2+3\n" } }, { "input": { "stdin": "1+1+3+1+3\n" }, "output": { "stdout": "1+1+1+3+3\n" } }, { ...
{ "tags": [ "greedy", "implementation", "sortings", "strings" ], "title": "Helpful Maths" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string s;\n char temp;\n cin >> s;\n for (int x = 0; x < s.length(); x += 2) {\n for (int y = 0; y < s.length(); y += 2) {\n if (s[x] < s[y]) {\n temp = s[x];\n s[x] = s[y];\n s[y] = temp;\n }\n }\n }\n ...
Codeforces/361/C
# Levko and Array Recovery Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: 1. Increase all elements from li to ri by di. In other words, perform assignm...
[ { "input": { "stdin": "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8\n" }, "output": { "stdout": "YES\n8 7 4 7 " } }, { "input": { "stdin": "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13\n" }, "output": { "stdout": "NO\n" } }, { "input": { ...
{ "tags": [ "greedy", "implementation" ], "title": "Levko and Array Recovery" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool debug = false;\nint n, m, k;\nint dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};\nlong long ln, lk, lm;\nint a[5105], b[5105];\nint t[5105], l[5105], r[5105], x[5105];\nint main() {\n scanf(\"%d%d\", &n, &m);\n for (int(i) = 0; (i) < (int)(5105); (i)++) ...
Codeforces/385/A
# Bear and Raspberry The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following n days. According to the bear's data, on the i-th (1 ≤ i ≤ n) day, the price for one barrel of honey is going to is xi kilos of raspberry....
[ { "input": { "stdin": "6 2\n100 1 10 40 10 40\n" }, "output": { "stdout": "97\n" } }, { "input": { "stdin": "5 1\n5 10 7 3 20\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "3 0\n1 2 3\n" }, "output": { "stdout": "...
{ "tags": [ "brute force", "greedy", "implementation" ], "title": "Bear and Raspberry" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, c, l = 0;\n cin >> n >> c;\n int a[n];\n {\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n if (i == 1) {\n l = a[0] - a[1];\n }\n if (i >= 2 && (a[i - 1] - a[i] > l)) {\n l = a[i - 1] - a[i];\n ...
Codeforces/405/E
# Graph Cutting Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest. Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chr...
[ { "input": { "stdin": "3 2\n1 2\n2 3\n" }, "output": { "stdout": "1 2 3\n" } }, { "input": { "stdin": "3 3\n1 2\n2 3\n3 1\n" }, "output": { "stdout": "No solution\n" } }, { "input": { "stdin": "8 12\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n3 5\n3 6\n5 ...
{ "tags": [ "dfs and similar", "graphs" ], "title": "Graph Cutting" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int T = 1e5 + 5;\nstruct node {\n int num, x;\n} t;\nvector<node> p[T];\nbool flag[T];\nint dfs(int now) {\n queue<int> q;\n int l = p[now].size();\n for (int i = 0; i < l; i++) {\n node k = p[now][i];\n if (flag[k.num])\n continue;\n e...
Codeforces/433/A
# Kitahara Haruki's Gift Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends. Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to T...
[ { "input": { "stdin": "4\n100 100 100 200\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "3\n100 200 100\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "9\n100 100 100 200 100 100 200 100 200\n" }, "outp...
{ "tags": [ "brute force", "implementation" ], "title": "Kitahara Haruki's Gift" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, x = 0, y = 0;\n cin >> n;\n for (int i = 0; i < n; i++) {\n int a;\n cin >> a;\n if (a == 100)\n x++;\n else if (a == 200)\n y++;\n }\n if (x % 2 != 0 && y % 2 != 0)\n cout << \"NO\";\n else if (x % 2 != 0 &...
Codeforces/455/C
# Civilization Andrew plays a game called "Civilization". Dima helps him. The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct cities v1, v2, ..., vk, t...
[ { "input": { "stdin": "6 0 6\n2 1 2\n2 3 4\n2 5 6\n2 3 2\n2 5 3\n1 1\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "10 3 5\n1 2\n2 3\n1 4\n2 3 6\n1 6\n2 1 6\n2 8 7\n1 10\n" }, "output": { "stdout": "3\n0\n" } }, { "input": { "s...
{ "tags": [ "dfs and similar", "dp", "dsu", "ternary search", "trees" ], "title": "Civilization" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> adj[300005];\nint par[300005], longest[300005], Rank[300005];\nbool vis[300005];\nint find_par(int x) {\n if (par[x] == x) return x;\n return par[x] = find_par(par[x]);\n}\nvoid union_sets(int x, int y) {\n x = find_par(x), y = find_par(y);\n i...
Codeforces/478/A
# Initial Bet There are five people playing a game called "Generosity". Each person gives some non-zero number of coins b as an initial bet. After all players make their bets of b coins, the following operation is repeated for several times: a coin is passed from one player to some other player. Your task is to write...
[ { "input": { "stdin": "2 5 4 0 4\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "4 5 9 2 1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "99 100 100 100 100\n" }, "output": { "stdout": "-1\n" } ...
{ "tags": [ "implementation" ], "title": "Initial Bet" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int c1, c2, c3, c4, c5;\n std::cin >> c1 >> c2 >> c3 >> c4 >> c5;\n int sum = c1 + c2 + c3 + c4 + c5;\n std::cout << ((sum % 5 == 0 && sum != 0) ? (sum / 5) : -1);\n}\n", "java": "import java.io.*;\nimport java.util.StringTokenizer;\n\npublic class initialbet {...
Codeforces/500/C
# New Year Book Reading New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically...
[ { "input": { "stdin": "3 5\n1 2 3\n1 3 2 3 1\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "50 50\n75 71 23 37 28 23 69 75 5 62 3 11 96 100 13 50 57 51 8 90 4 6 84 27 11 89 95 81 10 62 48 52 69 87 97 95 30 74 21 42 36 64 31 80 81 50 56 53 33 99\n26 30 5 33 3...
{ "tags": [ "constructive algorithms", "greedy", "implementation", "math" ], "title": "New Year Book Reading" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m;\nint a[1010];\nint v[1010];\nint vis[1010];\nint main(void) {\n cin >> n >> m;\n for (int i = 1; i <= n; ++i) {\n cin >> a[i];\n }\n for (int i = 0; i < m; ++i) {\n cin >> v[i];\n }\n int ac = 0;\n for (int i = 0; i < m; ++i) {\n mems...
Codeforces/526/A
# King of Thieves In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way. <image> An interesting feature of the game is that you can des...
[ { "input": { "stdin": "16\n.**.*..*.***.**.\n" }, "output": { "stdout": "yes" } }, { "input": { "stdin": "11\n.*.*...*.*.\n" }, "output": { "stdout": "no" } }, { "input": { "stdin": "20\n.*..*...*....*.....*\n" }, "output": { "s...
{ "tags": [ "brute force", "implementation" ], "title": "King of Thieves" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstring s;\nint n;\nbool check(int pos, int len) {\n if (pos + 4 * len >= n) return false;\n for (int i = 0; i < 5; i++)\n if (s[pos + i * len] == '.') return false;\n return true;\n}\nint main() {\n cin >> n >> s;\n for (int i = 0; i < n; i++)\n for...
Codeforces/551/B
# ZgukistringZ Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one. GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Subst...
[ { "input": { "stdin": "pozdravstaklenidodiri\nniste\ndobri\n" }, "output": { "stdout": "nisteaadddiiklooprrvz\n" } }, { "input": { "stdin": "aaa\na\nb\n" }, "output": { "stdout": "aaa\n" } }, { "input": { "stdin": "abbbaaccca\nab\naca\n" ...
{ "tags": [ "brute force", "constructive algorithms", "implementation", "strings" ], "title": "ZgukistringZ" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar a[100005], b[100005], c[100005];\nvector<char> arr1, arr2, val;\nmap<char, int> mp1, mp2, mp3;\nint main() {\n cin >> a;\n cin >> b;\n cin >> c;\n int len2 = strlen(b);\n for (int i = 0; i < len2; i++) {\n if (mp1.count(b[i]) == 0) {\n arr1.p...
Codeforces/578/C
# Weakness and Poorness You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible. The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences)...
[ { "input": { "stdin": "10\n1 10 2 9 3 8 4 7 5 6\n" }, "output": { "stdout": "4.500000000000024\n" } }, { "input": { "stdin": "4\n1 2 3 4\n" }, "output": { "stdout": "2.000000000000003\n" } }, { "input": { "stdin": "3\n1 2 3\n" }, "out...
{ "tags": [ "ternary search" ], "title": "Weakness and Poorness" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst double e = exp(1.0);\nconst double INF = 1e9;\nconst double eps = 1e-11;\ndouble maxX = -INF;\ndouble minX = INF;\nconst int N = 200000 + 10;\ndouble a[N];\ndouble fx(double v, int n) {\n double tmp = a[0] - v;\n double neg_tmp = a[0] - v;\n bool flag...
Codeforces/5/A
# Chat Server's Outgoing Traffic Polycarp is working on a new project called "Polychat". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types...
[ { "input": { "stdin": "+Mike\nMike:hello\n+Kate\n+Dmitry\n-Dmitry\nKate:hi\n-Kate\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "+Mike\n-Mike\n+Mike\nMike:Hi I am here\n-Mike\n+Kate\n-Kate\n" }, "output": { "stdout": "14\n" } }, { ...
{ "tags": [ "implementation" ], "title": "Chat Server's Outgoing Traffic" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n char str[101];\n int num = 0, ans = 0, len;\n while (gets(str)) {\n if (str[0] == '+')\n num++;\n else if (str[0] == '-')\n num--;\n else {\n len = strlen(str) - 1;\n for (int i = 0; str[i] != ':'; i++) len--;\n ans += num * len;\...
Codeforces/621/D
# Rat Kwesh and Cheese Wet Shark asked Rat Kwesh to generate three positive real numbers x, y and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point. Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give th...
[ { "input": { "stdin": "1.1 3.4 2.5\n" }, "output": { "stdout": "z^y^x\n" } }, { "input": { "stdin": "1.9 1.8 1.7\n" }, "output": { "stdout": "(x^y)^z\n" } }, { "input": { "stdin": "2.0 2.0 2.0\n" }, "output": { "stdout": "x^y^z\...
{ "tags": [ "brute force", "constructive algorithms", "math" ], "title": "Rat Kwesh and Cheese" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong double x, y, z, ans = -1;\nint num;\nint main() {\n std::ios_base::sync_with_stdio(false);\n cin >> x >> y >> z;\n if (ans < pow(y, z) * log(x)) ans = pow(y, z) * log(x), num = 1;\n if (ans < pow(z, y) * log(x)) ans = pow(z, y) * log(x), num = 2;\n i...
Codeforces/643/B
# Bear and Two Paths Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities. Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a ...
[ { "input": { "stdin": "1000 999\n10 20 30 40\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "7 11\n2 4 7 3\n" }, "output": { "stdout": "2 7 1 5 6 3 4 \n7 2 1 5 6 4 3 " } }, { "input": { "stdin": "1000 1001\n217 636 713 516\n" ...
{ "tags": [ "constructive algorithms", "graphs" ], "title": "Bear and Two Paths" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint arr[1005], seq[1005];\nint main() {\n int n, k, a, b, c, d, j, i;\n scanf(\"%d\", &n);\n scanf(\"%d\", &k);\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n scanf(\"%d\", &c);\n scanf(\"%d\", &d);\n seq[1] = a;\n seq[2] = c;\n seq[n] = b;\n seq[n - 1...
Codeforces/670/B
# Game of Robots In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109. At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says h...
[ { "input": { "stdin": "4 5\n10 4 18 3\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "2 2\n1 2\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4 9\n5 1000000000 999999999 12\n" }, "output": { "stdout": "9...
{ "tags": [ "implementation" ], "title": "Game of Robots" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxx = 1e5 + 10;\nint a[maxx];\nint main() {\n int n, k;\n cin >> n >> k;\n for (int i = 1; i <= n; i++) cin >> a[i];\n for (int i = 1; i <= n; i++) {\n if (k > i) {\n k -= i;\n } else\n break;\n }\n cout << a[k] << endl;\n ret...
Codeforces/691/D
# Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal permutation one can get? Let p and q be two permutations of the n...
[ { "input": { "stdin": "9 6\n1 2 3 4 5 6 7 8 9\n1 4\n4 7\n2 5\n5 8\n3 6\n6 9\n" }, "output": { "stdout": "7 8 9 4 5 6 1 2 3 \n" } }, { "input": { "stdin": "3 10\n2 3 1\n1 1\n3 3\n3 3\n3 2\n1 1\n2 2\n3 1\n1 3\n2 1\n3 3\n" }, "output": { "stdout": "3 2 1 \n" ...
{ "tags": [ "dfs and similar", "dsu", "math" ], "title": "Swaps in Permutation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m;\nvector<int> temp, idx;\nvector<int> graph[1000005];\nbool visit[1000005];\nint res[1000005], per[1000005];\nvoid dfs(int node) {\n visit[node] = true;\n temp.push_back(per[node]);\n idx.push_back(node);\n for (int i = 0; i < graph[node].size(); ...
Codeforces/716/D
# Complete The Graph ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m edges between them. Each edge of the graph is weighted, each weight is a positive integer. The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassign positive integer wei...
[ { "input": { "stdin": "5 5 13 0 4\n0 1 5\n2 1 2\n3 2 3\n1 4 0\n4 3 4\n" }, "output": { "stdout": "YES\n0 1 5\n2 1 2\n3 2 3\n1 4 8\n4 3 4\n" } }, { "input": { "stdin": "2 1 123456789 0 1\n0 1 0\n" }, "output": { "stdout": "YES\n0 1 123456789\n" } }, { ...
{ "tags": [ "binary search", "constructive algorithms", "graphs", "shortest paths" ], "title": "Complete The Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n, m, s, t, l, rest;\nlong long d[1010], d2[1010], w[10010];\nbool zeredg[10010];\npair<long long, long long> edg[10010];\nvector<vector<long long> > edglist;\nvoid dijkstra(long long dist[], bool sec) {\n set<pair<long long, long long> > najbl;\n ...
Codeforces/737/A
# Road to Cinema Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in t minutes. There is a straight road of length s from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema i...
[ { "input": { "stdin": "3 1 8 10\n10 8\n5 7\n11 9\n3\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "2 2 10 18\n10 4\n20 6\n5 3\n" }, "output": { "stdout": "20\n" } }, { "input": { "stdin": "1 1 2 2\n1000000000 1000000000\n1\n" ...
{ "tags": [ "binary search", "greedy", "sortings" ], "title": "Road to Cinema" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Car {\n long long cost, cap;\n bool operator<(const Car& rhs) const {\n return cap < rhs.cap || (cap == rhs.cap && cost > rhs.cost);\n }\n};\nconst long long N = 1e6;\nlong long n, K, s, t;\nlong long gas[N];\nbool check(long long cap) {\n long l...
Codeforces/760/F
# Bacterial Melee Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z". The testtube is divided in...
[ { "input": { "stdin": "7\nabacaba\n" }, "output": { "stdout": "589\n" } }, { "input": { "stdin": "3\naaa\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "2\nab\n" }, "output": { "stdout": "3\n" } }, { "i...
{ "tags": [ "brute force", "combinatorics", "dp", "string suffix structures" ], "title": "Bacterial Melee" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint N = 1000000007;\nvector<vector<long long>> binom(5001, vector<long long>(5001));\nvoid find_binom() {\n for (int i = 0; i < binom.size(); ++i) {\n for (int j = 0; j < binom[1].size(); ++j) {\n if (j <= i) {\n if (j == 0 || j == i) {\n ...
Codeforces/784/B
# Kids' Riddle Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it? Input The input contains a single integer n (0 ≤ n ≤ 2000000000). Output Output a single integer. Examples Input 11 Output 2 Input 14 Output 0 Input 61441 Output 2 Input 571576 Output 10 Input 212...
[ { "input": { "stdin": "14\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "2128506\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "11\n" }, "output": { "stdout": "2" } }, { "input": { "st...
{ "tags": [ "*special" ], "title": "Kids' Riddle" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstring transform(int x, int y, string s) {\n string res = \"\";\n int sum = 0;\n for (int i = 0; i < s.length(); ++i) {\n if (s[i] == '-') continue;\n if (s[i] >= '0' && s[i] <= '9') {\n sum = sum * x + s[i] - '0';\n } else {\n sum = sum ...
Codeforces/805/A
# Fake NP Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path. You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of ti...
[ { "input": { "stdin": "19 29\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3 6\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "252662256 252662260\n" }, "output": { "stdout": "2\n" } }, { ...
{ "tags": [ "greedy", "math" ], "title": "Fake NP" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n long long l, r;\n scanf(\"%lld%lld\", &l, &r);\n if (l != r) {\n printf(\"2\");\n } else if (l == r) {\n printf(\"%lld\", l);\n }\n return 0;\n}\n", "java": "import java.util.*;\n\npublic class CF_805A{\n public static void main(String[] args){\n Sc...
Codeforces/830/A
# Office Keys There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to d...
[ { "input": { "stdin": "1 2 10\n11\n15 7\n" }, "output": { "stdout": "7" } }, { "input": { "stdin": "2 4 50\n20 100\n60 10 40 80\n" }, "output": { "stdout": "50" } }, { "input": { "stdin": "1 1 10\n10\n10\n" }, "output": { "stdou...
{ "tags": [ "binary search", "brute force", "dp", "greedy", "sortings" ], "title": "Office Keys" }
{ "cpp": "#include <bits/stdc++.h>\ntemplate <class t>\nvoid maxi(t &x, t y) {\n if (x < y) x = y;\n}\ntemplate <class t>\nvoid mini(t &x, t y) {\n if (x > y) x = y;\n}\nusing namespace std;\nint n, k;\nlong long a[3005], b[3005], dp[3005][3005], p;\nlong long solve(int i, int j) {\n if (i == n) return 0;\n if (j...
Codeforces/851/B
# Arpa and an exam about geometry Arpa is taking a geometry exam. Here is the last problem of the exam. You are given three points a, b, c. Find a point and an angle such that if we rotate the page around the point by the angle, the new position of a is the same as the old position of b, and the new position of b is...
[ { "input": { "stdin": "0 1 1 1 1 0\n" }, "output": { "stdout": "Yes\n" } }, { "input": { "stdin": "1 1 0 0 1000 1000\n" }, "output": { "stdout": "No\n" } }, { "input": { "stdin": "264193194 -448876521 736684426 -633906160 -328597212 -47935734...
{ "tags": [ "geometry", "math" ], "title": "Arpa and an exam about geometry" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long ax, ay, bx, by, cx, cy;\nlong long dis(long long x, long long y, long long xx, long long yy) {\n return (xx - x) * (xx - x) + (yy - y) * (yy - y);\n}\nint main() {\n cin >> ax >> ay >> bx >> by >> cx >> cy;\n if (ax == bx && bx == cx && ay == by &...
Codeforces/875/D
# High Cry Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :) Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteri...
[ { "input": { "stdin": "5\n3 2 1 6 5\n" }, "output": { "stdout": "8" } }, { "input": { "stdin": "4\n3 3 3 3\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "228\n1 3 1 7 1 3 1 15 1 3 1 7 1 3 1 31 1 3 1 7 1 3 1 15 1 3 1 7 1 3 1 63 1 ...
{ "tags": [ "binary search", "bitmasks", "combinatorics", "data structures", "divide and conquer" ], "title": "High Cry" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n;\nlong long a[210000];\nint main() {\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> a[i];\n }\n long long ans = n * (n + 1) / 2;\n long long lidx[n];\n long long ridx[n];\n for (int i = 0; i < n; i++) {\n lidx[i] = 0;\n ridx[i...
Codeforces/89/C
# Chip Play Let's consider the following game. We have a rectangular field n × m in size. Some squares of the field contain chips. Each chip has an arrow painted on it. Thus, each chip on the field points in one of the following directions: up, down, left or right. The player may choose a chip and make a move with i...
[ { "input": { "stdin": "4 4\nDRLD\nU.UL\n.UUR\nRDDL\n" }, "output": { "stdout": "10 1\n" } }, { "input": { "stdin": "3 5\n.D...\nRRRLL\n.U...\n" }, "output": { "stdout": "6 2\n" } }, { "input": { "stdin": "10 10\n.L.....R..\n.........U\n..D......
{ "tags": [ "brute force", "data structures", "implementation" ], "title": "Chip Play" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int64;\ntypedef unsigned long long uint64;\nconst double pi = acos(-1.0);\nconst double eps = 1e-11;\ntemplate <class T>\ninline void checkmin(T &a, T b) {\n if (b < a) a = b;\n}\ntemplate <class T>\ninline void checkmax(T &a, T b) {\n if (...
Codeforces/920/E
# Connected Components? You are given an undirected graph consisting of n vertices and <image> edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge ...
[ { "input": { "stdin": "5 5\n1 2\n3 4\n3 2\n4 2\n2 5\n" }, "output": { "stdout": "2\n1 4 \n" } }, { "input": { "stdin": "7 20\n4 6\n6 7\n4 5\n1 2\n2 4\n1 7\n3 5\n2 1\n6 2\n6 1\n7 3\n3 2\n3 6\n3 1\n3 4\n2 5\n1 6\n7 4\n6 3\n7 5\n" }, "output": { "stdout": "3\n1 2...
{ "tags": [ "data structures", "dfs and similar", "dsu", "graphs" ], "title": "Connected Components?" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 200009;\nint n, m, a[MAXN];\nvector<int> v[MAXN];\nlist<int> l;\nbool vis[MAXN], con[MAXN];\nint bfs(int x) {\n queue<int> q;\n q.push(x);\n int tmp = 0;\n while (!q.empty()) {\n x = q.front();\n q.pop();\n if (vis[x]) continue;\n...
Codeforces/949/C
# Data Center Maintenance BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!). Main feature of services offered by BigData Inc. is the access availability ...
[ { "input": { "stdin": "3 3 5\n4 4 0\n1 3\n3 2\n3 1\n" }, "output": { "stdout": "1\n3 \n" } }, { "input": { "stdin": "4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3\n" }, "output": { "stdout": "4\n1 2 3 4 \n" } }, { "input": { "stdin": "10 9 5\n0 0 0...
{ "tags": [ "dfs and similar", "graphs" ], "title": "Data Center Maintenance" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 1e5 + 10;\nint uptime[maxn];\nvector<int> ve[maxn];\nint res[maxn];\nint pp[maxn], cnt[maxn];\nint e = 0, dns[maxn], low[maxn], tot, stack1[maxn], vis[maxn];\nint cd[maxn];\nmap<pair<int, int>, int> cao;\nvoid Tarjan(int pos) {\n low[pos] = d...
Codeforces/977/B
# Two-gram Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams. You are given a string s consisting of n capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive ch...
[ { "input": { "stdin": "5\nZZZAA\n" }, "output": { "stdout": "ZZ\n" } }, { "input": { "stdin": "7\nABACABA\n" }, "output": { "stdout": "AB\n" } }, { "input": { "stdin": "15\nMIRZOYANOVECLOX\n" }, "output": { "stdout": "MI\n" ...
{ "tags": [ "implementation", "strings" ], "title": "Two-gram" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m = 0, k, j, i;\n cin >> n;\n string s, s1, s2;\n cin >> s;\n char c;\n map<string, int> m1;\n for (i = 0; i < n - 1; i++) {\n s1.push_back(s[i]);\n s1.push_back(s[i + 1]);\n m1[s1]++;\n if (m < m1[s1]) {\n m = max...
Codeforces/996/F
# Game Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to m...
[ { "input": { "stdin": "2 0\n1 1 1 1\n" }, "output": { "stdout": "1.0000000000\n" } }, { "input": { "stdin": "2 2\n0 1 2 3\n2 5\n0 4\n" }, "output": { "stdout": "1.5000000000\n2.2500000000\n3.2500000000\n" } }, { "input": { "stdin": "1 0\n2 3\...
{ "tags": [ "math" ], "title": "Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n int n, r;\n cin >> n >> r;\n long double res = 0;\n vector<int> v(1 << n);\n for (int i = 0; i < (int)1 << n; i++) {\n cin >> v[i];\n res += v[i];\n }\n long double pw = 1;\n for (int...
HackerEarth/battle-of-stalingrad-1
The bloodiest battle of World War II has started. Germany and its allies have attacked the Soviet union army to gain control of the city of Stalingrad (now Volgograd) in the south-western Soviet Union.The war has become intensive and the soviet union's 64th army is fighting against the germans. General "Vasily Chuikov"...
[ { "input": { "stdin": "36\n2 2 2 8 9 4 8 5\n9 1 7 6 10 6 9 3\n10 2 4 6 6 2 1 1\n9 6 2 3 5 7 7 3\n6 6 3 9 10 9 1 3\n1 3 10 5 10 10 4 1\n10 3 10 4 5 5 3 4\n10 3 4 9 3 3 7 3\n6 9 9 2 6 4 3 3\n7 2 1 10 4 7 6 2\n3 4 3 4 3 2 9 5\n7 8 2 9 8 3 2 3\n5 4 6 9 4 4 8 3\n5 2 2 8 1 5 7 5\n4 7 10 2 10 9 2 3\n4 8 2 3 6 6 ...
{ "tags": [], "title": "battle-of-stalingrad-1" }
{ "cpp": null, "java": null, "python": "def Area(x1,y1,x2,y2,x3,y3):\n\tar = abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0)\n\treturn ar\n\nT = int(raw_input())\n\nwhile T>0:\n\tdata = raw_input()\n\t\n\tinputdata = data.split(' ')\n\tSX = int(inputdata[0])\n\tSY = int(inputdata[1])\n\tSAX = int(inputdata[2])\n\t...
HackerEarth/class-homework
Ramesh and Suresh were in the same class and got home work from their mathematics teacher. The Homework consists of N strings and each string consists of only digits. The task which they need to perform is that they need to divide the string into 4 integers such that their sum is maximum. Note: Each integer should be...
[ { "input": { "stdin": "3\n4251\n52310\n00006\n\nSAMPLE" }, "output": { "stdout": "12\n56\nunlucky" } }, { "input": { "stdin": "100\n04\n9\n02163\n003000\n08041479\n80750340926070052\n90056\n11680\n90\n09020809009020989\n00460224000800\n0606\n6402000040\n3918080\n31052060072...
{ "tags": [], "title": "class-homework" }
{ "cpp": null, "java": null, "python": "T=input()\nbest=-1\nbound=10**12\nboundS=str(bound)\ndef findBest(s,off,n,sofar):\n\t#print \"looking for best fo\",s[off:],n\n\tremain=len(s)-off\n\tif remain<n: return\n\tglobal best\n\tif n==1:\n\t\tif remain>13:\n\t\t\treturn\n\t\tif s[off]==\"0\":\n\t\t\tif len(s)-off>...
HackerEarth/even-from-end-1
Problem Description Given a list of integers, find and display all even numbers from the end of the list. Input Format Each line of input begins with an integer N indicating the number of integer n that follow which comprises a list. Output Format All even numbers from the end of the list, each separated by a sin...
[ { "input": { "stdin": "5 10 25 12 4 1 \n3 16 28 100\n\nSAMPLE" }, "output": { "stdout": "4 12 10 \n100 28 16" } }, { "input": { "stdin": "21 3 -3 3 -3 3 -3 19 3 -3 3 -3 3 -3 19 3 -3 3 -3 3 -3 19" }, "output": { "stdout": "None to display" } }, { "i...
{ "tags": [], "title": "even-from-end-1" }
{ "cpp": null, "java": null, "python": "while True: \n try:\n s = raw_input()\n except: \n break\n if len(s.rstrip().lstrip())==0: \n break\n ser = map(int, s.split())\n srr = ser[1:][::-1]\n found = False\n for x in srr:\n if x % 2 == 0:\n ...
HackerEarth/hermione-vs-draco
Draco Malfoy and Hermione Granger have gotten into a "battle of brains". Draco was foolish enough to challenge her to a Arithmancy problem. Septima Vector, Arithmancy teacher at Hogwarts, has agreed to give them both a problem which they should solve overnight. The problem is as follows :- Firstly, a function F (fro...
[ { "input": { "stdin": "6\n3\n60\n100\n1024\n23456\n8735373\n\nSAMPLE" }, "output": { "stdout": "0\n14\n24\n253\n5861\n2183837\n" } }, { "input": { "stdin": "6\n2\n8\n011\n40\n29804\n234876\n\nANTSNE" }, "output": { "stdout": "0\n1\n2\n9\n7447\n58717\n" } ...
{ "tags": [], "title": "hermione-vs-draco" }
{ "cpp": null, "java": null, "python": "for t in range(input()):\n n=long(raw_input())\n count=0\n power=1\n d=n/pow(5,power)\n while d>=1:\n d=n/pow(5,power)\n power=power+1\n count=count+d\n print count" }
HackerEarth/magic-gcd
Given 2 numbers n1 and n2, following operations can be performed: 1) Decrement n1 by 1(if n1>1) 2) Decrement n2 by 1(if n2>1) 3) Incremenet n1 by 1 4) Incremenet n2 by 1 Find the maximum possible value of gcd(n1,n2) with atmost k operations. Note: Any of the 4 operations counts for one operation. gcd(n1,n2) refers to G...
[ { "input": { "stdin": "1\n9\n10\n1\n\nSAMPLE" }, "output": { "stdout": "10" } }, { "input": { "stdin": "4\n14723\n5512\n100\n28436\n33903\n100\n84816\n75747\n100\n92281\n31380\n100" }, "output": { "stdout": "1848\n1357\n3031\n2097" } }, { "input": ...
{ "tags": [], "title": "magic-gcd" }
{ "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\nfor _ in range (input()):\n\tn1 = int(raw_input())\n\tn2 = int(raw_input())\n\tk = int(raw_input())\n\tsmaller = min(n1,n2)\n\tg...
HackerEarth/new-world-11
The time has arrived when the world is going to end. But don't worry, because the new world yuga will start soon. Manu (carrier of mankind) has been assigned the job to carry all the necessary elements of current yuga to the upcoming yuga. There are N stones arranged in a straight line. In order to fulfill the task, M...
[ { "input": { "stdin": "2\n4 1\n2 15 36 43 \n3 2\n27 30 35 \n\nSAMPLE" }, "output": { "stdout": "41\n5\n" } }, { "input": { "stdin": "2\n4 1\n2 15 36 43 \n3 2\n18 30 35 \n\nSAMPLE" }, "output": { "stdout": "41\n12\n" } }, { "input": { "stdin":...
{ "tags": [], "title": "new-world-11" }
{ "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'''\ndef check_steps(steps,val):\n\tcounter=0\n\ttmp=0\n\tn=len(steps)\n\tfor i in range(n):\n\t\tif tmp+steps[i]<=val:\n\t\t\ttmp+=steps...
HackerEarth/professor-sharma
Professor Sharma gives the following problem to his students: given two integers X( ≥ 2) and Y( ≥ 2) and tells them to find the smallest positive integral exponent E such that the decimal expansion of X^E begins with Y. For example, if X = 8 and Y= 51, then X^3 = 512 begins with Y= 51, so E= 3. Professor Sharma has a...
[ { "input": { "stdin": "2\n5 156\n16 40\n\nSAMPLE" }, "output": { "stdout": "Case 1: 6\nCase 2: 3\n" } }, { "input": { "stdin": "4\n39 20\n183 11\n551 27\n85 16" }, "output": { "stdout": "Case 1: 9\nCase 2: 4\nCase 3: 6\nCase 4: 11\n" } }, { "input"...
{ "tags": [], "title": "professor-sharma" }
{ "cpp": null, "java": null, "python": "import sys\nT = int(sys.stdin.readline())\nfor t in range(T):\n X, Y = map(int, sys.stdin.readline().split(' '))\n Y = str(Y)\n E = 1\n P = str(X)\n l = len(Y)\n while P[:l] != Y:\n P = P[:500]\n P = str(int(P) * X)\n E += 1\n print...
HackerEarth/sherlock-and-kgb
The russian intelligence agency KGB make an encryption technique to send their passwords. Originally the password is of 3 characters. After encryption the password is converted into 3 numbers A-B-C. Now, Sherlock wants to decrypt the password encryption technique of KGB. Sherlock knows that every number has only 2 po...
[ { "input": { "stdin": "2\n27 25 4\n25 26 16\n\nSAMPLE" }, "output": { "stdout": "K-G-B\nX-Y-B" } }, { "input": { "stdin": "5\n81 125 16\n19683 15625 8192\n59049 15630 32768\n59050 78125 131072\n177147 390625 131073" }, "output": { "stdout": "K-G-B \nK-G-B \nK-...
{ "tags": [], "title": "sherlock-and-kgb" }
{ "cpp": null, "java": null, "python": "T = int(raw_input())\nfor ti in xrange(T):\n\ta,b,c = map(int,raw_input().split())\n\tif a%3:\n\t\ts1 = 'X'\n\telse:\n\t\twhile a%3==0:\n\t\t\ta /= 3\n\t\tif a==1:\n\t\t\ts1 = 'K'\n\t\telse:\n\t\t\ts1 = 'X'\n\tif b%5:\n\t\ts2 = 'Y'\n\telse:\n\t\twhile b%5==0:\n\t\t\tb /= 5\...
HackerEarth/the-competitive-class-3
Jiro is a fun loving lad. His classmates are very competitive and are striving for getting a good rank, while he loves seeing them fight for it. The results are going to be out soon. He is anxious to know what is the rank of each of his classmates. Rank of i'th student = 1+ (Number of students having strictly grea...
[ { "input": { "stdin": "4\n62 96 82 55\n\nSAMPLE" }, "output": { "stdout": "3 1 2 4\n" } }, { "input": { "stdin": "384\n87 78 16 94 36 87 93 50 22 63 28 91 60 64 27 41 27 73 37 12 69 68 30 83 31 63 24 68 36 30 3 23 59 70 68 94 57 12 43 30 74 22 20 85 38 99 25 16 71 14 27 92 ...
{ "tags": [], "title": "the-competitive-class-3" }
{ "cpp": null, "java": null, "python": "n=int(raw_input())\nlst=map(int,raw_input().split())\nrank=[]\nfor i in range(n):\n rank.append(0)\nfor i in range(n):\n count=1\n ele=lst[i]\n for j in range(n):\n if ele<lst[j]:\n count=count+1\n rank[i]=count\nfor i in range(n):\n print rank[i]," }
HackerEarth/xenny-and-range-sums
PowerShell had N natural numbers. He wanted to test Xenny's speed in finding the sum and difference of several numbers. He decided to ask Xenny several questions. In each question, he gave him two positive integers L and R. He asked him to find the sum of all integers from index L to index R (inclusive) and the differ...
[ { "input": { "stdin": "4 1\n1 2 3 4\n1 3\n\nSAMPLE" }, "output": { "stdout": "6 0\n" } }, { "input": { "stdin": "78 78\n-645462 -187679 693035 927066 431506 16023 -173045 698159 -995400 -687770 -747462 789670 862673 385513 579977 -156856 22209 378002 930156 353000 67263 -75...
{ "tags": [], "title": "xenny-and-range-sums" }
{ "cpp": null, "java": null, "python": "def main():\n\tN,Q=[int(x) for x in raw_input().split()]\n\tnums = [0]\n\tnums.extend([int(x) for x in raw_input().split()])\n\tfor i in xrange(1,N+1):\n\t\tnums[i] += nums[i-1]\n\t\t\n\tfor _ in xrange(Q):\n\t\tL,R = [int(x) for x in raw_input().split()]\n\t\tprint nums[R]...
p02652 AtCoder Grand Contest 045 - 01 Unbalanced
Given is a string S, where each character is `0`, `1`, or `?`. Consider making a string S' by replacing each occurrence of `?` with `0` or `1` (we can choose the character for each `?` independently). Let us define the unbalancedness of S' as follows: * (The unbalancedness of S') = \max \\{ The absolute difference be...
[ { "input": { "stdin": "??00????0??0????0?0??00??1???11?1?1???1?11?111???1" }, "output": { "stdout": "4" } }, { "input": { "stdin": "0??0" }, "output": { "stdout": "2" } }, { "input": { "stdin": "0??" }, "output": { "stdout": "1"...
{ "tags": [], "title": "p02652 AtCoder Grand Contest 045 - 01 Unbalanced" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long Int;\ntypedef pair<int,int> PII;\ntypedef vector<int> VInt;\n \n#define FOR(i, a, b) for(i = (a); i < (b); ++i)\n#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)\n#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)\n#...
p02781 AtCoder Beginner Contest 154 - Almost Everywhere Zero
Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten. Constraints * 1 \leq N < 10^{100} * 1 \leq K \leq 3 Input Input is given from Standard Input in the following format: N K Output Print the count. Examples Input 100 1 Output 19 Inpu...
[ { "input": { "stdin": "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999\n3" }, "output": { "stdout": "117879300" } }, { "input": { "stdin": "314159\n2" }, "output": { "stdout": "937" } }, { "input...
{ "tags": [], "title": "p02781 AtCoder Beginner Contest 154 - Almost Everywhere Zero" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)\n\nstring s;\nint n, k;\nll dp[1010][5][2];\n\nll dfs(int i = 0, int x = 0, bool l = true) {\n if (i == n) return x == k;\n if (x > k) return 0;\n ll &cc = dp...
p02916 AtCoder Beginner Contest 140 - Buffet
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once. The i-th dish (1 \leq i \leq N) he ate was Dish A_i. When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points. Additionally, when he eats Dish i+1 just after eating Dish i (1 \...
[ { "input": { "stdin": "2\n1 2\n50 50\n50" }, "output": { "stdout": "150" } }, { "input": { "stdin": "4\n2 3 4 1\n13 5 8 24\n45 9 15" }, "output": { "stdout": "74" } }, { "input": { "stdin": "3\n3 1 2\n2 5 4\n3 6" }, "output": { ...
{ "tags": [], "title": "p02916 AtCoder Beginner Contest 140 - Buffet" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\nint a[25],b[25],c[25],n,ans;\n\nint main()\n{\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;++i) scanf(\"%d\",a+i);\n\tfor(int i=1;i<=n;++i) scanf(\"%d\",b+i);\n\tfor(int i=2;i<=n;++i) scanf(\"%d\",c+i);\n\tfor(int i=1;i<=n;++i) {\n\t\tans+=b[a[i]];\n\t\tif(a[i-1]...
p03052 diverta 2019 Programming Contest - Edge Ordering
You are given a simple connected undirected graph G consisting of N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. Edge i connects Vertex a_i and b_i bidirectionally. It is guaranteed that the subgraph consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a spanning t...
[ { "input": { "stdin": "4 4\n1 2\n3 2\n3 4\n1 3" }, "output": { "stdout": "50" } }, { "input": { "stdin": "15 28\n10 7\n5 9\n2 13\n2 14\n6 1\n5 12\n2 10\n3 9\n10 15\n11 12\n12 6\n2 12\n12 8\n4 10\n15 3\n13 14\n1 15\n15 12\n4 14\n1 7\n5 11\n7 13\n9 10\n2 7\n1 9\n5 6\n12 14\n5...
{ "tags": [], "title": "p03052 diverta 2019 Programming Contest - Edge Ordering" }
{ "cpp": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"inline\", \"fast-math\", \"unroll-loops\", \"no-stack-protector\")\n#pragma GCC diagnostic error \"-fwhole-program\"\n#pragma GCC diagnostic error \"-fcse-skip-blocks\"\n#pragma GCC diagnostic error \"-funsafe-loop-optimizations\"\n#include<cstdio>\n#i...
p03194 CADDi 2018 for Beginners - Product and GCD
There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P. Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N. Constraints * 1 \leq N \leq 10^{12} * 1 \leq P \leq 10^{12} Input Input is...
[ { "input": { "stdin": "5 1" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4 972439611840" }, "output": { "stdout": "206" } }, { "input": { "stdin": "3 24" }, "output": { "stdout": "2" } }, { "input": { ...
{ "tags": [], "title": "p03194 CADDi 2018 for Beginners - Product and GCD" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main()\n{\n long long n, p;\n cin >> n >> p;\n\n long long ans = 1;\n for (int i = 2; 1LL * i * i <= p; i++) {\n int cnt = 0;\n while (p % i == 0) {\n p /= i;\n cnt++;\n }\n\n if (cnt >= n) {\...
p03343 AtCoder Regular Contest 098 - Range Minimum Queries
You are given an integer sequence A of length N and an integer K. You will perform the following operation on this sequence Q times: * Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one ...
[ { "input": { "stdin": "11 7 5\n24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784" }, "output": { "stdout": "451211184" } }, { "input": { "stdin": "5 3 2\n4 3 1 5 2" }, "output": { "stdout": "1" } ...
{ "tags": [], "title": "p03343 AtCoder Regular Contest 098 - Range Minimum Queries" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll; \nint n,k,q,a[2005],c[2005],bit[2005],ans=1e9;\nset<int>s;\nvoid upd(int x){for(;x<=n;x+=x&-x)bit[x]++;}\nint qry(int x){int r=0;for(;x;x-=x&-x)r+=bit[x];return r;}\nbool cmp(int x,int y){return a[x]<a[y];}\nint main()\n{\n\tscanf(\"%d%d%d...
p03503 AtCoder Beginner Contest 080 - Shopping Street
Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those ...
[ { "input": { "stdin": "1\n1 1 0 1 0 0 0 1 0 1\n3 4 5 6 7 8 9 -2 -3 4 -2" }, "output": { "stdout": "8" } }, { "input": { "stdin": "3\n1 1 1 1 1 1 0 0 1 1\n0 1 0 1 1 1 1 0 1 0\n1 0 1 1 0 1 0 1 0 1\n-8 6 -2 -8 -8 4 8 7 -6 2 2\n-9 2 0 1 7 -5 0 -2 -6 5 5\n6 -6 7 -9 6 -5 8 0 -9 -...
{ "tags": [], "title": "p03503 AtCoder Beginner Contest 080 - Shopping Street" }
{ "cpp": "#include <iostream>\n#include <algorithm>\n\nint f[105][12];\nint p[105][12];\nint N;\nint main(){\n std::cin>>N;\n for(int i=0;i<N;++i)\n for(int j=0;j<10;++j)\n std::cin>>f[i][j];\n \n for(int i=0;i<N;++i)\n for(int j=0;j<11;++j)\n std::cin>>p[i][j];\n \n long long ans=-(1LL<<3...
p03664 AtCoder Regular Contest 078 - Mole and Abandoned Mine
Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of N vertices numbered 1 through N and M edges. The i-th edge connects Vertices a_i and b_i, and it costs c_i yen (the currency of Japan) to remove it. Mole would like to remove som...
[ { "input": { "stdin": "15 22\n8 13 33418\n14 15 55849\n7 10 15207\n4 6 64328\n6 9 86902\n15 7 46978\n8 14 53526\n1 2 8720\n14 12 37748\n8 3 61543\n6 5 32425\n4 11 20932\n3 12 55123\n8 2 45333\n9 12 77796\n3 9 71922\n12 15 70793\n2 4 25485\n11 6 1436\n2 7 81563\n7 11 97843\n3 1 40491" }, "output": ...
{ "tags": [], "title": "p03664 AtCoder Regular Contest 078 - Mole and Abandoned Mine" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\nint N,M;\nint G[15][15];\n\nint dp[15][(1<<15)];\nint INF=2e9;\n\nint calc(int bit,int nbit){\n int sum=0;\n for(int i=0;i<N;i++)\n if(nbit>>i&1)\n for(int j=0;j<N;j++)\n if(~bit>>j&1)\n if(~nbit>>j&1)\n sum+=G[i][j];\n r...
p03819 AtCoder Regular Contest 068 - Snuke Line
Snuke has decided to play a game, where the player runs a railway company. There are M+1 stations on Snuke Line, numbered 0 through M. A train on Snuke Line stops at station 0 and every d-th station thereafter, where d is a predetermined constant for each train. For example, if d = 3, the train stops at station 0, 3, 6...
[ { "input": { "stdin": "3 3\n1 2\n2 3\n3 3" }, "output": { "stdout": "3\n2\n2" } }, { "input": { "stdin": "7 9\n1 7\n5 9\n5 7\n5 9\n1 1\n6 8\n3 4" }, "output": { "stdout": "7\n6\n6\n5\n4\n5\n5\n3\n2" } }, { "input": { "stdin": "7 11\n1 2\n5 9\...
{ "tags": [], "title": "p03819 AtCoder Regular Contest 068 - Snuke Line" }
{ "cpp": "#include <iostream>\n#include <algorithm>\n#include <cstring>\n\nconstexpr int MAX=(int)1e5;\n\nusing P=std::pair<int,int>;\n\nint n,m;\n\nint bit[MAX+2];\n\nP sec[MAX*3];\n\nbool used[MAX*3];\n\nvoid add(int k,int x) {\n\tfor(int i=k;i<=m;i+=(i&-i))\n\t\tbit[i]+=x;\n}\n\nint sum(int k) {\n\tint res=0;\n\tf...
p03986 AtCoder Grand Contest 005 - STring
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`. Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times: * Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occ...
[ { "input": { "stdin": "TSTTSS" }, "output": { "stdout": "4" } }, { "input": { "stdin": "SSTTST" }, "output": { "stdout": "0" } }, { "input": { "stdin": "TSSTTTSS" }, "output": { "stdout": "4" } }, { "input": { ...
{ "tags": [], "title": "p03986 AtCoder Grand Contest 005 - STring" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\nchar a[200100];\nint s,co;\n\nint main()\n{\n\ts=0;\n\tco=0;\n\tscanf(\"%s\",a);\n\tfor(int i=0;a[i];i++)\n\t{\n\t\tif(a[i]=='S')\n\t\t{\n\t\t\ts++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif(s)\n\t\t\t{\n\t\t\t\ts--;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tco++;\n\t\t...
AIZU/p00074
# Videotape There is a 120 minute videotape with standard recording. When I set the VCR counter to 00:00:00 with the tape completely rewound and recorded in standard recording mode, I got a certain counter value. Enter this counter value (hours, minutes, seconds), find the length of the remaining tape (recordable time...
[ { "input": { "stdin": "1 30 0\n-1 -1 -1" }, "output": { "stdout": "00:30:00\n01:30:00" } }, { "input": { "stdin": "1 29 0\n-1 -1 -1" }, "output": { "stdout": "00:31:00\n01:33:00\n" } }, { "input": { "stdin": "1 14 0\n-1 -1 -1" }, "out...
{ "tags": [], "title": "Videotape" }
{ "cpp": "#include <iostream>\n#include <cstdio>\nusing namespace std;\n\nint hms2s(int h, int m, int s) {\n int ret = s;\n for (int i = 0; i < h; i++) {\n ret += 3600;\n }\n for (int i = 0; i < m; i++) {\n ret += 60;\n }\n return ret;\n}\n\nvoid printhms(int s) {\n int h = 0, m = 0...
AIZU/p00206
# Next Trip You want to go on a trip with a friend. However, friends who have a habit of spending money cannot easily save travel expenses. I don't know when my friends will go on a trip if they continue their current lives. So, if you want to travel early, you decide to create a program to help your friends save in a...
[ { "input": { "stdin": "10000\n5000 3150\n5000 5000\n0 0\n5000 1050\n5000 3980\n5000 210\n5000 5000\n5000 5000\n0 0\n5000 2100\n5000 2100\n5000 2100\n29170\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n...
{ "tags": [], "title": "Next Trip" }
{ "cpp": "#include<stdio.h>\n\nint main(){\n\twhile(1){\n\t\tint to=0,ans=-1;\n\t\tbool f=true;\n\t\tint e;\n\t\tscanf(\"%d\",&e);\n\t\tif(e==0)return 0;\n\t\tfor(int i=0;i<12;i++){\n\t\t\tint a,b;\n\t\t\tscanf(\"%d %d\",&a,&b);\n\t\t\tif(f)to+=a-b;\n\t\t\tif(f&&to>=e){\n\t\t\t\tans=i+1;\n\t\t\t\tf=false;\n\t\t\t}\n\...
AIZU/p00365
# Age Difference A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the differen...
[ { "input": { "stdin": "2008 2 29\n2015 3 1" }, "output": { "stdout": "8" } }, { "input": { "stdin": "1999 9 9\n2001 11 3" }, "output": { "stdout": "3" } }, { "input": { "stdin": "-9 0 3\n58 0 1" }, "output": { "stdout": "67\n" ...
{ "tags": [], "title": "Age Difference" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\ntypedef pair<int ,P> P3;\ntypedef pair<P ,P> PP;\nconst ll MOD = ll(1e9+7);\nconst int IINF = INT_MAX;\nconst ll LLINF = LLONG_MAX;\nconst int MAX_N = int(1e5 + 5);\nconst double ...
AIZU/p00573
# Commuter Pass There are N stations in the city where JOI lives, and they are numbered 1, 2, ..., and N, respectively. In addition, there are M railway lines, numbered 1, 2, ..., and M, respectively. The railway line i (1 \ leq i \ leq M) connects station A_i and station B_i in both directions, and the fare is C_i ye...
[ { "input": { "stdin": "6 6\n1 6\n1 4\n1 2 1\n2 3 1\n3 5 1\n2 4 3\n4 5 2\n5 6 1" }, "output": { "stdout": "2" } }, { "input": { "stdin": "11 6\n1 6\n1 1\n1 2 1\n2 3 1\n2 5 1\n2 4 2\n6 8 2\n5 6 1" }, "output": { "stdout": "0\n" } }, { "input": { ...
{ "tags": [], "title": "Commuter Pass" }
{ "cpp": "#include <cstdio>\n#include <cstring>\n#include <cstdlib>\n#include <cmath>\n#include <ctime>\n#include <cctype>\n\n#include <algorithm>\n#include <functional>\n#include <queue>\n#include <set>\n#include <map>\n#include <vector>\n#include <iostream>\n#include <limits>\n#include <numeric>\n\n#define LOG(FMT....
AIZU/p00720
# Earth Observation with a Mobile Robot Team A new type of mobile robot has been developed for environmental earth observation. It moves around on the ground, acquiring and recording various sorts of observational data using high precision sensors. Robots of this type have short range wireless communication devices an...
[ { "input": { "stdin": "3 5 10\nred\n0 0 0\n5 0 0\ngreen\n0 5 5\n5 6 1\nblue\n0 40 5\n5 0 0\n3 10 5\natom\n0 47 32\n5 -10 -7\n10 1 0\npluto\n0 0 0\n7 0 0\n10 3 3\ngesicht\n0 25 7\n5 -7 -2\n10 -1 10\n4 100 7\nimpulse\n0 -500 0\n100 10 1\nfreedom\n0 -491 0\n100 9 2\ndestiny\n0 -472 0\n100 7 4\nstrike\n0 -482...
{ "tags": [], "title": "Earth Observation with a Mobile Robot Team" }
{ "cpp": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\n#define EQ(a,b) (abs((a)-(b)) < EPS)\nusing namespace std;\ntypedef double D;\ntypedef complex<D> P;\ntypedef pair<int,int> pii;\ntypedef pair<D, pii> data;\ntypedef vector<int> vi;\nconst D EPS = 1e...
AIZU/p00860
# The Morning after Halloween You are working for an amusement park as an operator of an obakeyashiki, or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corri...
[ { "input": { "stdin": "5 5 2\n#####\n#A#B#\n# #\n#b#a#\n#####\n16 4 3\n################\n## ########## ##\n# ABCcba #\n################\n16 16 3\n################\n### ## # ##\n## # ## # c#\n# ## ########b#\n# ## # # # #\n# # ## # # ##\n## a# # # # #\n### ## #### ## #\n## # ...
{ "tags": [], "title": "The Morning after Halloween" }
{ "cpp": "#include <cstdio>\n#include <queue>\n#include <cstring>\n#include <string>\n#include <cctype>\n#include <iostream>\nusing namespace std;\n\nchar f[16][16];\nint fx,fy,N;\nint dx[] = {0, 0, -1, 1, 0};\nint dy[] = {0, -1, 0, 0, 1};\n\nclass Ghost\n{\npublic:\n\tint x[3],y[3],c;\n\tbool r;\n\t\n\tGhost(int* i,...
AIZU/p00991
# Grid Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find th...
[ { "input": { "stdin": "500 500 0 0 200 200" }, "output": { "stdout": "34807775" } }, { "input": { "stdin": "4 4 0 0 1 1" }, "output": { "stdout": "2" } }, { "input": { "stdin": "4 4 0 0 3 3" }, "output": { "stdout": "2" } ...
{ "tags": [], "title": "Grid" }
{ "cpp": "#define _USE_MATH_DEFINES\n#define INF 0x3f3f3f3f\n#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <cmath>\n#include <cstdlib>\n#include <algorithm>\n#include <queue>\n#include <stack>\n#include <limits>\n#include <map>\n#include <string>\n#include <cstring>\n#include <set>\n#include <d...
AIZU/p01123
# Let's Move Tiles! <!-- Problem G --> Let's Move Tiles! You have a framed square board forming a grid of square cells. Each of the cells is either empty or with a square tile fitting in the cell engraved with a Roman character. You can tilt the board to one of four directions, away from you, toward you, to the le...
[ { "input": { "stdin": "4\n..E.\n.AD.\nB...\n..C.\nD\n4\n..E.\n.AD.\nB...\n..C.\nDR\n3\n...\n.A.\nBC.\n((URD)3L)2R\n5\n...P.\nPPPPP\nPPP..\nPPPPP\n..P..\nLRLR(LR)12RLLR\n20\n....................\n....................\n.III..CC..PPP...CC..\n..I..C..C.P..P.C..C.\n..I..C....P..P.C....\n..I..C....PPP..C....\n....
{ "tags": [], "title": "Let's Move Tiles!" }
{ "cpp": "#include <iostream>\n#include <string>\n#include <cstdio>\n#include <vector>\n#include <numeric>\n#include <cstdlib>\n#include <cctype>\nusing namespace std;\n\n#define ALL(v) begin(v),end(v)\n#define REP(i,n) for(int i=0;i<(n);++i)\n#define RREP(i,n) for(int i=(n)-1;i>=0;--i)\n\ntypedef long long LL;\ntype...
AIZU/p01262
# Adaptive Time Slicing Quantization Nathan O. Davis is a student at the department of integrated systems. Today he learned digital quanti- zation in a class. It is a process that approximates analog data (e.g. electrical pressure) by a finite set of discrete values or integers. He had an assignment to write a progra...
[ { "input": { "stdin": "5 2 1\n0.1 0.2 0.3 0.4 0.5\n6 2 2\n0.1 0.2 0.3 0.4 0.5 0.6\n0 0 0" }, "output": { "stdout": "0.01\n0.00" } }, { "input": { "stdin": "5 2 1\n0.1 0.3034585214713487 1.3035920676398525 1.4606179572636666 0.5\n6 2 2\n0.1 0.2 0.5263750498409592 1.101037034...
{ "tags": [], "title": "Adaptive Time Slicing Quantization" }
{ "cpp": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\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#inclu...