id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
AIZU/p00127
# Pocket Pager Input One day, Taro received a strange email with only the number "519345213244" in the text. The email was from my cousin, who was 10 years older than me, so when I called and asked, "Oh, I sent it with a pocket bell because I was in a hurry. It's convenient. Nice to meet you!" I got it. You know this ...
[ { "input": { "stdin": "341143514535\n314\n143565553551655311343411652235654535651124615163\n551544654451431564\n4\n3411\n6363636363\n153414" }, "output": { "stdout": "naruto\nNA\ndo you wanna go to aizu?\nyes sure!\nNA\nna\n?????\nend" } }, { "input": { "stdin": "3411435145...
{ "tags": [], "title": "Pocket Pager Input" }
{ "cpp": "#include <iostream>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\nusing namespace std;\n\n#define REP(i,n) for(int i=0;i<(int)n;++i)\n#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)\n#define ALL(c) (c).begin...
AIZU/p00260
# Cats Going Straight There was a large mansion surrounded by high walls. The owner of the mansion loved cats so much that he always prepared delicious food for the occasional cats. The hungry cats jumped over the high walls and rushed straight to the rice that was everywhere in the mansion. One day the husband found...
[ { "input": { "stdin": "8\n0 0\n3 2\n6 2\n8 6\n6 5\n7 7\n0 4\n3 4\n8\n0 0\n5 3\n5 2\n4 1\n6 1\n8 6\n6 4\n2 4\n0" }, "output": { "stdout": "1\n2" } }, { "input": { "stdin": "8\n-1 0\n6 2\n6 2\n8 6\n6 5\n7 7\n0 4\n3 2\n8\n0 0\n6 3\n5 2\n4 1\n6 1\n13 6\n6 4\n3 4\n0" }, ...
{ "tags": [], "title": "Cats Going Straight" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef complex<double> P;\n\nstruct data{\n double d;\n P p;\n bool operator < (const data &a)const{\n return d < a.d;\n }\n};\n\ndouble eps=1e-8;\ndouble PI=acos(-1);\n\nbool eq(double a,double b){return (b-a<eps&&a-b<eps);}\nbool eq(P a,P b){return (a...
AIZU/p00447
# Searching Constellation problem You are looking for a constellation in a picture of the starry sky. The photo always contains exactly one figure with the same shape, orientation, and size as the constellation you are looking for. However, there is a possibility that extra stars are shown in the photograph other tha...
[ { "input": { "stdin": "5\n8 5\n6 4\n4 3\n7 10\n0 10\n10\n10 5\n2 7\n9 7\n8 10\n10 2\n1 2\n8 1\n6 7\n6 0\n0 9\n5\n904207 809784\n845370 244806\n499091 59863\n638406 182509\n435076 362268\n10\n757559 866424\n114810 239537\n519926 989458\n461089 424480\n674361 448440\n81851 150384\n459107 795405\n299682 6700...
{ "tags": [], "title": "Searching Constellation" }
{ "cpp": "#include<iostream>\n#include<string>\n#include<algorithm>\n#include<math.h>\n#include<queue>\n#include<set>\n#include<map>\n\nusing namespace std;\n\n\nint M, N;\nint main() {\n\twhile (cin >> M, M)\n\t{\n\t\tint sx[200], sy[200], skyx[1000], skyy[1000];\n\t\tset<pair<int, int>> ex;\n\n\t\tfor (int i = 0; i...
AIZU/p00638
# Old Bridges Long long ago, there was a thief. Looking for treasures, he was running about all over the world. One day, he heard a rumor that there were islands that had large amount of treasures, so he decided to head for there. Finally he found n islands that had treasures and one island that had nothing. Most of ...
[ { "input": { "stdin": "3\n2 3\n3 6\n1 2\n3\n2 3\n3 5\n1 2\n0" }, "output": { "stdout": "Yes\nNo" } }, { "input": { "stdin": "3\n2 5\n0 16\n2 2\n3\n2 3\n3 0\n2 3\n0" }, "output": { "stdout": "Yes\nNo\n" } }, { "input": { "stdin": "3\n2 5\n0 8\...
{ "tags": [], "title": "Old Bridges" }
{ "cpp": "#include <iostream>\n#include <utility>\n#include <algorithm>\nusing namespace std;\n\ntypedef pair<int, int> pii;\n\nint main()\n{\n int n, m, k;\n while(1){\n m = 0;\n k = 0;\n cin >> n;\n if(n == 0){\n break;\n }else{\n int num[n][2];\n pii p[n];\n for(int i = 0; i < ...
AIZU/p00781
# Lattice Practices Once upon a time, there was a king who loved beautiful costumes very much. The king had a special cocoon bed to make excellent cloth of silk. The cocoon bed had 16 small square rooms, forming a 4 × 4 lattice, for 16 silkworms. The cocoon bed can be depicted as follows: <image> The cocoon bed can ...
[ { "input": { "stdin": "10000 01000 00100 11000 01100 11111 01110 11100 10110 11110\n10101 01000 00000 11001 01100 11101 01110 11100 10110 11010\nEND" }, "output": { "stdout": "40\n6" } }, { "input": { "stdin": "00000 00000 00100 11000 00100 10111 01111 11100 00111 01111\n11...
{ "tags": [], "title": "Lattice Practices" }
{ "cpp": "#include <iostream>\n#include <sstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#include <map>\n#include <bitset>\n#include <numeric>\n#include <climits>\n#include <cfloat>\nusing ...
AIZU/p00914
# Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesn't matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of set elements and their sum to be k and s, respective...
[ { "input": { "stdin": "9 3 23\n9 3 22\n10 3 28\n16 10 107\n20 8 102\n20 10 105\n20 10 155\n3 4 3\n4 2 11\n0 0 0" }, "output": { "stdout": "1\n2\n0\n20\n1542\n5448\n1\n0\n0" } }, { "input": { "stdin": "9 3 23\n9 3 22\n10 6 28\n16 10 107\n20 8 102\n17 10 105\n20 10 155\n3 5 3...
{ "tags": [], "title": "Equal Sum Sets" }
{ "cpp": "#include<iostream>\n#include<string>\n#include<math.h>\nusing namespace std;\nint main(){\n\tint n,k,s;\n\twhile(true){\n\t cin>>n>>k>>s;\n\t\tif(n==0&&k==0&&s==0)\n\t\t\tbreak;\n\t\tint count=0;\n\t\tif(n==0&&k==0&&s==0)\n\t\t\tbreak;\n\t\tfor(int i=0;i<pow(2.0,n+0.0);i++){\n\t\t\tint scount=0,kcount=0;...
AIZU/p01046
# Yu-kun Likes a lot of Money Background The kindergarten attached to the University of Aizu is a kindergarten where children who love programming gather. Yu, one of the kindergarten children, loves money as much as programming. Yu-kun visited the island where treasures sleep to make money today. Yu-kun has obtained ...
[ { "input": { "stdin": "3 3 1 10\n@#b\n.#.\n.#.\nb 100" }, "output": { "stdout": "0" } }, { "input": { "stdin": "3 3 1 20\n@*C\n..*\n...\nC 10" }, "output": { "stdout": "0" } }, { "input": { "stdin": "3 3 1 10\n@0.\n...\n...\n0 100" }, ...
{ "tags": [], "title": "Yu-kun Likes a lot of Money" }
{ "cpp": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i;\n\t\t\trank[i]=0;\n\t\t}\n\t}\n\tint find(int x){\n\t\...
AIZU/p01179
# Cousin's Aunt Sarah is a girl who likes reading books. One day, she wondered about the relationship of a family in a mystery novel. The story said, * B is A’s father’s brother’s son, and * C is B’s aunt. Then she asked herself, “So how many degrees of kinship are there between A and C?” There are two possible ...
[ { "input": { "stdin": "7\nC is A’s father’s brother’s son’s aunt\nC is A’s mother’s brother’s son’s aunt\nC is A’s son’s mother’s mother’s son\nC is A’s aunt’s niece’s aunt’s niece\nC is A’s father’s son’s brother\nC is A’s son’s son’s mother\nC is A" }, "output": { "stdout": "5 3\n5 1\n2 2\...
{ "tags": [], "title": "Cousin's Aunt" }
{ "cpp": "#include <algorithm>\n#include <cassert>\n#include <climits>\n#include <iostream>\n#include <string>\n#include <vector>\nusing namespace std;\nusing namespace std::placeholders;\n\nenum Relation {\n FATHER, MOTHER, SON, DAUGHTER, HUSBAND, WIFE, BROTHER, SISTER, GRANDFATHER,\n GRANDMOTHER, GRANDSON, GRANDD...
AIZU/p01316
# Differential Pulse Code Modulation Differential pulse code modulation is one of the compression methods mainly used when compressing audio signals. The audio signal is treated as an integer sequence (impulse sequence) on the computer. The integer sequence is a sample of the input signal at regular time intervals an...
[ { "input": { "stdin": "2 7\n4\n2\n1\n0\n-1\n-2\n-4\n131\n137\n2 7\n4\n2\n1\n0\n-1\n-2\n-4\n131\n123\n10 7\n-4\n-2\n-1\n0\n1\n2\n4\n132\n134\n135\n134\n132\n128\n124\n122\n121\n122\n5 1\n255\n0\n0\n0\n0\n0\n4 1\n0\n255\n0\n255\n0\n0 0" }, "output": { "stdout": "2\n8\n0\n325125\n65026" } ...
{ "tags": [], "title": "Differential Pulse Code Modulation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX_N=2e4+10,MAX_M=20,MAX=256,INF=1e9;\n\nint N,M,nxt,ans;\nint C[MAX_M],x[MAX_N];\nint dp[MAX_N][MAX];\n\nvoid solve(){\n for (int i=0;i<MAX_N;++i)\n for (int j=0;j<MAX;++j)\n dp[i][j]=INF;\n dp[0][128]=0;\n for (int i=0;i...
AIZU/p01484
# Icy Composer Time Limit: 8 sec / Memory Limit: 64 MB Example Input 5 3 2 aaaaa aaa aab Output 1 6
[ { "input": { "stdin": "5 3 2\naaaaa\naaa\naab" }, "output": { "stdout": "1 6" } }, { "input": { "stdin": "6 3 2\nabaac\naab\nbaa" }, "output": { "stdout": "2 6\n" } }, { "input": { "stdin": "14 0 2\naaaaa\nabb\naab" }, "output": { ...
{ "tags": [], "title": "Icy Composer" }
{ "cpp": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#include <queue>\n#include <string>\n#include <map>\n#include <set>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long...
AIZU/p01646
# Dictionary Problem Statement We found a dictionary of the Ancient Civilization Mayo (ACM) during excavation of the ruins. After analysis of the dictionary, we revealed they used a language that had not more than 26 letters. So one of us mapped each letter to a different English alphabet and typed all the words in t...
[ { "input": { "stdin": "4\ncba\ncab\nb\na\n3\nbca\nab\na\n5\nabc\nacb\nb\nc\nc\n5\nabc\nacb\nc\nb\nb\n0" }, "output": { "stdout": "yes\nno\nyes\nno" } }, { "input": { "stdin": "4\ncbb\ncab\nb\na\n3\nbca\nab\na\n5\nabc\nacb\nb\nc\nc\n0\ncba\nabc\nc\nb\nc\n1" }, "outpu...
{ "tags": [], "title": "Dictionary" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint N;\nvector<vector<int>> v(26);\nint vis[26] = {};\n\nbool dfs(int n){\n vis[n] = -1;\n bool ok = true;\n for(auto x:v[n]){\n if(vis[x]==0) ok = ok && dfs(x);\n else if(vis[x]==-1) return false;\n ...
AIZU/p01797
# Kimagure Cleaner Example Input 2 -3 4 L 2 5 ? 3 5 Output 2 L 4 L 3
[ { "input": { "stdin": "2 -3 4\nL 2 5\n? 3 5" }, "output": { "stdout": "2\nL 4\nL 3" } } ]
{ "tags": [], "title": "Kimagure Cleaner" }
{ "cpp": "//\n// Problem: Kimagagure Cleaner\n// Solution by: MORI Shingo\n// O(n*2^(n3/8))\n//\n// implement1 & debug1 214min\n// implement2 86min\n// debug2 122min\n// \n#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n#in...
AIZU/p01931
# Check answers problem AOR Ika is studying to pass the test. AOR Ika-chan solved the $ N $ question. After that, round the solved problem according to the following procedure. 1. Check the correctness of the answer. 2. If the answer is correct, write a circle mark, and if it is incorrect, write a cross mark on the...
[ { "input": { "stdin": "3\noxx" }, "output": { "stdout": "2" } }, { "input": { "stdin": "3\nzny" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "3\nymz" }, "output": { "stdout": "3\n" } }, { "input": { ...
{ "tags": [], "title": "Check answers" }
{ "cpp": "#include<bits/stdc++.h>\n#define vi vector<int>\n#define vvi vector<vector<int> >\n#define vl vector<ll>\n#define vvl vector<vector<ll>>\n#define vb vector<bool>\n#define vc vector<char>\n#define vs vector<string>\nusing ll = long long;\nusing ld =long double;\n//#define int ll\n#define INF 1e9\n#define EPS...
AIZU/p02069
# Universal and Existential Quantifiers Problem Statement You are given a list of $N$ intervals. The $i$-th interval is $[l_i, r_i)$, which denotes a range of numbers greater than or equal to $l_i$ and strictly less than $r_i$. In this task, you consider the following two numbers: * The minimum integer $x$ such that...
[ { "input": { "stdin": "" }, "output": { "stdout": "" } } ]
{ "tags": [], "title": "Universal and Existential Quantifiers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) repi(i, 0, n)\n#define repi(i, a, b) for(int i=(int)(a);i<(int)(b);++i)\n#define ll long long\n\nconstexpr int SIZE = 1000000;\nvoid solve(){\n int N;\n ll L;\n cin >> N >> L;\n pair<ll, ll> interval[N];\n vector<ll> compressTe...
AIZU/p02211
# Apple Adventure Apple adventure square1001 and E869120 got lost in the grid world of $ H $ rows and $ W $ rows! Said the god of this world. "When you collect $ K $ apples and they meet, you'll be back in the original world." Upon hearing this word, square1001 decided to collect more than $ K $ of apples and head...
[ { "input": { "stdin": "5 5 2\ns..#a\n.#...\na#e.#\n...#a\n.#..." }, "output": { "stdout": "14" } }, { "input": { "stdin": "5 5 2\ns..#a\n...#.\na#e.#\n...#a\n.#..." }, "output": { "stdout": "14\n" } }, { "input": { "stdin": "5 5 1\nc#..s\n.#/...
{ "tags": [], "title": "Apple Adventure" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<((x))<<endl\ntemplate<class T,class U> ostream& operator<<(ost...
AIZU/p02365
# Minimum-Cost Arborescence Find the sum of the weights of edges of the Minimum-Cost Arborescence with the root r for a given weighted directed graph G = (V, E). Constraints * 1 ≤ |V| ≤ 100 * 0 ≤ |E| ≤ 1,000 * 0 ≤ wi ≤ 10,000 * G has arborescence(s) with the root r Input |V| |E| r s0 t0 w0 s1 t1 w1 : s|E|-1 t|E|-...
[ { "input": { "stdin": "4 6 0\n0 1 3\n0 2 2\n2 0 1\n2 3 1\n3 0 1\n3 1 5" }, "output": { "stdout": "6" } }, { "input": { "stdin": "6 10 0\n0 2 7\n0 1 1\n0 3 5\n1 4 9\n2 1 6\n1 3 2\n3 4 3\n4 2 2\n2 5 8\n3 5 3" }, "output": { "stdout": "11" } }, { "inp...
{ "tags": [], "title": "Minimum-Cost Arborescence" }
{ "cpp": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << \"(\" << o.first << \",\" << o.second << \")\...
CodeChef/acdemy
Sherlock Holmes has decided to start a new academy to some of the young lads. He has conducted several tests and finally selected N equally brilliant students.Now he don't know whether to train all the N students or not. Now since Holmes was in a confusion, Watson came up with an idea. He wanted to test the obedience o...
[ { "input": { "stdin": "2\n5\n2 3 5 1 2\n3\n1 2 3" }, "output": { "stdout": "3\n3" } } ]
{ "tags": [], "title": "acdemy" }
{ "cpp": null, "java": null, "python": "# your code goes here\nfrom sys import stdin, stdout\nt = int(stdin.readline())\nwhile t:\n\tt -= 1\n\tn = int(stdin.readline())\n\ta = map(int, stdin.readline().strip().split(' '))\n\tif n <= 2:\n\t\tstdout.write(str(n)+\"\\n\")\n\telse:\n\t\tm = 2\n\t\tc = 2\n\t\ti = 2\n\...
CodeChef/chefrp
Rupsa recently started to intern under Chef. He gave her N type of ingredients of varying quantity A1, A2, ..., AN respectively to store it. But as she is lazy to arrange them she puts them all in a storage box. Chef comes up with a new recipe and decides to prepare it. He asks Rupsa to get two units of each type ingr...
[ { "input": { "stdin": "2\n2\n2 2\n1\n6" }, "output": { "stdout": "4\n2\n" } }, { "input": { "stdin": "2\n3\n4 4\n0\n6" }, "output": { "stdout": "6\n2\n" } }, { "input": { "stdin": "2\n-1\n5 13\n0\n6" }, "output": { "stdout": "15...
{ "tags": [], "title": "chefrp" }
{ "cpp": null, "java": null, "python": "def ingr(l):\n s=0\n minfreq=l[0]\n for i in range(0,len(l),1):\n if l[i]<2:\n return -1\n for i in range(0,len(l),1):\n s+=l[i]\n minfreq=min(minfreq,l[i])\n return (s-minfreq+2)\nt=int(input())\nwhile t:\n t=t-1\n n=int...
CodeChef/donuts
There is new delicious item in Chef's menu - a doughnut chain. Doughnuts connected successively in line forming a chain. Chain of 3 doughnuts Chef has received an urgent order for making a chain of N doughnuts. He noticed that there are exactly N cooked doughnuts in the kitchen, some of which are already connected i...
[ { "input": { "stdin": "2\n11 3\n4 3 4\n6 3\n3 2 1" }, "output": { "stdout": "2\n1\n" } }, { "input": { "stdin": "2\n22 6\n2 3 4\n6 1\n2 2 1" }, "output": { "stdout": "4\n0\n" } }, { "input": { "stdin": "2\n22 6\n2 3 7\n6 3\n2 2 0" }, ...
{ "tags": [], "title": "donuts" }
{ "cpp": null, "java": null, "python": "for t in xrange(input()):\n n, m = map(int, raw_input().strip().split())\n arr = map(int, raw_input().strip().split())\n arr.sort()\n cuts = 0\n for i in xrange(m):\n k = m - i - cuts - 1\n if k <= arr[i]:\n ans = cuts + k\n ...
CodeChef/ism1
Hackers! Hackers! Everywhere! Some days back your email ID was hacked. Some one read the personal messages and Love Letters you sent to your girl friend. That's a terrible thing, well, you know how are the boys at ISM. So, you have decided that from now onwards you will write Love Letters to your girlfriend in a differ...
[ { "input": { "stdin": "3\ncan you meet me outside ccd today\na\nthis year hard kaur is coming to entertain us" }, "output": { "stdout": "nac uoy teem em edistuo dcc yadot\na\nsiht raey drah ruak si gnimoc ot niatretne su" } }, { "input": { "stdin": "3\ncan pyu meet me ouuti...
{ "tags": [], "title": "ism1" }
{ "cpp": null, "java": null, "python": "t=input()\nwhile t>0:\n\tA=map(str,raw_input().split())\n\tB=[]\n\tfor i in xrange(len(A)):\n\t\tc=A[i]\n\t\tB.append(c[::-1])\n\tprint \" \".join(B)\n\tt=t-1" }
CodeChef/nopc10
Computation of the date either previous or forthcoming dates is quiet easy. But it is quiet difficult to calculate the day from a particular given date. You are required to find a day from a particular date given to you. Input It consists of a single line entry consisting of date in format dd mm yyyy. i.e. the input...
[ { "input": { "stdin": "14 3 2012" }, "output": { "stdout": "Wednesday" } }, { "input": { "stdin": "10 1 1946" }, "output": { "stdout": "Wednesday\n" } }, { "input": { "stdin": "3 -1 46" }, "output": { "stdout": "Wednesday\n" ...
{ "tags": [], "title": "nopc10" }
{ "cpp": null, "java": null, "python": "import datetime\ndt='21/03/2012'\nday, month, year = (int(x) for x in dt.split('/')) \nans=datetime.date(year,month,day)\nprint (ans.strftime(\"%A\"))" }
CodeChef/seg003
Problem description. The problem statement is simple , you are given an array and you have to perform two types of operation on it. Type 1 : update the value of array at the given index. Type 2 : find the maximum sum you can obtained in the given range ( L , R ) by taking any two index i and j , such that ( L <=...
[ { "input": { "stdin": "5 3 \n1 2 3 4 5\n2 1 5\n1 2 5\n2 1 5" }, "output": { "stdout": "9 \n10" } } ]
{ "tags": [], "title": "seg003" }
{ "cpp": null, "java": null, "python": "# your code goes here\nfrom math import ceil, log\nfrom sys import stdin, stdout\nst = []\ndef getMid(s, e):\n\treturn s + (e-s) / 2\n\ndef construct(st, arr, ss, se, si, type):\n\tif se == ss:\n\t\tif se%2 == type:\n\t\t\tst[si] = arr[ss]\n\t\telse:\n\t\t\tst[si] = 0\n\t\t...
Codeforces/1003/D
# Coins and Queries Polycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d). Polycarp wants to know answers on q queries. The j-th query is described as integer number b_j. The answer to the query is ...
[ { "input": { "stdin": "5 4\n2 4 8 2 4\n8\n5\n14\n10\n" }, "output": { "stdout": "1\n-1\n3\n2\n" } }, { "input": { "stdin": "1 10\n8\n1\n2\n3\n4\n5\n6\n7\n8\n9\n16\n" }, "output": { "stdout": "-1\n-1\n-1\n-1\n-1\n-1\n-1\n1\n-1\n-1\n" } }, { "input":...
{ "tags": [ "greedy" ], "title": "Coins and Queries" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n map<long long, int> two;\n map<int, long long> m_two;\n long long t = 1;\n for (int i = 0; i < 32; i++) {\n two[t] = i;\n m_two[i] = t;\n t *= 2;\n }\n int ar[33];\n memset(ar, 0, sizeof(ar));\n int n, q;\n cin >> n >> q;\n wh...
Codeforces/1027/E
# Inverse Coloring You are given a square board, consisting of n rows and n columns. Each tile in it should be colored either white or black. Let's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well. ...
[ { "input": { "stdin": "2 3\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "49 1808\n" }, "output": { "stdout": "359087121\n" } }, { "inp...
{ "tags": [ "combinatorics", "dp", "math" ], "title": "Inverse Coloring" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 503, mod = 998244353;\nint n, m, ans;\nint f[2][maxn][maxn], s[maxn];\nint inv(int a) {\n int b = mod - 2, ans = 1;\n while (b) {\n if (b & 1) ans = 1ll * ans * a % mod;\n a = 1ll * a * a % mod;\n b >>= 1;\n }\n return ans;\n}\nin...
Codeforces/1046/D
# Interstellar battle In the intergalactic empire Bubbledom there are N planets, of which some pairs are directly connected by two-way wormholes. There are N-1 wormholes. The wormholes are of extreme religious importance in Bubbledom, a set of planets in Bubbledom consider themselves one intergalactic kingdom if and o...
[ { "input": { "stdin": "5\n0.50 0.29 0.49 0.95 0.83\n2 3\n0 3\n3 4\n2 1\n3\n4 0.66\n1 0.69\n0 0.36\n" }, "output": { "stdout": "1.6804000000\n1.4844000000\n1.6174000000\n" } }, { "input": { "stdin": "26\n0.98 0.64 0.06 0.90 0.01 0.73 0.21 0.98 0.65 1.00 0.87 0.85 0.01 0.06 0...
{ "tags": [ "math", "probabilities", "trees" ], "title": "Interstellar battle" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 2;\nvector<int> adj[N];\nint par[N];\ndouble ar[N], f[N];\nvoid dfs(int x, int p) {\n for (int i = 0; i < adj[x].size(); i++) {\n if (adj[x][i] != p) {\n f[x] += ar[adj[x][i]];\n par[adj[x][i]] = x;\n dfs(adj[x][i], x);\n...
Codeforces/1070/C
# Cloud Computing Buber is a Berland technology company that specializes in waste of investor's money. Recently Buber decided to transfer its infrastructure to a cloud. The company decided to rent CPU cores in the cloud for n consecutive days, which are numbered from 1 to n. Buber requires k CPU cores each day. The c...
[ { "input": { "stdin": "7 13 5\n2 3 10 7\n3 5 10 10\n1 2 10 6\n4 5 10 9\n3 4 10 8\n" }, "output": { "stdout": "462\n" } }, { "input": { "stdin": "4 100 3\n3 3 2 5\n1 1 3 2\n2 4 4 4\n" }, "output": { "stdout": "64\n" } }, { "input": { "stdin": ...
{ "tags": [ "data structures", "greedy" ], "title": "Cloud Computing" }
{ "cpp": "#include <bits/stdc++.h>\ninline int read() {\n char ch = getchar();\n int ret = 0, f = 1;\n while (ch < '0' || ch > '9') {\n if (ch == '-') f = -1;\n ch = getchar();\n }\n for (; ch >= '0' && ch <= '9'; ch = getchar()) ret = ret * 10 + ch - '0';\n return ret * f;\n}\nusing namespace std;\nconst...
Codeforces/1091/G
# New Year and the Factorisation Collaboration Integer factorisation is hard. The RSA Factoring Challenge offered $100 000 for factoring RSA-1024, a 1024-bit long product of two prime numbers. To this date, nobody was able to claim the prize. We want you to factorise a 1024-bit number. Since your programming language...
[ { "input": { "stdin": "21\n\n7\n\n17\n\n15\n\n17\n\n11\n\n-1\n\n15\n\n" }, "output": { "stdout": "+ 12 16\n\n- 6 10\n\n* 8 15\n\n/ 5 4\n\nsqrt 16\n\nsqrt 5\n\n^ 6 12\n\n! 2 3 7" } }, { "input": { "stdin": "3\n23096722104754207127290818652586833139892168247130866425398877835...
{ "tags": [ "interactive", "math", "number theory" ], "title": "New Year and the Factorisation Collaboration" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\ntemplate <typename T>\nvector<T> &operator--(vector<T> &v) {\n for (auto &i : v) --i;\n return v;\n}\ntemplate <typename T>\nvector<T> &operator++(vector<T> &v) {\n for (auto &i : v) ++i;\n return v;\n}\ntemp...
Codeforces/1110/E
# Magic Stones Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 ≤ i ≤ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen ...
[ { "input": { "stdin": "4\n7 2 4 12\n7 15 10 12\n" }, "output": { "stdout": "Yes\n" } }, { "input": { "stdin": "3\n4 4 4\n1 2 3\n" }, "output": { "stdout": "No\n" } }, { "input": { "stdin": "10\n62159435 282618243 791521863 214307200 976959598...
{ "tags": [ "constructive algorithms", "math", "sortings" ], "title": "Magic Stones" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n;\nvector<long long> c, t, sc, st;\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cin >> n;\n for (int i = 0; i < n; i++) {\n long long a;\n cin >> a;\n c.push_back(a);\n }\n for (int i = 0; i < n; i++) {\n long lo...
Codeforces/1140/B
# Good String You have a string s of length n consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you ...
[ { "input": { "stdin": "3\n2\n&lt;&gt;\n3\n&gt;&lt;&lt;\n1\n&gt;\n" }, "output": { "stdout": "0\n0\n0\n" } }, { "input": { "stdin": "1\n9\n>>>>>>>><\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "13\n1\n>\n1\n>\n1\n>\n1\n>\n1\n>...
{ "tags": [ "implementation", "strings" ], "title": "Good String" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nstring s;\nint t;\nint main() {\n cin >> t;\n for (int i = 1; i <= t; i++) {\n cin >> n;\n cin >> s;\n bool kt = false;\n int mi = 200, cr = 0, r = 0, l = 0;\n for (int j = 0; j < n; j++) {\n if (s[j] == '<' && cr == 0)\n l...
Codeforces/1158/F
# Density of subarrays Let c be some positive integer. Let's call an array a_1, a_2, …, a_n of positive integers c-array, if for all i condition 1 ≤ a_i ≤ c is satisfied. Let's call c-array b_1, b_2, …, b_k a subarray of c-array a_1, a_2, …, a_n, if there exists such set of k indices 1 ≤ i_1 < i_2 < … < i_k ≤ n that b...
[ { "input": { "stdin": "5 2\n1 2 1 2 1\n" }, "output": { "stdout": "10 17 4 0 0 0 " } }, { "input": { "stdin": "3 3\n1 2 3\n" }, "output": { "stdout": "6 1 0 0 " } }, { "input": { "stdin": "4 1\n1 1 1 1\n" }, "output": { "stdout"...
{ "tags": [ "dp", "math" ], "title": "Density of subarrays" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int md = 998244353;\ninline void add(int &x, int y) {\n x += y;\n if (x >= md) {\n x -= md;\n }\n}\ninline void sub(int &x, int y) {\n x -= y;\n if (x < 0) {\n x += md;\n }\n}\ninline int mul(int x, int y) { return (int)((long long)x * y % md...
Codeforces/1180/E
# Serge and Dining Room Serge came to the school dining room and discovered that there is a big queue here. There are m pupils in the queue. He's not sure now if he wants to wait until the queue will clear, so he wants to know which dish he will receive if he does. As Serge is very tired, he asks you to compute it ins...
[ { "input": { "stdin": "1 1\n1\n1\n1\n2 1 100\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "4 6\n1 8 2 4\n3 3 6 1 5 2\n3\n1 1 1\n2 5 10\n1 1 6\n" }, "output": { "stdout": "8\n-1\n4\n" } }, { "input": { "stdin": "1 1\n1\n1\n1\n...
{ "tags": [ "binary search", "data structures", "graph matchings", "greedy", "implementation", "math", "trees" ], "title": "Serge and Dining Room" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, q, a[1000010], b[1000010];\nstruct se {\n struct node {\n int sum;\n int maxn;\n node() { sum = maxn = 0; }\n node(int s, int m) : sum(s), maxn(m) {}\n node operator+(const node &other) const {\n node res = node();\n res.sum...
Codeforces/1199/E
# Matching vs Independent Set You are given a graph with 3 ⋅ n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with ...
[ { "input": { "stdin": "4\n1 2\n1 3\n1 2\n1 2\n1 3\n1 2\n2 5\n1 2\n3 1\n1 4\n5 1\n1 6\n2 15\n1 2\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n4 5\n4 6\n5 6\n" }, "output": { "stdout": "Matching\n1 \nMatching\n1 \nIndSet\n3 4 \nMatching\n1 10 \n\n" } }, { "input": { ...
{ "tags": [ "constructive algorithms", "graphs", "greedy", "sortings" ], "title": "Matching vs Independent Set" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n long long t;\n cin >> t;\n for (; t > 0; t--) {\n long long n, m;\n cin >> n >> m;\n n *= 3;\n vector<bool> a((size_t)n, false);\n vector<long long> b;\n for (long lo...
Codeforces/1216/D
# Swords There were n types of swords in the theater basement which had been used during the plays. Moreover there were exactly x swords of each type. y people have broken into the theater basement and each of them has taken exactly z swords of some single type. Note that different people might have taken different ty...
[ { "input": { "stdin": "2\n2 9\n" }, "output": { "stdout": "1 7\n" } }, { "input": { "stdin": "3\n3 12 6\n" }, "output": { "stdout": "5 3\n" } }, { "input": { "stdin": "7\n2 1000000000 4 6 8 4 2\n" }, "output": { "stdout": "29999...
{ "tags": [ "math" ], "title": "Swords" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid _print(long long t) { cerr << t; }\nvoid _print(int t) { cerr << t; }\nvoid _print(string t) { cerr << t; }\nvoid _print(char t) { cerr << t; }\nvoid _print(long double t) { cerr << t; }\nvoid _print(double t) { cerr << t; }\nvoid _print(unsigned long lon...
Codeforces/1239/E
# Turtle Kolya has a turtle and a field of size 2 × n. The field rows are numbered from 1 to 2 from top to bottom, while the columns are numbered from 1 to n from left to right. Suppose in each cell of the field there is a lettuce leaf. The energy value of lettuce leaf in row i and column j is equal to a_{i,j}. The t...
[ { "input": { "stdin": "3\n1 0 1\n0 0 0\n" }, "output": { "stdout": "0 0 1 \n1 0 0 " } }, { "input": { "stdin": "2\n1 4\n2 3\n" }, "output": { "stdout": "1 3\n4 2\n" } }, { "input": { "stdin": "3\n0 0 0\n0 0 0\n" }, "output": { "...
{ "tags": [ "dp", "implementation" ], "title": "Turtle" }
{ "cpp": "#include <bits/stdc++.h>\nconst int N = 26, M = 50006;\nint n, a[3][N], b[2 * N], f[N][M * N];\nbool chosen[2 * N];\nint main() {\n std::cin >> n;\n for (int i = 1; i <= 2 * n; ++i) {\n std::cin >> b[i];\n }\n std::sort(b + 1, b + 2 * n + 1);\n a[1][1] = b[1], a[2][n] = b[2];\n int sum = 0;\n f[0]...
Codeforces/1257/G
# Divisor Set You are given an integer x represented as a product of n its prime divisors p_1 ⋅ p_2, ⋅ … ⋅ p_n. Let S be the set of all positive integer divisors of x (including 1 and x itself). We call a set of integers D good if (and only if) there is no pair a ∈ D, b ∈ D such that a ≠ b and a divides b. Find a go...
[ { "input": { "stdin": "3\n2999999 43 2999957\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "6\n2 3 2 3 2 2\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "16\n2 2 2 2 2 3 3 3 3 5 5 7 7 11 13 17\n" }, "outp...
{ "tags": [ "divide and conquer", "fft", "greedy", "math", "number theory" ], "title": "Divisor Set" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxl = 20;\nconst int maxn = 1 << maxl;\nconst int mod = 998244353;\nint rev[maxn];\nint qpow(int a, int b) {\n int ans = 1;\n while (b) {\n if (b & 1) ans = 1ll * ans * a % mod;\n a = 1ll * a * a % mod;\n b >>= 1;\n }\n return ans;\n}\n...
Codeforces/1281/B
# Azamon Web Services Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his p...
[ { "input": { "stdin": "3\nAZAMON APPLE\nAZAMON AAAAAAAAAAALIBABA\nAPPLE BANANA\n" }, "output": { "stdout": "AAZMON\n---\nAEPLP\n" } }, { "input": { "stdin": "3\nAZAMON APPLE\nAZAMON AAAAAAAAAAALIBABA\nAPPLE BANANA\n" }, "output": { "stdout": "AAZMON\n---\nAEPL...
{ "tags": [ "greedy" ], "title": "Azamon Web Services" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long check(string a, string b) {\n for (long long i = 0; i < min(a.size(), b.size()); i++) {\n if (a[i] > b[i])\n return 0;\n else if (a[i] < b[i])\n return 1;\n }\n if (a.size() < b.size())\n return 1;\n else\n return 0;\n}\nvoi...
Codeforces/1301/B
# Motarack's Birthday Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array a of n non-negative integers. Dark created that array 1000 years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjace...
[ { "input": { "stdin": "7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5\n" }, "output": { "stdout": "1 11\n5 37\n3 6\n0 0\n0 0\n1 2\n3 4\n" } }, { "input": { "stdin": "7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6...
{ "tags": [ "binary search", "greedy", "ternary search" ], "title": "Motarack's Birthday" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int fa[1022];\nlong long int powe(long long int d, long long int s) {\n if (s == 1)\n return d;\n else if (s == 0)\n return 1;\n else {\n long long int u, v = d;\n for (u = 1; u < s; u++) {\n d = d * v;\n }\n return d;\n }\...
Codeforces/1325/B
# CopyCopyCopyCopyCopy Ehab has an array a of length n. He has just enough free time to make a new array consisting of n copies of the old array, written back-to-back. What will be the length of the new array's longest increasing subsequence? A sequence a is a subsequence of an array b if a can be obtained from b by ...
[ { "input": { "stdin": "2\n3\n3 2 1\n6\n3 1 4 1 5 9\n" }, "output": { "stdout": "3\n5\n" } }, { "input": { "stdin": "1\n5\n1 3 4 5 2\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "1\n3\n1 1 274005660\n" }, "output": { ...
{ "tags": [ "greedy", "implementation" ], "title": "CopyCopyCopyCopyCopy" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD = 1000000007;\nint dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};\nint dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};\nint dx4[] = {0, 0, 1, -1};\nint dy4[] = {1, -1, 0, 0};\nconst int maxx = 100005;\nbool sortinrev(const pair<int, int> &a, const pair<int, int...
Codeforces/1344/A
# Hilbert's Hotel Hilbert's Hotel is a very unusual hotel since the number of rooms is infinite! In fact, there is exactly one room for every integer, including zero and negative integers. Even stranger, the hotel is currently at full capacity, meaning there is exactly one guest in every room. The hotel's manager, Dav...
[ { "input": { "stdin": "6\n1\n14\n2\n1 -1\n4\n5 5 5 1\n3\n3 2 1\n2\n0 1\n5\n-239 -2 -100 -3 -11\n" }, "output": { "stdout": "YES\nYES\nYES\nNO\nNO\nYES\n" } }, { "input": { "stdin": "10\n3\n-15 -33 79\n16\n45 -84 19 85 69 -64 93 -70 0 -53 2 -52 -55 66 33 -60\n2\n14 -2\n4\n-6...
{ "tags": [ "math", "number theory", "sortings" ], "title": "Hilbert's Hotel" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int read() {\n int x = 0, f = 1;\n char c = getchar();\n for (; !isdigit(c); c = getchar())\n if (c == '-') f = -1;\n for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';\n return x * f;\n}\nmap<int, int> mp;\ninline void solve() {\n int N =...
Codeforces/1366/A
# Shovels and Swords Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick. Each tool can be sold for exactly...
[ { "input": { "stdin": "4\n4 4\n1000000000 0\n7 15\n8 7\n" }, "output": { "stdout": "2\n0\n7\n5\n" } }, { "input": { "stdin": "1\n656 656\n" }, "output": { "stdout": "437\n" } }, { "input": { "stdin": "1\n666 666\n" }, "output": { ...
{ "tags": [ "binary search", "greedy", "math" ], "title": "Shovels and Swords" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int t;\n cin >> t;\n while (t--) {\n long long int a, b;\n cin >> a >> b;\n long long int l = min(min(a, b), (a + b) / 3);\n cout << l << endl;\n }\n}\n", "java": "//package learning;\nimport java.util.*;\nimport java.io.*;\nim...
Codeforces/1386/A
# Colors Linda likes to change her hair color from time to time, and would be pleased if her boyfriend Archie would notice the difference between the previous and the new color. Archie always comments on Linda's hair color if and only if he notices a difference — so Linda always knows whether Archie has spotted the di...
[ { "input": { "stdin": "1\n\n7\n\n1\n\n1\n\n0\n\n0\n\n1\n" }, "output": { "stdout": "? 3\n? 6\n? 5\n? 7\n= 3\n" } }, { "input": { "stdin": "46\n63 1\n63 63\n123 1\n123 123\n63 2\n63 62\n123 2\n123 122\n63 3\n63 61\n123 3\n123 121\n63 4\n63 60\n123 4\n123 120\n63 5\n63 59\n12...
{ "tags": [ "*special", "binary search", "constructive algorithms", "interactive" ], "title": "Colors" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing vi = vector<long long>;\nusing vvi = vector<vi>;\nusing vvvi = vector<vvi>;\nusing vvvvi = vector<vvvi>;\nusing vp = vector<pair<long long, long long> >;\nusing vvp = vector<vp>;\nusing vb = vector<bool>;\nusing vvb = vector<vb>;\nconst long long inf = 1...
Codeforces/1408/A
# Circle Coloring You are given three sequences: a_1, a_2, …, a_n; b_1, b_2, …, b_n; c_1, c_2, …, c_n. For each i, a_i ≠ b_i, a_i ≠ c_i, b_i ≠ c_i. Find a sequence p_1, p_2, …, p_n, that satisfy the following conditions: * p_i ∈ \\{a_i, b_i, c_i\} * p_i ≠ p_{(i mod n) + 1}. In other words, for each element, ...
[ { "input": { "stdin": "5\n3\n1 1 1\n2 2 2\n3 3 3\n4\n1 2 1 2\n2 1 2 1\n3 4 3 4\n7\n1 3 3 1 1 1 1\n2 4 4 3 2 2 4\n4 2 2 2 4 4 2\n3\n1 2 1\n2 3 3\n3 1 2\n10\n1 1 1 2 2 2 3 3 3 1\n2 2 2 3 3 3 1 1 1 2\n3 3 3 1 1 1 2 2 2 3\n" }, "output": { "stdout": "1 2 3\n1 2 1 2\n1 3 4 1 2 1 4\n1 2 3\n1 2 1 2...
{ "tags": [ "constructive algorithms" ], "title": "Circle Coloring" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[110], b[110], c[110];\nint main() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) scanf(\"%d\", &a[i]);\n for (int i = 1; i <= n; i++) scanf(\"%d\", &b[i]);\n for (int i = 1; ...
Codeforces/1428/D
# Bouncing Boomerangs To improve the boomerang throwing skills of the animals, Zookeeper has set up an n × n grid with some targets, where each row and each column has at most 2 targets each. The rows are numbered from 1 to n from top to bottom, and the columns are numbered from 1 to n from left to right. For each c...
[ { "input": { "stdin": "1\n0\n" }, "output": { "stdout": "0\n\n" } }, { "input": { "stdin": "6\n2 0 3 0 1 1\n" }, "output": { "stdout": "5\n6 6\n5 5\n4 3\n4 5\n6 1\n" } }, { "input": { "stdin": "6\n3 2 2 2 1 1\n" }, "output": { "...
{ "tags": [ "constructive algorithms", "greedy", "implementation" ], "title": "Bouncing Boomerangs" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"O3\")\nusing namespace std;\ntemplate <class c>\nstruct rge {\n c b, e;\n};\ntemplate <class c>\nrge<c> range(c i, c j) {\n return rge<c>{i, j};\n}\ntemplate <class c>\nauto dud(c* x) -> decltype(cerr << *x, 0);\ntemplate <class c>\nchar dud(...);\nstruct d...
Codeforces/1451/D
# Circle Game Utkarsh is forced to play yet another one of Ashish's games. The game progresses turn by turn and as usual, Ashish moves first. Consider the 2D plane. There is a token which is initially at (0,0). In one move a player must increase either the x coordinate or the y coordinate of the token by exactly k. I...
[ { "input": { "stdin": "5\n2 1\n5 2\n10 3\n25 4\n15441 33\n" }, "output": { "stdout": "\nUtkarsh\nAshish\nUtkarsh\nUtkarsh\nAshish\n" } }, { "input": { "stdin": "5\n3 1\n1 1\n10 3\n22 5\n25479 33\n" }, "output": { "stdout": "Utkarsh\nAshish\nUtkarsh\nUtkarsh\nA...
{ "tags": [ "games", "geometry", "math" ], "title": "Circle Game" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <string>\n#include <set>\n#include <algorithm>\n#include <map>\n\nusing namespace std;\n\nint main() {\n int t;\n cin>>t;\n for (int re=0; re<t; re++) {\n long long d, k;\n cin>>d>>k;\n long long A=0;\n for (long long a...
Codeforces/1475/D
# Cleaning the Phone Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory. Polycarp wants to free at least m units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. H...
[ { "input": { "stdin": "5\n5 7\n5 3 2 1 4\n2 1 1 2 1\n1 3\n2\n1\n5 10\n2 3 2 3 2\n1 2 1 2 1\n4 10\n5 1 3 4\n1 2 1 2\n4 5\n3 2 1 2\n2 1 2 1\n" }, "output": { "stdout": "\n2\n-1\n6\n4\n3\n" } }, { "input": { "stdin": "1\n17 20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 10 10\n1 1 1 1 1 1 ...
{ "tags": [ "binary search", "dp", "sortings", "two pointers" ], "title": "Cleaning the Phone" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define PI 3.14159265\nll mod = 1000000007;\ntemplate<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }\ntemplate<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }\nint main() \n{\n #ifndef ONL...
Codeforces/1500/B
# Two chandeliers Vasya is a CEO of a big construction company. And as any other big boss he has a spacious, richly furnished office with two crystal chandeliers. To stay motivated Vasya needs the color of light at his office to change every day. That's why he ordered both chandeliers that can change its color cyclica...
[ { "input": { "stdin": "3 8 41\n1 3 2\n1 6 4 3 5 7 2 8\n" }, "output": { "stdout": "\n47\n" } }, { "input": { "stdin": "1 2 31\n1\n1 2\n" }, "output": { "stdout": "\n62\n" } }, { "input": { "stdin": "4 2 4\n4 2 3 1\n2 1\n" }, "output":...
{ "tags": [ "binary search", "brute force", "chinese remainder theorem", "math", "number theory" ], "title": "Two chandeliers" }
{ "cpp": "#include<bits/stdc++.h>\n#include<unordered_map>\n#include<unordered_set>\n#define f(i,a,b) for( int i=a;i<=b;++i)\n#define ff(i,a,b) for( int i=a;i>=b;--i)\n#define debug(x) cerr << #x << \" : \" << x << \" \" << endl\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef lo...
Codeforces/1525/D
# Armchairs There are n armchairs, numbered from 1 to n from left to right. Some armchairs are occupied by people (at most one person per armchair), others are not. The number of occupied armchairs is not greater than n/2. For some reason, you would like to tell people to move from their armchairs to some other ones....
[ { "input": { "stdin": "6\n1 1 1 0 0 0\n" }, "output": { "stdout": "\n9\n" } }, { "input": { "stdin": "5\n0 0 0 0 0\n" }, "output": { "stdout": "\n0\n" } }, { "input": { "stdin": "7\n1 0 0 1 0 0 1\n" }, "output": { "stdout": "\n3...
{ "tags": [ "dp", "flows", "graph matchings", "greedy" ], "title": "Armchairs" }
{ "cpp": "#include<iostream>\n#include<algorithm>\n#include<cmath>\n#include<cstdio>\n#include<string>\n#include<cstring>\n#include<queue>\n#include<vector>\n#include<stack>\n#include<map>\n#define ll long long\n\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int maxn = 1e6+5;\n\nint a[maxn],y[maxn],my[max...
Codeforces/157/A
# Game Outcome Sherlock Holmes and Dr. Watson played some game on a checkered board n × n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to ...
[ { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "2\n1 2\n3 4\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "4\n5 7 8 4\n9 5 3 2\n1 6 6 4\n9 5 7 3\n" }, "output": { "stdout"...
{ "tags": [ "brute force" ], "title": "Game Outcome" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint gcd(int p, int q) { return q == 0 ? p : gcd(q, p % q); }\nint main() {\n int n, num[35][35], sumh[35], suml[35];\n cin >> n;\n memset(sumh, 0, sizeof(sumh));\n memset(suml, 0, sizeof(suml));\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n...
Codeforces/178/A1
# Educational Game The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some p...
[ { "input": { "stdin": "8\n1 2 3 4 5 6 7 8\n" }, "output": { "stdout": "1\n3\n6\n10\n16\n24\n40\n" } }, { "input": { "stdin": "4\n1 0 1 2\n" }, "output": { "stdout": "1\n1\n3\n" } }, { "input": { "stdin": "80\n72 66 82 46 44 22 63 92 71 65 5 3...
{ "tags": [ "greedy" ], "title": "Educational Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint *p;\nint n;\nint main() {\n cin >> n;\n p = new int[n];\n for (int i = 0; i < n; i++) cin >> p[i];\n int ans = 0;\n for (int i = 0; i < n - 1; i++) {\n while (p[i] > 0) {\n int t = log2(n);\n while (i + (2 << t) >= n && t) t--;\n p[i...
Codeforces/19/B
# Checkout Assistant Bob came to a cash & carry store, put n items into his trolley, and went to the checkout counter to pay. Each item is described by its price ci and time ti in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other it...
[ { "input": { "stdin": "4\n2 10\n0 20\n1 5\n1 3\n" }, "output": { "stdout": "8" } }, { "input": { "stdin": "3\n0 1\n0 10\n0 100\n" }, "output": { "stdout": "111" } }, { "input": { "stdin": "5\n0 968804136\n0 736567537\n2 343136264\n0 259899572...
{ "tags": [ "dp" ], "title": "Checkout Assistant" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long dp[2005][2005];\nvector<pair<long long, long long> > a;\nlong long func(long long i, long long j) {\n if (i < 0) {\n if (j == 0) {\n return 0;\n }\n return 1e18;\n }\n if (dp[i][j] != -1) {\n return dp[i][j];\n }\n long long k =...
Codeforces/223/C
# Partial Sums You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: 1. First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 ≤ i ≤ n) of array s equals <image>. The operation x m...
[ { "input": { "stdin": "5 0\n3 14 15 92 6\n" }, "output": { "stdout": "3 14 15 92 6 " } }, { "input": { "stdin": "3 1\n1 2 3\n" }, "output": { "stdout": "1 3 6 " } }, { "input": { "stdin": "10 1000000\n1 2 3 4 84 5 6 7 8 9\n" }, "outpu...
{ "tags": [ "combinatorics", "math", "number theory" ], "title": "Partial Sums" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = int(1e9 + 7);\nint n, a[2222], k, c[2222], b[2222];\nint modPow(int a, int b) {\n int res = 1;\n while (b > 0) {\n if (b & 1) {\n res = (1LL * a * res) % mod;\n }\n b >>= 1;\n a = (1LL * a * a) % mod;\n }\n return res;\n}\n...
Codeforces/248/A
# Cupboards One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house. Karlsson's gaze immediately fell...
[ { "input": { "stdin": "5\n0 1\n1 0\n0 1\n1 1\n0 1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "8\n0 1\n1 0\n0 1\n1 1\n0 1\n1 0\n0 1\n1 0\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "8\n1 0\n1 0\n1 0\n0 1\n0 1...
{ "tags": [ "implementation" ], "title": "Cupboards" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(0);\n long long n, cnt1 = 0, cnt2 = 0, l, r;\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> l >> r;\n cnt1 += l;\n cnt2 += r;\n }\n cout << min(cnt1, n - cnt1) + min(cnt2, n - cnt2);\n}\n", "java"...
Codeforces/272/B
# Dima and Sequence Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence: * f(0) = 0; * f(2·x) = f(x); * f(2·x + 1) = f(x) + 1. Dima wonders, how many pairs of indexe...
[ { "input": { "stdin": "3\n1 2 4\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "3\n5 3 1\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "6\n396640239 62005863 473635171 329666981 510631133 207643327\n" }, "outp...
{ "tags": [ "implementation", "math" ], "title": "Dima and Sequence" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxN = 100000 + 100;\nconst int maxValue = 100000 + 10;\nlong long a[maxN];\nlong long f[maxValue];\nlong long get(long long x) {\n long long ans = 0;\n while (x != 0) {\n ans += x % 2;\n x /= 2;\n }\n return ans;\n}\nint main() {\n int n;...
Codeforces/295/D
# Greg and Caves Greg has a pad. The pad's screen is an n × m rectangle, each cell can be either black or white. We'll consider the pad rows to be numbered with integers from 1 to n from top to bottom. Similarly, the pad's columns are numbered with integers from 1 to m from left to right. Greg thinks that the pad's s...
[ { "input": { "stdin": "3 5\n" }, "output": { "stdout": "451\n" } }, { "input": { "stdin": "1 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4 4\n" }, "output": { "stdout": "485\n" } }, { "input": { ...
{ "tags": [ "combinatorics", "dp" ], "title": "Greg and Caves" }
{ "cpp": "/* 2013-07-26 11:30:35.756820 */#include<cstdio>\nusing namespace std;\n\nconst int N = 2010;\n\ntypedef long long LL;\nconst LL MOD = 1e9 + 7;\n\nLL dp[N][N], sp[N][N], pp[N][N], qp[N][N], qq[N][N], ss[N][N];\nint n, m;\n\nvoid F(LL &a) {\n\tif (a >= MOD) a %= MOD;\n\twhile (a < 0) a += MOD;\n}\n\nint main...
Codeforces/319/B
# Psychos in a Line There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step. You're given ...
[ { "input": { "stdin": "6\n1 2 3 4 5 6\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "10\n10 9 7 8 6 5 3 4 2 1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "0\n" ...
{ "tags": [ "data structures", "implementation" ], "title": "Psychos in a Line" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 1e5 + 100;\nvector<int> adj[maxn];\nint a[maxn];\nbool mark[maxn];\nint dfs(int v) {\n int ret = 0;\n for (auto u : adj[v]) ret = max(ret + 1, dfs(u));\n return ret;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n co...
Codeforces/343/B
# Alternating Current Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed...
[ { "input": { "stdin": "-++-\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "++\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "+-\n" }, "output": { "stdout": "NO\n" } }, { "input": { ...
{ "tags": [ "data structures", "greedy", "implementation" ], "title": "Alternating Current" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string wire;\n cin >> wire;\n stack<int> st;\n for (int i = 0; i < wire.length(); i++) {\n if (!st.empty())\n if (wire[i] == st.top())\n st.pop();\n else\n st.push(wire[i]);\n else\n st.push(wire[i]);\n }\...
Codeforces/366/D
# Dima and Trap Graph Dima and Inna love spending time together. The problem is, Seryozha isn't too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal... Dima constructed a trap graph. He shouted: "Hey Seryozha, have a look at my cool graph!" to...
[ { "input": { "stdin": "4 4\n1 2 1 10\n2 4 3 5\n1 3 1 5\n2 4 2 7\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "5 6\n1 2 1 10\n2 5 11 20\n1 4 2 5\n1 3 10 11\n3 4 12 10000\n4 5 6 6\n" }, "output": { "stdout": "Nice work, Dima!\n" } }, { ...
{ "tags": [ "binary search", "data structures", "dfs and similar", "dsu", "shortest paths", "two pointers" ], "title": "Dima and Trap Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = (int)(1e3) + 100;\nconst int INF = (1 << 30);\nconst double PI = acos(-1.0);\nconst double EPS = (1e-6);\nvector<pair<int, pair<int, int> > > g[MAXN];\nbool used[MAXN];\nint n, m, ans = -INF;\nset<int> second;\nvoid dfs(int v, int L, int R) {\...
Codeforces/38/C
# Blinds The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that i...
[ { "input": { "stdin": "5 3\n5 5 7 3 1\n" }, "output": { "stdout": "15\n" } }, { "input": { "stdin": "4 2\n1 2 3 4\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "2 3\n1 2\n" }, "output": { "stdout": "0\n" } }...
{ "tags": [ "brute force" ], "title": "Blinds" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int lenght[128];\n int n, l, maxx = -1;\n scanf(\"%d %d\", &n, &l);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &lenght[i]);\n maxx = max(maxx, lenght[i]);\n }\n int res = 0;\n for (int len = l; len <= maxx; len++) {\n int te...
Codeforces/40/B
# Repaintings A chessboard n × m in size is given. During the zero minute we repaint all the black squares to the 0 color. During the i-th minute we repaint to the i color the initially black squares that have exactly four corner-adjacent squares painted i - 1 (all such squares are repainted simultaneously). This proc...
[ { "input": { "stdin": "1 1\n1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 3\n2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3 3\n1\n" }, "output": { "stdout": "4\n" } }, { "input"...
{ "tags": [ "math" ], "title": "Repaintings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ndouble eps = 1e-7;\nint getCountBySize(int a, int b) {\n if (a <= 0 || b <= 0) return 0;\n if (a % 2 && b % 2) return a * b / 2 + 1;\n return a * b / 2;\n}\nint main() {\n int a, b, x;\n cin >> a >> b >> x;\n x--;\n cout << getCountBySize(a - 2 * x, b -...
Codeforces/438/A
# The Child and Toy On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy. The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remo...
[ { "input": { "stdin": "4 4\n100 100 100 100\n1 2\n2 3\n2 4\n3 4\n" }, "output": { "stdout": "400\n" } }, { "input": { "stdin": "4 3\n10 20 30 40\n1 4\n1 2\n2 3\n" }, "output": { "stdout": "40\n" } }, { "input": { "stdin": "7 10\n40 10 20 10 2...
{ "tags": [ "graphs", "greedy", "sortings" ], "title": "The Child and Toy" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long MOD = 1000000007;\nlong double EPS = 1e-10;\nlong double PI = acos(-1.0);\ndouble tick() {\n static clock_t oldt;\n clock_t newt = clock();\n double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC;\n oldt = newt;\n return diff;\n}\ntemplate <typenam...
Codeforces/45/H
# Road Problem The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions. The ...
[ { "input": { "stdin": "4 4\n1 2\n2 3\n2 4\n3 4\n" }, "output": { "stdout": "1\n1 3\n" } }, { "input": { "stdin": "4 3\n1 2\n2 3\n3 4\n" }, "output": { "stdout": "1\n1 4\n" } }, { "input": { "stdin": "10 9\n7 9\n8 9\n8 2\n10 6\n8 3\n9 4\n2 6\n...
{ "tags": [ "graphs" ], "title": "Road Problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Edge {\n int v, next;\n} edge[1050 * 1050 * 2];\nint e, first[1050];\nint n, lv, pre[1050];\nset<pair<int, int> > st;\npair<int, int> p1[1050 * 1050], p2[1050 * 1050];\nint m;\nint cnt, low[1050], dfn[1050];\nbool flag[1050];\nint dist[1050];\nint from...
Codeforces/483/B
# Friends and Presents You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friend...
[ { "input": { "stdin": "3 1 2 3\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "1 3 2 3\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "1 999999999 29983 29989\n" }, "output": { "stdout": "1000033345\n...
{ "tags": [ "binary search", "math" ], "title": "Friends and Presents" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nsigned main(void) {\n ios::sync_with_stdio(false);\n register long long A, B, X, Y;\n cin >> A >> B >> X >> Y;\n cout << (long long)floor(max(\n 1.0 * (A + B - 1) / (X * Y - 1) * X * Y,\n max(1.0 * (A - 1) / (X - 1) * X, 1.0 * (B ...
Codeforces/507/B
# Amr and Pins Amr loves Geometry. One day he came up with a very interesting problem. Amr has a circle of radius r and center in point (x, y). He wants the circle center to be in new position (x', y'). In one step Amr can put a pin to the border of the circle in a certain point, then rotate the circle around that p...
[ { "input": { "stdin": "1 1 1 4 4\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "2 0 0 0 4\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "4 5 6 5 6\n" }, "output": { "stdout": "0\n" } }, { ...
{ "tags": [ "geometry", "math" ], "title": "Amr and Pins" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n double r, x, y, x1, y1;\n cin >> r >> x >> y >> x1 >> y1;\n int an =\n ceil(sqrt(((x - x1) * (x - x1)) + ((y - y1) * (y - y1))) / (2.000 * r));\n cout << an;\n}\n", "java": "import java.io.BufferedReader;\nimport java.io.File;\nimport...
Codeforces/530/C
# Diophantine equation You are given an equation A * X + B * Y = C, A, B, C are positive integer coefficients, X and Y are variables which can have positive integer values only. Output the number of solutions of this equation and the solutions themselves. Input The only line of input contains integers A, B and C (1 ...
[ { "input": { "stdin": "3 5 35\n" }, "output": { "stdout": "2\n5 4\n10 1\n" } }, { "input": { "stdin": "3 35 5\n" }, "output": { "stdout": "0\n" } } ]
{ "tags": [ "*special" ], "title": "Diophantine equation" }
{ "cpp": null, "java": null, "python": null }
Codeforces/556/C
# Case of Matryoshkas Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a...
[ { "input": { "stdin": "3 2\n2 1 2\n1 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "7 3\n3 1 3 7\n2 2 5\n2 4 6\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "100 3\n45 1 2 3 4 5 6 7 8 9 19 21 24 27 28 30 34 35...
{ "tags": [ "implementation" ], "title": "Case of Matryoshkas" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[100010];\nint main() {\n int i, n, t, k, j, m, s1;\n cin >> n >> k;\n s1 = n - k + n - 1;\n for (i = 0; i < k; i++) {\n cin >> m;\n for (j = 0; j < m; j++) {\n cin >> a[j];\n }\n if (a[0] == 1) {\n j = 1;\n while (a[j] == a...
Codeforces/582/A
# GCD Table The GCD table G of size n × n for an array of positive integers a of length n is defined by formula <image> Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both x and y, it is denoted as <image>. For example, for array ...
[ { "input": { "stdin": "4\n2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2\n" }, "output": { "stdout": "6 4 3 2 " } }, { "input": { "stdin": "1\n42\n" }, "output": { "stdout": "42 " } }, { "input": { "stdin": "2\n1 1 1 1\n" }, "output": { "stdou...
{ "tags": [ "constructive algorithms", "greedy", "number theory" ], "title": "GCD Table" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint gcd(int a, int b) {\n if (!b) return a;\n return gcd(b, a % b);\n}\nclass Comp {\n public:\n bool operator()(int a, int b) const { return a > b; }\n};\nint main() {\n int n;\n cin >> n;\n map<int, int, Comp> a;\n vector<int> ans;\n for (int i{0}; i...
Codeforces/604/A
# Uncowed Forces Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himse...
[ { "input": { "stdin": "20 40 60 80 100\n0 1 2 3 4\n1 0\n" }, "output": { "stdout": "4900\n" } }, { "input": { "stdin": "119 119 119 119 119\n0 0 0 0 0\n10 0\n" }, "output": { "stdout": "4930\n" } }, { "input": { "stdin": "36 102 73 101 19\n5 ...
{ "tags": [ "implementation" ], "title": "Uncowed Forces" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n std::ios_base::sync_with_stdio(0);\n int n, i, j, k, cnt;\n long double p[] = {500, 1000, 1500, 2000, 2500};\n long double m[10], w[10], s, u, sum = 0;\n for (i = 0; i <= 4; i++) cin >> m[i];\n for (i = 0; i <= 4; i++) cin >> w[i];\n cin ...
Codeforces/626/D
# Jerry's Protest Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who aw...
[ { "input": { "stdin": "3\n1 2 10\n" }, "output": { "stdout": "0.0740740741" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "0.0000000000" } }, { "input": { "stdin": "11\n54 91 4 6 88 23 38 71 77 26 84\n" }, "output": {...
{ "tags": [ "brute force", "combinatorics", "dp", "probabilities" ], "title": "Jerry's Protest" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ndouble diffprob[5010], ssum[5010];\nint balls[5010], N;\nint main() {\n cin.sync_with_stdio(0);\n cin.tie(0);\n cin >> N;\n for (int i = 0; i < N; ++i) {\n cin >> balls[i];\n for (int j = i - 1; j >= 0; --j) {\n diffprob[abs(balls[i] - balls[j])...
Codeforces/650/B
# Image Preview Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo ...
[ { "input": { "stdin": "5 2 4 13\nhhwhh\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "3 1 100 10\nwhw\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "5 2 4 1000\nhhwhh\n" }, "output": { "stdout": "5" ...
{ "tags": [ "binary search", "brute force", "dp", "two pointers" ], "title": "Image Preview" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n, x, y, i, j, t, aa, s1[500010], s2[500010], a, b, T, ans, anss, p;\nchar c[500010];\nlong long cal(long long z, long long xx, long long yy) {\n if (z < s2[n]) return n + 1;\n long long m, x = xx, y = yy;\n while (x < y) {\n m = (x + y) / 2;\n...
Codeforces/675/E
# Trains and Statistic Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station. Let ρi, j be the minimum number of tickets one needs to buy in order to get from ...
[ { "input": { "stdin": "4\n4 4 4\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "5\n2 3 5 5\n" }, "output": { "stdout": "17\n" } }, { "input": { "stdin": "7\n7 3 4 6 6 7\n" }, "output": { "stdout": "35\n" } },...
{ "tags": [ "data structures", "dp", "greedy" ], "title": "Trains and Statistic" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long mx = 1e5 + 66;\nconst long long mod = 1e9 + 7;\nconst long long inf = 8e18 + 66;\nconst int llg = 18;\nconst long long mxk = 31700000;\nlong long power(long long a, long long b, long long md) {\n if (b == 0) return 1;\n long long c = power(a,...
Codeforces/699/F
# Limak and Shooting Points Bearland is a dangerous place. Limak can’t travel on foot. Instead, he has k magic teleportation stones. Each stone can be used at most once. The i-th stone allows to teleport to a point (axi, ayi). Limak can use stones in any order. There are n monsters in Bearland. The i-th of them stand...
[ { "input": { "stdin": "3 8\n10 20\n0 0\n20 40\n300 600\n30 60\n170 340\n50 100\n28 56\n90 180\n-4 -8\n-1 -2\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "2 4\n-2 -1\n4 5\n4 2\n2 1\n4 -1\n1 -1\n" }, "output": { "stdout": "3\n" } }, { ...
{ "tags": [ "brute force", "geometry", "math" ], "title": "Limak and Shooting Points" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Point {\n Point() {}\n Point(long long xx, long long yy) : x(xx), y(yy) {}\n long long x;\n long long y;\n};\nconst int N = 1005;\nconst int K = 8;\nint n, k;\nPoint m[N];\nPoint p[K];\nint srt[K][N];\nint idx[K][N];\ninline bool inALine(const Point...
Codeforces/720/C
# Homework Today Peter has got an additional homework for tomorrow. The teacher has given three integers to him: n, m and k, and asked him to mark one or more squares on a square grid of size n × m. The marked squares must form a connected figure, and there must be exactly k triples of marked squares that form an L-...
[ { "input": { "stdin": "3\n3 3 4\n3 3 5\n3 3 3\n" }, "output": { "stdout": "***\n*.*\n***\n\n***\n**.\n...\n\n***\n*.*\n**.\n\n" } }, { "input": { "stdin": "1\n317 314 18260\n" }, "output": { "stdout": "****************.............................................
{ "tags": [ "constructive algorithms" ], "title": "Homework" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 100100;\nint n, m, k;\nbool rev;\nvector<int> a[N];\nconst int BOUND = 11;\nvoid clear() {\n for (int i = 0; i < n; i++) {\n a[i].clear();\n a[i].resize(m);\n for (int j = 0; j < m; j++) a[i][j] = 0;\n }\n}\nint calcSquare(int x, int y...
Codeforces/741/E
# Arpa’s abnormal DNA and Mehrdad’s deep interest All of us know that girls in Arpa’s land are... ok, you’ve got the idea :D Anyone knows that Arpa isn't a normal man, he is ... well, sorry, I can't explain it more. Mehrdad is interested about the reason, so he asked Sipa, one of the best biology scientists in Arpa's...
[ { "input": { "stdin": "abbbbbbaaa baababaaab 10\n1 2 1 0 0\n2 7 8 4 7\n2 3 9 2 8\n3 4 6 1 1\n0 8 5 2 4\n2 8 10 4 7\n7 10 1 0 0\n1 4 6 0 2\n0 9 8 0 6\n4 8 5 0 1\n" }, "output": { "stdout": "1 4 2 -1 2 4 10 1 1 5\n" } }, { "input": { "stdin": "abc d 4\n0 3 2 0 0\n0 3 1 0 0\n1...
{ "tags": [ "data structures", "string suffix structures" ], "title": "Arpa’s abnormal DNA and Mehrdad’s deep interest" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 0x3f3f3f3f;\nint q, Len, LenS, LenT;\nint Log[200010], Min[200010][19];\nint rk[200010], tp[200010], sa[200010], st[200010], height[200010], m = 75;\nchar S[100010], T[100010], str[200010];\nint ans[100010], id[100010], _id[100010];\nint Min2[1...
Codeforces/765/D
# Artsem and Saunders Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem. Let [n] denote the set {1, ..., n}. We will also write f: [x] → [y] when a function f is defined in integer points 1, ..., x, and all its values are integers from 1 to y. Now then, you ar...
[ { "input": { "stdin": "3\n1 2 3\n" }, "output": { "stdout": "3\n1 2 3 \n1 2 3 " } }, { "input": { "stdin": "3\n2 2 2\n" }, "output": { "stdout": "1\n1 1 1 \n2 " } }, { "input": { "stdin": "2\n2 1\n" }, "output": { "stdout": "-1"...
{ "tags": [ "constructive algorithms", "dsu", "math" ], "title": "Artsem and Saunders" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mx = 1e5 + 10;\nint n, m, f[mx], g[mx], h[mx];\nbool flag[mx];\nint main() {\n memset(flag, 0, sizeof(flag));\n scanf(\"%d\", &n);\n m = 0;\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", &f[i]);\n if (f[i] == i) {\n g[i] = ++m;\n ...
Codeforces/789/A
# Anastasia and pebbles Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park. She has only two pockets. She can put at most k pebbles in each...
[ { "input": { "stdin": "5 4\n3 1 8 9 7\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "3 2\n2 3 4\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "10 1\n1 1 1 1 1 1 1 1 1 1\n" }, "output": { "stdout": "...
{ "tags": [ "implementation", "math" ], "title": "Anastasia and pebbles" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid readi(int &x) {\n int v = 0, f = 1;\n char c = getchar();\n while (!isdigit(c) && c != '-') c = getchar();\n if (c == '-')\n f = -1;\n else\n v = v * 10 + c - '0';\n while (isdigit(c = getchar())) v = v * 10 + c - '0';\n x = v * f;\n}\nvoid r...
Codeforces/80/C
# Heroes The year of 2012 is coming... According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to ...
[ { "input": { "stdin": "3\nTroll likes Dracul\nDracul likes Anka\nSnowy likes Hexadecimal\n210 200 180\n" }, "output": { "stdout": "30 3\n" } }, { "input": { "stdin": "2\nAnka likes Chapay\nChapay likes Anka\n10000 50 50\n" }, "output": { "stdout": "1950 2\n" ...
{ "tags": [ "brute force", "implementation" ], "title": "Heroes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint anslike, dif, cnt[10][10], like[10][10], val[100];\nmap<string, int> mat;\nvoid dfs(int now) {\n int i, j, k;\n if (now == 7) {\n int Max = 0, Min = 2000000000, likes = 0;\n for (i = 0; i < 3; i++) {\n if (cnt[i][0] == 0) return;\n int tm...
Codeforces/835/A
# Key races Two boys decided to compete in text typing on the site "Key races". During the competition, they have to type a text consisting of s characters. The first participant types one character in v1 milliseconds and has ping t1 milliseconds. The second participant types one character in v2 milliseconds and has p...
[ { "input": { "stdin": "4 5 3 1 5\n" }, "output": { "stdout": "Friendship\n" } }, { "input": { "stdin": "5 1 2 1 2\n" }, "output": { "stdout": "First\n" } }, { "input": { "stdin": "3 3 1 1 1\n" }, "output": { "stdout": "Second\n"...
{ "tags": [ "math" ], "title": "Key races" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int a, b, c, d, e, x, y;\n cin >> a >> b >> c >> d >> e;\n x = (a * b) + (2 * d);\n y = (a * c) + (2 * e);\n if (y > x)\n cout << \"First\";\n else if (y < x)\n cout << \"Second\";\n else\n cout << \"Friendship\";\n return 0;\n}...
Codeforces/855/D
# Rowena Ravenclaw's Diadem Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her. Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would ha...
[ { "input": { "stdin": "3\n-1 -1\n1 0\n2 0\n2\n1 1 3\n2 1 3\n" }, "output": { "stdout": "YES\nNO\n" } }, { "input": { "stdin": "3\n-1 -1\n1 0\n1 1\n2\n2 2 3\n2 3 2\n" }, "output": { "stdout": "YES\nNO\n" } }, { "input": { "stdin": "20\n-1 -1\n...
{ "tags": [ "trees" ], "title": "Rowena Ravenclaw's Diadem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 120000;\nconst int LOG = 20;\nint n;\nint tin[MAXN];\nint tout[MAXN];\nvector<int> eds[MAXN];\nint p[MAXN];\nint t[MAXN];\nint up[LOG][MAXN];\nint u0[MAXN];\nint u1[MAXN];\nint h[MAXN];\nint tm1;\nint is_p(int a, int b) { return tin[a] <= tin[...
Codeforces/87/B
# Vasya and Types Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language'...
[ { "input": { "stdin": "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &amp;b b\ntypeof b\ntypeof c\ntypedef &amp;&amp;b* c\ntypeof c\ntypedef &amp;b* c\ntypeof c\ntypedef &amp;void b\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &amp;&amp;b* c\ntypeof c\n" }, "output": { "...
{ "tags": [ "implementation", "strings" ], "title": "Vasya and Types" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nmap<string, int> mp;\nstring tt = \"void\";\nint check(string s) {\n int ct1 = 0, ct2 = 0;\n int len = s.size();\n string result;\n for (int i = 0; i < len; i++) {\n if (s[i] == '&')\n ct1++;\n else if (s[i] == '*')\n ct2++;\n else\n ...
Codeforces/903/D
# Almost Difference Let's denote a function <image> You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n. Input The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a. The second line contains n i...
[ { "input": { "stdin": "4\n6 6 5 5\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "5\n1 2 3 1 3\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4\n6 6 4 4\n" }, "output": { "stdout": "-8" } }, { ...
{ "tags": [ "data structures", "math" ], "title": "Almost Difference" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[200001];\nint main() {\n int i, N;\n cin >> N;\n map<int, int> m;\n long long sum = 0;\n for (i = 1; i <= N; i++) {\n int x;\n cin >> x;\n a[i] = x;\n m[x]++;\n sum += x;\n }\n long double result = 0;\n for (i = 1; i <= N; i++) {\n...
Codeforces/925/E
# May Holidays It's May in Flatland, and there are m days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long vacations in May. Of course, not all managers of the company like this. There are n employees in the com...
[ { "input": { "stdin": "5 6\n1 2 3 4\n4 0 0 1 0\n1 5 2 3 -5 -1\n" }, "output": { "stdout": "0 2 1 0 0 0 " } }, { "input": { "stdin": "7 8\n4 5 1 1 5 5\n0 0 0 1 2 0 0\n2 6 3 7 -2 4 -3 1\n" }, "output": { "stdout": "1 1 1 2 2 2 1 0 " } }, { "input": {...
{ "tags": [ "data structures", "trees" ], "title": "May Holidays" }
{ "cpp": "#include <bits/stdc++.h>\nconst int maxn = 1E+5 + 5;\nconst int maxB = 605;\nconst int B = 600;\nconst int INF = 0x3f3f3f3f;\nint n, m, ind, a[maxn];\nint fa[maxn], siz[maxn];\nint top[maxn], son[maxn], dfn[maxn];\nstd::vector<int> to[maxn];\ninline void DFS1(int u) {\n siz[u] = 1;\n for (int v : to[u]) {...
Codeforces/954/F
# Runner's Problem You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column. You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to: * (i - 1, j + 1) — only...
[ { "input": { "stdin": "2 5\n1 3 4\n2 2 3\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "50 100\n1 71 96\n2 34 52\n2 16 95\n1 54 55\n1 65 85\n1 76 92\n2 19 91\n1 26 43\n2 83 95\n2 70 88\n2 67 88\n1 9 75\n2 4 50\n2 9 11\n1 77 92\n1 28 58\n1 23 72\n1 24 75\n2 12...
{ "tags": [ "dp", "matrices", "sortings" ], "title": "Runner's Problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint Y[] = {-1, 1, 0, 0, -1, -1, 1, 1};\nint X[] = {0, 0, -1, 1, -1, 1, -1, 1};\nint DIR4 = 4;\nint DIR8 = 8;\nlong long power(long long a, long long n) {\n if (a == 0) return 0;\n if (a == 1 || n == 0) return 1;\n if (n == 1) return a % 1000000007;\n long ...
Codeforces/980/E
# The Number Games The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented by one contestant. The nation has n districts numbered from 1 to n, each district has exactly one path connecting it to every other district. The number of fans of a contestant fr...
[ { "input": { "stdin": "6 3\n2 1\n2 6\n4 2\n5 6\n2 3\n" }, "output": { "stdout": "1 3 4 " } }, { "input": { "stdin": "8 4\n2 6\n2 7\n7 8\n1 2\n3 1\n2 4\n7 5\n" }, "output": { "stdout": "1 3 4 5 " } }, { "input": { "stdin": "64 63\n11 51\n64 11...
{ "tags": [ "data structures", "greedy", "trees" ], "title": "The Number Games" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e6 + 5, M = 2e6 + 5;\nint E = -1, pnt[M], nxt[M], head[N];\nint fa[N][20], vis[N], dth[N];\nvoid add(int u, int v) {\n E++;\n pnt[E] = v;\n nxt[E] = head[u];\n head[u] = E;\n}\nvoid dfs(int u, int f, int d) {\n fa[u][0] = f;\n dth[u] = d;\...
Codeforces/9/E
# Interesting Graph and Apples Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life «interesting graph and apples». An undirected graph is called interesting, if each of its vertices belongs to one cycle only — a funny ring — and does not...
[ { "input": { "stdin": "3 2\n1 2\n2 3\n" }, "output": { "stdout": "YES\n1\n1 3\n" } }, { "input": { "stdin": "42 28\n7 19\n15 24\n3 42\n18 5\n32 27\n26 20\n40 30\n35 2\n14 8\n22 10\n36 4\n16 14\n21 29\n37 40\n2 12\n30 21\n19 17\n39 34\n31 28\n20 3\n4 33\n11 42\n26 21\n9 10\n...
{ "tags": [ "dfs and similar", "dsu", "graphs" ], "title": "Interesting Graph and Apples" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> e[55];\nint n, m;\nbool v[55];\nvector<pair<int, int> > ans;\nbool check[55][55];\nint num[55];\nvoid cc(int x, int c) {\n num[x] = c;\n for (int i = 0; i < e[x].size(); i++)\n if (num[e[x][i]] < c) cc(e[x][i], c);\n}\nbool can() {\n int cnt ...
HackerEarth/bhavanas-and-bhuvanas-bet
Bhavana and Bhuvana place bet every time they see something interesting that can occur with a probability 0.5. Both of them place a bet for a higher amount which they don’t posses at that time. But when one wins she demands other for the bet amount. For example if Bhuvana wins she asks bhavana for the bet amount. But...
[ { "input": { "stdin": "2\n3\n10" }, "output": { "stdout": "1\n3" } } ]
{ "tags": [], "title": "bhavanas-and-bhuvanas-bet" }
{ "cpp": null, "java": null, "python": "from sys import stdin\nt=stdin.readline()\na=stdin.readlines()\nfor i in a:\n\tn = int(i)\n\tcur = 1\n\tans = 0\n\twhile cur<=n:\n\t\tcur = cur*2\n\t\tans+=1\n\tans-=1\n\tif n==0:\n\t\tans = 0\n\tif ans==0:\n\t\tans = -2147483648\n\tprint ans" }
HackerEarth/cost-of-data-11
Strings can be efficiently stored as a data structure, to have efficient searching methods. A new startup is going to use this method, but they don't have much space. So they want to check beforehand how much memory will be required for their data. Following method describes the way in which this startup's engineers s...
[ { "input": { "stdin": "5\net\neq\nbd\nbe\nbdp\n\nSAMPLE" }, "output": { "stdout": "8" } }, { "input": { "stdin": "5\nft\nfr\ncd\nde\nb`p\n\nSQDALK" }, "output": { "stdout": "11\n" } }, { "input": { "stdin": "5\nft\nre\nec\nce\nb`p\n\nSQDALK" ...
{ "tags": [], "title": "cost-of-data-11" }
{ "cpp": null, "java": null, "python": "import fileinput\n\nclass Node(object):\n def __init__(self,data):\n self.data=data\n self.children={} # {'data' => Node}\n self.max_score =0\n \n def add_child(self,data,score):\n if self.children.has_key(data):\n pass\n ...
HackerEarth/find-the-nth-prime
Given the value of n, print the n'th prime number. Input : A single integer n. Output : A single number which is the n'th prime number. Constraints : 1 ≤ n ≤ 1000 SAMPLE INPUT 2 SAMPLE OUTPUT 3 Explanation The first few prime numbers are: 2,3,5,7. So, the answer is 3.
[ { "input": { "stdin": "2\n\nSAMPLE" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "2\n\nDVMULK" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "2\n\nCUKOVG" }, "output": { "stdout": "3\n" } }, { ...
{ "tags": [], "title": "find-the-nth-prime" }
{ "cpp": null, "java": null, "python": "number = 0\nprime_found = 0 \nprim_count = 0\n\ninput = int(raw_input())\n\nif(input > 1000):\n exit(\"Please Enter Correct Input\")\n\nwhile ( prim_count < input ):\n #print (number)\n for index in range (1,number+1):\n #print ('index = ' , index )\n ...
HackerEarth/insect-colony-2
Yesterday Oz heard a story about insect colony. The specialty of insects is that they splits sometimes i.e an insect of size A can split into two insects of positive integral sizes B and C such that A = B + C. Also sometimes they attack each other i.e. two insects of sizes P and Q will become R = P XOR Q . You are give...
[ { "input": { "stdin": "2\n2 9 17\n1 1\n\nSAMPLE" }, "output": { "stdout": "Yes\nNo\n" } }, { "input": { "stdin": "10\n75 995991040 148627929 566366239 458028006 823890903 945174886 530827147 546010999 720763988 183796976 748924885 868038355 870234651 273702889 972313979 607...
{ "tags": [], "title": "insect-colony-2" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\nfor i in range(t):\n\tarr = list(map(int,raw_input().split()))\n\tarr = arr[1:]\n\tans = sum(arr)\n\tif ans%2 == 1:\n\t print 'No'\n\telse:\n\t print 'Yes'" }
HackerEarth/max-min
you are given an array of size N. You need to select K elements from the array such that the difference between the max number among selected and the min number among selected array elements should be minimum. Print the minimum difference. INPUT: First line N number i.e., length of array Second line K number Next N l...
[ { "input": { "stdin": "5\n3\n1\n2\n3\n4\n5\n\nSAMPLE" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5\n1\n0\n5\n4\n4\n0\n\nQAMRLD" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5\n3\n0\n3\n3\n1\n5\n\nQAMRLE" }, ...
{ "tags": [], "title": "max-min" }
{ "cpp": null, "java": null, "python": "N = input()\nK = input()\n\nassert 1 <= N <= 10**5\nassert 1 <= K <= N\n\nlis = []\nfor i in range(N):\n lis.append(input())\n\nlis.sort()\n\nfor i in lis:\n assert 0 <= i <= 10**9\n\nanswer = 100000000000000000000\n\nfor index in range(N-K+1):\n diff = lis[index+K...
HackerEarth/once-upon-a-time-in-time-land
In a mystical TimeLand, a person's health and wealth is measured in terms of time(seconds) left. Suppose a person there has 24x60x60 = 86400 seconds left, then he would live for another 1 day. A person dies when his time left becomes 0. Some time-amount can be borrowed from other person, or time-banks. Some time-amoun...
[ { "input": { "stdin": "2\n10 1\n1 2 -3 -5 4 6 -3 2 -1 2\n10 2\n1 2 -3 -5 4 6 -3 2 -1 2\n\nSAMPLE" }, "output": { "stdout": "12\n10\n" } }, { "input": { "stdin": "30\n16 3\n-334 -500 -169 -724 -478 -358 -962 -464 -705 -145 -281 -827 -961 -491 -995 -942 \n22 2\n391 604 902 15...
{ "tags": [], "title": "once-upon-a-time-in-time-land" }
{ "cpp": null, "java": null, "python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\ndef find_time(second, length, k):\n\tf = [0] * (length + k + 2)\n\tstart = length\n\twhile start >= 0:\n\t\tf[start] = max(f[start+1...
HackerEarth/rams-love-for-sita-2
Sita loves chocolate and Ram being his boyfriend wants to give Sita as many chocolates as he can. So, he goes to a chocolate store with Rs. N in his pocket. The price of each chocolate is Rs. C. The store offers a discount that for every M wrappers he gives to the store, he gets one chocolate for free. How many chocola...
[ { "input": { "stdin": "3\n10 2 5\n12 4 4\n6 2 2\n\nSAMPLE" }, "output": { "stdout": "6\n3\n5\n" } }, { "input": { "stdin": "3\n24 2 5\n8 5 2\n23 1 3\n\nSAMLPF" }, "output": { "stdout": "14\n1\n34\n" } }, { "input": { "stdin": "3\n3 2 5\n24 4 ...
{ "tags": [], "title": "rams-love-for-sita-2" }
{ "cpp": null, "java": null, "python": "for _ in range(input()):\n n,c,m = map(int,raw_input().split())\n k = n/c\n ans = k\n while k >= m:\n ans += k/m\n k = k/m + k%m\n print ans" }
HackerEarth/simple-prime-factorization
Surya loves to play with primes. One day, he asked his friend don to print any number in the form of multiple of prime factors. Help don in solving the problem. Input The first line will contain an integer t (1 ≤ t ≤ 10^6) denoting number of test case. For each test case, you will be given an integer n (2 ≤ n ≤ 10^6...
[ { "input": { "stdin": "3\n32\n100\n9085\n\nSAMPLE" }, "output": { "stdout": "2^5\n2^2*5^2\n2^0*5^1*23^1*79^1" } }, { "input": { "stdin": "1000\n999001\n999002\n999003\n999004\n999005\n999006\n999007\n999008\n999009\n999010\n999011\n999012\n999013\n999014\n999015\n999016\n99...
{ "tags": [], "title": "simple-prime-factorization" }
{ "cpp": null, "java": null, "python": "def primes(n):\n primfac = []\n d = 2\n while d*d <= n:\n while (n % d) == 0:\n primfac.append(d)\n n //= d\n d += 1\n if n > 1:\n primfac.append(n)\n return primfac\nt = int(raw_input())\nfor _ in range(t):\n l = ...
HackerEarth/the-strongest-string-1-1
Maga and Alex are good at string manipulation problems. Just now they have faced a problem related to string. But it is not a standard string problem. They have no idea to solve it. They need your help. A string is called unique if all characters of string are distinct. String s_1 is called subsequence of string s_2 ...
[ { "input": { "stdin": "5\nabvzx\n\nSAMPLE" }, "output": { "stdout": "zx\n" } }, { "input": { "stdin": "5\nxywba\n\nETAMOL" }, "output": { "stdout": "ywba\n" } }, { "input": { "stdin": "5\nbywaw\n\nSOMALF" }, "output": { "stdout"...
{ "tags": [], "title": "the-strongest-string-1-1" }
{ "cpp": null, "java": null, "python": "from string import ascii_lowercase\nal = ascii_lowercase[::-1]\nn = int(raw_input())\ns = raw_input().strip()\nt = \"\"\nps = 0\nfor c in al:\n i = s.find(c, ps)\n if i == -1:\n continue\n else:\n t += c\n ps = i\nprint t" }
p02548 AtCoder Beginner Contest 179 - A x B + C
Given is a positive integer N. How many tuples (A,B,C) of positive integers satisfy A \times B + C = N? Constraints * 2 \leq N \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N Output Print the answer. Examples Input 3 Output 3 Input 100 ...
[ { "input": { "stdin": "3" }, "output": { "stdout": "3" } }, { "input": { "stdin": "100" }, "output": { "stdout": "473" } }, { "input": { "stdin": "1000000" }, "output": { "stdout": "13969985" } }, { "input": { ...
{ "tags": [], "title": "p02548 AtCoder Beginner Contest 179 - A x B + C" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\nint64_t n,ans;\n ans=0;\n cin>>n;\n for(int64_t i=1;i<n;i++){\nans=ans+(n-1)/i; \n }\n cout<<ans<<endl; \n} ", "java": "import java.util.Scanner;\n\npublic class Main {\n public static void main(String[] args) {\n Scanner sc = ...
p02679 AtCoder Beginner Contest 168 - ∙ (Bullet)
We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively. We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time. The i-th and j-th sardines (i \neq j) are on bad terms if and o...
[ { "input": { "stdin": "3\n1 2\n-1 1\n2 -1" }, "output": { "stdout": "5" } }, { "input": { "stdin": "10\n3 2\n3 2\n-1 1\n2 -1\n-3 -9\n-8 12\n7 7\n8 1\n8 2\n8 4" }, "output": { "stdout": "479" } }, { "input": { "stdin": "3\n1 0\n-2 0\n0 2" ...
{ "tags": [], "title": "p02679 AtCoder Beginner Contest 168 - ∙ (Bullet)" }
{ "cpp": "#include<iostream>\n#include<vector>\n#include<map>\n#include<algorithm>\nusing namespace std;\nconst long mod=1e9+7;\nlong power(long a,long b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}\nlong N;\nlong c00,c01,c10;\nlong gcd(long a,long b){return b?gcd(b,a%b):a;}\nint main()\n{\n\tcin>>N;\n\tvector<pair...
p02807 Dwango Programming Contest 6th - Fusing Slimes
There are N slimes standing on a number line. The i-th slime from the left is at position x_i. It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}. Niwango will perform N-1 operations. The i-th operation consists of the following procedures: * Choose an integer k between 1 and N-i (inclusive) with equ...
[ { "input": { "stdin": "12\n161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932" }, "output": { "stdout": "750927044" } }, { "input": { "stdin": "3\n1 2 3" }, "output": { "stdout": "5" } ...
{ "tags": [], "title": "p02807 Dwango Programming Contest 6th - Fusing Slimes" }
{ "cpp": "#include <bits/stdc++.h>\n#define rep(i, n) for(int i = 0; i < (n); ++i)\nusing namespace std;\ntypedef long long ll;\n\nconst ll MAX = 1e5;\nconst ll MOD = 1e9+7;\n\nll fac[MAX], finv[MAX], inv[MAX];\n\n// テーブルを作る前処理\nvoid COMinit() {\n fac[0] = fac[1] = 1;\n finv[0] = finv[1] = 1;\n inv[1] = 1;\n...
p02943 AtCoder Grand Contest 037 - Reversing and Concatenating
Takahashi has a string S of length N consisting of lowercase English letters. On this string, he will perform the following operation K times: * Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order. * Let S' be some contiguous substring of U with length N, an...
[ { "input": { "stdin": "5 1\nbacba" }, "output": { "stdout": "aabca" } }, { "input": { "stdin": "10 2\nbbaabbbaab" }, "output": { "stdout": "aaaabbaabb" } }, { "input": { "stdin": "10 1\nbaaabbbaab" }, "output": { "stdout": "aaab...
{ "tags": [], "title": "p02943 AtCoder Grand Contest 037 - Reversing and Concatenating" }
{ "cpp": "#include <bits/stdc++.h>\n\n#define MIN_INT -2147483648\n#define MAX_INT 2147483647\n#define MIN_LONG -9223372036854775808L\n#define MAX_LONG 9223372036854775807L\n\n#define long long long int\n\nusing namespace std;\n\n// @author: pashka\n\nint main() {\n ios::sync_with_stdio(false);\n\n int n, k;\n ...
p03080 ExaWizards 2019 - Red or Blue
There are N people numbered 1 to N. Each person wears a red hat or a blue hat. You are given a string s representing the colors of the people. Person i wears a red hat if s_i is `R`, and a blue hat if s_i is `B`. Determine if there are more people wearing a red hat than people wearing a blue hat. Constraints * 1 \l...
[ { "input": { "stdin": "4\nRRBR" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "4\nBRBR" }, "output": { "stdout": "No" } }, { "input": { "stdin": "4\nSBCO" }, "output": { "stdout": "No\n" } }, { "input": {...
{ "tags": [], "title": "p03080 ExaWizards 2019 - Red or Blue" }
{ "cpp": "#include <iostream>\nusing namespace std;\nint main()\n{\n\tint n,r=0,b=0;string s;\n\tcin>>n>>s;\n\tfor (int i=0;i<s.size();i++)\n\t{\n\t\tif (s[i]=='R') {r++;}\n\t\telse {b++;}\n\t}\n\tif (r>b) {cout<<\"Yes\\n\";}\n\telse {cout<<\"No\\n\";}\n\treturn 0;\n}", "java": "\nimport java.util.*;\n\npublic clas...
p03225 Tenka1 Programmer Contest - Equilateral
There are some coins in the xy-plane. The positions of the coins are represented by a grid of characters with H rows and W columns. If the character at the i-th row and j-th column, s_{ij}, is `#`, there is one coin at point (i,j); if that character is `.`, there is no coin at point (i,j). There are no other coins in t...
[ { "input": { "stdin": "5 4\n#.##\n.##.\n#...\n..##\n...#" }, "output": { "stdout": "3" } }, { "input": { "stdin": "13 27\n......#.........#.......#..\n...#.....###..\n..............#####...##...\n...#######......#...#######\n...#.....#.....###...#...#.\n...#######....#.#.#....
{ "tags": [], "title": "p03225 Tenka1 Programmer Contest - Equilateral" }
{ "cpp": "#include <algorithm>\n#include <iostream>\n#include <cstring>\n#include <cstdlib>\n#include <utility>\n#include <cstdio>\n#include <vector>\n#include <string>\n#include <queue>\n#include <stack>\n#include <cmath>\n#include <set>\n#include <map>\n#define my_abs(x) ((x) < 0 ? -(x) : (x))\n#define mp std::make...
p03371 AtCoder Beginner Contest 095 - Half and Half
"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the curr...
[ { "input": { "stdin": "1500 2000 1600 3 2" }, "output": { "stdout": "7900" } }, { "input": { "stdin": "1500 2000 500 90000 100000" }, "output": { "stdout": "100000000" } }, { "input": { "stdin": "1500 2000 1900 3 2" }, "output": { ...
{ "tags": [], "title": "p03371 AtCoder Beginner Contest 095 - Half and Half" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nint a, b, c, x, y, ans;\n\nint main() {\n cin >> a >> b >> c >> x >> y;\n if (2 * c < a + b) {\n\tint mn = min(x, y);\n\tans += 2 * mn * c;\n\tx -= mn;\n\ty -= mn;\n }\n return cout << ans + min(a, 2 * c) * x + min(b, 2 * c) * y, 0;\n}", "jav...