id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
CodeChef/brcktsrm
Problem description. Vipul is a hardworking super-hero who maintains the bracket ratio of all the strings in the world. Recently he indulged himself in saving the string population so much that he lost his ability for checking brackets (luckily, not permanently ).Being his super-hero friendΒ help him in his time of hardship. Input The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single string S denoting the string to be checked. Output For each test case, output a single line printing "YES" or "NO" (without " " and in uppercase only) , denoting if the brackets in the given string is balanced or not . Constraints 1 ≀ T ≀ 10 1 ≀ length of S ≀ 60 Example Input: 3 ((())) (())() ()(() Output: YES YES NO Β  Explanation Example is self-explanatory.
[ { "input": { "stdin": "3\n((()))\n(())()\n()(()" }, "output": { "stdout": "YES\nYES\nNO" } }, { "input": { "stdin": "3\n)('()(\n)(())'\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)()(*(\n(())))\n)())(" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n))'()(\n)(())'\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)()()(\n))()('\n(())(" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n()('))\n))))'(\n((()'" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)))((*\n(())))\n()())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)('()(\n))))'(\n((()'" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n()('))\n('))))\n((()'" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)('()'\n)('))(\n')()(" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)*)(((\n(())))\n()(*)" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)()()(\n'()())\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)('()(\n)())((\n')('(" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)')()(\n))()('\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)((())\n'())))\n()())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)()(*(\n(())))\n()())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)))(()\n(())))\n()())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)('(((\n)(())'\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()()\n&())()\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n))'()(\n'))((*\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()()\n(())()\n((()(" }, "output": { "stdout": "NO\nYES\nNO\n" } }, { "input": { "stdin": "3\n)()()(\n))()('\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)))(((\n))))((\n()(*)" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()))\n(())()\n()())" }, "output": { "stdout": "YES\nYES\nNO\n" } }, { "input": { "stdin": "3\n)('()(\n)('))(\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)('()(\n)())'(\n')(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)))(((\n(())))\n()())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()))\n(())))\n()())" }, "output": { "stdout": "YES\nNO\nNO\n" } }, { "input": { "stdin": "3\n)))((*\n(())))\n))())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n))'()(\n'))(()\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)))(((\n(())()\n()())" }, "output": { "stdout": "NO\nYES\nNO\n" } }, { "input": { "stdin": "3\n)()(*(\n(())))\n)()((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()()\n'())()\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()()\n(())()\n')()(" }, "output": { "stdout": "NO\nYES\nNO\n" } }, { "input": { "stdin": "3\n)')())\n'()())\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n()((()\n&())()\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)*)(()\n(())))\n()())" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n))*(((\n(())))\n()(*)" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()))\n)()(()\n')()(" }, "output": { "stdout": "YES\nNO\nNO\n" } }, { "input": { "stdin": "3\n)('()(\n())'()\n((()'" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)(()()\n)())('\n(((((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)()(*(\n(()())\n*)(*)" }, "output": { "stdout": "NO\nYES\nNO\n" } }, { "input": { "stdin": "3\n)('()(\n)())((\n')(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)*)(((\n))()((\n()(*)" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()))\n(())()\n))(((" }, "output": { "stdout": "YES\nYES\nNO\n" } }, { "input": { "stdin": "3\n)(()()\n'())()\n()(((" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((()()\n(())()\n()(((" }, "output": { "stdout": "NO\nYES\nNO\n" } }, { "input": { "stdin": "3\n)()()(\n&()())\n()()(" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n)((())\n'())))\n()'))" }, "output": { "stdout": "NO\nNO\nNO\n" } }, { "input": { "stdin": "3\n((())(\n*())()\n()(()" }, "output": { "stdout": "NO\nNO\nNO\n" } } ]
{ "tags": [], "title": "brcktsrm" }
{ "cpp": null, "java": null, "python": "for _ in range(input()):\n try:\n eval(raw_input())\n print 'YES'\n except TypeError:\n print 'YES'\n except:\n print 'NO'" }
CodeChef/comm3
The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they cannot communicate directly. The Chef invested in top-of-the-line transceivers which have a few advanced features. One is that even if two people cannot talk directly because they are out of range, if there is another transceiver that is close enough to both, then the two transceivers can still communicate with each other using the third transceiver as an intermediate device. There has been a minor emergency in the Chef's restaurant and he needs to communicate with both the head server and the sous-chef right away. Help the Chef determine if it is possible for all three people to communicate with each other, even if two must communicate through the third because they are too far apart. Input The first line contains a single positive integer T ≀ 100 indicating the number of test cases to follow. The first line of each test case contains a positive integer R ≀ 1,000 indicating that two transceivers can communicate directly without an intermediate transceiver if they are at most R meters away from each other. The remaining three lines of the test case describe the current locations of the Chef, the head server, and the sous-chef, respectively. Each such line contains two integers X,Y (at most 10,000 in absolute value) indicating that the respective person is located at position X,Y. Output For each test case you are to output a single line containing a single string. If it is possible for all three to communicate then you should output "yes". Otherwise, you should output "no". To be clear, we say that two transceivers are close enough to communicate directly if the length of the straight line connecting their X,Y coordinates is at most R. Example Input: 3 1 0 1 0 0 1 0 2 0 1 0 0 1 0 2 0 0 0 2 2 1 Output: yes yes no
[ { "input": { "stdin": "3\n1\n0 1\n0 0\n1 0\n2\n0 1\n0 0\n1 0\n2\n0 0\n0 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 -1\n1\n2 0\n0 -1\n1 -2\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nno\nyes\n" } }, { "input": { "stdin": "3\n1\n0 1\n0 -1\n1 0\n2\n0 1\n0 0\n1 1\n2\n0 0\n0 2\n2 1" }, "output": { "stdout": "no\nyes\nno\n" } }, { "input": { "stdin": "3\n3\n0 -1\n-2 -2\n1 0\n2\n-1 0\n0 0\n1 0\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 -1\n1\n2 0\n0 -1\n1 -2\n2\n1 0\n1 2\n1 2" }, "output": { "stdout": "yes\nno\nyes\n" } }, { "input": { "stdin": "3\n4\n0 -1\n-1 -1\n1 1\n2\n0 -1\n0 0\n2 -1\n2\n2 0\n1 2\n0 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 0\n1\n2 0\n0 -1\n1 -2\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n2\n0 1\n0 -1\n2 -1\n1\n2 0\n0 -1\n1 -2\n2\n0 0\n1 2\n1 2" }, "output": { "stdout": "yes\nno\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n1 0\n2\n0 0\n0 0\n1 0\n2\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 1\n1\n3 0\n0 -1\n1 -2\n2\n1 1\n0 2\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 1\n2\n2 1\n0 -1\n1 -2\n2\n1 1\n1 2\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n3\n0 -1\n-1 -2\n1 0\n2\n-1 0\n0 0\n1 0\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 0\n1 -2\n1 0\n3\n0 0\n0 0\n1 -1\n3\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n1 0\n1 -1\n1 -1\n2\n1 0\n2 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 0\n0 0\n1 0\n2\n0 1\n0 0\n1 0\n2\n0 0\n0 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n2 0\n2\n1 0\n0 -1\n0 -2\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "no\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n1 0\n3\n0 0\n0 0\n1 0\n2\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -2\n-1 -1\n1 0\n2\n0 0\n0 0\n1 -1\n1\n1 -1\n1 2\n1 1" }, "output": { "stdout": "no\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 0\n1 -2\n1 0\n3\n0 1\n0 0\n1 -1\n3\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -2\n-1 -1\n1 0\n2\n-1 0\n-1 0\n1 -1\n1\n1 -1\n1 2\n1 1" }, "output": { "stdout": "no\nno\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 0\n1\n2 1\n0 -1\n1 -2\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n1 0\n2\n1 0\n1 -1\n1 -1\n2\n1 0\n2 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 1\n1\n2 0\n0 -1\n1 -2\n2\n1 0\n0 2\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 0\n2\n1 0\n0 -2\n1 -2\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "no\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n0 1\n0 -1\n1 -1\n2\n2 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 0\n1 -1\n1 0\n3\n0 0\n0 0\n1 -1\n2\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n1 0\n0 -1\n1 -1\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n2 0\n2\n1 0\n0 -1\n1 -1\n3\n1 0\n1 2\n1 1" }, "output": { "stdout": "no\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n0 0\n0 -1\n1 -1\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 0\n1 -1\n1 0\n3\n0 0\n0 0\n1 -1\n3\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n1 0\n2\n0 0\n0 0\n1 0\n2\n1 0\n1 2\n2 2" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n1\n0 0\n0 0\n1 0\n4\n0 1\n0 0\n0 0\n2\n0 0\n0 3\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n2 0\n2\n1 0\n1 -1\n1 -1\n3\n1 0\n2 2\n1 0" }, "output": { "stdout": "no\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 1\n0 0\n1 0\n2\n0 1\n0 0\n1 0\n2\n0 0\n0 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 0\n0 -1\n1 0\n2\n0 1\n0 0\n1 0\n2\n0 0\n0 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n3\n0 -1\n-2 -1\n1 0\n2\n-1 0\n0 0\n1 0\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 0\n1 -1\n1 0\n2\n0 0\n0 0\n1 -1\n2\n0 0\n1 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n0 1\n0 -1\n1 -1\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -2\n2 1\n2\n2 1\n0 -1\n1 -2\n2\n1 1\n1 3\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n1\n0 0\n0 0\n1 0\n4\n0 2\n0 1\n0 0\n2\n0 0\n0 3\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n2 0\n2\n1 0\n1 -1\n1 -1\n3\n1 0\n2 1\n1 0" }, "output": { "stdout": "no\nyes\nyes\n" } }, { "input": { "stdin": "3\n1\n0 1\n0 0\n1 0\n4\n0 1\n0 0\n1 0\n2\n0 0\n0 3\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n3\n0 -1\n-1 -1\n1 0\n2\n-1 0\n0 0\n1 0\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 1\n0 -1\n2 -1\n1\n2 0\n0 -1\n1 -2\n2\n1 0\n1 2\n1 2" }, "output": { "stdout": "yes\nno\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n1 0\n1 -1\n1 -1\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 2\n0 0\n1 0\n2\n-1 1\n0 0\n1 0\n2\n0 0\n0 2\n2 1" }, "output": { "stdout": "yes\nyes\nno\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n1 0\n2\n1 0\n1 -1\n1 -1\n2\n1 0\n2 2\n2 1" }, "output": { "stdout": "yes\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n2 0\n2\n1 0\n0 -1\n1 -1\n2\n1 0\n1 2\n1 1" }, "output": { "stdout": "no\nyes\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n0 -1\n2 0\n2\n2 0\n0 -1\n1 -2\n2\n1 1\n1 2\n1 1" }, "output": { "stdout": "no\nno\nyes\n" } }, { "input": { "stdin": "3\n2\n0 -1\n-1 -1\n2 0\n2\n1 0\n1 -1\n1 -1\n3\n1 0\n1 2\n1 0" }, "output": { "stdout": "no\nyes\nyes\n" } } ]
{ "tags": [], "title": "comm3" }
{ "cpp": null, "java": null, "python": "#!/usr/bin/python\nfrom math import sqrt\nN=input()\nfor i in range(N):\n\tR=input()\n\tx,y=map(int,raw_input().split())\n\tp,q=map(int,raw_input().split())\n\ta,b=map(int,raw_input().split())\n\tl=sqrt(((x-p)**2)+((y-q)**2))\n\tm=sqrt(((x-a)**2)+((y-b)**2))\n\tn=sqrt(((a-p)**2)+((b-q)**2))\n\t#print \"(%0.2f %0.2f)->(%0.2f %0.2f) = %0.2f \" %(x,y,p,q,l)\n\t#print \"(%0.2f %0.2f)->(%0.2f %0.2f) = %0.2f \" %(x,y,a,b,m)\n\t#print \"(%0.2f %0.2f)->(%0.2f %0.2f) = %0.2f \" %(p,q,a,b,n)\n\tcount=0\n\tif l>R:\n\t\tcount+=1\n\tif m>R:\n\t\tcount+=1\n\tif n>R:\n\t\tcount+=1\n\tif count>=2:\n\t\tprint \"no\"\n\telse:\n\t\tprint \"yes\"" }
CodeChef/gcd2
Frank explained its friend Felman the algorithm of Euclides to calculate the GCD of two numbers. Then Felman implements it algorithm int gcd(int a, int b) { if (b==0) return a; else return gcd(b,a%b); } and it proposes to Frank that makes it but with a little integer and another integer that has up to 250 digits. Your task is to help Frank programming an efficient code for the challenge of Felman. Input The first line of the input file contains a number representing the number of lines to follow. Each line consists of two number A and B (0 ≀ A ≀ 40000 and A ≀ B < 10^250). Output Print for each pair (A,B) in the input one integer representing the GCD of A and B. Example Input: 2 2 6 10 11 Output: 2 1
[ { "input": { "stdin": "2\n2 6\n10 11" }, "output": { "stdout": "2\n1\n" } }, { "input": { "stdin": "2\n0 23\n39 1" }, "output": { "stdout": "23\n1\n" } }, { "input": { "stdin": "2\n0 5\n1 17" }, "output": { "stdout": "5\n1\n" } }, { "input": { "stdin": "2\n5 3\n10 11" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "2\n0 1\n14 13" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "2\n3 8\n8 4" }, "output": { "stdout": "1\n4\n" } }, { "input": { "stdin": "2\n0 14\n16 1" }, "output": { "stdout": "14\n1\n" } }, { "input": { "stdin": "2\n2 3\n14 11" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "2\n0 20\n10 1" }, "output": { "stdout": "20\n1\n" } }, { "input": { "stdin": "2\n1 2\n0 12" }, "output": { "stdout": "1\n12\n" } }, { "input": { "stdin": "2\n2 6\n4 20" }, "output": { "stdout": "2\n4\n" } }, { "input": { "stdin": "2\n2 6\n5 2" }, "output": { "stdout": "2\n1\n" } }, { "input": { "stdin": "2\n1 3\n10 5" }, "output": { "stdout": "1\n5\n" } }, { "input": { "stdin": "2\n2 3\n10 20" }, "output": { "stdout": "1\n10\n" } }, { "input": { "stdin": "2\n0 6\n0 2" }, "output": { "stdout": "6\n2\n" } }, { "input": { "stdin": "2\n0 18\n7 1" }, "output": { "stdout": "18\n1\n" } }, { "input": { "stdin": "2\n3 6\n8 11" }, "output": { "stdout": "3\n1\n" } }, { "input": { "stdin": "2\n0 6\n12 20" }, "output": { "stdout": "6\n4\n" } }, { "input": { "stdin": "2\n0 10\n8 10" }, "output": { "stdout": "10\n2\n" } }, { "input": { "stdin": "2\n1 2\n0 20" }, "output": { "stdout": "1\n20\n" } }, { "input": { "stdin": "2\n0 10\n3 3" }, "output": { "stdout": "10\n3\n" } }, { "input": { "stdin": "2\n4 10\n8 6" }, "output": { "stdout": "2\n2\n" } }, { "input": { "stdin": "2\n3 6\n9 18" }, "output": { "stdout": "3\n9\n" } }, { "input": { "stdin": "2\n2 3\n10 11" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "2\n1 8\n0 9" }, "output": { "stdout": "1\n9\n" } }, { "input": { "stdin": "2\n0 12\n0 11" }, "output": { "stdout": "12\n11\n" } }, { "input": { "stdin": "2\n0 5\n8 6" }, "output": { "stdout": "5\n2\n" } }, { "input": { "stdin": "2\n0 2\n15 30" }, "output": { "stdout": "2\n15\n" } }, { "input": { "stdin": "2\n1 2\n14 7" }, "output": { "stdout": "1\n7\n" } }, { "input": { "stdin": "2\n0 8\n4 2" }, "output": { "stdout": "8\n2\n" } }, { "input": { "stdin": "2\n7 7\n16 16" }, "output": { "stdout": "7\n16\n" } }, { "input": { "stdin": "2\n0 2\n12 30" }, "output": { "stdout": "2\n6\n" } }, { "input": { "stdin": "2\n0 4\n10 5" }, "output": { "stdout": "4\n5\n" } }, { "input": { "stdin": "2\n0 4\n0 11" }, "output": { "stdout": "4\n11\n" } }, { "input": { "stdin": "2\n2 3\n0 25" }, "output": { "stdout": "1\n25\n" } }, { "input": { "stdin": "2\n7 14\n8 5" }, "output": { "stdout": "7\n1\n" } }, { "input": { "stdin": "2\n5 2\n10 2" }, "output": { "stdout": "1\n2\n" } }, { "input": { "stdin": "2\n3 6\n8 8" }, "output": { "stdout": "3\n8\n" } }, { "input": { "stdin": "2\n3 6\n9 15" }, "output": { "stdout": "3\n3\n" } }, { "input": { "stdin": "2\n0 6\n10 11" }, "output": { "stdout": "6\n1\n" } }, { "input": { "stdin": "2\n5 10\n14 14" }, "output": { "stdout": "5\n14\n" } }, { "input": { "stdin": "2\n0 52\n11 1" }, "output": { "stdout": "52\n1\n" } }, { "input": { "stdin": "2\n3 9\n0 15" }, "output": { "stdout": "3\n15\n" } }, { "input": { "stdin": "2\n5 3\n10 2" }, "output": { "stdout": "1\n2\n" } }, { "input": { "stdin": "2\n0 32\n12 1" }, "output": { "stdout": "32\n1\n" } }, { "input": { "stdin": "2\n5 2\n10 11" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "2\n0 6\n21 28" }, "output": { "stdout": "6\n7\n" } }, { "input": { "stdin": "2\n3 6\n6 6" }, "output": { "stdout": "3\n6\n" } }, { "input": { "stdin": "2\n0 8\n0 5" }, "output": { "stdout": "8\n5\n" } }, { "input": { "stdin": "2\n0 20\n12 2" }, "output": { "stdout": "20\n2\n" } } ]
{ "tags": [], "title": "gcd2" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\ndef gcd(a,b):\n\tif(b==0):\n\t\treturn a\n\telse:\n\t\treturn gcd(b,a%b);\nwhile(t):\n\tx = raw_input()\n\tx = x.split()\n\ta = int(x[0])\n\tb = int(x[1])\n\tprint gcd(a,b)\n\tt = t-1" }
CodeChef/luckybal
A Little Elephant from the Zoo of Lviv likes lucky strings, i.e., the strings that consist only of the lucky digits 4 and 7. The Little Elephant calls some string T of the length M balanced if there exists at least one integer X (1 ≀ X ≀ M) such that the number of digits 4 in the substring T[1, X - 1] is equal to the number of digits 7 in the substring T[X, M]. For example, the string S = 7477447 is balanced since S[1, 4] = 7477 has 1 digit 4 and S[5, 7] = 447 has 1 digit 7. On the other hand, one can verify that the string S = 7 is not balanced. The Little Elephant has the string S of the length N. He wants to know the number of such pairs of integers (L; R) that 1 ≀ L ≀ R ≀ N and the substring S[L, R] is balanced. Help him to find this number. Notes. Let S be some lucky string. Then |S| denotes the length of the string S; S[i] (1 ≀ i ≀ |S|) denotes the i^th character of S (the numeration of characters starts from 1); S[L, R] (1 ≀ L ≀ R ≀ |S|) denotes the string with the following sequence of characters: S[L], S[L + 1], ..., S[R], and is called a substring of S. For L > R we mean by S[L, R] an empty string. Input The first line of the input file contains a single integer T, the number of test cases. Each of the following T lines contains one string, the string S for the corresponding test case. The input file does not contain any whitespaces. Output For each test case output a single line containing the answer for this test case. Constraints 1 ≀ T ≀ 10 1 ≀ |S| ≀ 100000 S consists only of the lucky digits 4 and 7. Example Input: 4 47 74 477 4747477 Output: 2 2 3 23 Explanation In the first test case balance substrings are S[1, 1] = 4 and S[1, 2] = 47. In the second test case balance substrings are S[2, 2] = 4 and S[1, 2] = 74. Unfortunately, we can't provide you with the explanations of the third and the fourth test cases. You should figure it out by yourself. Please, don't ask about this in comments.
[ { "input": { "stdin": "4\n47\n74\n477\n4747477" }, "output": { "stdout": "2\n2\n3\n23\n" } }, { "input": { "stdin": "4\n7\n47\n477\n4747477" }, "output": { "stdout": "0\n2\n3\n23\n" } }, { "input": { "stdin": "4\n7\n74\n477\n4747477" }, "output": { "stdout": "0\n2\n3\n23\n" } }, { "input": { "stdin": "4\n4\n74\n477\n4747477" }, "output": { "stdout": "1\n2\n3\n23\n" } }, { "input": { "stdin": "4\n44\n74\n477\n4747477" }, "output": { "stdout": "3\n2\n3\n23\n" } }, { "input": { "stdin": "4\n47\n7\n477\n4747477" }, "output": { "stdout": "2\n0\n3\n23\n" } }, { "input": { "stdin": "4\n7\n77\n477\n4747477" }, "output": { "stdout": "0\n0\n3\n23\n" } }, { "input": { "stdin": "4\n7\n7\n447\n4747477" }, "output": { "stdout": "0\n0\n5\n23\n" } }, { "input": { "stdin": "4\n44\n44\n477\n4747477" }, "output": { "stdout": "3\n3\n3\n23\n" } }, { "input": { "stdin": "4\n47\n47\n477\n4747477" }, "output": { "stdout": "2\n2\n3\n23\n" } }, { "input": { "stdin": "4\n47\n4\n477\n4747477" }, "output": { "stdout": "2\n1\n3\n23\n" } }, { "input": { "stdin": "4\n7\n7\n477\n4747477" }, "output": { "stdout": "0\n0\n3\n23\n" } }, { "input": { "stdin": "4\n74\n47\n477\n4747477" }, "output": { "stdout": "2\n2\n3\n23\n" } }, { "input": { "stdin": "4\n47\n77\n477\n4747477" }, "output": { "stdout": "2\n0\n3\n23\n" } }, { "input": { "stdin": "4\n44\n47\n477\n4747477" }, "output": { "stdout": "3\n2\n3\n23\n" } }, { "input": { "stdin": "4\n7\n44\n7\n4747477" }, "output": { "stdout": "0\n3\n0\n23\n" } }, { "input": { "stdin": "4\n7\n44\n477\n4747477" }, "output": { "stdout": "0\n3\n3\n23\n" } }, { "input": { "stdin": "4\n47\n44\n477\n4747477" }, "output": { "stdout": "2\n3\n3\n23\n" } }, { "input": { "stdin": "4\n4\n44\n477\n4747477" }, "output": { "stdout": "1\n3\n3\n23\n" } }, { "input": { "stdin": "4\n4\n47\n477\n4747477" }, "output": { "stdout": "1\n2\n3\n23\n" } } ]
{ "tags": [], "title": "luckybal" }
{ "cpp": null, "java": null, "python": "for i in range(input()):\n s=raw_input()\n pos=-1\n ans=0\n for j in range(len(s)):\n if s[j]=='4':\n pos=j\n if pos!=-1:\n ans+=pos+1\n print ans" }
CodeChef/prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
[ { "input": { "stdin": "4\naaa\nabc\nabdbca\nabba" }, "output": { "stdout": "YES\nNO\nYES\nYES\n" } }, { "input": { "stdin": "4\n`a_\naa`\nc^`add\nca^a" }, "output": { "stdout": "NO\nYES\nNO\nYES\n" } }, { "input": { "stdin": "4\n`a_\naa`\ndda`^c\ncb^a" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n_b`\na``\ncd``^c\nb^`c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n^ba\naa`\ncbc__b\n`cab" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\naaa\nabc\nabdbca\naaca" }, "output": { "stdout": "YES\nNO\nYES\nYES\n" } }, { "input": { "stdin": "4\naab\nacc\n`cd`bb\naaad" }, "output": { "stdout": "YES\nYES\nNO\nYES\n" } }, { "input": { "stdin": "4\n_b`\na``\ncd``^c\nb^_c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n_ba\n`aa\nbb_c_c\n`dab" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nab_\n`aa\nbb_c_c\nbad`" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\nabc\nabd`cb\naaca" }, "output": { "stdout": "YES\nNO\nNO\nYES\n" } }, { "input": { "stdin": "4\naa_\n`aa\nbb_c_c\nabd`" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\naab\nacc\n`bd`bb\naaad" }, "output": { "stdout": "YES\nYES\nNO\nYES\n" } }, { "input": { "stdin": "4\nab_\n`aa\nbb_c_c\nabd`" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\naa^\n`a`\ndbc`_c\nc^aa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\ne`_\n_\\^\nd_`c_d\nbc^_" }, "output": { "stdout": "NO\nNO\nYES\nNO\n" } }, { "input": { "stdin": "4\naab\nacc\n`bd`cb\naaad" }, "output": { "stdout": "YES\nYES\nNO\nYES\n" } }, { "input": { "stdin": "4\nbaa\na`a\nbb`c`c\ndba`" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\naa`\ndda`^c\naa^c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\naa^\n`aa\ncbc__c\n^caa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\naa_\n`a`\ndbc`_c\nc^aa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n^aa\naa`\ncbc__b\n_cab" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\nabc\nbb`db`\naabd" }, "output": { "stdout": "YES\nNO\nNO\nNO\n" } }, { "input": { "stdin": "4\n_a`\na``\ncda`^c\n`^bc" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\nabc\n`bd`cb\nacaa" }, "output": { "stdout": "YES\nNO\nNO\nYES\n" } }, { "input": { "stdin": "4\naa^\n`a`\ncbc`_c\nc^aa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\n`a`\ndad`^c\naa^c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\na`a\nbb`c_c\ndba`" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\n`aa\nbb`cb`\ndba`" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\nabc\n`bd`cb\nadaa" }, "output": { "stdout": "YES\nNO\nNO\nYES\n" } }, { "input": { "stdin": "4\naab\nabc\n`bdbc`\naaca" }, "output": { "stdout": "YES\nNO\nYES\nYES\n" } }, { "input": { "stdin": "4\naa^\n`aa\ncbc`_c\n^caa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\na``\ncd``^c\n`^bc" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\n``a\ncda`^c\n`^bc" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\n`a`\nc_`dad\nc^aa" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\na``\ndda`^c\na^bc" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\na``\ndda`^c\naa^c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n^aa\naa`\ncbc__c\n^caa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\naaa\ncb`cb`\naabd" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`b_\na``\ncd``^c\nb^`c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\na``\ncd``^c\nb^`c" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\naaa\nabc\nabdbc`\naaca" }, "output": { "stdout": "YES\nNO\nNO\nYES\n" } }, { "input": { "stdin": "4\naa^\n``a\ncbc`_c\n^caa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n^ba\naa`\ncbc__b\n`dab" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n`a_\n`a`\nc_`cad\nc^aa" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n_ba\naa`\ncbc__b\n`dab" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbab\naba\ncb`cb`\naabd" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nba`\n`aa\nbb`c_c\ndba`" }, "output": { "stdout": "NO\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\n^aa\naa`\ncbc__c\n_caa" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } }, { "input": { "stdin": "4\nbaa\naaa\nbb`cb`\ndba`" }, "output": { "stdout": "YES\nYES\nNO\nNO\n" } } ]
{ "tags": [], "title": "prpaln" }
{ "cpp": null, "java": null, "python": "import math\nimport sys\ndef checkpal(s):\n return s==s[::-1]\nfor a in range(input()):\n s=raw_input()\n l=len(s)\n if(l==2):\n print \"YES\"\n else:\n if checkpal(s):\n print \"YES\"\n else:\n while s[0] == s[-1] and len(s)>2:\n\t s=s[1:-1]\n\t if checkpal(s[1:]) or checkpal(s[:-1]):\n\t\tprint \"YES\"\n else:\n\t\tprint \"NO\"" }
CodeChef/tf01
An established group of scientists are working on finding solution to NP hard problems. They claim Subset Sum as an NP-hard problem. The problem is to determine whether there exists a subset of a given set S whose sum is a given number K. You are a computer engineer and you claim to solve this problem given that all numbers in the set are non-negative. Given a set S of size N of non-negative integers, find whether there exists a subset whose sum is K. Input First line of input contains T, the number of test cases. T test cases follow. Each test case contains 2 lines. First line contains two integers N and K. Next line contains N space separated non-negative integers (each less than 100000). 0 < T < 1000 0 < N < 1000 0 < K < 1000 Output Output T lines, one for each test case. Every line should be either 0 or 1 depending on whether such a subset exists or not. Example Input: 2 5 10 3 4 6 1 9 3 2 1 3 4 Output: 1 0
[ { "input": { "stdin": "2\n5 10\n3 4 6 1 9\n3 2\n1 3 4" }, "output": { "stdout": "1\n0" } } ]
{ "tags": [], "title": "tf01" }
{ "cpp": null, "java": null, "python": "import sys\n\nfor __ in range(input()) :\n n , k = map(int,sys.stdin.readline().split())\n lists = map(int,sys.stdin.readline().split())\n dp = [0]*(k+1)\n dp[0]=1\n for i in lists :\n for j in range(k-i,-1,-1) :\n if dp[k] :\n break\n if dp[j] :\n dp[j+i] = 1\n print dp[k]" }
Codeforces/1012/E
# Cycle sort You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≀ i_j ≀ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 β†’ i_2 β†’ … i_k β†’ i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≀ n ≀ 200 000, 0 ≀ s ≀ 200 000)β€”the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_nβ€”elements of the array (1 ≀ a_i ≀ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number qβ€” the minimum number of operations required to sort the array. On the next 2 β‹… q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≀ k ≀ n)β€”the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≀ i_j ≀ n)β€”the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 β†’ 4 β†’ 1 (of length 2), then apply the cycle 2 β†’ 3 β†’ 5 β†’ 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 β†’ 2 β†’ 1 and 3 β†’ 4 β†’ 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
[ { "input": { "stdin": "5 5\n3 2 3 1 1\n" }, "output": { "stdout": "1\n5\n1 4 2 3 5 \n" } }, { "input": { "stdin": "4 3\n2 1 4 3\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "2 0\n2 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 0\n884430748 884430748 708433020 708433020 708433020\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "2 1\n1 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "2 0\n2 1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 2\n65390026 770505072 65390026 65390026 65390026\n" }, "output": { "stdout": "1\n2\n2 5 \n" } }, { "input": { "stdin": "5 4\n812067558 674124159 106041640 106041640 674124159\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 4\n167616600 574805150 651016425 150949603 379708534\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 5\n472778319 561757623 989296065 99763286 352037329\n" }, "output": { "stdout": "1\n5\n1 3 5 2 4 \n" } }, { "input": { "stdin": "5 4\n201429826 845081337 219611799 598937628 680006294\n" }, "output": { "stdout": "1\n4\n2 5 4 3 \n" } }, { "input": { "stdin": "2 2\n2 1\n" }, "output": { "stdout": "1\n2\n1 2 \n" } }, { "input": { "stdin": "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 6\n971458729 608568364 891718769 464295315 98863653\n" }, "output": { "stdout": "2\n2\n1 5\n3\n2 3 4\n" } }, { "input": { "stdin": "5 5\n641494999 641494999 228574099 535883079 535883079\n" }, "output": { "stdout": "1\n5\n1 4 2 5 3 \n" } }, { "input": { "stdin": "5 5\n815605413 4894095 624809427 264202135 152952491\n" }, "output": { "stdout": "2\n3\n1 5 2\n2\n3 4\n" } }, { "input": { "stdin": "1 0\n258769137\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "2 0\n1 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "1 0\n2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 4\n335381650 691981363 691981363 335381650 335381650\n" }, "output": { "stdout": "1\n4\n2 4 3 5 \n" } }, { "input": { "stdin": "5 4\n81224924 319704343 319704343 210445208 128525140\n" }, "output": { "stdout": "1\n4\n2 4 3 5 \n" } }, { "input": { "stdin": "5 4\n579487081 564229995 665920667 665920667 644707366\n" }, "output": { "stdout": "2\n2\n1 2\n2\n3 5\n" } }, { "input": { "stdin": "5 200000\n682659092 302185582 518778252 29821187 14969298\n" }, "output": { "stdout": "2\n2\n1 5\n3\n2 3 4\n" } }, { "input": { "stdin": "5 2\n539341452 4894095 624809427 264202135 209695341\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 4\n201429826 845081337 219611799 598937628 1811343985\n" }, "output": { "stdout": "1\n3\n2 4 3 \n" } }, { "input": { "stdin": "5 -1\n884430748 884430748 267312749 708433020 708433020\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "4 3\n2 2 6 3\n" }, "output": { "stdout": "1\n2\n3 4 \n" } }, { "input": { "stdin": "1 2\n6 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 5\n539341452 4894095 624809427 264202135 152952491\n" }, "output": { "stdout": "1\n5\n1 4 3 5 2 \n" } }, { "input": { "stdin": "5 200000\n682659092 302185582 185982759 29821187 14969298\n" }, "output": { "stdout": "2\n2\n1 5 \n2\n2 4 \n" } }, { "input": { "stdin": "5 2\n65390026 1454694739 65390026 65390026 100733760\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 1\n641494999 641494999 228574099 535883079 535883079\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 200000\n682659092 302185582 185982759 20056766 14969298\n" }, "output": { "stdout": "2\n2\n1 5 \n2\n2 4 \n" } }, { "input": { "stdin": "5 3\n971458729 608568364 891718769 268781113 98863653\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 4\n579487081 564229995 288357375 665920667 644707366\n" }, "output": { "stdout": "2\n2\n1 3 \n2\n4 5 \n" } }, { "input": { "stdin": "5 4\n335381650 691981363 484551438 335381650 201569796\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "4 3\n2 1 6 3\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 4\n167616600 574805150 651016425 232181430 379708534\n" }, "output": { "stdout": "2\n2\n2 4 \n2\n3 5 \n" } }, { "input": { "stdin": "1 0\n483419747\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 1\n81224924 319704343 319704343 210445208 128525140\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 2\n539341452 4894095 624809427 264202135 152952491\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "0 0\n2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "1 1\n682471575\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 2\n501175 1454694739 65390026 65390026 100733760\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 4\n579487081 92588299 288357375 946230441 644707366\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "5 6\n335381650 691981363 484551438 335381650 201569796\n" }, "output": { "stdout": "1\n5\n1 3 4 2 5 \n" } }, { "input": { "stdin": "1 2\n4 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 4\n579487081 564229995 288357375 946230441 644707366\n" }, "output": { "stdout": "2\n2\n1 3 \n2\n4 5 \n" } }, { "input": { "stdin": "1 1\n483419747\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 4\n201429826 845081337 219611799 598937628 1013339305\n" }, "output": { "stdout": "1\n3\n2 4 3 \n" } } ]
{ "tags": [ "dsu", "math" ], "title": "Cycle sort" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vb = vector<bool>;\nusing vd = vector<double>;\nusing vs = vector<string>;\nusing pii = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pdd = pair<double, double>;\nusing vpii = vector<pii>;\nusing vvpii = vector<vpii>;\nusing vpll = vector<pll>;\nusing vvpll = vector<vpll>;\nusing vpdd = vector<pdd>;\nusing vvpdd = vector<vpdd>;\ntemplate <typename T>\nvoid ckmin(T& a, const T& b) {\n a = min(a, b);\n}\ntemplate <typename T>\nvoid ckmax(T& a, const T& b) {\n a = max(a, b);\n}\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nnamespace __input {\ntemplate <class T1, class T2>\nvoid re(pair<T1, T2>& p);\ntemplate <class T>\nvoid re(vector<T>& a);\ntemplate <class T, size_t SZ>\nvoid re(array<T, SZ>& a);\ntemplate <class T>\nvoid re(T& x) {\n cin >> x;\n}\nvoid re(double& x) {\n string t;\n re(t);\n x = stod(t);\n}\ntemplate <class Arg, class... Args>\nvoid re(Arg& first, Args&... rest) {\n re(first);\n re(rest...);\n}\ntemplate <class T1, class T2>\nvoid re(pair<T1, T2>& p) {\n re(p.first, p.second);\n}\ntemplate <class T>\nvoid re(vector<T>& a) {\n for (int i = 0; i < (int((a).size())); i++) re(a[i]);\n}\ntemplate <class T, size_t SZ>\nvoid re(array<T, SZ>& a) {\n for (int i = 0; i < (SZ); i++) re(a[i]);\n}\n} // namespace __input\nusing namespace __input;\nnamespace __output {\ntemplate <class T1, class T2>\nvoid pr(const pair<T1, T2>& x);\ntemplate <class T, size_t SZ>\nvoid pr(const array<T, SZ>& x);\ntemplate <class T>\nvoid pr(const vector<T>& x);\ntemplate <class T>\nvoid pr(const deque<T>& x);\ntemplate <class T>\nvoid pr(const set<T>& x);\ntemplate <class T1, class T2>\nvoid pr(const map<T1, T2>& x);\ntemplate <class T>\nvoid pr(const T& x) {\n cout << x;\n}\ntemplate <class Arg, class... Args>\nvoid pr(const Arg& first, const Args&... rest) {\n pr(first);\n pr(rest...);\n}\ntemplate <class T1, class T2>\nvoid pr(const pair<T1, T2>& x) {\n pr(\"{\", x.first, \", \", x.second, \"}\");\n}\ntemplate <class T, bool pretty = true>\nvoid prContain(const T& x) {\n if (pretty) pr(\"{\");\n bool fst = 1;\n for (const auto& a : x) pr(!fst ? pretty ? \", \" : \" \" : \"\", a), fst = 0;\n if (pretty) pr(\"}\");\n}\ntemplate <class T>\nvoid pc(const T& x) {\n prContain<T, false>(x);\n pr(\"\\n\");\n}\ntemplate <class T, size_t SZ>\nvoid pr(const array<T, SZ>& x) {\n prContain(x);\n}\ntemplate <class T>\nvoid pr(const vector<T>& x) {\n prContain(x);\n}\ntemplate <class T>\nvoid pr(const deque<T>& x) {\n prContain(x);\n}\ntemplate <class T>\nvoid pr(const set<T>& x) {\n prContain(x);\n}\ntemplate <class T1, class T2>\nvoid pr(const map<T1, T2>& x) {\n prContain(x);\n}\nvoid ps() { pr(\"\\n\"); }\ntemplate <class Arg>\nvoid ps(const Arg& first) {\n pr(first);\n ps();\n}\ntemplate <class Arg, class... Args>\nvoid ps(const Arg& first, const Args&... rest) {\n pr(first, \" \");\n ps(rest...);\n}\n} // namespace __output\nusing namespace __output;\nnamespace __algorithm {\ntemplate <typename T>\nvoid dedup(vector<T>& v) {\n sort((v).begin(), (v).end());\n v.erase(unique((v).begin(), (v).end()), v.end());\n}\ntemplate <typename T>\ntypename vector<T>::iterator find(vector<T>& v, const T& x) {\n auto it = lower_bound((v).begin(), (v).end(), x);\n return it != v.end() && *it == x ? it : v.end();\n}\ntemplate <typename T>\nsize_t index(vector<T>& v, const T& x) {\n auto it = find(v, x);\n assert(it != v.end() && *it == x);\n return it - v.begin();\n}\ntemplate <typename C, typename T, typename OP>\nvector<T> prefixes(const C& v, T id, OP op) {\n vector<T> r(int((v).size()) + 1, id);\n for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]);\n return r;\n}\ntemplate <typename C, typename T, typename OP>\nvector<T> suffixes(const C& v, T id, OP op) {\n vector<T> r(int((v).size()) + 1, id);\n for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]);\n return r;\n}\n} // namespace __algorithm\nusing namespace __algorithm;\nstruct monostate {\n friend istream& operator>>(istream& is,\n const __attribute__((unused)) monostate& ms) {\n return is;\n }\n friend ostream& operator<<(ostream& os,\n const __attribute__((unused)) monostate& ms) {\n return os;\n }\n} ms;\ntemplate <typename W = monostate>\nstruct wedge {\n int u, v, i;\n W w;\n wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {}\n int operator[](int loc) const { return u ^ v ^ loc; }\n friend void re(wedge& e) {\n re(e.u, e.v, e.w);\n --e.u, --e.v;\n }\n friend void pr(const wedge& e) { pr(e.u, \"<-\", e.w, \"->\", e.v); }\n};\nnamespace __io {\nvoid setIn(string second) { freopen(second.c_str(), \"r\", stdin); }\nvoid setOut(string second) { freopen(second.c_str(), \"w\", stdout); }\nvoid setIO(string second = \"\") {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.precision(15);\n if (int((second).size())) {\n setIn(second + \".in\"), setOut(second + \".out\");\n }\n}\n} // namespace __io\nusing namespace __io;\nstruct uf_monostate {\n uf_monostate(__attribute__((unused)) int id) {}\n void merge(__attribute__((unused)) uf_monostate& o,\n __attribute__((unused)) const monostate& e) {}\n};\ntemplate <typename T = uf_monostate, typename E = monostate>\nstruct union_find {\n struct node {\n int par, rnk, size;\n T state;\n node(int id = 0) : par(id), rnk(0), size(1), state(id) {}\n void merge(node& o, E& e) {\n if (rnk == o.rnk) rnk++;\n if (size < o.size) swap(state, o.state);\n size += o.size;\n state.merge(o.state, e);\n }\n };\n int cc;\n vector<node> uf;\n union_find(int N = 0) : uf(N), cc(N) {\n for (int i = 0; i < N; i++) uf[i] = node(i);\n }\n int rep(int i) {\n if (i != uf[i].par) uf[i].par = rep(uf[i].par);\n return uf[i].par;\n }\n bool unio(int a, int b, E& e = ms) {\n a = rep(a), b = rep(b);\n if (a == b) return false;\n if (uf[a].rnk < uf[b].rnk) swap(a, b);\n uf[a].merge(uf[b], e);\n uf[b].par = a;\n cc--;\n return true;\n }\n T& state(int i) { return uf[rep(i)].state; }\n};\nint main() {\n setIO();\n int N, S;\n re(N, S);\n vi a(N);\n re(a);\n vi ti(N);\n vi st = a, did(N);\n sort((st).begin(), (st).end());\n vvi occ(N);\n for (int i = 0; i < (N); i++)\n if (a[i] != st[i]) {\n int w = index(st, a[i]);\n while (a[w + did[w]] == st[w + did[w]]) did[w]++;\n ti[i] = w + did[w]++;\n occ[w].push_back(i);\n } else\n ti[i] = i;\n vb vis(N);\n union_find<> uf(N);\n for (int i = 0; i < (N); i++)\n if (!vis[i]) {\n vis[i] = true;\n for (int t = ti[i]; t != i; t = ti[t]) {\n uf.unio(i, t);\n vis[t] = true;\n }\n }\n for (int i = 0; i < (N); i++)\n for (int j = 0; j < (int((occ[i]).size()) - 1); j++) {\n if (uf.unio(occ[i][j], occ[i][j + 1])) {\n swap(ti[occ[i][j]], ti[occ[i][j + 1]]);\n }\n }\n int wr = 0;\n for (int i = 0; i < (N); i++)\n if (a[i] != st[i]) wr++;\n if (wr > S) {\n ps(-1);\n return 0;\n }\n if (a == st) {\n ps(0);\n return 0;\n }\n vvi cyc;\n for (int i = 0; i < (N); i++)\n if (i == uf.rep(i) && i != ti[i]) {\n cyc.push_back({i + 1});\n for (int t = ti[i]; t != i; t = ti[t]) cyc.back().push_back(t + 1);\n }\n int merge = min(S - wr, int((cyc).size()));\n if (merge > 2) {\n vi loop, fix;\n for (int c = (int((cyc).size()) - merge); c < (int((cyc).size())); c++) {\n loop.insert(loop.end(), (cyc[c]).begin(), (cyc[c]).end());\n fix.push_back(cyc[c].front());\n }\n reverse((fix).begin(), (fix).end());\n cyc.erase(cyc.end() - merge, cyc.end());\n cyc.push_back(loop);\n cyc.push_back(fix);\n }\n ps(int((cyc).size()));\n for (auto& c : cyc) ps(int((c).size())), pc(c);\n return 0;\n}\n", "java": "import java.io.OutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.util.Arrays;\nimport java.io.IOException;\nimport java.util.Random;\nimport java.util.ArrayList;\nimport java.io.UncheckedIOException;\nimport java.util.List;\nimport java.io.Closeable;\nimport java.io.Writer;\nimport java.io.OutputStreamWriter;\nimport java.io.InputStream;\n\n/**\n * Built using CHelper plug-in\n * Actual solution is at the top\n */\npublic class Main {\n public static void main(String[] args) throws Exception {\n Thread thread = new Thread(null, new TaskAdapter(), \"\", 1 << 27);\n thread.start();\n thread.join();\n }\n\n static class TaskAdapter implements Runnable {\n @Override\n public void run() {\n InputStream inputStream = System.in;\n OutputStream outputStream = System.out;\n FastInput in = new FastInput(inputStream);\n FastOutput out = new FastOutput(outputStream);\n ECycleSort solver = new ECycleSort();\n solver.solve(1, in, out);\n out.close();\n }\n }\n\n static class ECycleSort {\n int n;\n int[] a;\n\n public void solve(int testNumber, FastInput in, FastOutput out) {\n n = in.readInt();\n int s = in.readInt();\n a = new int[n];\n for (int i = 0; i < n; i++) {\n a[i] = in.readInt();\n }\n int[] b = a.clone();\n Randomized.shuffle(b);\n Arrays.sort(b);\n int[] same = new int[n];\n int sum = 0;\n for (int i = 0; i < n; i++) {\n if (a[i] == b[i]) {\n same[i] = 1;\n }\n }\n for (int x : same) {\n sum += x;\n }\n if (n - sum > s) {\n out.println(-1);\n return;\n }\n IntegerList permList = new IntegerList(n);\n for (int i = 0; i < n; i++) {\n if (same[i] == 0) {\n permList.add(i);\n }\n }\n int[] perm = permList.toArray();\n CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length);\n DSU dsu = new DSU(n);\n for (int i = 0; i < perm.length; i++) {\n int from = perm[i];\n int to = permList.get(i);\n dsu.merge(from, to);\n }\n for (int i = 1; i < perm.length; i++) {\n if (a[perm[i]] != a[perm[i - 1]]) {\n continue;\n }\n if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) {\n continue;\n }\n dsu.merge(perm[i], perm[i - 1]);\n SequenceUtils.swap(perm, i, i - 1);\n }\n IntegerList first = new IntegerList();\n if (perm.length > 0) {\n int remain = s - (n - sum) - 1;\n first.add(perm[0]);\n for (int i = 1; remain > 0 && i < perm.length; i++) {\n if (dsu.find(perm[i - 1]) != dsu.find(perm[i])) {\n remain--;\n first.add(perm[i]);\n dsu.merge(perm[i - 1], perm[i]);\n }\n }\n\n int last = a[first.get(0)];\n for (int i = 1; i < first.size(); i++) {\n int y = first.get(i);\n int tmp = a[y];\n a[y] = last;\n last = tmp;\n }\n a[first.get(0)] = last;\n //System.err.println(Arrays.toString(a));\n }\n\n List<IntegerList> circles = new ArrayList<>();\n if (first.size() > 1) {\n circles.add(first);\n }\n circles.addAll(solve());\n out.println(circles.size());\n for (IntegerList list : circles) {\n out.println(list.size());\n for (int i = 0; i < list.size(); i++) {\n out.append(list.get(i) + 1).append(' ');\n }\n out.println();\n }\n }\n\n public List<IntegerList> solve() {\n int[] b = a.clone();\n Randomized.shuffle(b);\n Arrays.sort(b);\n int[] same = new int[n];\n for (int i = 0; i < n; i++) {\n if (a[i] == b[i]) {\n same[i] = 1;\n }\n }\n IntegerList permList = new IntegerList(n);\n for (int i = 0; i < n; i++) {\n if (same[i] == 0) {\n permList.add(i);\n }\n }\n int[] perm = permList.toArray();\n CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length);\n DSU dsu = new DSU(n);\n for (int i = 0; i < perm.length; i++) {\n int from = perm[i];\n int to = permList.get(i);\n dsu.merge(from, to);\n }\n for (int i = 1; i < perm.length; i++) {\n if (a[perm[i]] != a[perm[i - 1]]) {\n continue;\n }\n if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) {\n continue;\n }\n dsu.merge(perm[i], perm[i - 1]);\n SequenceUtils.swap(perm, i, i - 1);\n }\n\n int[] index = new int[n];\n for (int i = 0; i < n; i++) {\n if (same[i] == 1) {\n index[i] = i;\n }\n }\n for (int i = 0; i < perm.length; i++) {\n index[perm[i]] = permList.get(i);\n }\n PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index);\n List<IntegerList> circles = pp.extractCircles(2);\n\n return circles;\n }\n\n }\n\n static class Randomized {\n private static Random random = new Random(0);\n\n public static void shuffle(int[] data) {\n shuffle(data, 0, data.length - 1);\n }\n\n public static void shuffle(int[] data, int from, int to) {\n to--;\n for (int i = from; i <= to; i++) {\n int s = nextInt(i, to);\n int tmp = data[i];\n data[i] = data[s];\n data[s] = tmp;\n }\n }\n\n public static int nextInt(int l, int r) {\n return random.nextInt(r - l + 1) + l;\n }\n\n }\n\n static class SequenceUtils {\n public static void swap(int[] data, int i, int j) {\n int tmp = data[i];\n data[i] = data[j];\n data[j] = tmp;\n }\n\n public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {\n if ((ar - al) != (br - bl)) {\n return false;\n }\n for (int i = al, j = bl; i <= ar; i++, j++) {\n if (a[i] != b[j]) {\n return false;\n }\n }\n return true;\n }\n\n }\n\n static class FastInput {\n private final InputStream is;\n private byte[] buf = new byte[1 << 20];\n private int bufLen;\n private int bufOffset;\n private int next;\n\n public FastInput(InputStream is) {\n this.is = is;\n }\n\n private int read() {\n while (bufLen == bufOffset) {\n bufOffset = 0;\n try {\n bufLen = is.read(buf);\n } catch (IOException e) {\n bufLen = -1;\n }\n if (bufLen == -1) {\n return -1;\n }\n }\n return buf[bufOffset++];\n }\n\n public void skipBlank() {\n while (next >= 0 && next <= 32) {\n next = read();\n }\n }\n\n public int readInt() {\n int sign = 1;\n\n skipBlank();\n if (next == '+' || next == '-') {\n sign = next == '+' ? 1 : -1;\n next = read();\n }\n\n int val = 0;\n if (sign == 1) {\n while (next >= '0' && next <= '9') {\n val = val * 10 + next - '0';\n next = read();\n }\n } else {\n while (next >= '0' && next <= '9') {\n val = val * 10 - next + '0';\n next = read();\n }\n }\n\n return val;\n }\n\n }\n\n static class FastOutput implements AutoCloseable, Closeable, Appendable {\n private StringBuilder cache = new StringBuilder(10 << 20);\n private final Writer os;\n\n public FastOutput append(CharSequence csq) {\n cache.append(csq);\n return this;\n }\n\n public FastOutput append(CharSequence csq, int start, int end) {\n cache.append(csq, start, end);\n return this;\n }\n\n public FastOutput(Writer os) {\n this.os = os;\n }\n\n public FastOutput(OutputStream os) {\n this(new OutputStreamWriter(os));\n }\n\n public FastOutput append(char c) {\n cache.append(c);\n return this;\n }\n\n public FastOutput append(int c) {\n cache.append(c);\n return this;\n }\n\n public FastOutput println(int c) {\n cache.append(c);\n println();\n return this;\n }\n\n public FastOutput println() {\n cache.append(System.lineSeparator());\n return this;\n }\n\n public FastOutput flush() {\n try {\n os.append(cache);\n os.flush();\n cache.setLength(0);\n } catch (IOException e) {\n throw new UncheckedIOException(e);\n }\n return this;\n }\n\n public void close() {\n flush();\n try {\n os.close();\n } catch (IOException e) {\n throw new UncheckedIOException(e);\n }\n }\n\n public String toString() {\n return cache.toString();\n }\n\n }\n\n static class IntegerList implements Cloneable {\n private int size;\n private int cap;\n private int[] data;\n private static final int[] EMPTY = new int[0];\n\n public IntegerList(int cap) {\n this.cap = cap;\n if (cap == 0) {\n data = EMPTY;\n } else {\n data = new int[cap];\n }\n }\n\n public IntegerList(IntegerList list) {\n this.size = list.size;\n this.cap = list.cap;\n this.data = Arrays.copyOf(list.data, size);\n }\n\n public IntegerList() {\n this(0);\n }\n\n public void ensureSpace(int req) {\n if (req > cap) {\n while (cap < req) {\n cap = Math.max(cap + 10, 2 * cap);\n }\n data = Arrays.copyOf(data, cap);\n }\n }\n\n private void checkRange(int i) {\n if (i < 0 || i >= size) {\n throw new ArrayIndexOutOfBoundsException();\n }\n }\n\n public int get(int i) {\n checkRange(i);\n return data[i];\n }\n\n public void add(int x) {\n ensureSpace(size + 1);\n data[size++] = x;\n }\n\n public void addAll(int[] x, int offset, int len) {\n ensureSpace(size + len);\n System.arraycopy(x, offset, data, size, len);\n size += len;\n }\n\n public void addAll(IntegerList list) {\n addAll(list.data, 0, list.size);\n }\n\n public int size() {\n return size;\n }\n\n public int[] toArray() {\n return Arrays.copyOf(data, size);\n }\n\n public String toString() {\n return Arrays.toString(toArray());\n }\n\n public boolean equals(Object obj) {\n if (!(obj instanceof IntegerList)) {\n return false;\n }\n IntegerList other = (IntegerList) obj;\n return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);\n }\n\n public int hashCode() {\n int h = 1;\n for (int i = 0; i < size; i++) {\n h = h * 31 + Integer.hashCode(data[i]);\n }\n return h;\n }\n\n public IntegerList clone() {\n IntegerList ans = new IntegerList(size);\n ans.addAll(this);\n return ans;\n }\n\n }\n\n static class DSU {\n int[] p;\n int[] rank;\n\n public DSU(int n) {\n p = new int[n];\n rank = new int[n];\n reset();\n }\n\n public void reset() {\n for (int i = 0; i < p.length; i++) {\n p[i] = i;\n rank[i] = 0;\n }\n }\n\n public int find(int a) {\n return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a]));\n }\n\n public void merge(int a, int b) {\n a = find(a);\n b = find(b);\n if (a == b) {\n return;\n }\n if (rank[a] == rank[b]) {\n rank[a]++;\n }\n if (rank[a] > rank[b]) {\n p[b] = a;\n } else {\n p[a] = b;\n }\n }\n\n }\n\n static interface IntComparator {\n public int compare(int a, int b);\n\n }\n\n static class PermutationUtils {\n private static final long[] PERMUTATION_CNT = new long[21];\n\n static {\n PERMUTATION_CNT[0] = 1;\n for (int i = 1; i <= 20; i++) {\n PERMUTATION_CNT[i] = PERMUTATION_CNT[i - 1] * i;\n }\n }\n\n public static class PowerPermutation {\n int[] g;\n int[] idx;\n int[] l;\n int[] r;\n int n;\n\n public List<IntegerList> extractCircles(int threshold) {\n List<IntegerList> ans = new ArrayList<>(n);\n for (int i = 0; i < n; i = r[i] + 1) {\n int size = r[i] - l[i] + 1;\n if (size < threshold) {\n continue;\n }\n IntegerList list = new IntegerList(r[i] - l[i] + 1);\n for (int j = l[i]; j <= r[i]; j++) {\n list.add(g[j]);\n }\n ans.add(list);\n }\n return ans;\n }\n\n public PowerPermutation(int[] p) {\n this(p, p.length);\n }\n\n public PowerPermutation(int[] p, int len) {\n n = len;\n boolean[] visit = new boolean[n];\n g = new int[n];\n l = new int[n];\n r = new int[n];\n idx = new int[n];\n int wpos = 0;\n for (int i = 0; i < n; i++) {\n int val = p[i];\n if (visit[val]) {\n continue;\n }\n visit[val] = true;\n g[wpos] = val;\n l[wpos] = wpos;\n idx[val] = wpos;\n wpos++;\n while (true) {\n int x = p[g[wpos - 1]];\n if (visit[x]) {\n break;\n }\n visit[x] = true;\n g[wpos] = x;\n l[wpos] = l[wpos - 1];\n idx[x] = wpos;\n wpos++;\n }\n for (int j = l[wpos - 1]; j < wpos; j++) {\n r[j] = wpos - 1;\n }\n }\n }\n\n public int apply(int x, int p) {\n int i = idx[x];\n int dist = DigitUtils.mod((i - l[i]) + p, r[i] - l[i] + 1);\n return g[dist + l[i]];\n }\n\n public String toString() {\n StringBuilder builder = new StringBuilder();\n for (int i = 0; i < n; i++) {\n builder.append(apply(i, 1)).append(' ');\n }\n return builder.toString();\n }\n\n }\n\n }\n\n static class CompareUtils {\n private static final int THRESHOLD = 4;\n\n private CompareUtils() {\n }\n\n public static <T> void insertSort(int[] data, IntComparator cmp, int l, int r) {\n for (int i = l + 1; i <= r; i++) {\n int j = i;\n int val = data[i];\n while (j > l && cmp.compare(data[j - 1], val) > 0) {\n data[j] = data[j - 1];\n j--;\n }\n data[j] = val;\n }\n }\n\n public static void quickSort(int[] data, IntComparator cmp, int f, int t) {\n if (t - f <= THRESHOLD) {\n insertSort(data, cmp, f, t - 1);\n return;\n }\n SequenceUtils.swap(data, f, Randomized.nextInt(f, t - 1));\n int l = f;\n int r = t;\n int m = l + 1;\n while (m < r) {\n int c = cmp.compare(data[m], data[l]);\n if (c == 0) {\n m++;\n } else if (c < 0) {\n SequenceUtils.swap(data, l, m);\n l++;\n m++;\n } else {\n SequenceUtils.swap(data, m, --r);\n }\n }\n quickSort(data, cmp, f, l);\n quickSort(data, cmp, m, t);\n }\n\n }\n\n static class DigitUtils {\n private DigitUtils() {\n }\n\n public static int mod(int x, int mod) {\n x %= mod;\n if (x < 0) {\n x += mod;\n }\n return x;\n }\n\n }\n}\n\n", "python": null }
Codeforces/1037/E
# Trips There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends. We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold: * Either this person does not go on the trip, * Or at least k of his friends also go on the trip. Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends. For each day, find the maximum number of people that can go on the trip on that day. Input The first line contains three integers n, m, and k (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ m ≀ 2 β‹… 10^5, 1 ≀ k < n) β€” the number of people, the number of days and the number of friends each person on the trip should have in the group. The i-th (1 ≀ i ≀ m) of the next m lines contains two integers x and y (1≀ x, y≀ n, xβ‰  y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before. Output Print exactly m lines, where the i-th of them (1≀ i≀ m) contains the maximum number of people that can go on the trip on the evening of the day i. Examples Input 4 4 2 2 3 1 2 1 3 1 4 Output 0 0 3 3 Input 5 8 2 2 1 4 2 5 4 5 2 4 3 5 1 4 1 3 2 Output 0 0 0 3 3 4 4 5 Input 5 7 2 1 5 3 2 2 5 3 4 1 2 5 3 1 3 Output 0 0 0 0 3 4 4 Note In the first example, * 1,2,3 can go on day 3 and 4. In the second example, * 2,4,5 can go on day 4 and 5. * 1,2,4,5 can go on day 6 and 7. * 1,2,3,4,5 can go on day 8. In the third example, * 1,2,5 can go on day 5. * 1,2,3,5 can go on day 6 and 7.
[ { "input": { "stdin": "4 4 2\n2 3\n1 2\n1 3\n1 4\n" }, "output": { "stdout": "0\n0\n3\n3\n" } }, { "input": { "stdin": "5 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n" }, "output": { "stdout": "0\n0\n0\n3\n3\n4\n4\n5\n" } }, { "input": { "stdin": "5 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" }, "output": { "stdout": "0\n0\n0\n0\n3\n4\n4\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n7\n7\n7\n7\n7\n11\n11\n11\n11\n15\n15\n15\n15\n16\n" } }, { "input": { "stdin": "2 1 1\n2 1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "27 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n7\n10\n10\n11\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 7\n12 5\n7 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n7\n11\n11\n12\n" } }, { "input": { "stdin": "9 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n7 1\n3 2\n" }, "output": { "stdout": "0\n0\n0\n3\n3\n4\n4\n5\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n11 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n1 14\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n7\n7\n7\n7\n7\n9\n12\n12\n12\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 5\n7 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n9 3\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n4\n4\n4\n4\n4\n11\n11\n11\n12\n13\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n13 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n2 14\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n5\n5\n11\n11\n13\n13\n13\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 2\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n6\n6\n6\n6\n6\n10\n10\n12\n13\n14\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 5\n13 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 4\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n11\n11\n14\n14\n15\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 12\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n4\n9\n9\n9\n10\n10\n10\n12\n12\n13\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 3\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n10\n10\n10\n10\n10\n14\n14\n14\n14\n15\n" } }, { "input": { "stdin": "2 1 3\n2 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n1 14\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n9\n12\n12\n12\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n7\n12\n12\n13\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 7\n7 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n25 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 2\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "27 20 2\n10 3\n5 1\n20 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n5\n5\n5\n5\n8\n8\n9\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 5\n7 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 4\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n6 12\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n4\n9\n9\n9\n9\n9\n9\n12\n12\n13\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 7\n7 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "27 20 2\n10 3\n5 3\n20 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 23\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n5\n5\n5\n5\n8\n8\n9\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n11 9\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n1 14\n3 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n4\n4\n6\n6\n6\n6\n6\n9\n10\n12\n12\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n11 9\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n1 14\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n4\n4\n6\n6\n6\n6\n6\n9\n12\n12\n12\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 3\n10 3\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n10\n10\n10\n10\n10\n14\n14\n14\n14\n15\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 5\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n7\n7\n7\n9\n10\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 3\n10 3\n14 4\n15 14\n4 13\n13 3\n1 8\n7 15\n2 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n10\n10\n10\n10\n10\n12\n12\n14\n14\n15\n" } }, { "input": { "stdin": "27 20 2\n10 3\n5 3\n20 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n5\n5\n5\n5\n8\n8\n9\n" } }, { "input": { "stdin": "27 20 2\n10 3\n5 1\n20 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 16\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n5\n5\n5\n5\n8\n8\n9\n" } }, { "input": { "stdin": "27 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n19 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n7\n10\n10\n11\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 5\n13 6\n12 10\n3 6\n1 10\n11 16\n11 1\n16 4\n3 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "9 7 2\n1 5\n3 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" }, "output": { "stdout": "0\n0\n0\n0\n3\n4\n4\n" } }, { "input": { "stdin": "9 8 2\n2 1\n4 2\n5 4\n5 2\n4 3\n5 1\n4 1\n3 2\n" }, "output": { "stdout": "0\n0\n0\n3\n3\n4\n4\n5\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 5\n13 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 4\n3 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n11\n11\n13\n13\n14\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n4\n9\n9\n9\n9\n13\n13\n13\n13\n14\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 3\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 13\n13 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n11\n11\n15\n15\n16\n" } }, { "input": { "stdin": "2 1 2\n2 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "27 20 3\n10 3\n5 1\n20 5\n12 5\n13 6\n12 10\n2 6\n1 10\n11 16\n11 1\n16 4\n10 2\n14 4\n15 24\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n" } }, { "input": { "stdin": "9 7 2\n1 5\n4 2\n2 5\n3 4\n1 2\n5 3\n1 3\n" }, "output": { "stdout": "0\n0\n0\n0\n3\n5\n5\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n13 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n4 3\n13 15\n1 14\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n5\n5\n5\n5\n11\n11\n13\n13\n13\n" } }, { "input": { "stdin": "16 20 2\n10 4\n5 3\n10 5\n12 7\n7 6\n9 12\n9 6\n1 10\n11 16\n11 1\n16 3\n10 3\n14 4\n15 14\n4 13\n13 3\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n0\n0\n0\n0\n4\n4\n4\n4\n10\n10\n10\n10\n10\n12\n12\n14\n14\n15\n" } }, { "input": { "stdin": "16 20 2\n10 3\n5 3\n10 5\n12 7\n7 6\n9 10\n2 6\n1 10\n11 16\n11 1\n16 2\n10 2\n14 4\n15 14\n3 13\n1 15\n1 8\n7 15\n1 7\n8 15\n" }, "output": { "stdout": "0\n0\n3\n3\n3\n3\n3\n3\n3\n3\n3\n7\n7\n7\n7\n7\n7\n10\n10\n11\n" } } ]
{ "tags": [ "graphs" ], "title": "Trips" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, k;\nint a[200005];\nint b[200005];\nvector<int> adj[200005];\nbool removed[200005];\nint nc[200005];\nint ans[200005];\nset<pair<int, int>> remedges;\nint main() {\n ios::sync_with_stdio(false);\n scanf(\"%d %d %d\", &n, &m, &k);\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n }\n for (int i = 0; i < m; i++) {\n adj[a[i]].push_back(b[i]);\n adj[b[i]].push_back(a[i]);\n nc[a[i]]++;\n nc[b[i]]++;\n }\n int cans = n;\n for (int i = 1; i <= n; i++) {\n if (!removed[i]) {\n if (nc[i] < k) {\n removed[i] = true;\n cans--;\n queue<int> q;\n q.push(i);\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n for (int j = 0; j < adj[cur].size(); j++) {\n int x = adj[cur][j];\n if (!removed[x]) {\n nc[x]--;\n if (nc[x] < k) {\n removed[x] = true;\n cans--;\n q.push(x);\n }\n }\n }\n }\n }\n }\n }\n ans[m - 1] = cans;\n for (int cm = m - 1; cm >= 0; cm--) {\n int ca = a[cm];\n int cb = b[cm];\n if (!removed[ca] && !removed[cb]) {\n nc[ca]--;\n nc[cb]--;\n remedges.insert(make_pair(ca, cb));\n remedges.insert(make_pair(cb, ca));\n if (nc[ca] < k) {\n removed[ca] = true;\n cans--;\n queue<int> q;\n q.push(ca);\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n for (int j = 0; j < adj[cur].size(); j++) {\n int x = adj[cur][j];\n if (remedges.find(make_pair(cur, x)) != remedges.end()) {\n continue;\n }\n if (!removed[x]) {\n nc[x]--;\n if (nc[x] < k) {\n removed[x] = true;\n cans--;\n q.push(x);\n }\n }\n }\n }\n }\n if (nc[cb] < k && !removed[cb]) {\n removed[cb] = true;\n cans--;\n queue<int> q;\n q.push(cb);\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n for (int j = 0; j < adj[cur].size(); j++) {\n int x = adj[cur][j];\n if (remedges.find(make_pair(cur, x)) != remedges.end()) {\n continue;\n }\n if (!removed[x]) {\n nc[x]--;\n if (nc[x] < k) {\n removed[x] = true;\n cans--;\n q.push(x);\n }\n }\n }\n }\n }\n }\n if (cm > 0) {\n ans[cm - 1] = cans;\n }\n }\n for (int i = 0; i < m; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n}\n", "java": "import java.io.OutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.PrintWriter;\nimport java.util.HashSet;\nimport java.util.Arrays;\nimport java.io.FilterInputStream;\nimport java.io.BufferedInputStream;\nimport java.util.TreeSet;\nimport java.io.InputStream;\n\n/**\n * @author khokharnikunj8\n */\n\npublic class Main {\n public static void main(String[] args) {\n new Thread(null, new Runnable() {\n public void run() {\n new Main().solve();\n }\n }, \"1\", 1 << 26).start();\n }\n\n void solve() {\n InputStream inputStream = System.in;\n OutputStream outputStream = System.out;\n ScanReader in = new ScanReader(inputStream);\n PrintWriter out = new PrintWriter(outputStream);\n ETrips solver = new ETrips();\n solver.solve(1, in, out);\n out.close();\n }\n\n static class ETrips {\n int[][] G;\n\n public void solve(int testNumber, ScanReader in, PrintWriter out) {\n int n = in.scanInt();\n int m = in.scanInt();\n int k = in.scanInt();\n TreeSet<pair> bstCustom = new TreeSet<>();\n int from[] = new int[m];\n int to[] = new int[m];\n for (int i = 0; i < m; i++) {\n from[i] = in.scanInt();\n to[i] = in.scanInt();\n }\n int[] ans = new int[m];\n G = CodeHash.packGraph(from, to, n);\n int degree[] = new int[n + 1];\n for (int i = 1; i <= n; i++) bstCustom.add(new pair(degree[i] = G[i].length, i));\n boolean[] is_inside = new boolean[n + 1];\n Arrays.fill(is_inside, true);\n HashSet<Long> set = new HashSet<>();\n while (bstCustom.size() > 0 && bstCustom.first().x < k) {\n pair tt = bstCustom.first();\n for (int i : G[tt.y]) {\n if (is_inside[i]) {\n if (set.contains(i * 1000000000l + tt.y) || set.contains(tt.y * 1000000000l + i))\n continue;\n bstCustom.remove(new pair(degree[i], i));\n degree[i]--;\n degree[tt.y]--;\n bstCustom.add(new pair(degree[i], i));\n set.add(i * 1000000000l + tt.y);\n }\n }\n is_inside[tt.y] = false;\n bstCustom.remove(tt);\n }\n ans[m - 1] = bstCustom.size();\n for (int i = m - 1; i >= 1; i--) {\n if (is_inside[from[i]] && is_inside[to[i]]) {\n bstCustom.remove(new pair(degree[from[i]], from[i]));\n degree[from[i]]--;\n bstCustom.add(new pair(degree[from[i]], from[i]));\n bstCustom.remove(new pair(degree[to[i]], to[i]));\n degree[to[i]]--;\n bstCustom.add(new pair(degree[to[i]], to[i]));\n set.add(1000000000l * from[i] + to[i]);\n }\n while (bstCustom.size() > 0 && bstCustom.first().x < k) {\n pair tt = bstCustom.first();\n for (int j : G[tt.y]) {\n if (is_inside[j]) {\n if (set.contains(j * 1000000000l + tt.y) || set.contains(tt.y * 1000000000l + j))\n continue;\n bstCustom.remove(new pair(degree[j], j));\n degree[j]--;\n bstCustom.add(new pair(degree[j], j));\n set.add(j * 1000000000l + tt.y);\n }\n }\n is_inside[tt.y] = false;\n bstCustom.remove(tt);\n }\n ans[i - 1] = bstCustom.size();\n }\n for (int i = 0; i < m; i++) out.println(ans[i]);\n }\n\n class pair implements Comparable<pair> {\n int x;\n int y;\n\n public int compareTo(pair o) {\n if (this.x == o.x) return this.y - o.y;\n return this.x - o.x;\n }\n\n public pair(int x, int y) {\n this.x = x;\n this.y = y;\n }\n\n }\n\n }\n\n static class CodeHash {\n public static int[][] packGraph(int[] from, int[] to, int n) {\n int[][] g = new int[n + 1][];\n int p[] = new int[n + 1];\n for (int i : from) p[i]++;\n for (int i : to) p[i]++;\n for (int i = 0; i <= n; i++) g[i] = new int[p[i]];\n for (int i = 0; i < from.length; i++) {\n g[from[i]][--p[from[i]]] = to[i];\n g[to[i]][--p[to[i]]] = from[i];\n }\n return g;\n }\n\n }\n\n static class ScanReader {\n private byte[] buf = new byte[4 * 1024];\n private int index;\n private BufferedInputStream in;\n private int total;\n\n public ScanReader(InputStream inputStream) {\n in = new BufferedInputStream(inputStream);\n }\n\n private int scan() {\n if (index >= total) {\n index = 0;\n try {\n total = in.read(buf);\n } catch (Exception e) {\n e.printStackTrace();\n }\n if (total <= 0) return -1;\n }\n return buf[index++];\n }\n\n public int scanInt() {\n int integer = 0;\n int n = scan();\n while (isWhiteSpace(n)) n = scan();\n int neg = 1;\n if (n == '-') {\n neg = -1;\n n = scan();\n }\n while (!isWhiteSpace(n)) {\n if (n >= '0' && n <= '9') {\n integer *= 10;\n integer += n - '0';\n n = scan();\n }\n }\n return neg * integer;\n }\n\n private boolean isWhiteSpace(int n) {\n if (n == ' ' || n == '\\n' || n == '\\r' || n == '\\t' || n == -1) return true;\n else return false;\n }\n\n }\n}\n\n", "python": "\nimport sys\nrange = xrange\n\nn,m,k = [int(x) for x in sys.stdin.readline().split()]\n\ninp = [int(x)-1 for x in sys.stdin.read().split()]\nii = 0\n\ncoupl = [[] for _ in range(n)]\ntime = [[] for _ in range(n)]\nnfr = [0]*n\n\nfor i in range(m):\n a,b = inp[ii],inp[ii+1]\n ii += 2\n coupl[a].append(b)\n coupl[b].append(a)\n time[a].append(i)\n time[b].append(i)\n nfr[a] += 1\n nfr[b] += 1\n\nnotf = 0\n\nrem = [i for i in range(n) if nfr[i]<k]\nwhile rem:\n node = rem.pop()\n notf += 1\n for nei in coupl[node]:\n if nfr[nei]==k:\n rem.append(nei)\n nfr[nei]-=1\n\nout = []\nfor j in reversed(range(m)):\n out.append(n-notf)\n a,b = inp[j*2],inp[j*2+1]\n nfra = nfr[a]\n nfrb = nfr[b]\n\n if nfra>=k:\n if nfrb==k:\n rem.append(b)\n nfr[b]-=1\n if nfrb>=k:\n if nfra==k:\n rem.append(a)\n nfr[a]-=1\n \n while rem:\n node = rem.pop()\n notf += 1\n for i in range(len(coupl[node])):\n nei = coupl[node][i]\n t = time[node][i]\n if t<j:\n if nfr[nei]==k:\n rem.append(nei)\n nfr[nei]-=1\nprint '\\n'.join(str(x) for x in reversed(out))" }
Codeforces/1060/A
# Phone Numbers Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n β€” the number of cards with digits that you have (1 ≀ n ≀ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
[ { "input": { "stdin": "22\n0011223344556677889988\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "11\n00000000008\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "11\n31415926535\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "51\n882889888888689888850888388887688788888888888858888\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "55\n7271714707719515303911625619272900050990324951111943573\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "65\n44542121362830719677175203560438858260878894083124543850593761845\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "54\n438283821340622774637957966575424773837418828888614203\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "42\n885887846290886288816884858898812858495482\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "11\n55814018693\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "31\n0868889888343881888987888838808\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "21\n888888888888000000000\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "62\n18888883884288488882387888486858887882838885288886472818688888\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "30\n888888888888888888888888888888\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "64\n8885984815868480968883818886281846682409262501034555933863969284\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "44\n15920309219313427633220119270900111650391207\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "50\n88888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "20\n88888888888888888888\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "32\n88888888888888888888888888888888\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "21\n888111111111111111111\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1\n8\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "40\n8888888888888888888888888888888888888888\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "33\n888800000000000000000000000000000\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "21\n881234567900123456790\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "22\n4215079217017196952791\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "1\n0\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "11\n80000000000\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "32\n88000000000000000000000000000000\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "11\n88888888888\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "21\n888000000000000000000\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "66\n747099435917145962031075767196746707764157706291155762576312312094\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "22\n8899999999999999999999\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "11\n81234567123\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "41\n78888884888874788841882882888088888588888\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "10\n8888888888\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "66\n157941266854773786962397310504192100434183957442977444078457168272\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "44\n30153452341853403190257244993442815171970194\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "63\n728385948188688801288285888788852829888898565895847689806684688\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "21\n888888555555555555555\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "53\n85838985300863473289888099788588319484149888886832906\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "60\n888888888888888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "11\n24572366390\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "32\n88257478884887437239023185588797\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "43\n7404899846883344886153727489084158470112581\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "8\n12345678\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "21\n582586788289484878588\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "33\n270375004567749549929235905225024\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "50\n88000000000000000000000000000000000000000000000000\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "33\n429980628264468835720540136177288\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "27\n888000000000000000000000000\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "10\n8000000000\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "22\n6188156585823394680191\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "57\n888888888888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "11\n81234567090\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "21\n880000000000000000000\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "52\n8878588869084488848898838898788838337877898817818888\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "61\n8880888836888988888988888887388888888888868898887888818888888\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "55\n3982037603326093160114589190899881252765957832414122484\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" }, "output": { "stdout": "9\n" } } ]
{ "tags": [ "brute force" ], "title": "Phone Numbers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar s[105];\nint main() {\n int n;\n cin >> n;\n cin >> s;\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == '8') cnt++;\n }\n int ans = min(cnt, n / 11);\n cout << ans << endl;\n return 0;\n}\n", "java": "import java .util.*;\npublic class a{\n\tpublic static void main(String args[]){\n\t\tScanner sc=new Scanner(System.in);\n\t\tint n=sc.nextInt();\n\t\tString x=sc.next();\n\t\tint arr[]=new int[10];\n\t\tArrays.fill(arr,0);\n\t\tfor(int i=0;i<n;i+=1){\n\t\t\tif(x.charAt(i)=='8')\n\t\t\tarr[9]++;\n\t\t\telse if(x.charAt(i)=='9')\n\t\t\tarr[8]++;\n\t\t\telse\n\t\t\tarr[x.charAt(i)-'0']++;\n\n\t\t}\n\t\tint ans=0;\n\t\tint c=0;\n\t\twhile(true){\n\t\t\tif(make(arr))\n\t\t\t\tc++;\n\t\t\telse\n\t\t\t{\n\t\t\t\tSystem.out.println(c);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t}\n\t}\n\tstatic boolean make(int arr[]){\n\t\tif(arr[9]==0)\n\t\t\treturn false;\n\t\tarr[9]--;\n\t\tint count=10;\n\t\tfor(int i=0;i<10;i++){\n\t\t\tif(arr[i]<count){\n\t\t\t\tcount-=arr[i];\n\t\t\t\tarr[i]=0;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tarr[i]-=count;\n\t\t\t\tcount=0;\n\n\t\t\t}\n\t\t}\n\t\tif(count==0)\n\t\t\treturn true;\n\t\treturn false;\n\t}\n}", "python": "n = int(input())\ns = input()\ne = min(s.count(\"8\"), len(s)//11)\nnums = (len(s)-e)//10\nprint(min(e,nums))\n" }
Codeforces/1081/G
# Mergesort Strikes Back Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≀ r<q and rd ≑ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≀ n, k ≀ 10^5, 10^8 ≀ q ≀ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 Γ— 2 ≑ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 Γ— 3 ≑ 2 \pmod {998244353}.
[ { "input": { "stdin": "3 2 998244353\n" }, "output": { "stdout": "665496236\n" } }, { "input": { "stdin": "9 3 998244353\n" }, "output": { "stdout": "449209967\n" } }, { "input": { "stdin": "3 1 998244353\n" }, "output": { "stdout": "499122178\n" } }, { "input": { "stdin": "9 4 998244353\n" }, "output": { "stdout": "665496237\n" } }, { "input": { "stdin": "53812 4 967428361\n" }, "output": { "stdout": "950881274\n" } }, { "input": { "stdin": "7 2 400166453\n" }, "output": { "stdout": "37158321\n" } }, { "input": { "stdin": "75727 16 485722667\n" }, "output": { "stdout": "166058860\n" } }, { "input": { "stdin": "65536 10 802338989\n" }, "output": { "stdout": "462855383\n" } }, { "input": { "stdin": "65535 12 196344479\n" }, "output": { "stdout": "7405077\n" } }, { "input": { "stdin": "5000 4 961162523\n" }, "output": { "stdout": "935148925\n" } }, { "input": { "stdin": "13694 5 579788161\n" }, "output": { "stdout": "20837734\n" } }, { "input": { "stdin": "99999 14 746231791\n" }, "output": { "stdout": "534083991\n" } }, { "input": { "stdin": "14823 8 622667251\n" }, "output": { "stdout": "282687828\n" } }, { "input": { "stdin": "65536 1 262776883\n" }, "output": { "stdout": "22617908\n" } }, { "input": { "stdin": "65535 4 585040979\n" }, "output": { "stdout": "73478343\n" } }, { "input": { "stdin": "1 2 932173633\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "65535 13 543456539\n" }, "output": { "stdout": "170536956\n" } }, { "input": { "stdin": "56907 7 653135281\n" }, "output": { "stdout": "367828981\n" } }, { "input": { "stdin": "65535 16 589256509\n" }, "output": { "stdout": "362272581\n" } }, { "input": { "stdin": "79602 9 341282581\n" }, "output": { "stdout": "15283453\n" } }, { "input": { "stdin": "65535 15 148502831\n" }, "output": { "stdout": "46429722\n" } }, { "input": { "stdin": "91299 13 883710911\n" }, "output": { "stdout": "238048909\n" } }, { "input": { "stdin": "65536 7 999999937\n" }, "output": { "stdout": "195101941\n" } }, { "input": { "stdin": "65535 3 200770211\n" }, "output": { "stdout": "26568059\n" } }, { "input": { "stdin": "4558 9 768001957\n" }, "output": { "stdout": "338635790\n" } }, { "input": { "stdin": "78790 14 947580449\n" }, "output": { "stdout": "804769289\n" } }, { "input": { "stdin": "11045 4 779484089\n" }, "output": { "stdout": "766560946\n" } }, { "input": { "stdin": "65536 7 474924587\n" }, "output": { "stdout": "244871950\n" } }, { "input": { "stdin": "100000 1 327496733\n" }, "output": { "stdout": "207497869\n" } }, { "input": { "stdin": "7 4 674998729\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "93705 8 728681249\n" }, "output": { "stdout": "90464274\n" } }, { "input": { "stdin": "65535 7 775068599\n" }, "output": { "stdout": "580904942\n" } }, { "input": { "stdin": "93014 3 464769397\n" }, "output": { "stdout": "3096497\n" } }, { "input": { "stdin": "65536 9 512750233\n" }, "output": { "stdout": "56371267\n" } }, { "input": { "stdin": "65536 8 624488609\n" }, "output": { "stdout": "456424095\n" } }, { "input": { "stdin": "2 2 105534269\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4 2 717931793\n" }, "output": { "stdout": "59827651\n" } }, { "input": { "stdin": "29670 1 798626077\n" }, "output": { "stdout": "619382846\n" } }, { "input": { "stdin": "1 100000 355399153\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4866 5 828460181\n" }, "output": { "stdout": "236115936\n" } }, { "input": { "stdin": "5000 3 947484677\n" }, "output": { "stdout": "453430334\n" } }, { "input": { "stdin": "4862 11 340369703\n" }, "output": { "stdout": "187513462\n" } }, { "input": { "stdin": "67260 11 159230609\n" }, "output": { "stdout": "38214063\n" } }, { "input": { "stdin": "96560 6 621206447\n" }, "output": { "stdout": "336730170\n" } }, { "input": { "stdin": "6 4 142235399\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "319 6 736338271\n" }, "output": { "stdout": "133698563\n" } }, { "input": { "stdin": "99999 4 721319531\n" }, "output": { "stdout": "482453887\n" } }, { "input": { "stdin": "5000 5000 824957897\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "95449 16 477786341\n" }, "output": { "stdout": "181225428\n" } }, { "input": { "stdin": "65536 4 530056207\n" }, "output": { "stdout": "175616225\n" } }, { "input": { "stdin": "5 2 488196377\n" }, "output": { "stdout": "455649955\n" } }, { "input": { "stdin": "99999 10 201673531\n" }, "output": { "stdout": "6497465\n" } }, { "input": { "stdin": "8 2 401001541\n" }, "output": { "stdout": "108365903\n" } }, { "input": { "stdin": "65536 2 547031129\n" }, "output": { "stdout": "68232417\n" } }, { "input": { "stdin": "65535 6 100000007\n" }, "output": { "stdout": "19616415\n" } }, { "input": { "stdin": "87440 14 373345151\n" }, "output": { "stdout": "58174995\n" } }, { "input": { "stdin": "99999 5 950991961\n" }, "output": { "stdout": "236965854\n" } }, { "input": { "stdin": "65535 10 764125471\n" }, "output": { "stdout": "44493100\n" } }, { "input": { "stdin": "39062 3 557718113\n" }, "output": { "stdout": "311741364\n" } }, { "input": { "stdin": "100000 4 866430809\n" }, "output": { "stdout": "315155497\n" } }, { "input": { "stdin": "99999 7 612486629\n" }, "output": { "stdout": "160702769\n" } }, { "input": { "stdin": "65610 7 576223171\n" }, "output": { "stdout": "475692890\n" } }, { "input": { "stdin": "3 3 537728333\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "79173 7 329778431\n" }, "output": { "stdout": "112881569\n" } }, { "input": { "stdin": "19679 2 978579983\n" }, "output": { "stdout": "166411803\n" } }, { "input": { "stdin": "65535 1 969378797\n" }, "output": { "stdout": "589003274\n" } }, { "input": { "stdin": "8 4 617453693\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "99999 2 594212063\n" }, "output": { "stdout": "241186421\n" } }, { "input": { "stdin": "99999 3 538530137\n" }, "output": { "stdout": "420705596\n" } }, { "input": { "stdin": "99999 15 385602223\n" }, "output": { "stdout": "286320285\n" } }, { "input": { "stdin": "65535 2 332622313\n" }, "output": { "stdout": "23332505\n" } }, { "input": { "stdin": "31581 2 803297119\n" }, "output": { "stdout": "335334542\n" } }, { "input": { "stdin": "65536 16 307380313\n" }, "output": { "stdout": "210721421\n" } }, { "input": { "stdin": "5 4 294228373\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "12657 1 328355033\n" }, "output": { "stdout": "40046748\n" } }, { "input": { "stdin": "4 3 691608353\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "65536 17 355422121\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "2 3 738541207\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "68102 2 409693891\n" }, "output": { "stdout": "248567049\n" } }, { "input": { "stdin": "65535 14 379941571\n" }, "output": { "stdout": "364848655\n" } }, { "input": { "stdin": "65536 12 883299773\n" }, "output": { "stdout": "385022293\n" } }, { "input": { "stdin": "59614 14 431666281\n" }, "output": { "stdout": "382826545\n" } }, { "input": { "stdin": "99999 11 739822453\n" }, "output": { "stdout": "286705678\n" } }, { "input": { "stdin": "20621 4 420701179\n" }, "output": { "stdout": "289746143\n" } }, { "input": { "stdin": "65536 14 292184353\n" }, "output": { "stdout": "211044160\n" } }, { "input": { "stdin": "23880 14 515153497\n" }, "output": { "stdout": "28372663\n" } }, { "input": { "stdin": "99999 8 616151843\n" }, "output": { "stdout": "506564910\n" } }, { "input": { "stdin": "33727 15 177545087\n" }, "output": { "stdout": "137396822\n" } }, { "input": { "stdin": "8 3 930233189\n" }, "output": { "stdout": "465116600\n" } }, { "input": { "stdin": "65536 6 526215803\n" }, "output": { "stdout": "158448501\n" } }, { "input": { "stdin": "9292 12 386116849\n" }, "output": { "stdout": "339357599\n" } }, { "input": { "stdin": "3 2 457143689\n" }, "output": { "stdout": "304762460\n" } }, { "input": { "stdin": "5 3 698057369\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "64554 13 711786883\n" }, "output": { "stdout": "501856006\n" } }, { "input": { "stdin": "99999 18 278747437\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "6 3 706327789\n" }, "output": { "stdout": "529745844\n" } }, { "input": { "stdin": "6 2 126580711\n" }, "output": { "stdout": "61180682\n" } }, { "input": { "stdin": "100000 3 372547751\n" }, "output": { "stdout": "341122978\n" } }, { "input": { "stdin": "99999 17 222262553\n" }, "output": { "stdout": "24806593\n" } }, { "input": { "stdin": "7 3 957060541\n" }, "output": { "stdout": "239265139\n" } }, { "input": { "stdin": "99999 6 769267349\n" }, "output": { "stdout": "537638613\n" } }, { "input": { "stdin": "58791 1 627994511\n" }, "output": { "stdout": "550083467\n" } }, { "input": { "stdin": "92275 9 505206379\n" }, "output": { "stdout": "372045131\n" } }, { "input": { "stdin": "65535 9 939195329\n" }, "output": { "stdout": "374972142\n" } }, { "input": { "stdin": "65535 8 629794369\n" }, "output": { "stdout": "563763277\n" } }, { "input": { "stdin": "65536 11 506680939\n" }, "output": { "stdout": "206613192\n" } }, { "input": { "stdin": "99999 1 501051697\n" }, "output": { "stdout": "245192364\n" } }, { "input": { "stdin": "5000 2 444286949\n" }, "output": { "stdout": "161627985\n" } }, { "input": { "stdin": "99999 12 608975467\n" }, "output": { "stdout": "317105066\n" } }, { "input": { "stdin": "99999 16 424240459\n" }, "output": { "stdout": "81192002\n" } }, { "input": { "stdin": "65535 5 492219967\n" }, "output": { "stdout": "1097877\n" } }, { "input": { "stdin": "9569 7 974022443\n" }, "output": { "stdout": "297115301\n" } }, { "input": { "stdin": "100000 2 330782867\n" }, "output": { "stdout": "20709968\n" } }, { "input": { "stdin": "65536 5 347538067\n" }, "output": { "stdout": "50947333\n" } }, { "input": { "stdin": "99999 9 543989543\n" }, "output": { "stdout": "330542468\n" } }, { "input": { "stdin": "93976 8 747153793\n" }, "output": { "stdout": "239386990\n" } }, { "input": { "stdin": "42288 6 367611719\n" }, "output": { "stdout": "235655808\n" } }, { "input": { "stdin": "100000 100000 658399519\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "65536 3 759400619\n" }, "output": { "stdout": "725177449\n" } }, { "input": { "stdin": "65536 13 543490043\n" }, "output": { "stdout": "510005251\n" } }, { "input": { "stdin": "99999 13 838056061\n" }, "output": { "stdout": "772746099\n" } }, { "input": { "stdin": "65535 6 563701807\n" }, "output": { "stdout": "478190145\n" } }, { "input": { "stdin": "65535 17 131827369\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "1 1 807831149\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "65536 15 568071787\n" }, "output": { "stdout": "2593916\n" } }, { "input": { "stdin": "65535 11 390043253\n" }, "output": { "stdout": "2137720\n" } }, { "input": { "stdin": "58370 15 756534617\n" }, "output": { "stdout": "174119749\n" } }, { "input": { "stdin": "74973 12 872697443\n" }, "output": { "stdout": "741474461\n" } } ]
{ "tags": [ "math", "probabilities" ], "title": "Mergesort Strikes Back" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 2e5 + 7;\nlong long MOD, f[N];\nlong long qpow(long long x, long long y) {\n long long z = 1;\n while (y > 0) {\n if (y % 2) z = z * x % MOD;\n x = x * x % MOD;\n y /= 2;\n }\n return z;\n}\nmap<int, int> ma;\npair<int, int> a[10];\nvoid dfs(int x, int k) {\n if (k == 1 || x == 1) {\n ma[x]++;\n return;\n }\n dfs((x + 1) / 2, k - 1);\n dfs(x / 2, k - 1);\n}\nint main() {\n int n, k;\n scanf(\"%d%d%lld\", &n, &k, &MOD);\n dfs(n, k);\n for (int i = 1; i <= n * 2; i++)\n f[i] = (f[i - 1] - qpow(i, MOD - 2) + qpow(2, MOD - 2) + MOD) % MOD;\n int cnt = 0;\n for (auto p : ma) {\n a[++cnt] = p;\n }\n long long ans = 0;\n for (int i = 1; i <= cnt; i++) {\n ans += 1LL * a[i].second * a[i].first % MOD * (a[i].first - 1) % MOD *\n qpow(4, MOD - 2) % MOD;\n long long sum = 0;\n for (int j = 1; j <= a[i].first; j++) {\n sum = (sum + f[j + a[i].first] - f[j] + MOD) % MOD;\n }\n ans += sum * a[i].second % MOD * (a[i].second - 1) % MOD *\n qpow(2, MOD - 2) % MOD;\n }\n if (cnt == 2) {\n long long sum = 0;\n for (int j = 1; j <= a[1].first; j++) {\n sum = (sum + f[j + a[2].first] - f[j] + MOD) % MOD;\n }\n ans += sum * a[1].second % MOD * a[2].second % MOD;\n }\n printf(\"%lld\", ans % MOD);\n return 0;\n}\n", "java": null, "python": null }
Codeforces/1101/A
# Minimum Integer You are given q queries in the following form: Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i]. Can you answer all the queries? Recall that a number x belongs to segment [l, r] if l ≀ x ≀ r. Input The first line contains one integer q (1 ≀ q ≀ 500) β€” the number of queries. Then q lines follow, each containing a query given in the format l_i r_i d_i (1 ≀ l_i ≀ r_i ≀ 10^9, 1 ≀ d_i ≀ 10^9). l_i, r_i and d_i are integers. Output For each query print one integer: the answer to this query. Example Input 5 2 4 2 5 10 4 3 10 1 1 2 3 4 6 5 Output 6 4 1 3 10
[ { "input": { "stdin": "5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n" }, "output": { "stdout": "6\n4\n1\n3\n10\n" } }, { "input": { "stdin": "20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n" }, "output": { "stdout": "1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n" } }, { "input": { "stdin": "1\n78 79 79\n" }, "output": { "stdout": "158\n" } }, { "input": { "stdin": "1\n6 6 6\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n" }, "output": { "stdout": "2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n" } }, { "input": { "stdin": "1\n78 1000 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1\n77 10000 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n" }, "output": { "stdout": "1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n" } }, { "input": { "stdin": "10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n" }, "output": { "stdout": "1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n" } }, { "input": { "stdin": "1\n78 80 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n" }, "output": { "stdout": "1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n" } }, { "input": { "stdin": "1\n1 1 123456789\n" }, "output": { "stdout": "123456789\n" } }, { "input": { "stdin": "1\n80 100 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n" }, "output": { "stdout": "1\n1\n1\n1\n1\n" } }, { "input": { "stdin": "1\n78 10000 1\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1\n79 80 100\n" }, "output": { "stdout": "100\n" } }, { "input": { "stdin": "5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n" }, "output": { "stdout": "1000000001\n1000000000\n999999999\n999999998\n999999997\n" } }, { "input": { "stdin": "5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n" }, "output": { "stdout": "1000000001\n2000000000\n1\n1000000000\n10\n" } }, { "input": { "stdin": "30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n" }, "output": { "stdout": "1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n" } }, { "input": { "stdin": "1\n78 89 34\n" }, "output": { "stdout": "34\n" } }, { "input": { "stdin": "1\n1 1 1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1\n1 3 2\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n" }, "output": { "stdout": "999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n" } }, { "input": { "stdin": "4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n" }, "output": { "stdout": "1000000000\n999999999\n999999998\n999999997\n" } }, { "input": { "stdin": "5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n" }, "output": { "stdout": "1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n" } }, { "input": { "stdin": "2\n1 1 2\n1 1 2\n" }, "output": { "stdout": "2\n2\n" } }, { "input": { "stdin": "1\n80 100 80\n" }, "output": { "stdout": "160\n" } }, { "input": { "stdin": "25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n" }, "output": { "stdout": "1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n" } }, { "input": { "stdin": "30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n" }, "output": { "stdout": "1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n" } }, { "input": { "stdin": "16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n" }, "output": { "stdout": "1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n" } }, { "input": { "stdin": "1\n1 1000000000 6\n" }, "output": { "stdout": "1000000002\n" } }, { "input": { "stdin": "1\n5 5 5\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "1\n2 5 6\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n" }, "output": { "stdout": "999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n" } }, { "input": { "stdin": "5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n" }, "output": { "stdout": "10\n4\n1\n3\n10\n" } }, { "input": { "stdin": "1\n1 1000000000 1017\n" }, "output": { "stdout": "1000000845\n" } }, { "input": { "stdin": "1\n1 1000000000 2\n" }, "output": { "stdout": "1000000002\n" } }, { "input": { "stdin": "5\n80 101 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n" }, "output": { "stdout": "10\n4\n1\n3\n10\n" } }, { "input": { "stdin": "4\n1 474817329 1\n1 999999998 1\n1 999999997 1\n2 999999996 1\n" }, "output": { "stdout": "474817330\n999999999\n999999998\n1\n" } }, { "input": { "stdin": "1\n2 3 6\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "1\n0 3 2\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "1\n69 89 56\n" }, "output": { "stdout": "56\n" } }, { "input": { "stdin": "30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 749549584 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n" }, "output": { "stdout": "1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n749549586\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n" } }, { "input": { "stdin": "1\n80 100 109\n" }, "output": { "stdout": "109\n" } }, { "input": { "stdin": "1\n79 306 100\n" }, "output": { "stdout": "400\n" } }, { "input": { "stdin": "10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000010 1\n1 1000000000 1\n" }, "output": { "stdout": "1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000011\n1000000001\n" } }, { "input": { "stdin": "4\n1 999999999 1\n1 999999998 2\n1 999999997 1\n1 999999996 1\n" }, "output": { "stdout": "1000000000\n1000000000\n999999998\n999999997\n" } }, { "input": { "stdin": "16\n2 1000000000 1\n1 1000000000 1\n1 1000100000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1001000000 1\n1 1000000000 1\n" }, "output": { "stdout": "1\n1000000001\n1000100001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1001000001\n1000000001\n" } }, { "input": { "stdin": "20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000010 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n" }, "output": { "stdout": "1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000011\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n" } }, { "input": { "stdin": "5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 4 5\n" }, "output": { "stdout": "1000000001\n2000000000\n1\n1000000000\n5\n" } } ]
{ "tags": [ "math" ], "title": "Minimum Integer" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long t;\n cin >> t;\n long long le, re, dis;\n for (int i = 0; i < t; i++) {\n cin >> le >> re >> dis;\n long long le1 = le / dis;\n long long le2 = re / dis;\n if (le % dis == 0)\n le1 = (le / dis - 1) * dis;\n else\n le1 = (le / dis) * dis;\n le2 = (re / dis + 1) * dis;\n if (le1 > 0)\n cout << min(le1, dis) << endl;\n else\n cout << le2 << endl;\n }\n return 0;\n}\n", "java": "//package math_codet;\n\nimport java.io.*;\nimport java.util.*;\n\npublic class lets_do {\n InputReader in;\n PrintWriter out;\n Helper_class h;\n final long mod = 1000000007;\n public static void main(String[] args) throws java.lang.Exception{\n new lets_do().run();\n }\n void run() throws Exception{\n in=new InputReader(System.in);\n out = new PrintWriter(System.out);\n h = new Helper_class();\n int t = h.ni();\n while(t-->0)\n solve();\n out.flush();\n out.close();\n }\n void solve(){\n long l = h.nl();\n long r = h.nl();\n long n = h.nl();\n if(n >= l && n <= r) {\n \tlong xx = r / n;\n \tlong xxx = (xx + 1) * n;\n \tlong xxxx = (xx + 2) * n;\n \tif(xxx >= r)\n \t\th.pn(xxx);\n \telse\n \t\th.pn(xxxx);\n }\n else\n \th.pn(n);\n\n }\n static final Comparator<Pair> com=new Comparator<Pair>(){\n public int compare(Pair a, Pair b){\n int xx = Long.compare(a.x, b.x);\n if(xx == 0)\n return Integer.compare(b.y, a.y);\n else\n return xx;\n }\n };\n class Pair{\n long x;\n int y;\n Pair(long p, int q){\n x = p;\n y = q;\n }\n }\n class Edge{\n int u , v;\n long x;\n Edge(int a, int b, long p){\n u = a;\n v = b;\n x = p;\n }\n int other(int x) {\n return u ^ v ^ x;\n }\n }\n class Helper_class{\n long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}\n int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}\n int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));}\n void p(Object o){out.print(o);}\n void pn(Object o){out.println(o);}\n void pni(Object o){out.println(o);out.flush();}\n String n(){return in.next();}\n String nln(){return in.nextLine();}\n int ni(){return Integer.parseInt(in.next());}\n long nl(){return Long.parseLong(in.next());}\n double nd(){return Double.parseDouble(in.next());}\n\n long mul(long a,long b){\n if(a>=mod)a%=mod;\n if(b>=mod)b%=mod;\n a*=b;\n if(a>=mod)a%=mod;\n return a;\n }\n long modPow(long a, long p){\n long o = 1;\n while(p>0){\n if((p&1)==1)o = mul(o,a);\n a = mul(a,a);\n p>>=1;\n }\n return o;\n }\n long add(long a, long b){\n if(a>=mod)a%=mod;\n if(b>=mod)b%=mod;\n if(b<0)b+=mod;\n a+=b;\n if(a>=mod)a-=mod;\n return a;\n }\n }\n\n class InputReader{\n private InputStream stream;\n private byte[] buf = new byte[1024];\n private int curChar;\n private int numChars;\n \n public InputReader(InputStream stream) {\n this.stream = stream;\n }\n \n public int read() {\n if (numChars == -1)\n throw new UnknownError();\n if (curChar >= numChars) {\n curChar = 0;\n try {\n numChars = stream.read(buf);\n } catch (IOException e) {\n throw new UnknownError();\n }\n if (numChars <= 0)\n return -1;\n }\n return buf[curChar++];\n }\n \n public int peek() {\n if (numChars == -1)\n return -1;\n if (curChar >= numChars) {\n curChar = 0;\n try {\n numChars = stream.read(buf);\n } catch (IOException e) {\n return -1;\n }\n if (numChars <= 0)\n return -1;\n }\n return buf[curChar];\n }\n \n public void skip(int x) {\n while (x-- > 0)\n read();\n }\n \n public int nextInt() {\n return Integer.parseInt(next());\n }\n \n public long nextLong() {\n return Long.parseLong(next());\n }\n \n public String nextString() {\n return next();\n }\n \n public String next() {\n int c = read();\n while (isSpaceChar(c))\n c = read();\n StringBuffer res = new StringBuffer();\n do {\n res.appendCodePoint(c);\n c = read();\n } while (!isSpaceChar(c));\n \n return res.toString();\n }\n \n public String nextLine() {\n StringBuffer buf = new StringBuffer();\n int c = read();\n while (c != '\\n' && c != -1) {\n if (c != '\\r')\n buf.appendCodePoint(c);\n c = read();\n }\n return buf.toString();\n }\n \n public double nextDouble() {\n int c = read();\n while (isSpaceChar(c))\n c = read();\n int sgn = 1;\n if (c == '-') {\n sgn = -1;\n c = read();\n }\n double res = 0;\n while (!isSpaceChar(c) && c != '.') {\n if (c == 'e' || c == 'E')\n return res * Math.pow(10, nextInt());\n if (c < '0' || c > '9')\n throw new InputMismatchException();\n res *= 10;\n res += c - '0';\n c = read();\n }\n if (c == '.') {\n c = read();\n double m = 1;\n while (!isSpaceChar(c)) {\n if (c == 'e' || c == 'E')\n return res * Math.pow(10, nextInt());\n if (c < '0' || c > '9')\n throw new InputMismatchException();\n m /= 10;\n res += (c - '0') * m;\n c = read();\n }\n }\n return res * sgn;\n }\n \n public boolean hasNext() {\n int value;\n while (isSpaceChar(value = peek()) && value != -1)\n read();\n return value != -1;\n }\n \n private boolean isSpaceChar(int c) {\n return c == ' ' || c == '\\n' || c == '\\r' || c == '\\t' || c == -1;\n }\n }\n}", "python": "query=int(input())\nfor i in range(query):\n\tl,r,d=map(int,input().split())\n\tif(d<l or d>r):\n\t\tprint(d)\n\telse:\n\t\tprint((r//d)*d+d)" }
Codeforces/1129/D
# Isolation Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
[ { "input": { "stdin": "5 5\n1 2 3 4 5\n" }, "output": { "stdout": "16" } }, { "input": { "stdin": "3 1\n1 1 2\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "5 2\n1 1 2 1 3\n" }, "output": { "stdout": "14" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "16" } }, { "input": { "stdin": "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n" }, "output": { "stdout": "726975503" } }, { "input": { "stdin": "10 1\n4 2 9 2 1 4 4 1 4 10\n" }, "output": { "stdout": "24" } }, { "input": { "stdin": "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n" }, "output": { "stdout": "244208148" } }, { "input": { "stdin": "1 1\n1\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "2 2\n2 1\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "2 1\n2 2\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 1 66 30 47 92 88 51 20 33 12 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100 1\n7 75 6 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100 1\n7 75 6 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 41 30 48 64 10 61 8 60 74 13 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 12 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 2\n2 1 2 1 3\n" }, "output": { "stdout": "14\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 18 3 13 17 48 8 31 32 6 31 31 49 9 40 18 21 23 41 17 30 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100 1\n7 75 6 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 41 30 48 64 10 61 8 60 74 13 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 47 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 25 41 17 31 45 47 33 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 18 3 13 17 48 8 31 32 6 31 31 49 9 40 18 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 35 4 47 4 24\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 67 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 56 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 5 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 25 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 1 66 30 47 92 88 51 20 33 12 98 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 66 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 1 84 3 3 3 97 9 94 98 86 63\n" }, "output": { "stdout": "834061098\n" } }, { "input": { "stdin": "100 10\n59 95 5 20 91 78 31 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 7 61 4 59 5 55 5 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 2 73 15 42 94 100 8 50 29 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n" }, "output": { "stdout": "955855650\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 2\n2 1 1 1 3\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "100 10\n59 95 5 20 91 78 31 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 2 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n" }, "output": { "stdout": "105389903\n" } }, { "input": { "stdin": "10 1\n4 2 9 2 2 7 4 1 4 10\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 67 98 85 76 5 25 98 73 78 55 4 44 7 10 8 12 10 56 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 69 88 4 66 30 47 92 88 51 5 33 25 50 95 78 10 69\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "100 1\n7 75 6 62 10 31 43 96 32 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 41 30 48 64 10 61 8 60 74 13 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 47 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "2 2\n2 2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 13 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 25 41 17 31 45 47 33 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "100 10\n59 95 5 20 91 78 31 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 7 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 2 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n" }, "output": { "stdout": "105389903\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 13 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 25 41 17 31 45 47 33 1 12 15 50 40 38 4 20 1 9 37 4 47 1 24\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 66 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 98 86 63\n" }, "output": { "stdout": "564452064\n" } }, { "input": { "stdin": "2 2\n2 1 1 1 0\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 56 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 1 3 13 17 48 8 31 32 6 31 31 49 9 40 18 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 35 4 47 4 24\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 67 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 56 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 69 88 4 66 30 47 92 88 51 5 33 25 50 95 78 10 69\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 66 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n" }, "output": { "stdout": "853187053\n" } }, { "input": { "stdin": "100 10\n59 95 5 20 91 78 31 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 7 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 2 73 15 42 94 100 8 50 29 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n" }, "output": { "stdout": "105389903\n" } }, { "input": { "stdin": "10 1\n4 2 9 2 4 7 4 1 4 10\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 88 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 1 66 30 47 92 88 91 20 33 12 98 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "10 1\n4 2 9 2 2 4 4 1 3 10\n" }, "output": { "stdout": "24\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 18 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 9 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "100 30\n31 57 41 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n" }, "output": { "stdout": "873835092\n" } }, { "input": { "stdin": "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 18 3 13 17 48 8 31 32 6 31 31 49 9 40 18 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "100 1\n7 75 3 82 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 46 56 54 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 50 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n" }, "output": { "stdout": "1\n" } } ]
{ "tags": [ "data structures", "dp" ], "title": "Isolation" }
{ "cpp": "#include <bits/stdc++.h>\nint bb[1 + 100000], dp[1 + 100000], ss[((100000 + 200 - 1) / 200)],\n dq[((100000 + 200 - 1) / 200)][200 + 1 + 200];\nvoid update(int h) {\n int *qq = dq[h];\n int i, t, c;\n t = 0;\n memset(qq, 0, (200 + 1 + 200) * sizeof *qq);\n for (i = (h + 1) * 200; i > h * 200; i--) {\n t += bb[i];\n qq[200 + t] = (qq[200 + t] + dp[i - 1]) % 998244353;\n }\n for (c = 1; c <= 200 + 200; c++) qq[c] = (qq[c] + qq[c - 1]) % 998244353;\n}\nint main() {\n static int pp[1 + 100000], ii[1 + 100000];\n int n, m, k, h, i, j;\n scanf(\"%d%d\", &n, &k);\n for (i = 1; i <= n; i++) {\n int a;\n scanf(\"%d\", &a);\n pp[i] = ii[a];\n ii[a] = i;\n }\n dp[0] = 1;\n for (j = 1; j <= n; j++) {\n int p, x, t;\n m = (j - 1) / 200;\n ss[m] += 1 - bb[j];\n bb[j] = 1;\n if ((p = pp[j])) {\n h = (p - 1) / 200;\n ss[h] += -1 - bb[p];\n bb[p] = -1;\n if (p <= m * 200) update(h);\n if ((p = pp[p])) {\n h = (p - 1) / 200;\n ss[h] += 0 - bb[p];\n bb[p] = 0;\n if (p <= m * 200) update(h);\n }\n }\n x = 0;\n t = 0;\n ++t;\n --t;\n for (i = j; i > m * 200; i--) {\n t += bb[i];\n if (t <= k) x = (x + dp[i - 1]) % 998244353;\n }\n for (h = m - 1; h >= 0; h--) {\n if (k - t >= -200)\n x = (x + dq[h][200 + (200 < k - t ? 200 : k - t)]) % 998244353;\n t += ss[h];\n }\n dp[j] = x;\n if (j % 200 == 0) update(m);\n }\n printf(\"%d\\n\", dp[n]);\n return 0;\n}\n", "java": "// upsolve with rainboy\nimport java.io.*;\nimport java.util.*;\n\npublic class CF1129D {\n\tstatic final int MD = 998244353, A = 100000, B = 500;\n\tstatic int[] bb, dp, ss;\n\tstatic int[][] dq;\n\tstatic void update(int h) {\n\t\tint[] qq = dq[h];\n\t\tArrays.fill(qq, 0);\n\t\tint t = 0;\n\t\tfor (int i = (h + 1) * B; i > h * B; i--) {\n\t\t\tt += bb[i];\n\t\t\tqq[B + t] = (qq[B + t] + dp[i - 1]) % MD;\n\t\t}\n\t\tfor (int c = 1; c <= B + B; c++)\n\t\t\tqq[c] = (qq[c] + qq[c - 1]) % MD;\n\t}\n\tpublic static void main(String[] args) throws IOException {\n\t\tBufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st = new StringTokenizer(br.readLine());\n\t\tint n = Integer.parseInt(st.nextToken());\n\t\tint k = Integer.parseInt(st.nextToken());\n\t\tst = new StringTokenizer(br.readLine());\n\t\tint[] pp = new int[1 + n];\n\t\tint[] ii = new int[1 + A];\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tint a = Integer.parseInt(st.nextToken());\n\t\t\tpp[i] = ii[a]; ii[a] = i;\n\t\t}\n\t\tbb = new int[1 + n];\n\t\tdp = new int[1 + n];\n\t\tdp[0] = 1;\n\t\tint m = (n + B - 1) / B;\n\t\tss = new int[m];\n\t\tdq = new int[m][B + 1 + B];\n\t\tfor (int j = 1; j <= n; j++) {\n\t\t\tint p;\n\t\t\tm = (j - 1) / B;\n\t\t\tss[m] += 1 - bb[j]; bb[j] = 1;\n\t\t\tif ((p = pp[j]) != 0) {\n\t\t\t\tint h = (p - 1) / B;\n\t\t\t\tss[h] += -1 - bb[p]; bb[p] = -1;\n\t\t\t\tif (p <= m * B)\n\t\t\t\t\tupdate(h);\n\t\t\t\tif ((p = pp[p]) != 0) {\n\t\t\t\t\th = (p - 1) / B;\n\t\t\t\t\tss[h] += 0 - bb[p]; bb[p] = 0;\n\t\t\t\t\tif (p <= m * B)\n\t\t\t\t\t\tupdate(h);\n\t\t\t\t}\n\t\t\t}\n\t\t\tint x = 0, t = 0;\n\t\t\tfor (int i = j; i > m * B; i--)\n\t\t\t\tif ((t += bb[i]) <= k)\n\t\t\t\t\tx = (x + dp[i - 1]) % MD;\n\t\t\tfor (int h = m - 1; h >= 0; h--) {\n\t\t\t\tif (k - t >= -B)\n\t\t\t\t\tx = (x + dq[h][B + Math.min(B, k - t)]) % MD;\n\t\t\t\tt += ss[h];\n\t\t\t}\n\t\t\tdp[j] = x;\n\t\t\tif (j % B == 0)\n\t\t\t\tupdate(m);\n\t\t}\n\t\tSystem.out.println(dp[n]);\n\t}\n}\n", "python": null }
Codeforces/1149/E
# Election Promises In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
[ { "input": { "stdin": "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "3 3\n314 159 265\n1 2\n1 3\n3 2\n" }, "output": { "stdout": "WIN\n0 0 0 \n" } }, { "input": { "stdin": "4 2\n2 1 1 5\n1 2\n3 4\n" }, "output": { "stdout": "WIN\n1 5 1 5 \n" } }, { "input": { "stdin": "4 2\n1 5 1 5\n1 2\n3 4\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "2 1\n1000000000 1000000000\n2 1\n" }, "output": { "stdout": "WIN\n0 0 \n" } }, { "input": { "stdin": "3 2\n123 345 567\n1 2\n3 2\n" }, "output": { "stdout": "WIN\n123 0 123 \n" } }, { "input": { "stdin": "3 0\n4 1 4\n" }, "output": { "stdout": "WIN\n4 0 4 \n" } }, { "input": { "stdin": "3 2\n1 0 1\n3 2\n2 1\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "3 0\n1 2 3\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "3 2\n1 1 1\n1 2\n2 3\n" }, "output": { "stdout": "WIN\n1 0 1 \n" } }, { "input": { "stdin": "2 0\n1000000000 1000000000\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "2 0\n123456789 987654321\n" }, "output": { "stdout": "WIN\n123456789 123456789 \n" } }, { "input": { "stdin": "3 2\n123 345 123\n1 2\n3 2\n" }, "output": { "stdout": "WIN\n123 0 123 \n" } }, { "input": { "stdin": "1 0\n271828182\n" }, "output": { "stdout": "WIN\n0 \n" } }, { "input": { "stdin": "2 1\n1000000000 1000000000\n1 2\n" }, "output": { "stdout": "WIN\n0 0 \n" } }, { "input": { "stdin": "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n" }, "output": { "stdout": "WIN\n678678678 395063145 1073741823 2 3 1 \n" } }, { "input": { "stdin": "3 2\n2 3 4\n1 2\n1 3\n" }, "output": { "stdout": "WIN\n0 4 4 \n" } }, { "input": { "stdin": "1 0\n0\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "3 0\n1000000100 1010100000\n1 2\n" }, "output": { "stdout": "WIN\n1000000100 1000000101 1 \n" } }, { "input": { "stdin": "1 0\n137880027\n" }, "output": { "stdout": "WIN\n0 \n" } }, { "input": { "stdin": "6 4\n2 3 5 5 9 6\n1 3\n2 4\n3 5\n4 6\n" }, "output": { "stdout": "WIN\n2 3 5 5 7 6 \n" } }, { "input": { "stdin": "3 0\n3 1 4\n" }, "output": { "stdout": "WIN\n3 1 2 \n" } }, { "input": { "stdin": "2 0\n1 1 3\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "2 0\n1000000100 1010100000\n1 2\n" }, "output": { "stdout": "WIN\n1000000100 1000000100 \n" } }, { "input": { "stdin": "2 0\n1000010001 1100100000\n" }, "output": { "stdout": "WIN\n1000010001 1000010001 \n" } }, { "input": { "stdin": "3 0\n2 1 4\n" }, "output": { "stdout": "WIN\n2 1 3 \n" } }, { "input": { "stdin": "1 0\n1000000000 1000000000\n" }, "output": { "stdout": "WIN\n0 \n" } }, { "input": { "stdin": "2 0\n1101000100 1010100010\n1 1\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "2 0\n1000000000 1100100000\n" }, "output": { "stdout": "WIN\n1000000000 1000000000 \n" } }, { "input": { "stdin": "2 0\n1101010100 1010100010\n1 1\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "2 0\n1101010100 1010100010\n2 1\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "2 1\n1000000000 1000000001\n2 1\n" }, "output": { "stdout": "WIN\n0 0 \n" } }, { "input": { "stdin": "2 0\n1000000100 1010100010\n1 1\n" }, "output": { "stdout": "WIN\n1000000100 1000000100 \n" } }, { "input": { "stdin": "2 0\n1000000100 1010000000\n1 2\n" }, "output": { "stdout": "WIN\n1000000100 1000000100 \n" } }, { "input": { "stdin": "2 0\n123456789 635671754\n" }, "output": { "stdout": "WIN\n123456789 123456789 \n" } }, { "input": { "stdin": "3 2\n2 0 4\n3 1\n2 1\n" }, "output": { "stdout": "WIN\n0 0 0 \n" } }, { "input": { "stdin": "2 0\n1100000100 1010100010\n1 1\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "2 0\n1000000100 1010100010\n1 2\n" }, "output": { "stdout": "WIN\n1000000100 1000000100 \n" } }, { "input": { "stdin": "2 0\n1100000110 1010100010\n1 1\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "6 4\n2 2 9 5 9 6\n1 3\n2 4\n3 5\n4 6\n" }, "output": { "stdout": "WIN\n2 2 5 5 6 6 \n" } }, { "input": { "stdin": "3 2\n1 1 2\n3 2\n2 1\n" }, "output": { "stdout": "WIN\n2 0 2 \n" } }, { "input": { "stdin": "3 0\n4 1 8\n" }, "output": { "stdout": "WIN\n4 1 5 \n" } }, { "input": { "stdin": "2 0\n1111010100 1010100010\n2 0\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "1 0\n123456789 635671754\n" }, "output": { "stdout": "WIN\n0 \n" } }, { "input": { "stdin": "3 0\n4 2 8\n" }, "output": { "stdout": "WIN\n4 2 6 \n" } }, { "input": { "stdin": "2 0\n123456789 1174603023\n" }, "output": { "stdout": "WIN\n123456789 123456789 \n" } }, { "input": { "stdin": "6 4\n2 2 5 5 9 6\n1 3\n2 4\n3 5\n4 6\n" }, "output": { "stdout": "WIN\n2 2 5 5 6 6 \n" } }, { "input": { "stdin": "3 2\n2 0 2\n3 2\n2 1\n" }, "output": { "stdout": "LOSE\n" } }, { "input": { "stdin": "2 0\n1101010100 1010100010\n2 0\n" }, "output": { "stdout": "WIN\n1010100010 1010100010 \n" } }, { "input": { "stdin": "3 0\n7 2 8\n" }, "output": { "stdout": "WIN\n7 2 5 \n" } } ]
{ "tags": [ "games", "graphs" ], "title": "Election Promises" }
{ "cpp": "#include <bits/stdc++.h>\nconst int N = 200005;\nusing namespace std;\nlong long n, m, num[N], sy[N], ds[N], bj[N], sum[N];\nvector<long long> to[N], pre[N];\nqueue<long long> que;\nint main() {\n long long i, j, t, p, q;\n scanf(\"%d%d\", &n, &m);\n for (i = 1; i <= n; i++) scanf(\"%lld\", &num[i]);\n for (i = 1; i <= m; i++) {\n scanf(\"%lld%lld\", &p, &q);\n pre[q].push_back(p);\n to[p].push_back(q);\n ds[p]++;\n }\n for (i = 1; i <= n; i++)\n if (!ds[i]) que.push(i);\n while (!que.empty()) {\n q = que.front();\n que.pop();\n for (i = 0; i < pre[q].size(); i++) {\n t = pre[q][i];\n ds[t]--;\n if (!ds[t]) que.push(t);\n }\n for (i = 0; i < to[q].size(); i++) {\n t = to[q][i];\n bj[sy[t]] = q;\n }\n for (i = 0; bj[i] == q; i++)\n ;\n sy[q] = i;\n sum[sy[q]] ^= num[q];\n }\n for (i = n; i >= 0; i--) {\n if (!sum[i]) continue;\n puts(\"WIN\");\n for (j = 1; j <= n; j++)\n if (sy[j] == i && (num[j] ^ sum[i]) < num[j]) break;\n q = j, num[q] ^= sum[i];\n for (j = 0; j < to[q].size(); j++) {\n t = to[q][j];\n if (sum[sy[t]] && bj[sy[t]] != -1) {\n bj[sy[t]] = -1;\n num[t] ^= sum[sy[t]];\n }\n }\n for (i = 1; i <= n; i++) printf(\"%lld \", num[i]);\n return 0;\n }\n puts(\"LOSE\");\n}\n", "java": "import java.io.*;\nimport java.util.*;\nimport java.math.*;\nimport java.lang.*;\n \nimport static java.lang.Math.*;\n \npublic class Main implements Runnable {\n static class InputReader {\n private InputStream stream;\n private byte[] buf = new byte[1024];\n private int curChar;\n private int numChars;\n private SpaceCharFilter filter;\n private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n \n public InputReader(InputStream stream) {\n this.stream = stream;\n }\n \n public int read() {\n if (numChars==-1) \n throw new InputMismatchException();\n \n if (curChar >= numChars) {\n curChar = 0;\n try {\n numChars = stream.read(buf);\n }\n catch (IOException e) {\n throw new InputMismatchException();\n }\n \n if(numChars <= 0) \n return -1;\n }\n return buf[curChar++];\n }\n \n public String nextLine() {\n String str = \"\";\n try {\n str = br.readLine();\n }\n catch (IOException e) {\n e.printStackTrace();\n }\n return str;\n }\n public int nextInt() {\n int c = read();\n \n while(isSpaceChar(c)) \n c = read();\n \n int sgn = 1;\n \n if (c == '-') {\n sgn = -1;\n c = read();\n }\n \n int res = 0;\n do {\n if(c<'0'||c>'9') \n throw new InputMismatchException();\n res *= 10;\n res += c - '0';\n c = read();\n }\n while (!isSpaceChar(c)); \n \n return res * sgn;\n }\n \n public long nextLong() {\n int c = read();\n while (isSpaceChar(c))\n c = read();\n int sgn = 1;\n if (c == '-') {\n sgn = -1;\n c = read();\n }\n long res = 0;\n \n do {\n if (c < '0' || c > '9')\n throw new InputMismatchException();\n res *= 10;\n res += c - '0';\n c = read();\n }\n while (!isSpaceChar(c));\n return res * sgn;\n }\n \n public double nextDouble() {\n int c = read();\n while (isSpaceChar(c))\n c = read();\n int sgn = 1;\n if (c == '-') {\n sgn = -1;\n c = read();\n }\n double res = 0;\n while (!isSpaceChar(c) && c != '.') {\n if (c == 'e' || c == 'E')\n return res * Math.pow(10, nextInt());\n if (c < '0' || c > '9')\n throw new InputMismatchException();\n res *= 10;\n res += c - '0';\n c = read();\n }\n if (c == '.') {\n c = read();\n double m = 1;\n while (!isSpaceChar(c)) {\n if (c == 'e' || c == 'E')\n return res * Math.pow(10, nextInt());\n if (c < '0' || c > '9')\n throw new InputMismatchException();\n m /= 10;\n res += (c - '0') * m;\n c = read();\n }\n }\n return res * sgn;\n }\n \n public String readString() {\n int c = read();\n while (isSpaceChar(c))\n c = read();\n StringBuilder res = new StringBuilder();\n do {\n res.appendCodePoint(c);\n c = read();\n } \n while (!isSpaceChar(c));\n \n return res.toString();\n }\n \n public boolean isSpaceChar(int c) {\n if (filter != null)\n return filter.isSpaceChar(c);\n return c == ' ' || c == '\\n' || c == '\\r' || c == '\\t' || c == -1;\n }\n \n public String next() {\n return readString();\n }\n \n public interface SpaceCharFilter {\n public boolean isSpaceChar(int ch);\n }\n }\n public static void main(String args[]) throws Exception {\n new Thread(null, new Main(),\"Main\",1<<27).start();\n } \n void findCol(int i) {\n HashSet<Integer> set = new HashSet<>();\n\n for(int j : adj[i]) {\n if(col[j] == -1)\n findCol(j);\n\n set.add(col[j]);\n }\n\n for(int j = 0; ; ++j) {\n if(!set.contains(j)) {\n col[i] = j;\n break;\n }\n }\n }\n ArrayList<Integer> adj[];\n int col[];\n public void run() {\n InputReader sc = new InputReader(System.in);\n PrintWriter w = new PrintWriter(System.out);\n\n int n = sc.nextInt();\n int m = sc.nextInt();\n\n int h[] = new int[n];\n for(int i = 0; i < n; ++i)\n h[i] = sc.nextInt();\n\n adj = new ArrayList[n];\n for(int i = 0; i < n; ++i)\n adj[i] = new ArrayList<>();\n\n for(int i = 0; i < m; ++i) {\n int u = sc.nextInt() - 1;\n int v = sc.nextInt() - 1;\n adj[u].add(v);\n }\n\n col = new int[n];\n Arrays.fill(col, -1);\n for(int i = 0; i < n; ++i) {\n if(col[i] == -1)\n findCol(i);\n }\n\n int xor[] = new int[n];\n for(int i = 0; i < n; ++i)\n xor[col[i]] ^= h[i];\n\n int curCol = -1;\n for(int i = 0; i < n; ++i) {\n if(xor[i] != 0)\n curCol = i;\n }\n\n if(curCol == -1)\n w.print(\"LOSE\");\n else {\n w.println(\"WIN\");\n\n int ind = -1;\n for(int i = 0; i < n; ++i) {\n if(col[i] == curCol) {\n if((h[i] ^ xor[curCol]) < h[i])\n ind = i;\n }\n }\n\n h[ind] ^= xor[col[ind]];\n xor[col[ind]] = 0;\n\n for(int j : adj[ind]) {\n h[j] ^= xor[col[j]];\n xor[col[j]] = 0;\n }\n\n for(int i : h)\n w.print(i + \" \");\n }\n\n w.close();\n }\n}", "python": null }
Codeforces/1170/E
# Sliding Doors Imagine that you are the CEO of a big old-fashioned company. Unlike any modern and progressive company (such as JetBrains), your company has a dress code. That's why you have already allocated a spacious room for your employees where they can change their clothes. Moreover, you've already purchased an m-compartment wardrobe, so the i-th employee can keep his/her belongings in the i-th cell (of course, all compartments have equal widths). The issue has occurred: the wardrobe has sliding doors! More specifically, the wardrobe has n doors (numbered from left to right) and the j-th door has width equal to a_j wardrobe's cells. The wardrobe has single rails so that no two doors can slide past each other. <image> Extremely schematic example of a wardrobe: m=9, n=2, a_1=2, a_2=3. The problem is as follows: sometimes to open some cells you must close some other cells (since all doors are placed on the single track). For example, if you have a 4-compartment wardrobe (i.e. m=4) with n=2 one-cell doors (i.e. a_1=a_2=1) and you need to open the 1-st and the 3-rd cells, you have to close the 2-nd and the 4-th cells. As CEO, you have a complete schedule for the next q days. Now you are wondering: is it possible that all employees who will come on the k-th day can access their cells simultaneously? Input The first line contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ m ≀ 4 β‹… 10^5) β€” the number of doors and compartments respectively. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ m, βˆ‘{a_i} ≀ m) β€” the corresponding widths of the doors. The third line contains a single integer q (1 ≀ q ≀ 2 β‹… 10^5) β€” the number of days you have to process. The next q lines describe schedule of each day. Each schedule is represented as an integer c_k followed by c_k integers w_1, w_2, ..., w_{c_k} (1 ≀ c_k ≀ 2 β‹… 10^5, 1 ≀ w_1 < w_2 < ... < w_{c_k} ≀ m) β€” the number of employees who will come on the k-th day, and their indices in ascending order. It's guaranteed that βˆ‘{c_k} doesn't exceed 2 β‹… 10^5. Output Print q answers. Each answer is "YES" or "NO" (case insensitive). Print "YES" if it is possible, that all employees on the corresponding day can access their compartments simultaneously. Example Input 3 10 2 3 2 6 1 5 2 1 10 2 2 9 2 5 6 3 1 7 8 4 1 2 3 4 Output YES YES NO NO YES NO
[ { "input": { "stdin": "3 10\n2 3 2\n6\n1 5\n2 1 10\n2 2 9\n2 5 6\n3 1 7 8\n4 1 2 3 4\n" }, "output": { "stdout": "\nYES\nYES\nNO\nNO\nYES\nNO\n" } } ]
{ "tags": [ "*special", "binary search" ], "title": "Sliding Doors" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1189/D1
# Add on a Tree Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems. You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v. For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5. <image> Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations? Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice. Input The first line contains a single integer n (2 ≀ n ≀ 10^5) β€” the number of nodes. Each of the next n-1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree. Output If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO". Otherwise, output "YES". You can print each letter in any case (upper or lower). Examples Input 2 1 2 Output YES Input 3 1 2 2 3 Output NO Input 5 1 2 1 3 1 4 2 5 Output NO Input 6 1 2 1 3 1 4 2 5 2 6 Output YES Note In the first example, we can add any real x to the value written on the only edge (1, 2). <image> In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3). <image> Below you can see graphs from examples 3, 4: <image> <image>
[ { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "3\n1 2\n2 3\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "5\n1 2\n1 3\n1 4\n2 5\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "6\n1 2\n1 3\n1 4\n2 5\n2 6\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "50\n16 4\n17 9\n31 19\n22 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n10 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "10\n8 1\n1 2\n8 9\n8 5\n1 3\n1 10\n1 6\n1 7\n8 4\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "5\n5 1\n5 4\n4 3\n1 2\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "7\n1 2\n2 3\n1 4\n1 5\n3 6\n3 7\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "3\n1 3\n2 3\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n30 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n26 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "7\n1 2\n2 3\n3 4\n3 5\n1 6\n1 7\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "20\n19 16\n19 18\n20 7\n9 4\n6 17\n14 2\n9 15\n2 13\n5 11\n19 12\n12 20\n16 9\n11 8\n19 5\n3 1\n19 14\n5 3\n12 10\n19 6\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "10\n9 5\n7 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "4\n2 4\n2 3\n2 1\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "4\n1 4\n3 2\n1 3\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "3\n1 2\n1 3\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "5\n1 2\n1 5\n1 3\n1 4\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "20\n14 9\n12 13\n10 15\n2 1\n20 19\n16 6\n16 3\n17 14\n3 5\n2 11\n3 10\n15 8\n14 2\n6 4\n3 20\n5 18\n1 7\n1 16\n4 12\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n9 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "8\n1 2\n2 3\n3 4\n1 7\n1 8\n4 5\n4 6\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "5\n5 1\n5 2\n5 3\n5 4\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "20\n13 1\n18 2\n3 7\n18 5\n20 16\n3 12\n18 9\n3 10\n18 11\n13 6\n3 18\n20 15\n20 17\n3 13\n3 4\n13 14\n3 20\n18 8\n3 19\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "10\n8 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "60\n26 6\n59 30\n31 12\n32 3\n38 23\n59 29\n53 9\n59 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n20 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n49 46\n17 33\n30 31\n30 39\n26 19\n24 36\n8 49\n38 14\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10\n9 5\n4 1\n9 10\n7 2\n5 4\n9 6\n2 9\n10 8\n1 3\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10\n9 5\n4 1\n9 10\n7 1\n5 4\n9 6\n2 9\n10 8\n1 3\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10\n8 1\n1 2\n8 9\n8 5\n1 3\n3 10\n1 6\n1 7\n8 4\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "7\n1 2\n1 3\n2 4\n2 5\n1 6\n1 7\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "6\n1 2\n1 3\n2 4\n2 5\n2 6\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n59 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n20 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n49 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 17\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n23 40\n49 34\n49 17\n43 25\n19 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n48 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n23 40\n49 34\n49 17\n43 25\n19 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "7\n1 2\n1 3\n2 1\n2 5\n1 6\n3 7\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10\n8 2\n1 2\n8 9\n8 5\n1 3\n2 10\n1 6\n2 7\n8 4\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "6\n1 2\n1 3\n2 4\n4 2\n1 6\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "50\n49 6\n43 7\n1 27\n19 35\n15 37\n16 12\n19 21\n16 28\n49 9\n48 39\n13 1\n2 48\n9 50\n44 3\n41 32\n48 31\n49 33\n6 11\n13 20\n49 22\n13 41\n1 29\n13 46\n15 47\n34 2\n49 13\n48 14\n34 24\n16 36\n13 40\n49 34\n49 17\n43 25\n11 23\n10 15\n19 26\n34 44\n16 42\n19 18\n46 8\n29 38\n1 45\n12 43\n13 16\n46 30\n15 5\n49 10\n11 19\n32 4\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10\n1 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n2 3\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "20\n7 5\n14 14\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n11 3\n15 2\n17 4\n2 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "20\n7 5\n14 14\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n2 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "4\n1 4\n2 2\n2 4\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "50\n16 4\n17 9\n31 19\n3 10\n8 1\n40 30\n3 31\n20 29\n47 27\n22 25\n32 34\n12 15\n40 32\n20 33\n47 12\n6 24\n46 41\n14 23\n12 35\n31 42\n46 28\n31 20\n46 37\n1 39\n29 49\n37 47\n40 6\n42 36\n47 2\n24 46\n2 13\n8 45\n41 3\n32 17\n4 7\n47 26\n28 8\n41 50\n34 44\n33 21\n25 5\n16 40\n3 14\n8 18\n28 11\n32 22\n2 38\n3 48\n44 43\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "10\n1 2\n5 6\n1 8\n2 9\n1 4\n8 10\n10 5\n2 7\n3 3\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "20\n7 5\n14 13\n17 6\n3 8\n16 12\n18 9\n3 18\n14 1\n17 3\n15 2\n17 4\n2 11\n2 7\n15 17\n3 20\n16 10\n17 14\n2 16\n1 19\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "60\n26 6\n59 30\n31 12\n31 3\n38 23\n59 29\n53 9\n38 56\n53 54\n29 21\n17 55\n59 38\n26 16\n24 59\n24 25\n17 35\n24 41\n30 15\n31 27\n8 44\n26 5\n26 48\n8 32\n53 17\n3 34\n3 51\n20 28\n47 10\n53 60\n36 42\n24 53\n59 22\n53 40\n26 52\n36 4\n59 8\n29 37\n36 20\n17 47\n53 18\n3 50\n30 2\n17 7\n8 58\n59 1\n31 11\n24 26\n24 43\n53 57\n59 45\n47 13\n49 46\n17 33\n30 31\n26 39\n26 19\n24 36\n8 49\n38 14\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "5\n5 1\n5 4\n4 3\n2 2\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "6\n1 2\n1 3\n2 4\n4 2\n2 6\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "7\n1 2\n2 3\n3 5\n3 5\n1 6\n1 7\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "7\n1 2\n3 3\n1 4\n1 5\n3 6\n3 7\n" }, "output": { "stdout": "YES\n" } } ]
{ "tags": [ "trees" ], "title": "Add on a Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9 + 7;\nconst int BASE = 1e9 + 7;\nconst int dx[4] = {-1, 1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nconst int N = 1e5 + 1;\nvector<int> a[N];\nint pa[N], n;\nint F[N];\nvoid DFS(int p, int u) {\n bool Check = true;\n for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) {\n int v = *it;\n if (v == p) continue;\n Check = false;\n DFS(u, v);\n pa[v] = u;\n F[u] = F[u] + F[v];\n }\n F[u] += Check;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n cin >> n;\n int u, v;\n for (int i = 1; i < n; i++) {\n cin >> u >> v;\n a[u].push_back(v);\n a[v].push_back(u);\n }\n bool Check = true;\n for (int u = 1; u <= n; u++) {\n for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) {\n int v = *it;\n if (a[u].size() == 1) {\n if (a[v].size() == 1) continue;\n if (a[v].size() < 3) {\n Check = false;\n }\n } else if (a[v].size() == 1) {\n if (a[u].size() == 1) continue;\n if (a[u].size() < 3) {\n Check = false;\n }\n } else {\n if (a[v].size() < 3 || a[u].size() < 3) {\n Check = false;\n }\n }\n }\n }\n cout << (Check ? \"YES\" : \"NO\");\n}\n", "java": "import java.util.*;\nimport java.io.*;\nimport java.math.*;\n \npublic class Solution {\n static PrintWriter out = new PrintWriter(System.out);\n static Reader in = new Reader();\n public static void main(String[] args) throws IOException {\n //PrintWriter out = new PrintWriter(new File(\"out.txt\"));\n //Reader in = new Reader(new FileInputStream(\"in.txt\"));\n Solution solver = new Solution();\n solver.solve();\n out.flush();\n out.close();\n \n }\n \n static int maxn = 5*(int)1e5+5;\n static int mod=(int)1e9+7;\n static int n, m, t, k, q;\n \n static int[] deg;\n \n void solve() throws IOException{\n n = in.nextInt();\n \n deg = new int[n+1];\n \n for (int i = 1; i < n; i++) {\n deg[in.nextInt()]++;\n deg[in.nextInt()]++;\n }\n \n boolean yes = true;\n for (int i = 1;i <= n; i++)\n if (deg[i] == 2)\n yes = false;\n \n if(yes) out.println(\"YES\");\n else out.println(\"NO\");\n }\n \n //<>\n \n static class Reader {\n \n private InputStream mIs;\n private byte[] buf = new byte[1024];\n private int curChar;\n private int numChars;\n \n public Reader() {\n this(System.in);\n }\n \n public Reader(InputStream is) {\n mIs = is;\n }\n \n public int read() {\n if (numChars == -1) {\n throw new InputMismatchException();\n \n }\n if (curChar >= numChars) {\n curChar = 0;\n try {\n numChars = mIs.read(buf);\n } catch (IOException e) {\n throw new InputMismatchException();\n }\n if (numChars <= 0) {\n return -1;\n }\n }\n return buf[curChar++];\n }\n \n public String nextLine() {\n int c = read();\n while (isSpaceChar(c)) {\n c = read();\n }\n StringBuilder res = new StringBuilder();\n do {\n res.appendCodePoint(c);\n c = read();\n } while (!isEndOfLine(c));\n return res.toString();\n }\n \n public String next() {\n int c = read();\n while (isSpaceChar(c)) {\n c = read();\n }\n StringBuilder res = new StringBuilder();\n do {\n res.appendCodePoint(c);\n c = read();\n } while (!isSpaceChar(c));\n return res.toString();\n }\n \n double nextDouble()\n {\n return Double.parseDouble(next());\n }\n \n public long nextLong() {\n int c = read();\n while (isSpaceChar(c)) {\n c = read();\n }\n int sgn = 1;\n if (c == '-') {\n sgn = -1;\n c = read();\n }\n long res = 0;\n do {\n if (c < '0' || c > '9') {\n throw new InputMismatchException();\n }\n res *= 10;\n res += c - '0';\n c = read();\n } while (!isSpaceChar(c));\n return res * sgn;\n }\n \n public int nextInt() {\n int c = read();\n while (isSpaceChar(c)) {\n c = read();\n }\n int sgn = 1;\n if (c == '-') {\n sgn = -1;\n c = read();\n }\n int res = 0;\n do {\n if (c < '0' || c > '9') {\n throw new InputMismatchException();\n }\n res *= 10;\n res += c - '0';\n c = read();\n } while (!isSpaceChar(c));\n return res * sgn;\n }\n \n public boolean isSpaceChar(int c) {\n return c == ' ' || c == '\\n' || c == '\\r' || c == '\\t' || c == -1;\n }\n \n public boolean isEndOfLine(int c) {\n return c == '\\n' || c == '\\r' || c == -1;\n }\n \n }\n}\n", "python": "from __future__ import division, print_function\n\ndef main():\n n = int(input())\n d = array_of(int, n)\n \n for _ in range(n-1):\n u, v = input_as_list()\n d[u-1] += 1\n d[v-1] += 1\n \n print(\"NO\" if 2 in d else \"YES\")\n \n\nINF = float('inf')\nMOD = 10**9 + 7\n\nimport os, sys\nfrom atexit import register\nfrom io import BytesIO\nimport itertools\n\nif sys.version_info[0] < 3:\n input = raw_input\n range = xrange\n\n filter = itertools.ifilter\n map = itertools.imap\n zip = itertools.izip\n\nif \"LOCAL_\" in os.environ:\n debug_print = print\nelse:\n sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))\n sys.stdout = BytesIO()\n register(lambda: os.write(1, sys.stdout.getvalue()))\n\n input = lambda: sys.stdin.readline().rstrip('\\r\\n')\n debug_print = lambda *x, **y: None\n\n\ndef input_as_list():\n return list(map(int, input().split()))\n\ndef array_of(f, *dim):\n return [array_of(f, *dim[1:]) for _ in range(dim[0])] if dim else f()\n\nmain()" }
README.md exists but content is empty.
Downloads last month
129