id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
AIZU/p01984
# Tanka Number Number of tanka Wishing to die in the spring under the flowers This is one of the famous tanka poems that Saigyo Hoshi wrote. Tanka is a type of waka poem that has been popular in Japan for a long time, and most of it consists of five phrases and thirty-one sounds of 5, 7, 5, 7, and 7. By the way, th...
[ { "input": { "stdin": "1\n2\n3\n390\n1124\n1546\n314159265358979323\n0" }, "output": { "stdout": "10\n12\n13\n2020\n25252\n57577\n7744444777744474777777774774744777747477444774744744" } }, { "input": { "stdin": "1\n1\n4\n1531\n2317\n714\n225725068861419081\n0" }, "o...
{ "tags": [], "title": "Tanka Number" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nconst Int MAX = 2e18;\n\nvoid chmin(Int &a,Int b){if(a>b) a=b;}\n\nInt dfs(Int t,Int u,Int k){\n Int res=1LL<<(t-k);\n if(u!=3) res--;\n chmin(res,MAX);\n return res;\n}\n\nvoid restore(Int n){\n Int k=1;\n for(;;k++){\n Int tmp...
AIZU/p02130
# Combine Two Elements Problem Given $ N $ a pair of non-negative integers $ (a_i, b_i) $ and non-negative integers $ A $, $ B $. I want to do as many of the following operations as possible. * $ | a_i --b_i | \ leq A $ or $ B \ leq | a_i --b_i | \ leq Take out and delete the element $ i $ that satisfies 2A $ * $ |...
[ { "input": { "stdin": "10 7 12\n34 70\n36 0\n12 50\n76 46\n33 45\n61 21\n0 1\n24 3\n98 41\n23 84" }, "output": { "stdout": "5" } }, { "input": { "stdin": "5 3 5\n7 2\n13 1\n1 1\n2 9\n2 4" }, "output": { "stdout": "4" } }, { "input": { "stdin"...
{ "tags": [], "title": "Combine Two Elements" }
{ "cpp": "#include <iostream>\n#include <fstream>\n#include <cstdio>\n#include <cmath>\n#include <vector>\n#include <cstring>\n#include <string>\n#include <set>\n#include <map>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <assert.h>\nusing ...
AIZU/p02271
# Exhaustive Search Write a program which reads a sequence A of n elements and an integer M, and outputs "yes" if you can make M by adding elements in A, otherwise "no". You can use an element only once. You are given the sequence A and q questions where each question contains Mi. Notes You can solve this problem b...
[ { "input": { "stdin": "5\n1 5 7 10 21\n8\n2 4 17 8 22 21 100 35" }, "output": { "stdout": "no\nno\nyes\nyes\nyes\nyes\nno\nno" } }, { "input": { "stdin": "5\n0 5 10 -1 21\n8\n1 7 17 11 8 14 010 41" }, "output": { "stdout": "no\nno\nno\nno\nno\nyes\nyes\nno\n" ...
{ "tags": [], "title": "Exhaustive Search" }
{ "cpp": "#include <stdio.h>\n#include <iostream>\n\nusing namespace std;\nint n,A[50];\n\nint zentan(int i,int m){\n\tif(m == 0)return 1;\n\tif(i >= n)return 0;\n\tint res = zentan(i + 1,m)||zentan(i+1,m-A[i]);\n\treturn res;\n}\n\nint main(){\n\tint q,M;\n\tcin >> n;\n\tfor(int i = 0;i<n;i++){\n\t\tcin>>A[i];\n\t}\...
AIZU/p02418
# Ring Write a program which finds a pattern $p$ in a ring shaped text $s$. <image> Constraints * $1 \leq $ length of $p \leq $ length of $s \leq 100$ * $s$ and $p$ consists of lower-case letters Input In the first line, the text $s$ is given. In the second line, the pattern $p$ is given. Output If $p$ is in $...
[ { "input": { "stdin": "vanceknowledgetoad\nadvance" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "vanceknowledgetoad\nadvanced" }, "output": { "stdout": "No" } }, { "input": { "stdin": "eaovlgedexonkecnat\n`avcned" }, "ou...
{ "tags": [], "title": "Ring" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\nstring s,d;\ncin>>s>>d;\ns+=s;\nif(s.find(d)<s.size())cout<<\"Yes\"<<endl;\nelse cout<<\"No\"<<endl;\n}", "java": "import java.io.*;\n\npublic class Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\tString inputString, p...
CodeChef/bestbats
A cricket team consists of 11 players and some are good at batting, others are good at bowling and some of them are good at both batting and bowling. The batting coach wants to select exactly K players having maximum possible sum of scores. Given the batting score of each of the 11 players, find the number of ways in w...
[ { "input": { "stdin": "2\n1 2 3 4 5 6 7 8 9 10 11\n3\n2 5 1 2 4 1 6 5 2 2 1\n6" }, "output": { "stdout": "1\n6\n" } }, { "input": { "stdin": "2\n1 2 3 4 5 6 14 11 9 6 11\n3\n0 5 1 2 4 1 4 5 1 2 1\n11" }, "output": { "stdout": "1\n1\n" } }, { "input...
{ "tags": [], "title": "bestbats" }
{ "cpp": null, "java": null, "python": "import math\ndef ncr(n,r):\n f = math.factorial\n return f(n)/(f(r)*f(n-r))\n \nt = int(raw_input())\nfor i in range(t):\n a = map(int , raw_input().split())\n k = input()\n a.sort()\n temp=a[11 - k] \n r = n= 0\n for j in xrange(11-k, 11):\n ...
CodeChef/cn03
So, as you all know free WiFis are being installed in our institute. These are a special type of WiFi and they start to interfere when there is a signal coming from two different WiFis at a single location. The WiFi installation task is given to you. There are N suitable locations for the installation of WiFi. ...
[ { "input": { "stdin": "5 3\n1\n2\n8\n4\n9" }, "output": { "stdout": "3" } } ]
{ "tags": [], "title": "cn03" }
{ "cpp": null, "java": null, "python": "def f(d, N, C, X):\n\n\tcows_placed = 1\n\tlast_pos = X[0]\n\tfor i in xrange(1, N):\n\t\tif X[i] - last_pos >= d:\n\t\t\tcows_placed += 1\n\t\t\tlast_pos = X[i]\n\t\t\tif cows_placed == C:\n\t\t\t\treturn 1\n\n\treturn 0\n\nN, C = map(int, raw_input().split())\n\nX = []\nf...
CodeChef/forestga
Chef is the head of commercial logging industry that recently bought a farm containing N trees. You are given initial height of the i-th tree by Hi and the rate of growth of height as Ri meters per month. For simplicity, you can assume that all the trees are perfect cylinders of equal radius. This allows us to consider...
[ { "input": { "stdin": "3 74 51\n2 2\n5 7\n2 9" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "2 70 82\n2 4\n0 2\n3 7" }, "output": { "stdout": "20\n" } }, { "input": { "stdin": "2 70 82\n4 4\n0 2\n3 7" }, "output": { ...
{ "tags": [], "title": "forestga" }
{ "cpp": null, "java": null, "python": "from operator import itemgetter\n\ndef can_cut(time,limit,target):\n total = 0\n for i in range(number_of_trees):\n ht = tree_list[i][0]+tree_list[i][1]*time\n if ht>=limit:\n total+=ht\n return total >= target\n\n##def order_size(month,max...
CodeChef/linnum
Stuart is obsessed to numbers. He like all type of numbers in fact he is having a great collection of numbers in his room. His collection includes N different large numbers. But today he is searching for a number which is having maximum frequency of digit X. Numbers are large so he can’t do the task on his own. Help hi...
[ { "input": { "stdin": "2\n5\n345 1323 165 98 456\n3\n5\n335 876 98 1323 349\n3" }, "output": { "stdout": "1323\n335" } }, { "input": { "stdin": "2\n3\n626 877 26 234 77\n6\n2\n195 24 57 2224 226\n2" }, "output": { "stdout": "626\n24\n" } }, { "inpu...
{ "tags": [], "title": "linnum" }
{ "cpp": null, "java": null, "python": "for _ in range(input()):\n\tn=input()\n\ta=raw_input().split()\n\tx=raw_input()\n\tb=[0]*n\n\t#n=a[0].count(x)\n\t#print n\n\tfor i in range(n):\n\t\tc=a[i].count(x)\n\t\tb[i]=c\n\tm=max(b)\n\t#print b\n\tfor i in range(n):\n\t\tif b[i]==m:\n\t\t\tprint a[i]\n\t\t\tbreak" }
CodeChef/pree02
Problem Statement You have a number N and you want to calculate how many divisors of N are special. A number is said to be special if it is possible to remove some digits from it to get a number having 3, 5 or 6 only.For exemple number 38597 is special since it is posible to remove digits 8, 9, 7 to get 35. You can...
[ { "input": { "stdin": "2\n15\n75" }, "output": { "stdout": "3\n5" } } ]
{ "tags": [], "title": "pree02" }
{ "cpp": null, "java": null, "python": "from math import sqrt\nt=input()\nwhile t!=0:\n n=input()\n c=0\n for i in range(2,int(sqrt(n))+1):\n if n%i==0:\n if str(i).find('5')!=-1 or str(i).find('3')!=-1 or str(i).find('6')!=-1:\n ...
CodeChef/tadelive
Andy and Bob are the only two delivery men of Pizza-chef store. Today, the store received N orders. It's known that the amount of tips may be different when handled by different delivery man. More specifically, if Andy takes the i^th order, he would be tipped Ai dollars and if Bob takes this order, the tip would be Bi ...
[ { "input": { "stdin": "5 3 3\n1 2 3 4 5\n5 4 3 2 1" }, "output": { "stdout": "21\n" } }, { "input": { "stdin": "5 3 3\n1 2 3 4 5\n-1 4 2 1 3" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "5 3 3\n1 2 3 4 5\n5 4 2 1 1" }, ...
{ "tags": [], "title": "tadelive" }
{ "cpp": null, "java": null, "python": "def test(r,s,t):\n a=[(x,s[x]-t[x]) for x in range(r[0])]\n def f(a):\n if a[1]>0:\n return a[1]\n else:\n return -a[1]\n a=sorted(a,key=f,reverse=True)\n i=j=k=sum=0\n while i<r[0]:\n if a[i][1]>0:\n sum+...
Codeforces/1010/E
# Store Natasha was already going to fly back to Earth when she remembered that she needs to go to the Martian store to buy Martian souvenirs for her friends. It is known, that the Martian year lasts x_{max} months, month lasts y_{max} days, day lasts z_{max} seconds. Natasha also knows that this store works accordin...
[ { "input": { "stdin": "10 10 10 3 1 3\n2 6 2\n4 2 4\n6 4 6\n9 9 9\n3 3 3\n10 10 10\n8 8 8\n" }, "output": { "stdout": "CORRECT\nOPEN\nCLOSED\nUNKNOWN\n" } }, { "input": { "stdin": "10 10 10 1 1 1\n2 5 7\n2 5 7\n8 9 10\n" }, "output": { "stdout": "INCORRECT\n" ...
{ "tags": [ "data structures" ], "title": "Store" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 100005;\ntemplate <typename T>\ninline void read(T &AKNOI) {\n T x = 0, flag = 1;\n char ch = getchar();\n while (!isdigit(ch)) {\n if (ch == '-') flag = -1;\n ch = getchar();\n }\n while (isdigit(ch)) {\n x = x * 10 + ch - '0';\...
Codeforces/1034/E
# Little C Loves 3 III Little C loves number «3» very much. He loves all things about it. Now he is interested in the following problem: There are two arrays of 2^n intergers a_0,a_1,...,a_{2^n-1} and b_0,b_1,...,b_{2^n-1}. The task is for each i (0 ≤ i ≤ 2^n-1), to calculate c_i=∑ a_j ⋅ b_k (j|k=i and j\&k=0, wher...
[ { "input": { "stdin": "1\n11\n11\n" }, "output": { "stdout": "12" } }, { "input": { "stdin": "2\n0123\n3210\n" }, "output": { "stdout": "0322" } }, { "input": { "stdin": "2\n2123\n0323\n" }, "output": { "stdout": "0202" } ...
{ "tags": [ "bitmasks", "dp", "math" ], "title": "Little C Loves 3 III" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n int n;\n cin >> n;\n string a, b;\n cin >> a >> b;\n vector<long long> c(1 << n), d(1 << n);\n for (int i = 0; i < 1 << n; ++i) {\n c[i] = (a[i] - '0') * (1ll << (__builtin_popcount(i) << 1));\...
Codeforces/1057/B
# DDoS We get more and more news about DDoS-attacks of popular websites. Arseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds 100 ⋅ t, where t — the number of seconds in this time segment. Arseny knows statistics on the number of re...
[ { "input": { "stdin": "5\n100 200 1 1 1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "5\n1 2 3 4 5\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "2\n101 99\n" }, "output": { "stdout": "1\n" } ...
{ "tags": [ "*special", "brute force" ], "title": "DDoS" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstatic const int _ = []() {\n ios::sync_with_stdio(false);\n cin.sync_with_stdio(false);\n cout.sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\ntemplate <class T>\ninline vector<T> rv(long long n) {\n vector<T> v(n);\...
Codeforces/1080/A
# Petya and Origami Petya is having a party soon, and he has decided to invite his n friends. He wants to make invitations in the form of origami. For each invitation, he needs two red sheets, five green sheets, and eight blue sheets. The store sells an infinite number of notebooks of each color, but each notebook co...
[ { "input": { "stdin": "3 5\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "15 6\n" }, "output": { "stdout": "38\n" } }, { "input": { "stdin": "30931310 20\n" }, "output": { "stdout": "23198483\n" } }, { ...
{ "tags": [ "math" ], "title": "Petya and Origami" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long fun(long long a, long long b) {\n long long ans = a / b;\n if (a % b != 0) ans++;\n return ans;\n}\nvoid main1(int tc) {\n long long n, k;\n cin >> n >> k;\n long long a = 2 * n, b = 5 * n, c = 8 * n;\n cout << fun(a, k) + fun(b, k) + fun(c, k...
Codeforces/109/E
# Lucky Interval Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya came across an interval of numbers [a, a + l - 1]. Let F(x) be the n...
[ { "input": { "stdin": "7 4\n" }, "output": { "stdout": "17\n" } }, { "input": { "stdin": "4 7\n" }, "output": { "stdout": "14\n" } }, { "input": { "stdin": "1 1000000000\n" }, "output": { "stdout": "1000000001\n" } }, { ...
{ "tags": [ "brute force", "math" ], "title": "Lucky Interval" }
{ "cpp": "#include <bits/stdc++.h>\nint f[11][3][19][4];\nint n, k, dig_a[20], aadd;\nint typ[10] = {2, 2, 2, 1, 0, 2, 1, 0, 2, 2};\nint ten[10] = {1, 10, 100, 1000, 10000,\n 100000, 1000000, 10000000, 100000000, 1000000000};\nvoid turn(int i, int j, int p, int q, int d) {\n int v =...
Codeforces/1120/E
# The very same Munchhausen A positive integer a is given. Baron Munchausen claims that he knows such a positive integer n that if one multiplies n by a, the sum of its digits decreases a times. In other words, S(an) = S(n)/a, where S(x) denotes the sum of digits of the number x. Find out if what Baron told can be t...
[ { "input": { "stdin": "3\n" }, "output": { "stdout": "26667" } }, { "input": { "stdin": "2\n" }, "output": { "stdout": "15" } }, { "input": { "stdin": "10\n" }, "output": { "stdout": "-1\n" } }, { "input": { "s...
{ "tags": [ "brute force" ], "title": "The very same Munchhausen" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long a, g, A, B, sum;\nstring num, zero, ans;\ninline long long gcd(long long u, long long v) {\n for (; v;) {\n u %= v;\n swap(u, v);\n }\n return u;\n}\ninline long long S(long long u) {\n long long res = 0;\n for (; u; u /= 10) res += u % 10...
Codeforces/1148/A
# Another One Bites The Dust Let's call a string good if and only if it consists of only two types of letters — 'a' and 'b' and every two consecutive letters are distinct. For example "baba" and "aba" are good strings and "abb" is a bad string. You have a strings "a", b strings "b" and c strings "ab". You want to cho...
[ { "input": { "stdin": "2 2 1\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1 1 1\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "2 1 2\n" }, "output": { "stdout": "7\n" } }, { "input": {...
{ "tags": [ "greedy" ], "title": "Another One Bites The Dust" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n unsigned long long int a, b, c;\n cin >> a >> b >> c;\n unsigned long long int sol = c * 2;\n if (a == b)\n sol += a * 2;\n else\n sol += min(a, b) * 2 + 1;\n cout << sol;\n}\n", ...
Codeforces/1169/C
# Increasing by Modulo Toad Zitz has an array of integers, each integer is between 0 and m-1 inclusive. The integers are a_1, a_2, …, a_n. In one operation Zitz can choose an integer k and k indices i_1, i_2, …, i_k such that 1 ≤ i_1 < i_2 < … < i_k ≤ n. He should then change a_{i_j} to ((a_{i_j}+1) mod m) for each c...
[ { "input": { "stdin": "5 3\n0 0 0 1 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 7\n0 6 1 3 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "2 2\n0 1\n" }, "output": { "stdout": "0\n" } ...
{ "tags": [ "binary search", "greedy" ], "title": "Increasing by Modulo" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m;\n cin >> n >> m;\n vector<int> a(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n long long ans = 0;\n int l = 0, h = m - 1, mid;\n while (l != h) {\n mid = (l + h) / 2;\n int p = min(a[0], (a[0] + mid) % m);\n if (p < ...
Codeforces/1187/E
# Tree Painting You are given a tree (an undirected connected acyclic graph) consisting of n vertices. You are playing a game on this tree. Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an ed...
[ { "input": { "stdin": "5\n1 2\n1 3\n2 4\n2 5\n" }, "output": { "stdout": "14" } }, { "input": { "stdin": "9\n1 2\n2 3\n2 5\n2 6\n1 4\n4 9\n9 7\n9 8\n" }, "output": { "stdout": "36" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { ...
{ "tags": [ "dfs and similar", "dp", "trees" ], "title": "Tree Painting" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nvector<int> ve[200005];\nint siz[200005];\nlong long ans;\nvoid dfs(int a, int b) {\n siz[a] = 1;\n for (auto it : ve[a]) {\n if (it == b) continue;\n dfs(it, a);\n siz[a] += siz[it];\n }\n}\nvoid dfs2(int a, int b, long long s) {\n ans = ...
Codeforces/1206/D
# Shortest Cycle You are given n integer numbers a_1, a_2, ..., a_n. Consider graph on n nodes, in which nodes i, j (i≠ j) are connected if and only if, a_i AND a_j≠ 0, where AND denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Find the length of the shortest cycle in this gra...
[ { "input": { "stdin": "4\n3 6 28 9\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "4\n1 2 4 8\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5\n5 12 9 16 48\n" }, "output": { "stdout": "3\n" } ...
{ "tags": [ "bitmasks", "brute force", "graphs", "shortest paths" ], "title": "Shortest Cycle" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst double pi = acos(-1);\nconst double eps = 1e-6;\nconst long long INF = 0x3f3f3f3f3f3f3f3fll;\nconst int inf = 0x3f3f3f3f;\nconst int mod = 998244353;\nconst int maxn = 2e5 + 100;\nlong long a[maxn];\nint mp[300][300], dis[300][300];\nvoid solve() {\n in...
Codeforces/1223/G
# Wooden Raft Suppose you are stuck on a desert island. The only way to save yourself is to craft a wooden raft and go to the sea. Fortunately, you have a hand-made saw and a forest nearby. Moreover, you've already cut several trees and prepared it to the point that now you have n logs and the i-th log has length a_i....
[ { "input": { "stdin": "1\n9\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "9\n9 10 9 18 9 9 9 28 9\n" }, "output": { "stdout": "90\n" } }, { "input": { "stdin": "5\n12 12 16 16 16\n" }, "output": { "stdout": "55\n...
{ "tags": [ "binary search", "math", "number theory" ], "title": "Wooden Raft" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"O3\")\nusing namespace std;\nconst int maxN = 5 * (int)1e5 + 100;\nint pref[maxN];\nint n;\nint a[maxN];\nint get(int l, int r) {\n r = min(r, maxN - 1);\n if (l > r) return 0;\n if (l == 0) return pref[r];\n return pref[r] - pref[l - 1];\n}\nlong long cn...
Codeforces/1249/C1
# Good Numbers (easy version) The only difference between easy and hard versions is the maximum value of n. You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n. The positive integer is called good if it can be represented as ...
[ { "input": { "stdin": "7\n1\n2\n6\n13\n14\n3620\n10000\n" }, "output": { "stdout": "1\n3\n9\n13\n27\n6561\n19683\n" } }, { "input": { "stdin": "1\n450283905890997363\n" }, "output": { "stdout": "450283905890997363\n" } }, { "input": { "stdin"...
{ "tags": [ "brute force", "greedy", "implementation" ], "title": "Good Numbers (easy version)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint p[60], T;\nint main() {\n scanf(\"%d\", &T);\n while (T--) {\n memset(p, 0, sizeof(p));\n long long n, pos = 0, sum = 0, P = 1;\n scanf(\"%lld\", &n);\n while (n) p[pos++] = n % 3, n /= 3;\n int k = pos - 1;\n while (k && p[k] != 2) k--...
Codeforces/1267/J
# Just Arrange the Icons BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between 1 and n, inclusive; the i-th app...
[ { "input": { "stdin": "3\n11\n1 5 1 5 1 5 1 1 1 1 5\n6\n1 2 2 2 2 1\n5\n4 3 3 1 2\n" }, "output": { "stdout": "3\n3\n4\n" } }, { "input": { "stdin": "3\n11\n2 5 1 4 1 5 1 2 1 1 5\n6\n2 2 2 2 2 1\n5\n4 2 3 1 3\n" }, "output": { "stdout": "7\n4\n4\n" } }, ...
{ "tags": [ "greedy", "implementation", "math" ], "title": "Just Arrange the Icons" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx\")\nusing namespace std;\nusing ll = int64_t;\nconstexpr int M = 2 * 1000000 + 10;\nvector<int> cs;\nint cnt[M];\nbool processed[M];\nint how_much[M];\nvector<int> candidates;\nvector<int> si...
Codeforces/128/D
# Numbers One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this poi...
[ { "input": { "stdin": "6\n1 1 2 2 2 3\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "4\n1 2 3 2\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "6\n2 4 1 1 2 2\n" }, "output": { "stdout": "NO\n" ...
{ "tags": [ "constructive algorithms", "implementation" ], "title": "Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9 + 10;\nconst long long int INFLL = 1e18 + 10;\nconst long double EPS = 1e-8;\nconst long double EPSLD = 1e-14;\nconst long long int MOD = 1e9 + 7;\ntemplate <class T>\nT &chmin(T &a, const T &b) {\n return a = min(a, b);\n}\ntemplate <clas...
Codeforces/130/J
# Date calculation In Gregorian calendar a typical year consists of 365 days and 12 months. The numbers of days in the months are: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. If year index is divisible by 400, or divisible by 4 but not by 100, the year becomes leap year, with one extra day in the second month (the...
[ { "input": { "stdin": "2011\n324\n" }, "output": { "stdout": "20 11\n" } }, { "input": { "stdin": "2012\n274\n" }, "output": { "stdout": "30 9\n" } } ]
{ "tags": [ "*special" ], "title": "Date calculation" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1332/E
# Height All the Same Alice has got addicted to a game called Sirtet recently. In Sirtet, player is given an n × m grid. Initially a_{i,j} cubes are stacked up in the cell (i,j). Two cells are called adjacent if they share a side. Player can perform the following operations: * stack up one cube in two adjacent ce...
[ { "input": { "stdin": "2 2 1 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1 2 1 2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2 2 3 4\n" }, "output": { "stdout": "8\n" } }, { "inp...
{ "tags": [ "combinatorics", "constructive algorithms", "math", "matrices" ], "title": "Height All the Same" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long M = 998244353;\nlong long power(long long x, long long y) {\n long long res = 1;\n while (y) {\n if (y & 1) res = (res * x) % M;\n y = y >> 1;\n x = (x * x) % M;\n }\n return res;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n ...
Codeforces/1353/A
# Most Unstable Array You are given two integers n and m. You have to construct the array a of length n consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly m and the value ∑_{i=1}^{n-1} |a_i - a_{i+1}| is the maximum possible. Recall ...
[ { "input": { "stdin": "5\n1 100\n2 2\n5 5\n2 1000000000\n1000000000 1000000000\n" }, "output": { "stdout": "0\n2\n10\n1000000000\n2000000000\n" } }, { "input": { "stdin": "1\n9041 222\n" }, "output": { "stdout": "444\n" } }, { "input": { "std...
{ "tags": [ "constructive algorithms", "greedy", "math" ], "title": "Most Unstable Array" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int t;\n cin >> t;\n while (t--) {\n long long int m, n;\n cin >> n >> m;\n if (n == 1) {\n cout << \"0\" << endl;\n } else if (n == 2) {\n cout << m << endl;\n } else {\n cout << 2 * m << endl;\n }\n }\n}\n"...
Codeforces/1372/F
# Omkar and Modes Ray lost his array and needs to find it by asking Omkar. Omkar is willing to disclose that the array has the following qualities: 1. The array has n (1 ≤ n ≤ 2 ⋅ 10^5) elements. 2. Every element in the array a_i is an integer in the range 1 ≤ a_i ≤ 10^9. 3. The array is sorted in nondecreasi...
[ { "input": { "stdin": "6\n\n2 2\n\n2 2\n\n3 2\n\n2 1\n" }, "output": { "stdout": "? 1 1\n? 6 6\n! 2 2 2 2 2 2\n" } }, { "input": { "stdin": "8\n1 1 1 2 3 3 3 3\n" }, "output": { "stdout": "? 1 1\n? 8 8\n! 1 1 1 1 1 1 1 1\n" } }, { "input": { ...
{ "tags": [ "binary search", "divide and conquer", "interactive" ], "title": "Omkar and Modes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[200005];\nmap<int, int> mp;\nint xx = 0, ff = 0;\npair<int, int> chk(int l, int r, int prex, int pref) {\n if (r < l) return {-1, -1};\n if (!ff) printf(\"? %d %d\\n\", l, r);\n fflush(stdout);\n int x = xx, f = ff;\n if (!ff) {\n scanf(\"%d\", &...
Codeforces/1395/E
# Boboniu Walks on Graph Boboniu has a directed graph with n vertices and m edges. The out-degree of each vertex is at most k. Each edge has an integer weight between 1 and m. No two edges have equal weights. Boboniu likes to walk on the graph with some specific rules, which is represented by a tuple (c_1,c_2,…,c_k...
[ { "input": { "stdin": "4 6 3\n4 2 1\n1 2 2\n2 4 3\n4 1 4\n4 3 5\n3 1 6\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5 5 1\n1 4 1\n5 1 2\n2 5 3\n4 3 4\n3 2 5\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "6 13 4...
{ "tags": [ "brute force", "dfs and similar", "graphs", "hashing" ], "title": "Boboniu Walks on Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, k;\nvector<int> q(9);\nset<pair<int, int>> bad;\nbool check[9];\nint ans = 0;\nvoid go(int p) {\n if (p == k) {\n ++ans;\n return;\n }\n if (check[p]) {\n for (int j = 0; j <= p; ++j) {\n if (bad.count(make_pair(q[j] + 9 * j, q[p] + ...
Codeforces/141/A
# Amusing Joke So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests at last. When two "New Year and Christmas Men" meet, thear assistants cut out of cardboard the letters from the guest's name and the host's name in honor of this event. Then the hung the letters above the ...
[ { "input": { "stdin": "BABBONATALE\nFATHERCHRISTMAS\nBABCHRISTMASBONATALLEFATHER\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "SANTACLAUS\nDEDMOROZ\nSANTAMOROZDEDCLAUS\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin"...
{ "tags": [ "implementation", "sortings", "strings" ], "title": "Amusing Joke" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n char h[101], g[101], comb[101];\n cin >> h >> g >> comb;\n if (strlen(comb) != strlen(h) + strlen(g))\n cout << \"NO\";\n else {\n for (int i = 0; i < (int)strlen(g); i++) {\n for (int j = 0; j < (int)strlen(comb); j++) {\n ...
Codeforces/1438/A
# Specific Tastes of Andre Andre has very specific tastes. Recently he started falling in love with arrays. Andre calls an nonempty array b good, if sum of its elements is divisible by the length of this array. For example, array [2, 3, 1] is good, as sum of its elements — 6 — is divisible by 3, but array [1, 1, 2, ...
[ { "input": { "stdin": "3\n1\n2\n4\n" }, "output": { "stdout": "1\n1 1\n1 1 1 1\n" } }, { "input": { "stdin": "100\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n...
{ "tags": [ "constructive algorithms", "implementation" ], "title": "Specific Tastes of Andre " }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long add(long long x, long long y) {\n x += y;\n while (x >= 998244353) x -= 998244353;\n while (x < 0) x += 998244353;\n return x;\n}\nlong long mul(long long x, long long y) { return (x * 1ll * y) % 998244353; }\nlong long binpow(long long x, long l...
Codeforces/1462/F
# The Treasure of The Segments Polycarp found n segments on the street. A segment with the index i is described by two integers l_i and r_i — coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them. Polycarp believe...
[ { "input": { "stdin": "4\n3\n1 4\n2 3\n3 6\n4\n1 2\n2 3\n3 5\n4 5\n5\n1 2\n3 8\n4 5\n6 7\n9 10\n5\n1 5\n2 4\n3 5\n3 8\n4 8\n" }, "output": { "stdout": "\n0\n1\n2\n0\n" } }, { "input": { "stdin": "7\n1\n1 2\n1\n1 2\n1\n1 2\n1\n1 2\n1\n1 2\n1\n1 2\n1\n1 2\n" }, "outpu...
{ "tags": [ "binary search", "data structures", "greedy" ], "title": "The Treasure of The Segments" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n\nint main() {\n\tint T;\n\tcin >> T;\n\twhile (T--) {\n\t\tint n;\n\t\tscanf(\"%d\", &n);\n\n\t\tvector<pair<int, int>> sg(n);\n\t\tfor (auto &[l, r] : sg) {\n\t\t\tscanf(\"%d%d\", &l, &r);\n\t\t}\n\t\tint mx = 0;\n\n\t\tvector <int> st, ed;\n\t\tfor (aut...
Codeforces/1487/G
# String Counting You have c_1 letters 'a', c_2 letters 'b', ..., c_{26} letters 'z'. You want to build a beautiful string of length n from them (obviously, you cannot use the i-th letter more than c_i times). Each c_i is greater than n/3. A string is called beautiful if there are no palindromic contiguous substrings...
[ { "input": { "stdin": "3\n2 2 2 2 2 2 3 3 3 2 2 2 2 2 2 3 3 3 2 2 3 2 2 3 2 2\n" }, "output": { "stdout": "\n16900\n" } }, { "input": { "stdin": "400\n348 322 247 158 209 134 151 267 268 176 214 379 372 291 388 135 147 304 169 149 193 351 380 368 181 340\n" }, "outp...
{ "tags": [ "combinatorics", "dp", "fft", "math" ], "title": "String Counting" }
{ "cpp": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <algorithm>\n#include <cmath>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_set>\n#include <unordered_map>\n#include <queue>\n#include <ctime>\n#include <cassert>\n#include <complex>\n#include <string>\n#include <...
Codeforces/1511/D
# Min Cost String Let's define the cost of a string s as the number of index pairs i and j (1 ≤ i < j < |s|) such that s_i = s_j and s_{i+1} = s_{j+1}. You are given two positive integers n and k. Among all strings with length n that contain only the first k characters of the Latin alphabet, find a string with minimu...
[ { "input": { "stdin": "9 4\n" }, "output": { "stdout": "\naabacadbb\n" } }, { "input": { "stdin": "5 1\n" }, "output": { "stdout": "\naaaaa" } }, { "input": { "stdin": "10 26\n" }, "output": { "stdout": "\ncodeforces\n" } ...
{ "tags": [ "brute force", "constructive algorithms", "graphs", "greedy", "strings" ], "title": "Min Cost String" }
{ "cpp": "#include<bits/stdc++.h>\n#define ll long long int\n#define f first\n#define s second\n#define pb(x) push_back(x)\n#define inf (int)1e9\n#define PI 3.1415926535897932384626433832795\n#define MOD 1000000007\n\nusing namespace std;\n\nvoid input() {\n\n\tll n,k;\n\tcin>>n>>k;\n\n\tstring s1,s;\n\n\tfor(int i=0...
Codeforces/1539/B
# Love Song Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up q questions about this song. Each question is about a subsegment of the song starting from the l-th letter to the r-th letter. Vasya considers a substring made up from charac...
[ { "input": { "stdin": "7 3\nabacaba\n1 3\n2 5\n1 7\n" }, "output": { "stdout": "4\n7\n11\n" } }, { "input": { "stdin": "13 7\nsonoshikumiwo\n1 5\n2 10\n7 7\n1 13\n4 8\n2 5\n3 9\n" }, "output": { "stdout": "82\n125\n9\n191\n62\n63\n97\n" } }, { "inp...
{ "tags": [ "dp", "implementation", "strings" ], "title": "Love Song" }
{ "cpp": "#include<cstdio>\n#include<cstring>\n#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<set>\n#include<map>\n#include<string>\n#include<cstdlib>\n#include<ctime>\n#define rep(i,a,n) for(int i=(int)(a);i<(int)(n);++i)\n#define repb(i,a,n) for(int i=(int)(a);i<=(int)(n);++i)\...
Codeforces/165/D
# Beard Graph Let's define a non-oriented connected graph of n vertices and n - 1 edges as a beard, if all of its vertices except, perhaps, one, have the degree of 2 or 1 (that is, there exists no more than one vertex, whose degree is more than two). Let us remind you that the degree of a vertex is the number of edges...
[ { "input": { "stdin": "6\n1 5\n6 4\n2 3\n3 5\n5 6\n6\n3 3 4\n2 5\n3 2 6\n3 1 2\n2 3\n3 3 1\n" }, "output": { "stdout": "3\n-1\n3\n2\n" } }, { "input": { "stdin": "3\n1 2\n2 3\n7\n3 1 2\n3 1 3\n3 2 3\n2 2\n3 1 2\n3 1 3\n3 2 3\n" }, "output": { "stdout": "1\n2\n...
{ "tags": [ "data structures", "dsu", "trees" ], "title": "Beard Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1e5;\nconst int LOG = 17;\nvector<int> g[MAXN + 1];\nint pa[MAXN + 1][LOG + 1];\nint lev[MAXN + 1];\nint at[MAXN + 1];\nset<int> chain[MAXN + 1];\npair<int, int> edge[MAXN + 1];\nint rt, big = 0;\nint T = 0;\nvoid fast_io() {\n ios::sync_with...
Codeforces/186/E
# Clever Fat Rat The Fat Rat and his friend Сerealguy have had a bet whether at least a few oats are going to descend to them by some clever construction. The figure below shows the clever construction. <image> A more formal description of the clever construction is as follows. The clever construction consists of n ...
[ { "input": { "stdin": "2\n2 2\n1 2\n5\n" }, "output": { "stdout": "Fat Rat" } }, { "input": { "stdin": "1\n1\n2\n" }, "output": { "stdout": "Fat Rat" } }, { "input": { "stdin": "2\n2 2\n1 2\n4\n" }, "output": { "stdout": "Cereal...
{ "tags": [ "dp" ], "title": "Clever Fat Rat" }
{ "cpp": "#include <bits/stdc++.h>\nconst double pi = acos(-1);\nconst int MOD = 1e9 + 7;\nconst int INF = 1e9 + 7;\nconst int MAXN = 5e1 + 5;\nconst double eps = 1e-9;\nusing namespace std;\nint a[MAXN], w[MAXN][MAXN], dp[MAXN][MAXN][MAXN][MAXN];\nint dfs(int i, int j, int l, int r) {\n if (j > r || i + j - 1 < l |...
Codeforces/207/D7
# The Beaver's Problem - 3 The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem: You've got some training set of documents. For e...
[ { "input": { "stdin": "32000\nU.S. M-1 MONEY SUPPLY FALLS TWO BILLION DLRS\nNEW YORK, April 9 - U.S. M-1 money supply fell two billion\ndlrs to a seasonally adjusted 738.9 billion dlrs in the March\n30 week, the Federal Reserve said.\nThe previous week's M-1 level was revised to 740.9 billion\ndlrs from 7...
{ "tags": [], "title": "The Beaver's Problem - 3" }
{ "cpp": "#include <bits/stdc++.h>\nlong long n;\nint main() {\n std::cin.tie(0);\n std::ios::sync_with_stdio(false);\n std::cin >> n;\n if (n >= 34000) std::cout << \"3\\n\", exit(0);\n std::cout << \"2\\n\";\n return 0;\n}\n", "java": "import java.util.*;\n\npublic class cf207d {\n public static void main(...
Codeforces/233/A
# Perfect Permutation A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn. Nickolas adores permutations. He likes some permu...
[ { "input": { "stdin": "4\n" }, "output": { "stdout": "2 1 4 3 " } }, { "input": { "stdin": "1\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "2\n" }, "output": { "stdout": "2 1 " } }, { "input": { ...
{ "tags": [ "implementation", "math" ], "title": "Perfect Permutation" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int n, j;\n scanf(\"%d\", &n);\n if (n & 1) {\n printf(\"-1\\n\");\n return 0;\n }\n int a[100];\n for (j = 1; j <= n; j++) a[j - 1] = j;\n for (j = 0; j < n; j += 2) {\n a[j] = a[j] + a[j + 1] - (a[j + 1] = a[j]);\n printf(\"%d %d \", a[j], a[j + ...
Codeforces/258/E
# Little Elephant and Tree The Little Elephant loves trees very much, he especially loves root trees. He's got a tree consisting of n nodes (the nodes are numbered from 1 to n), with root at node number 1. Each node of the tree contains some list of numbers which initially is empty. The Little Elephant wants to app...
[ { "input": { "stdin": "5 1\n1 2\n1 3\n3 5\n3 4\n2 3\n" }, "output": { "stdout": "0 3 3 3 3 \n" } }, { "input": { "stdin": "11 3\n1 2\n2 3\n2 4\n1 5\n5 6\n5 7\n5 8\n6 9\n8 10\n8 11\n2 9\n3 6\n2 8\n" }, "output": { "stdout": "0 6 7 6 0 2 0 5 4 5 5 \n" } },...
{ "tags": [ "data structures", "dfs and similar", "trees" ], "title": "Little Elephant and Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m;\nvector<int> G[300000];\npair<int, int> req[300000];\nvector<int> que[300000];\nint tin[300000];\nint tout[300000];\nint tinorder[300000];\nint T = 0;\nvoid dfs(int v) {\n if (tin[v] != -1) return;\n tinorder[T] = v;\n tin[v] = T++;\n for (int i ...
Codeforces/281/E
# Game on Tree Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by v) and remove...
[ { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "1.5000000000\n" } }, { "input": { "stdin": "3\n1 2\n1 3\n" }, "output": { "stdout": "2.0000000000\n" } }, { "input": { "stdin": "6\n1 3\n2 4\n5 6\n3 6\n5 4\n" }, "output": { ...
{ "tags": [ "implementation", "math", "probabilities", "trees" ], "title": "Game on Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ndouble ans;\nint n, x, y, i, depth[100005];\nvector<int> q[100005];\nvoid dfs(int x, int father) {\n depth[x] = depth[father] + 1;\n for (auto v : q[x]) {\n if (v == father) continue;\n dfs(v, x);\n }\n}\nint main() {\n cin >> n;\n for (i = 1; i < n...
Codeforces/304/D
# Rectangle Puzzle II You are given a rectangle grid. That grid's size is n × m. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates — a pair of integers (x, y) (0 ≤ x ≤ n, 0 ≤ y ≤ m). Your task is to find a maximum sub-rectangle on the grid (x1, y1, x2, y2) so that it con...
[ { "input": { "stdin": "100 100 52 50 46 56\n" }, "output": { "stdout": "17 8 86 92\n" } }, { "input": { "stdin": "9 9 5 5 2 1\n" }, "output": { "stdout": "1 3 9 7\n" } }, { "input": { "stdin": "1000000000 1000000000 500000000 500000000 500000...
{ "tags": [ "implementation", "math" ], "title": "Rectangle Puzzle II" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint gcd(int i, int j) { return i % j ? gcd(j, i % j) : j; }\nint main() {\n int n, m, x, y, a, b, u, d, l, r;\n cin >> n >> m >> x >> y >> a >> b;\n int c = gcd(a, b);\n a /= c, b /= c;\n int t = min(n / a, m / b);\n r = min(n, x + t * a / 2), l = r - t ...
Codeforces/330/E
# Graph Reconstruction I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself. I would like to create a new graph in such a way that: * The new graph consi...
[ { "input": { "stdin": "8 7\n1 2\n2 3\n4 5\n5 6\n6 8\n8 7\n7 4\n" }, "output": { "stdout": "4 3\n3 7\n7 6\n6 1\n1 5\n5 2\n2 8\n" } }, { "input": { "stdin": "5 4\n1 2\n2 3\n3 4\n4 1\n" }, "output": { "stdout": "1 3\n3 5\n5 4\n4 2\n" } }, { "input": {...
{ "tags": [ "constructive algorithms" ], "title": "Graph Reconstruction" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\ntemplate <typename A, typename B = A>\nusing pr = pair<A, B>;\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\ninline pr<int> mp(int a, int b) {\n if (a > b) swap(a, b);\n return {a, b};\n}\nint main() {\n ios::s...
Codeforces/352/C
# Jeff and Rounding Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: * choose indexes i and j (i ≠ j) that haven't been chosen y...
[ { "input": { "stdin": "3\n0.000 0.500 0.750 1.000 2.000 3.000\n" }, "output": { "stdout": "0.250\n" } }, { "input": { "stdin": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\n" }, "output": { "stdout": "0.279\n" } }, { "input": { "s...
{ "tags": [ "dp", "greedy", "implementation", "math" ], "title": "Jeff and Rounding" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\nvoid checkmax(T &t, T x) {\n if (x > t) t = x;\n}\ntemplate <class T>\nvoid checkmin(T &t, T x) {\n if (x < t) t = x;\n}\ntemplate <class T>\nvoid _checkmax(T &t, T x) {\n if (t == -1 || x > t) t = x;\n}\ntemplate <class T>\nvoid _checkm...
Codeforces/376/D
# Maximum Submatrix 2 You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations? Let's assume that the rows of matrix a ar...
[ { "input": { "stdin": "4 3\n100\n011\n000\n101\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "2 2\n10\n11\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "1 1\n1\n" }, "output": { "stdout": "1" } },...
{ "tags": [ "data structures", "dp", "implementation", "sortings" ], "title": "Maximum Submatrix 2" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T>\nusing maxHeap = priority_queue<T, vector<T>, less<T>>;\ntemplate <typename T>\nusing minHeap = priority_queue<T, vector<T>, greater<T>>;\ntemplate <typename Iter>\nostream& _out(ostream& s, Iter b, Iter e) {\n s << \"[\";\n for (auto i...
Codeforces/399/A
# Pages User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are n pages numbered by integers from 1 to n. Assume that somebody is on the p-th page now. The navigation will look like this: << p - k p - k + 1 ... p - 1 (p) p + 1 ... p + k - 1 p + k >> When someo...
[ { "input": { "stdin": "6 5 2\n" }, "output": { "stdout": "<< 3 4 (5) 6 \n" } }, { "input": { "stdin": "6 1 2\n" }, "output": { "stdout": "(1) 2 3 >>\n" } }, { "input": { "stdin": "8 5 4\n" }, "output": { "stdout": "1 2 3 4 (5) ...
{ "tags": [ "implementation" ], "title": "Pages" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int a, b, c, i, j;\n scanf(\"%d %d %d\", &a, &b, &c);\n if (b == 1) {\n printf(\"(1) \");\n if (b + c >= a) {\n for (i = 2; i < 1 + c; i++) {\n printf(\"%d \", i);\n }\n } else if (b + c < a) {\n for (i = 2; i <= 1 + c; i++) {\n ...
Codeforces/421/B
# Start Up Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it? The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if ...
[ { "input": { "stdin": "XO\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "Z\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "AHA\n" }, "output": { "stdout": "YES\n" } }, { "input": { ...
{ "tags": [ "implementation" ], "title": "Start Up" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool ok[300];\nint main() {\n cin.tie(0);\n ios_base::sync_with_stdio(0);\n string str = \"AHIMOTUVWXY\";\n for (int i = 0; i < int(str.size()); ++i) ok[str[i]] = true;\n cin >> str;\n bool ans = true;\n const int n = str.size();\n for (int i = 0; i < ...
Codeforces/447/E
# DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ..., an. Moreover, there are m queries, ...
[ { "input": { "stdin": "4 4\n1 2 3 4\n1 1 4\n2 1 4\n1 2 4\n2 1 3\n" }, "output": { "stdout": "17\n12\n" } }, { "input": { "stdin": "2 2\n1 2\n2 1 2\n2 1 2\n" }, "output": { "stdout": "3\n3\n" } }, { "input": { "stdin": "10 20\n56 12 68 23 41 2...
{ "tags": [ "data structures", "math", "number theory" ], "title": "DZY Loves Fibonacci Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct mat {\n int dgt[2][2];\n friend mat operator*(const mat &a, const mat &b) {\n mat c;\n memset(c.dgt, 0, sizeof(c.dgt));\n for (int i = 0; i < 2; ++i)\n for (int j = 0; j < 2; ++j)\n for (int k = 0; k < 2; ++k)\n c.dgt[i][...
Codeforces/46/A
# Ball Game A kindergarten teacher Natalia Pavlovna has invented a new ball game. This game not only develops the children's physique, but also teaches them how to count. The game goes as follows. Kids stand in circle. Let's agree to think of the children as numbered with numbers from 1 to n clockwise and the child ...
[ { "input": { "stdin": "10\n" }, "output": { "stdout": "2 4 7 1 6 2 9 7 6 " } }, { "input": { "stdin": "3\n" }, "output": { "stdout": "2 1 " } }, { "input": { "stdin": "6\n" }, "output": { "stdout": "2 4 1 5 4 " } }, { ...
{ "tags": [ "brute force", "implementation" ], "title": "Ball Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\ninline T gcd(T a, T b) {\n if (a < 0) return gcd(-a, b);\n if (b < 0) return gcd(a, -b);\n return (b == 0) ? a : gcd(b, a % b);\n}\ntemplate <class T>\ninline T lcm(T a, T b) {\n if (a < 0) return lcm(-a, b);\n if (b < 0) return lcm(a,...
Codeforces/492/B
# Vanya and Lanterns Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the s...
[ { "input": { "stdin": "7 15\n15 5 3 7 9 14 0\n" }, "output": { "stdout": "2.5\n" } }, { "input": { "stdin": "2 5\n2 5\n" }, "output": { "stdout": "2.0\n" } }, { "input": { "stdin": "46 615683844\n431749087 271781274 274974690 324606253 480870...
{ "tags": [ "binary search", "implementation", "math", "sortings" ], "title": "Vanya and Lanterns" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, l;\n cin >> n >> l;\n int arr[n];\n for (int i = 0; i < n; i++) cin >> arr[i];\n sort(arr, arr + n);\n double ans = (double)(arr[0] - 0);\n for (int i = 1; i < n; i++) {\n if ((double)(arr[i] - arr[i - 1]) / 2 > ans)\n ans ...
Codeforces/516/A
# Drazil and Factorial Drazil is playing a math game with Varda. Let's define <image> for positive integer x as a product of factorials of its digits. For example, <image>. First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This number may possibly start with...
[ { "input": { "stdin": "4\n1234\n" }, "output": { "stdout": "33222\n" } }, { "input": { "stdin": "3\n555\n" }, "output": { "stdout": "555\n" } }, { "input": { "stdin": "15\n987654329876543\n" }, "output": { "stdout": "77777755553...
{ "tags": [ "greedy", "math", "sortings" ], "title": "Drazil and Factorial" }
{ "cpp": "#include <bits/stdc++.h>\nint n;\nchar str[20];\nint dp[107][64][17][17];\nbool visited[107][64][17][17];\nint ar[10][4], primes[] = {2, 3, 5, 7};\nvoid Generate() {\n memset(ar, 0, sizeof(ar));\n int i, j, k;\n for (i = 2; i < 10; i++) {\n int x = i;\n for (k = 0; k < 4; k++) {\n while ((x % ...
Codeforces/543/A
# Writing Code Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes. Let's call a sequence of non-negative integers v1, v2, ..., vn a plan, if v1 + v...
[ { "input": { "stdin": "3 6 5 1000000007\n1 2 3\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "3 3 3 100\n1 1 1\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "3 5 6 11\n1 2 1\n" }, "output": { "stdo...
{ "tags": [ "dp" ], "title": "Writing Code" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int mod;\nlong long int a[550];\nlong long int dp[2][501][501];\nint main() {\n long long int n, m, i, j, ans, b, z, k;\n memset(dp, 0, sizeof(dp));\n scanf(\"%lld\", &n);\n scanf(\"%lld\", &m);\n scanf(\"%lld\", &b);\n scanf(\"%lld\", &mod);\n...
Codeforces/56/C
# Corporation Mail The Beroil corporation structure is hierarchical, that is it can be represented as a tree. Let's examine the presentation of this structure as follows: * employee ::= name. | name:employee1,employee2, ... ,employeek. * name ::= name of an employee That is, the description of each employee c...
[ { "input": { "stdin": "MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY...\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "A:C:C:C:C.....\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "A:A..\n" }, "output": { ...
{ "tags": [ "data structures", "expression parsing", "implementation" ], "title": "Corporation Mail" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ii = pair<int, int>;\nusing pll = pair<ll, ll>;\nstring to_string(char s) { return \"\\'\" + string(1, s) + \"\\'\"; }\nstring to_string(string s) { return \"\\\"\" + s + \"\\\"\"; }\ntemplate <typename T>\nstring to_string(vector<...
Codeforces/591/A
# Wizards' Duel Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length l. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of p meters pe...
[ { "input": { "stdin": "199\n60\n40\n" }, "output": { "stdout": "119.4" } }, { "input": { "stdin": "100\n50\n50\n" }, "output": { "stdout": "50" } }, { "input": { "stdin": "978\n467\n371\n" }, "output": { "stdout": "545.019093078...
{ "tags": [ "implementation", "math" ], "title": "Wizards' Duel" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int l, p, q;\n scanf(\"%d%d%d\", &l, &p, &q);\n double time = (double)l / (p + q);\n printf(\"%lf\", (double)time * p);\n}\n", "java": "import java.util.Scanner;\n\npublic class acm {\n\tpublic static void main(String[]args)\n\t{\n\t\tScan...
Codeforces/612/E
# Square Root of Permutation A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, ...
[ { "input": { "stdin": "4\n2 1 3 4\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "4\n2 1 4 3\n" }, "output": { "stdout": "3 4 2 1 " } }, { "input": { "stdin": "5\n2 3 4 5 1\n" }, "output": { "stdout": "4 5 1 2 3\n...
{ "tags": [ "combinatorics", "constructive algorithms", "dfs and similar", "graphs", "math" ], "title": "Square Root of Permutation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T>\nusing V = vector<T>;\nint gcdex(int a, int b, int &x, int &y) {\n if (a == 0) {\n x = 0;\n y = 1;\n return b;\n }\n int x1, y1;\n int g = gcdex(b % a, a, x1, y1);\n x = y1 - (b / a) * x1;\n y = x1;\n return g;\n}\ninline ...
Codeforces/632/E
# Thief in a Shop A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai. The thief is greedy, so he will take exactly k product...
[ { "input": { "stdin": "3 3\n3 5 11\n" }, "output": { "stdout": "9 11 13 15 17 19 21 25 27 33 " } }, { "input": { "stdin": "3 2\n1 2 3\n" }, "output": { "stdout": "2 3 4 5 6 " } }, { "input": { "stdin": "5 5\n1 1 1 1 1\n" }, "output": ...
{ "tags": [ "divide and conquer", "dp", "fft", "math" ], "title": "Thief in a Shop" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1025, N = 3;\nint n, k, a[MAXN];\nvoid init() {\n scanf(\"%d %d\", &n, &k);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n}\nlong long MOD[N], G[N], INVG[N], o;\nlong long pow(long long x, long long k, long long p) {\n l...
Codeforces/661/B
# Seasons You are given a name of a month. Output the season of the year to which it belongs (based on Northern hemisphere). Input The input consists of a single string containing the name of one of the twelve months (January, February, March, April, May, June, July, August, September, October, November or December)...
[ { "input": { "stdin": "April\n" }, "output": { "stdout": "spring\n" } }, { "input": { "stdin": "November\n" }, "output": { "stdout": "autumn\n" } } ]
{ "tags": [ "*special" ], "title": "Seasons" }
{ "cpp": null, "java": null, "python": null }
Codeforces/685/B
# Kay and Snowflake After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of n nodes. The root of tree has index 1. Ka...
[ { "input": { "stdin": "7 4\n1 1 3 3 5 3\n1\n2\n3\n5\n" }, "output": { "stdout": "3\n2\n3\n5\n" } }, { "input": { "stdin": "2 2\n1\n1\n2\n" }, "output": { "stdout": "1\n2\n" } }, { "input": { "stdin": "7 4\n1 1 3 3 5 3\n2\n3\n3\n5\n" }, ...
{ "tags": [ "data structures", "dfs and similar", "dp", "trees" ], "title": "Kay and Snowflake" }
{ "cpp": "#include <bits/stdc++.h>\nnamespace XX {\ntemplate <int BLOCKSIZE>\nclass MemoryManager {\n public:\n static constexpr const int INIT =\n 1024 * 1024 / BLOCKSIZE ? 1024 * 1024 / BLOCKSIZE : 1;\n static MemoryManager* inst() {\n static MemoryManager ret;\n return &ret;\n }\n void* alloc() {\n ...
Codeforces/708/A
# Letters Cyclic Shift You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' <image> 'y' <image> 'x' <image> 'b' <image> 'a' <image> 'z'. In other words, each character is replaced with the previous character of E...
[ { "input": { "stdin": "codeforces\n" }, "output": { "stdout": "bncdenqbdr" } }, { "input": { "stdin": "abacaba\n" }, "output": { "stdout": "aaacaba" } }, { "input": { "stdin": "cabaccaacccabaacdbdcbcdbccbccbabbdadbdcdcdbdbcdcdbdadcbcda\n" ...
{ "tags": [ "constructive algorithms", "greedy", "implementation", "strings" ], "title": "Letters Cyclic Shift" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar a[100005];\nint flag = 0, k, b = 0;\nint main() {\n cin >> a;\n int n = strlen(a);\n if (n == 1 && a[0] == 'a')\n cout << \"z\";\n else {\n for (int i = 0; i < n; i++) {\n if (a[i] == 'a') {\n k = i;\n b = 1;\n break;\n...
Codeforces/72/G
# Fibonacci army King Cambyses loves Fibonacci numbers. He has several armies. Today he wants to make a new army for himself and he wants the number of men in this army to be the n-th Fibonacci number. Given n you should find n-th Fibonacci number. The set of Fibonacci numbers start with f0 = f1 = 1 and for each i ≥ ...
[ { "input": { "stdin": "2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1\n" }, "output": { "stdout": "1\n" } } ]
{ "tags": [ "*special", "dp" ], "title": "Fibonacci army" }
{ "cpp": null, "java": null, "python": null }
Codeforces/750/D
# New Year and Fireworks One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, sp...
[ { "input": { "stdin": "6\n1 1 1 1 1 3\n" }, "output": { "stdout": "85\n" } }, { "input": { "stdin": "1\n3\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "4\n4 2 2 3\n" }, "output": { "stdout": "39\n" } }, {...
{ "tags": [ "brute force", "data structures", "dfs and similar", "dp", "implementation" ], "title": "New Year and Fireworks" }
{ "cpp": "#include <bits/stdc++.h>\nconst int N = 333;\nconst int X = 160;\nbool dp[N][N][X][8];\nbool use[N][N];\nbool mark[X];\nint dx[] = {0, 1, 1, 1, 0, -1, -1, -1};\nint dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};\nint turn[N];\nint main() {\n std::ios::sync_with_stdio(0);\n int n;\n std::cin >> n;\n for (int i = 0;...
Codeforces/774/B
# Significant Cups Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance ci and width wi. Stepan decided to expose some of his cups on a shelf with width d in such a way, that: * th...
[ { "input": { "stdin": "3 1 8\n4 2\n5 5\n4 2\n3 2\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "2 2 2\n5 3\n6 3\n4 2\n8 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4\n...
{ "tags": [ "*special", "binary search", "data structures", "two pointers" ], "title": "Significant Cups" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n, m, d;\nconst int N = 100100;\nstruct node {\n long long c, w;\n bool operator<(const node t) const {\n if (c == t.c) return w < t.w;\n return c > t.c;\n }\n} a[N], b[N];\nint main() {\n scanf(\"%lld%lld%lld\", &n, &m, &d);\n for (int i ...
Codeforces/799/B
# T-shirt buying A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from ...
[ { "input": { "stdin": "5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1\n" }, "output": { "stdout": "200 400 300 500 911 -1 \n" } }, { "input": { "stdin": "2\n1000000000 1\n1 1\n1 2\n2\n2 1\n" }, "output": { "stdout": "1 1000000000 \n" } }, ...
{ "tags": [ "data structures", "implementation" ], "title": "T-shirt buying" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct SYM {\n int p, id;\n bool operator<(const SYM &a) const { return a.p < p; }\n} clo[200010];\nint vis[200010], c, n, m;\npriority_queue<SYM> q[4];\nint main() {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &clo[i].p);\n ...
Codeforces/819/B
# Mister B and PR Shifts Some time ago Mister B detected a strange signal from the space, which he started to study. After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shi...
[ { "input": { "stdin": "3\n1 2 3\n" }, "output": { "stdout": "0 0\n" } }, { "input": { "stdin": "3\n3 2 1\n" }, "output": { "stdout": "2 1\n" } }, { "input": { "stdin": "3\n2 3 1\n" }, "output": { "stdout": "0 1\n" } }, {...
{ "tags": [ "data structures", "implementation", "math" ], "title": "Mister B and PR Shifts" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f3f3f3f;\nconst int N = 2e6 + 100;\nint a[N], shift[N];\nint main() {\n int n, lower = 0;\n long long sum = 0;\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", a + i);\n if (a[i] > i) {\n shift[a[i] - i]++...
Codeforces/844/E
# Upgrading Tree You are given a tree with n vertices and you are allowed to perform no more than 2n transformations on it. Transformation is defined by three vertices x, y, y' and consists of deleting edge (x, y) and adding edge (x, y'). Transformation x, y, y' could be performed if all the following conditions are s...
[ { "input": { "stdin": "3\n3 2\n1 3\n" }, "output": { "stdout": "2\n3 2 2\n3 1 1\n" } }, { "input": { "stdin": "7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n" }, "output": { "stdout": "10\n4 3 1\n1 2 3\n4 1 2\n2 3 3\n4 2 3\n4 5 7\n7 6 5\n4 7 6\n6 5 5\n4 6 5\n" } }, ...
{ "tags": [ "constructive algorithms", "dfs and similar", "graphs", "math", "trees" ], "title": "Upgrading Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int c = 200002;\nint n, f[c], rf[c], h, l, ki, ut;\nvector<int> sz[c];\nvector<pair<int, pair<int, int> > > sol;\nbool v1[c], v2[c], cen[c];\nvoid add(int x, int y1, int y2) { sol.push_back({x, {y1, y2}}); }\nvoid dfs1(int a) {\n v1[a] = true, rf[a] = 1...
Codeforces/865/B
# Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices. It is known that the i-th contestant will eat si slices of p...
[ { "input": { "stdin": "3 12\n3 5 7\n4 6 7\n5 9 5\n" }, "output": { "stdout": "84\n" } }, { "input": { "stdin": "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6\n" }, "output": { "stdout": "314\n" } }, { "input": { "stdin": "1 100\n97065 9764...
{ "tags": [ "binary search", "sortings", "ternary search" ], "title": "Ordering Pizza" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long readi() {\n long long input = 0;\n char c = ' ';\n while (c < '-') {\n c = getchar();\n }\n bool negative = false;\n if (c == '-') {\n negative = true;\n c = getchar();\n }\n while (c >= '0') {\n input = 10 * input + (c - '0');\n ...
Codeforces/891/B
# Gluttony You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 ≤ xi ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e. <image> Input The first line contains one integer n ...
[ { "input": { "stdin": "4\n1000 100 10 1\n" }, "output": { "stdout": "1 1000 100 10 " } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "2 1 " } }, { "input": { "stdin": "22\n17 10 24 44 41 33 48 6 30 27 38 19 16 46 22 8 35 13 5 9...
{ "tags": [ "constructive algorithms", "greedy" ], "title": "Gluttony" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, ans[100];\npair<int, int> P[100];\nint main() {\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> P[i].first;\n P[i].second = i;\n }\n sort(P, P + n);\n for (int i = 0; i < n; i++) ans[P[i].second] = P[(i + 1) % n].first;\n for (int i = 0...
Codeforces/913/E
# Logical Expression You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of: * Operation AND ('&', ASCII code 38) * Operation OR ('|', ASCII code 124) * Operation NOT...
[ { "input": { "stdin": "4\n00110011\n00000111\n11110000\n00011111\n" }, "output": { "stdout": "y\n(y|z)&x\n!x\nx|y&z\n" } }, { "input": { "stdin": "4\n11000010\n11000010\n11001110\n10001001\n" }, "output": { "stdout": "!x&!y|!z&x&y\n!x&!y|!z&x&y\n!y|!z&x\n!y&!z...
{ "tags": [ "bitmasks", "dp", "shortest paths" ], "title": "Logical Expression" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstring f[300], t[300], e[300];\nchar s[20];\nint n;\nbool cmp(string& a, string b) {\n if (a == \"\") return 1;\n return a.size() == b.size() ? a > b : a.size() > b.size();\n}\nint main() {\n f[240] = \"x\";\n f[204] = \"y\";\n f[170] = \"z\";\n while (1...
Codeforces/935/E
# Fafa and Ancient Mathematics Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression. An Ahmes arithmetic expression can be defined as: ...
[ { "input": { "stdin": "((1?(5?7))?((6?2)?7))\n3 2\n" }, "output": { "stdout": "18" } }, { "input": { "stdin": "(1?1)\n1 0\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "((1?(5?7))?((6?2)?7))\n2 3\n" }, "output": { "...
{ "tags": [ "dfs and similar", "dp", "trees" ], "title": "Fafa and Ancient Mathematics" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 2e9, N = 10010;\nconst double eps = 1e-8;\ninline void read(int &x) {\n x = 0;\n int f = 1;\n char ch = getchar();\n while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();\n if (ch == '-') {\n f = -1;\n ch = getchar();\n }\n w...
Codeforces/961/D
# Pair Of Lines You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordinates are integers), and all points are distinct. You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?...
[ { "input": { "stdin": "5\n0 0\n0 1\n1 1\n1 -1\n2 2\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "5\n0 0\n1 0\n2 1\n1 1\n2 3\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "5\n0 0\n2 0\n1 1\n0 2\n5 1\n" }, ...
{ "tags": [ "geometry" ], "title": "Pair Of Lines" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct point {\n long long x, y;\n};\nstruct line {\n long long a, b, c;\n};\nline calculate_line(point a, point b) {\n line result;\n result.a = a.y - b.y;\n result.b = b.x - a.x;\n result.c = a.x * b.y - a.y * b.x;\n return result;\n}\nbool same_line(...
Codeforces/989/C
# A Mist of Florescence As the boat drifts down the river, a wood full of blossoms shows up on the riverfront. "I've been here once," Mino exclaims with delight, "it's breathtakingly amazing." "What is it like?" "Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?" There are four...
[ { "input": { "stdin": "5 3 2 1\n" }, "output": { "stdout": "50 50\nAAAAAAAAAAAAAAAAAAAAAAAAACBBBBBBBBBBBBBBBBBBBBBBBB\nAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBB\nAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBB\nAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBB\nAAAAAAAAAAAAAAAAA...
{ "tags": [ "constructive algorithms", "graphs" ], "title": "A Mist of Florescence" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 5;\nint a, b, c, d;\nchar s[60][60];\nint main() {\n ios::sync_with_stdio(false);\n cin >> a >> b >> c >> d, a--;\n for (int i = 1; i <= 8; i++) {\n if (i & 1) {\n for (int j = 2; j <= 50; j += 2) s[i][j] = 'A';\n for (int j...
HackerEarth/amit-chahals-love-for-juices-and-dhruv-katyals-astrology
Amit Chahal has a pious love for all kind of fruit juices. Almost every day ,he buys One litre pack of juice. For past one month , Amit Chahal is having a very bad time. One day while he was on a date, he Reverse Peristalsis . Amit Chahal guessed it is probably due to juices he has been drinking for a while. Amit Chah...
[ { "input": { "stdin": "3\n22\n3\n7\n\nSAMPLE" }, "output": { "stdout": "NO\nYES\nYES" } }, { "input": { "stdin": "3\n17\n16\n23\n\nSAMLQE" }, "output": { "stdout": "NO\nYES\nYES\n" } }, { "input": { "stdin": "3\n27\n50\n57\n\nTANOME" }, ...
{ "tags": [], "title": "amit-chahals-love-for-juices-and-dhruv-katyals-astrology" }
{ "cpp": null, "java": null, "python": "def isprime(number):\n if number==2:\n return 1\n if number%2==0: \n return 0\n N=number**0.5\n i=3\n while i<=N:\n if number%i==0:\n return 0\n i=i+2\n return 1\n\ndef main():\n for t in range(input()):\n number=long(raw_input())\n summ=0\n while number>0:\n sum...
HackerEarth/car-parking-4
You are looking for a place to park your car on a wall street. You can park at any position that meets the following requirements: 1. It is not directly in front of a private driveway. 2. It is not directly in front of a bus stop. 3. It is not 5 meters before a bus stop. 4. It is not 10 meters before a bus stop. 5....
[ { "input": { "stdin": "3\n---B--S-D--S--\nDDBDDBDDBDD\n--S--S--S--S--\n\nSAMPLE" }, "output": { "stdout": "4\n0\n2\n" } }, { "input": { "stdin": "46\n---B--S-D--S--\nDDBDDBDDBDD\n--S--S--S--S--\nSSD-B---BD-DDSB-----S-S--------S-B----BSB-S--B-S-D\n-SB---S----DD-D-DS---B--BD-...
{ "tags": [], "title": "car-parking-4" }
{ "cpp": null, "java": null, "python": "class StreetParking(object):\n def freeParks(self, street):\n spots = 0\n street_list = list(street)\n\n street_list[len(street_list):] = \".\", \".\"\n for i in range(2):\n street_list.insert(0, \".\")\n x_list = street_list...
HackerEarth/dfs-2
You have some boxes. All of them are assigned a unique positive integer. This number is written on them with a marker. Your task is simple that is to evaluate a number of queries. The description of a single query is given below: The query consists of 2 positive integers L and R. You have to report total number of bo...
[ { "input": { "stdin": "5\n4 3 2 1 1\n4\n1 2\n2 3\n5 6\n1 1\n\nSAMPLE" }, "output": { "stdout": "3\n2\n0\n2\n" } }, { "input": { "stdin": "5\n8 3 2 1 1\n4\n1 2\n1 3\n5 8\n1 2\n\nMASPLF" }, "output": { "stdout": "3\n4\n1\n3\n" } }, { "input": { ...
{ "tags": [], "title": "dfs-2" }
{ "cpp": null, "java": null, "python": "def bs(a,t):\n low=0\n high=len(a)-1\n while low<=high:\n mid=(low+high)/2\n if a[mid]<=t:\n \tlow=mid+1\n else:\n high=mid-1\n return high\ndef bsl(a,t):\n low=0\n high=len(a)-1\n while low<=high:\n mid=(lo...
HackerEarth/gcd-game-11
Life and death, win or lose - both have two sides to each other. And that's what Arjit and Chandu Don are fighting about. They are tired of gang-wars between each other, and thus decide to settle like men in a field, of Mathematics. But even while going to play the game of Mathematics, they have not given up on their ...
[ { "input": { "stdin": "1000\n1 1\n42 468\n335 1\n170 225\n479 359\n463 465\n206 146\n282 328\n462 492\n496 443\n328 437\n392 105\n403 154\n293 383\n422 217\n219 396\n448 227\n272 39\n370 413\n168 300\n36 395\n204 312\n323 334\n174 165\n142 212\n254 369\n48 145\n163 258\n38 360\n224 242\n30 279\n317 36\n19...
{ "tags": [], "title": "gcd-game-11" }
{ "cpp": null, "java": null, "python": "from sys import stdin\nfrom fractions import gcd\n\n# for result memoization\nregistry = {}\n\ndef get_result(a,b):\n\tif not registry.has_key((a,b)):\n\t\tif a== 1:\n\t\t\tregistry[(a,b)] = 0\n\t\telif b == 1:\n\t\t\tregistry[(a,b)] = 1\n\t\telse:\n\t\t\tgreatest_div =gcd(...
HackerEarth/largest-volume-84
Now our heroes - Maga and Alex are working for Oil Company as developers. Recently they have faced a great problem. They couldn’t find the correct solution. Could you help them? We are given r – radius of lower base and s – slant height. The figure can be cylinder or truncated cone. You have to find as largest volum...
[ { "input": { "stdin": "1 1\n\nSAMPLE" }, "output": { "stdout": "4.33" } }, { "input": { "stdin": "97 33" }, "output": { "stdout": "1028727.30" } }, { "input": { "stdin": "68 95" }, "output": { "stdout": "2291771.52" } }, ...
{ "tags": [], "title": "largest-volume-84" }
{ "cpp": null, "java": null, "python": "import math\nr,s=map(int,raw_input().split())\nmax_V=0\nR=r\nh=s\nwhile h>=0:\n\tt=s**2-(R-r)**2\n\tif t<0:\n\t\tbreak\n\th=t**.5\n\tV=(math.pi*h)*(r**2+R**2+R*r)/3.0\n\tif V>=max_V:\n\t\tmax_V=V\n\tR=R+0.001\n\t\nprint\"%0.2f\" % max_V" }
HackerEarth/monk-and-tasks
Monk A loves to complete all his tasks just before the deadlines for introducing unwanted thrill in his life. But, there is another Monk D who hates this habit of Monk A and thinks it's risky. To test Monk A, Monk D provided him tasks for N days in the form of an array Array, where the elements of the array represen...
[ { "input": { "stdin": "10\n384\n846930887 1681692778 1714636916 1957747794 424238336 719885387 1649760493 596516650 1189641422 1025202363 1350490028 783368691 1102520060 2044897764 1967513927 1365180541 1540383427 304089173 1303455737 35005212 521595369 294702568 1726956430 336465783 861021531 278722863 2...
{ "tags": [], "title": "monk-and-tasks" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\n\nfor i in range(t):\n\tn = int(raw_input())\t\n\tarr = map(int, raw_input().strip().split(' '))\n\ttasks = [None]*n\n\t\n\tfor j in range(n):\n\t\ttemp = arr[j]\n\t\tcount=0\n\t\twhile temp:\n\t\t\ttemp = temp&(temp-1)\n\t\t\tcount+=1\n\t\ttasks[j] = ...
HackerEarth/perfect-square-5
Shreyan is appearing for CAT examination and is stuck at a problem to find minimum value needed to be added or subtracted to make a number perfect square.You as his friend and a good programmer agrees to help him find the answer. INPUT: First line will contain no of test cases T (1 < T < 10000) Next T lines will ha...
[ { "input": { "stdin": "3\n25\n168\n19\n\nSAMPLE" }, "output": { "stdout": "YES\n+1\n-3\n" } }, { "input": { "stdin": "10010\n34691\n11183\n51314\n1000000\n999999\n0\n66666\n11111\n91111\n26110\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n2...
{ "tags": [], "title": "perfect-square-5" }
{ "cpp": null, "java": null, "python": "import math;\nt=int(input());\nfor i in range(0,t):\n\tn=int(input());\n\ts=\"\";\n\tk=int(math.sqrt(n));\n\td1=n-(k*k);\n\td2=((k+1)*(k+1))-n;\n\tif d1<d2 and d1!=0:\n\t\ts=\"-\"+str(d1);\n\t\tprint s;\n\telif d2<d1:\n\t\ts=\"+\"+str(d2);\n\t\tprint s;\n\telse:\n\t\tprint(...
HackerEarth/roy-and-matrix-sum-1
Roy has a matrix of size NxN. Rows and Columns are numbered from 0 to N-1. jth column of ith row contains absolute difference between i and j. In other words, Matrix[i][j] = abs(i-j) where 0 ≤ i, j < N. Your task is to find sum of this matrix i.e. sum = 0 for i=0 to N-1 for j=0 to N-1 sum += Matrix[i][...
[ { "input": { "stdin": "10\n9\n10\n99\n100\n999\n1000\n9999\n10000\n99999\n100000" }, "output": { "stdout": "240\n330\n323400\n333300\n332334000\n333333000\n333233340000\n333333330000\n333323333400000\n333333333300000\n" } }, { "input": { "stdin": "10\n8\n4\n146\n100\n950\n1...
{ "tags": [], "title": "roy-and-matrix-sum-1" }
{ "cpp": null, "java": null, "python": "T=input();\nfor _ in xrange(T):\n\tN=input();\tprint ((N-1)*(N)*(N+1))/3;" }
HackerEarth/strings-awesomeness
Vivek likes strings a lot but moreover he likes Awesome strings. Vivek calls a string str Awesome if zero or more letters of the string str can be rearranged to form the string "HackerEarth" (case insensitive). For eg : strings HackerEarth , Earthhacker , haeackerrth are all Awesome strings whereas strings HE, Mycareer...
[ { "input": { "stdin": "HackerEarthhACKEReARTH\n5\n1 11\n2 15\n5 15\n7 20\n7 22\n\nSAMPLE" }, "output": { "stdout": "1\n4\n1\n4\n6" } } ]
{ "tags": [], "title": "strings-awesomeness" }
{ "cpp": null, "java": null, "python": "p=[0]*500001\nc=[0]*26\nt=[0]*26\nc[ord('h')-97]=c[ord('a')-97]=c[ord('e')-97]=c[ord('r')-97]=2\nc[ord('c')-97]=c[ord('k')-97]=c[ord('t')-97]=1;\n\ns=raw_input().lower()\n\nk=len(s)\nif k>10:\n\tfor i in range(11):\n\t\tt[ord(s[i])-97]+=1\n\t\tif t==c:\n\t\t\tp[10]=1\n\tfor...
HackerEarth/vaishnav-and-factorials
As we have seen our little Vaishanavi playing with the plywood, her brother Vaishnav was playing with numbers. Read about Factorial As we all know this kid always thinks in a different way than others think. He have some numbers with him. He wants to find out that if he finds the factorial of the numbers that he ha...
[ { "input": { "stdin": "3\n1\n6\n8\n\nSAMPLE" }, "output": { "stdout": "0\n1\n1\n" } }, { "input": { "stdin": "100\n200\n201\n202\n203\n204\n205\n206\n207\n208\n209\n210\n211\n212\n213\n214\n215\n216\n217\n218\n219\n220\n221\n222\n223\n224\n225\n226\n227\n228\n229\n230\n231\...
{ "tags": [], "title": "vaishnav-and-factorials" }
{ "cpp": null, "java": null, "python": "for i in range(int(raw_input())):\n\tt=int(raw_input())\n\tfact=1\n\tfor i in range(1,t+1):\n\t\tfact=fact*i\n\tstr(fact).rstrip('0')\n\tprint str(fact).count('4')+str(fact).count('7')" }
p02601 M-SOLUTIONS Programming Contest 2020 - Magic 2
M-kun has the following three cards: * A red card with the integer A. * A green card with the integer B. * A blue card with the integer C. He is a genius magician who can do the following operation at most K times: * Choose one of the three cards and multiply the written integer by 2. His magic is successful if...
[ { "input": { "stdin": "7 4 2\n3" }, "output": { "stdout": "No" } }, { "input": { "stdin": "7 2 5\n3" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "12 7 7\n7" }, "output": { "stdout": "Yes\n" } }, { "inpu...
{ "tags": [], "title": "p02601 M-SOLUTIONS Programming Contest 2020 - Magic 2" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint main()\n{\n int a,b,c,k;\n cin>>a>>b>>c>>k;\n while(a>=b)\n b*=2,k--;\n while(b>=c)\n c*=2,k--;\n if(k>=0)cout<<\"Yes\"<<endl;\n else cout<<\"No\"<<endl;\n return 0;\n}\n", "java": "import java.util.Scanner;\nclass Main ...
p02732 AtCoder Beginner Contest 159 - Banned K
We have N balls. The i-th ball has an integer A_i written on it. For each k=1, 2, ..., N, solve the following problem and print the answer. * Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal. Constraint...
[ { "input": { "stdin": "8\n1 2 1 4 2 1 4 1" }, "output": { "stdout": "5\n7\n5\n7\n7\n5\n7\n5" } }, { "input": { "stdin": "5\n1 1 2 1 2" }, "output": { "stdout": "2\n2\n3\n2\n3" } }, { "input": { "stdin": "4\n1 2 3 4" }, "output": { ...
{ "tags": [], "title": "p02732 AtCoder Beginner Contest 159 - Banned K" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nint n,a[1<<18],h[1<<18];\nll tot;\nint main(){\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++)scanf(\"%d\",a+i),h[a[i]]++;\n\tfor(int i=1;i<=n;i++)tot+=1ll*h[i]*(h[i]-1)/2;\n\tfor(int i=1;i<=n;i++)printf(\"%lld\\n\",tot-(h[a[i]]-1));\n\tretur...
p02865 NIKKEI Programming Contest 2019-2 - Sum of Two Integers
How many ways are there to choose two distinct positive integers totaling N, disregarding the order? Constraints * 1 \leq N \leq 10^6 * N is an integer. Input Input is given from Standard Input in the following format: N Output Print the answer. Examples Input 4 Output 1 Input 999999 Output 499999
[ { "input": { "stdin": "4" }, "output": { "stdout": "1" } }, { "input": { "stdin": "999999" }, "output": { "stdout": "499999" } }, { "input": { "stdin": "21312" }, "output": { "stdout": "10655\n" } }, { "input": { ...
{ "tags": [], "title": "p02865 NIKKEI Programming Contest 2019-2 - Sum of Two Integers" }
{ "cpp": "#include <iostream>\nusing namespace std;\nint main()\n{\n \tint n;\n \tcin>>n;\n \tint s=(n-1)/2;\n \tcout<<s;\n return (0);\n}", "java": "import java.util.Scanner;\n \n public class Main { //クラス名はMain\n public static void main(String[] args) {\n //コード\n ...
p03000 AtCoder Beginner Contest 130 - Bounding
A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}. How many times will the ball make a bounce where the coordinate is at most X? Constraints * 1 \leq N \leq 100 * 1 \leq L_i ...
[ { "input": { "stdin": "4 9\n3 3 3 3" }, "output": { "stdout": "4" } }, { "input": { "stdin": "3 6\n3 4 5" }, "output": { "stdout": "2" } }, { "input": { "stdin": "4 9\n4 6 1 3" }, "output": { "stdout": "2\n" } }, { "...
{ "tags": [], "title": "p03000 AtCoder Beginner Contest 130 - Bounding" }
{ "cpp": "#include <iostream>\nusing namespace std;\nint main() {\n int n, x, ans = 1, at;\n cin >> n >> x;\n for (int i = 0; i < n; i++) {\n int l;\n cin >> l;\n at += l;\n if (at <= x) ans++;\n }\n cout << ans << endl;\n}\n", "java": "import java.util.Scanner;\n\nclass Main {\n public static v...
p03141 NIKKEI Programming Contest 2019 - Different Strokes
There are N dishes of cuisine placed in front of Takahashi and Aoki. For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N. When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness. Starting from Takahashi, they alternately choose one dish and ...
[ { "input": { "stdin": "3\n20 10\n20 20\n20 30" }, "output": { "stdout": "20" } }, { "input": { "stdin": "3\n10 10\n20 20\n30 30" }, "output": { "stdout": "20" } }, { "input": { "stdin": "6\n1 1000000000\n1 1000000000\n1 1000000000\n1 10000000...
{ "tags": [], "title": "p03141 NIKKEI Programming Contest 2019 - Different Strokes" }
{ "cpp": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\nconst ll MOD = 1e9 + 7;\nconst ll INF = 1LL << 60;\nconst double PI = 3.141592653589793238;\nconst double EPS = 1e-10;\n\nint main() {\n\tint N;\n\tcin >> N;\n\tll sum = 0;\n\tvector<ll> vec;\n\tfor (int i = 0; i < N; i++) {\n\t\tll A,...
p03285 AtCoder Beginner Contest 105 - Cakes and Donuts
La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each. Determine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes. Constraints * N is an integer between 1 and 10...
[ { "input": { "stdin": "40" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "3" }, "output": { "stdout": "No" } }, { "input": { "stdin": "11" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "...
{ "tags": [], "title": "p03285 AtCoder Beginner Contest 105 - Cakes and Donuts" }
{ "cpp": "#include<iostream>\nusing namespace std;\nint N;\nbool r;\nvoid rec(int n){\n if(n==0)r=true;\n if(n<=0)return;\n rec(n-4);\n rec(n-7);\n}\nint main(void){\n cin>>N;\n rec(N);\n cout<<(r?\"Yes\":\"No\")<<endl;\n return 0;\n}\n", "java": "import java.util.*;\n\nclass Main{\n public static void m...
p03441 AtCoder Petrozavodsk Contest 001 - Antennas on Tree
We have a tree with N vertices. The vertices are numbered 0 through N - 1, and the i-th edge (0 ≤ i < N - 1) comnnects Vertex a_i and b_i. For each pair of vertices u and v (0 ≤ u, v < N), we define the distance d(u, v) as the number of edges in the path u-v. It is expected that one of the vertices will be invaded by ...
[ { "input": { "stdin": "2\n0 1" }, "output": { "stdout": "1" } }, { "input": { "stdin": "5\n0 1\n0 2\n0 3\n3 4" }, "output": { "stdout": "2" } }, { "input": { "stdin": "10\n2 8\n6 0\n4 1\n7 6\n2 3\n8 6\n6 9\n2 4\n5 8" }, "output": { ...
{ "tags": [], "title": "p03441 AtCoder Petrozavodsk Contest 001 - Antennas on Tree" }
{ "cpp": "#include <stdio.h>\n#include <iostream>\n#include <vector>\n#include <assert.h>\n#include <set>\n#include <map>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <string>\n#include <sstream>\n#include <memory.h>\n#include <time.h>\n#include <string.h>\n#include <stdlib.h>\n#include <algorithm>...
p03599 AtCoder Beginner Contest 074 - Sugar Water
Snuke is making sugar water in a beaker. Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations. * Operation 1: Pour 100A grams of water into the beaker. * Operation 2: Pour 100B grams of water into the beaker....
[ { "input": { "stdin": "1 2 1 2 100 1000" }, "output": { "stdout": "200 100" } }, { "input": { "stdin": "1 2 10 20 15 200" }, "output": { "stdout": "110 10" } }, { "input": { "stdin": "17 19 22 26 55 2802" }, "output": { "stdout"...
{ "tags": [], "title": "p03599 AtCoder Beginner Contest 074 - Sugar Water" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int A,B,C,D,E,F;\n cin>>A>>B>>C>>D>>E>>F;\n int n=1,m=-1;\n for(int a=0;a*A*100<=F;a++){\n int x=F-a*A*100;\n for(int b=0;b*B*100<=x;b++){\n int y=min(x-b*B*100,(a*A+b*B)*E);\n for(int c=0;c*C<=y;c++)...
p03760 AtCoder Beginner Contest 058 - ∵∴∵
Snuke signed up for a new website which holds programming competitions. He worried that he might forget his password, and he took notes of it. Since directly recording his password would cause him trouble if stolen, he took two notes: one contains the characters at the odd-numbered positions, and the other contains the...
[ { "input": { "stdin": "atcoderbeginnercontest\natcoderregularcontest" }, "output": { "stdout": "aattccooddeerrbreeggiunlnaerrccoonntteesstt" } }, { "input": { "stdin": "xyz\nabc" }, "output": { "stdout": "xaybzc" } }, { "input": { "stdin": "f...
{ "tags": [], "title": "p03760 AtCoder Beginner Contest 058 - ∵∴∵" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n string O,E;\n cin>>O>>E;\n for(int i=0;i<E.size();i++)cout<<O.at(i)<<E.at(i);\n if(O.size()!=E.size())cout<<O.at(O.size()-1);\n cout<<endl;\n}", "java": "import java.util.Scanner;\n\npublic class Main {\n\n public static void main(Strin...
p03922 CODE FESTIVAL 2016 Final (Parallel) - Pair Cards
Takahashi is playing with N cards. The i-th card has an integer X_i on it. Takahashi is trying to create as many pairs of cards as possible satisfying one of the following conditions: * The integers on the two cards are the same. * The sum of the integers on the two cards is a multiple of M. Find the maximum numb...
[ { "input": { "stdin": "7 5\n3 1 4 1 5 9 2" }, "output": { "stdout": "3" } }, { "input": { "stdin": "15 10\n1 5 6 10 11 11 11 20 21 25 25 26 99 99 99" }, "output": { "stdout": "6" } }, { "input": { "stdin": "7 1\n4 1 6 1 0 1 3" }, "out...
{ "tags": [], "title": "p03922 CODE FESTIVAL 2016 Final (Parallel) - Pair Cards" }
{ "cpp": "#include<iostream>\n#include<iomanip>\n#include<math.h>\n#include<vector>\n#include<algorithm>\n#include<set>\n#include<map>\n#include<queue>\n#include<stack>\n#include<string>\n#include<time.h>\n#define INF 1000000000ll\n#define MOD 1000000007ll\n#define EPS 1e-10\n#define REP(i,m) for(long long i=0; i<m; ...
AIZU/p00023
# Circles Intersection You are given circle $A$ with radius $r_a$ and with central coordinate $(x_a, y_a)$ and circle $B$ with radius $r_b$ and with central coordinate $(x_b, y_b)$. Write a program which prints: * "2" if $B$ is in $A$, * "-2" if $A$ is in $B$, * "1" if circumference of $A$ and $B$ intersect, and * "...
[ { "input": { "stdin": "2\n0.0 0.0 5.0 0.0 0.0 4.0\n0.0 0.0 2.0 4.1 0.0 2.0" }, "output": { "stdout": "2\n0" } }, { "input": { "stdin": "2\n0.0 0.24407396280008453 5.0 1.4717826557130267 1.81180019862589 4.0\n0.9769597723465082 0.39921226227011497 2.0 4.456171562511242 0.735...
{ "tags": [], "title": "Circles Intersection" }
{ "cpp": "#include<iostream>\nusing namespace std;\nint main(){\n int n;\n cin >>n;\n while(n--){\n double a[3],b[3],dis,m;\n cin >>a[0]>>a[1]>>a[2]>>b[0]>>b[1]>>b[2];\n dis = (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]);\n if(dis>(a[2]+b[2])*(a[2]+b[2])) cout <<\"0\"<<endl;\n else if(dis<(a[2]-b...
AIZU/p00154
# Sum of Cards Let's play the game using a bag containing several cards with integers written on it. In each game, participants first declare one of their favorite number n. Then, take out an appropriate number of cards from the bag at a time, and if the sum of the numbers written on those cards is equal to n, you wil...
[ { "input": { "stdin": "5\n1 10\n5 3\n10 3\n25 2\n50 2\n4\n120\n500\n100\n168\n7\n1 10\n3 10\n5 10\n10 10\n25 10\n50 10\n100 10\n3\n452\n574\n787\n0" }, "output": { "stdout": "16\n0\n12\n7\n9789\n13658\n17466" } }, { "input": { "stdin": "5\n2 10\n6 4\n5 2\n25 2\n50 1\n4\n120...
{ "tags": [], "title": "Sum of Cards" }
{ "cpp": "#include <iostream>\n#include <stdio.h>\n\nusing namespace std;\n\nint m,cn[8],ck[8],an[10000];\n\nvoid cal(int s,int k) {\n\tint i;\n\tif (k==m) return;\n\tfor (i=0;i<=ck[k];i++) {\n\t\tif (i>0) an[s+i*cn[k]]++;\n\t\tcal(s+i*cn[k],k+1);}\n}\n\nint main() {\n int i,g,n;\n while(cin >> m) {\n\t if (m==0) bre...
AIZU/p00311
# Fishing Competition The brothers Hiroshi and Kenjiro came to Lake Inawashiro for fishing. The two decided to score as follows and compete with the total score of the fish they caught. * One char is a point * One yamame trout b points * Add c points for every 10 chars * Add d points for every 20 yamame trout Crea...
[ { "input": { "stdin": "5 1\n3 1\n1 2 5 5" }, "output": { "stdout": "hiroshi" } }, { "input": { "stdin": "0 20\n10 0\n1 1 10 0" }, "output": { "stdout": "even" } }, { "input": { "stdin": "5 1\n4 2\n1 2 5 5" }, "output": { "stdout...
{ "tags": [], "title": "Fishing Competition" }
{ "cpp": "#include <iostream>\nusing namespace std;\n\nint main() {\n\n\tint h1, h2, k1, k2, a, b, c, d, n, m;\n\n\tcin >> h1 >> h2 >> k1 >> k2 >> a >> b >> c >> d;\n\n\tn = a * h1 + b * h2 + c * (h1 / 10) + d * (h2 / 20);\n\tm = a * k1 + b * k2 + c * (k1 / 10) + d * (k2 / 20);\n\tif(n > m) \n\t\tcout << \"hiroshi\" ...
AIZU/p00475
# Exposition problem JOI City will hold a large-scale exposition. There are two themes for this exposition, and each of the N exhibition facilities in JOI City will exhibit on one of the two themes. The location of the facility is represented by plane coordinates (x, y). To move from the facility at position (x, y)...
[ { "input": { "stdin": "5\n0 0\n1 0\n-1 -2\n0 1\n-1 1" }, "output": { "stdout": "3" } }, { "input": { "stdin": "5\n-2 0\n118 -1\n15 -11\n-9 2\n0 -3" }, "output": { "stdout": "37\n" } }, { "input": { "stdin": "5\n-2 2\n-1 3\n0 -2\n-2 26\n-9 1" ...
{ "tags": [], "title": "Exposition" }
{ "cpp": "#include<iostream>\n#include<algorithm>\n#include<vector>\n#include<cstdio>\n#include<list>\n#include<queue>\n#include<string.h>\n#include<functional>\n#include<stack>\n#include<deque>\n#include<string>\n#include<limits.h>\n#include<map>\n#include<set>\n#include<unordered_map>\n#include<unordered_set>\n#def...
AIZU/p00667
# 11224111122411 My arm is stuck and I can't pull it out. I tried to pick up something that had rolled over to the back of my desk, and I inserted my arm while illuminating it with the screen of my cell phone instead of a flashlight, but I accidentally got caught in something and couldn't pull it out. It hurts if you...
[ { "input": { "stdin": "1\n11\n111111\n111111111111\n12345\n11111111119999999999\n11111111113333333333\n11111111118888888888\n11111111112222222222111111111\n11111111110000000000444444444\n11224111122411\n888888888888999999999999888888888888999999999999999999\n666666666666666777333333333338888888888\n111111...
{ "tags": [], "title": "11224111122411" }
{ "cpp": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <functional>\n#include <sstream>\n#include <complex...
AIZU/p00810
# Super Star During a voyage of the starship Hakodate-maru (see Problem A), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of "super stars". Do not take this term as a description of actors or singers. It is a revolutionary theory in astronomy....
[ { "input": { "stdin": "4\n10.00000 10.00000 10.00000\n20.00000 10.00000 10.00000\n20.00000 20.00000 10.00000\n10.00000 20.00000 10.00000\n4\n10.00000 10.00000 10.00000\n10.00000 50.00000 50.00000\n50.00000 10.00000 50.00000\n50.00000 50.00000 10.00000\n0" }, "output": { "stdout": "7.07107\n3...
{ "tags": [], "title": "Super Star" }
{ "cpp": "#include<cassert>\n#include<cstdio>\n#include<iostream>\n#include<vector>\n#include<cmath>\n#include<algorithm>\nusing namespace std;\n#define REP(i,b,n) for(int i=b;i<n;i++)\n#define rep(i,n) REP(i,0,n)\n\nconst double eps = 1e-10;\n\nclass Point{\npublic:\n double x,y,z;\n double dist(){\n return s...
AIZU/p00941
# Do Geese See God? Example Input crc 1 Output crc
[ { "input": { "stdin": "crc\n1" }, "output": { "stdout": "crc" } }, { "input": { "stdin": "epd\n0" }, "output": { "stdout": "deped\n" } }, { "input": { "stdin": "hfq\n1" }, "output": { "stdout": "hfqfh\n" } }, { "inpu...
{ "tags": [], "title": "Do Geese See God?" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <set>\n#include <map>\n#include <cassert>\n#include <numeric>\n#include <string>\n#include <cstring>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\nusing namespace std;\n\n#define REP(i, n) for(int i=0;i<(int)n;i++)\n#define...
AIZU/p01074
# Unhappy Class Problem Yamano Mifune Gakuen's 1st grade G group is a class where female students carrying misfortune gather. They face various challenges every day with the goal of being happy. In their class, they can take practical happiness courses as part of the test of happiness. From Monday to Friday, there a...
[ { "input": { "stdin": "3 7 3\n0 1 1 1\n0 1 1 2\n1 1 3 4\n1 1 1 1\n1 2 1 2\n2 1 1 3\n2 2 2 1" }, "output": { "stdout": "9" } }, { "input": { "stdin": "5 10 5\n0 1 1 2\n0 2 1 2\n0 1 2 3\n1 2 1 2\n1 4 2 3\n2 1 1 1\n2 1 1 2\n3 3 2 3\n4 1 1 2\n4 2 1 2" }, "output": { ...
{ "tags": [], "title": "Unhappy Class" }
{ "cpp": "#include <algorithm>\n#include <cfloat>\n#include <climits>\n#include <cmath>\n#include <complex>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <functional>\n#include <iostream>\n#include <map>\n#include <memory>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <stack>\...
AIZU/p01208
# Turn Left Taro got a driver’s license with a great effort in his campus days, but unfortunately there had been no opportunities for him to drive. He ended up obtaining a gold license. One day, he and his friends made a plan to go on a trip to Kyoto with you. At the end of their meeting, they agreed to go around by ...
[ { "input": { "stdin": "2 1\nKarasumaKitaoji 0 6150\nKarasumaNanajo 0 0\nKarasumaNanajo KarasumaKitaoji\nKarasumaKitaoji KarasumaNanajo\n3 2\nKujoOmiya 0 0\nKujoAburanokoji 400 0\nOmiyaNanajo 0 1150\nKujoOmiya KujoAburanokoji\nKujoOmiya OmiyaNanajo\nKujoAburanokoji OmiyaNanajo\n10 12\nKarasumaGojo 745 0\nH...
{ "tags": [], "title": "Turn Left" }
{ "cpp": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 200...