problem_id
stringlengths
32
32
name
stringclasses
1 value
problem
stringlengths
200
14k
solutions
stringlengths
12
1.12M
test_cases
stringlengths
37
74M
difficulty
stringclasses
3 values
language
stringclasses
1 value
source
stringclasses
7 values
num_solutions
int64
12
1.12M
starter_code
stringlengths
0
956
e838332c05fa3e3275689d1373bb240b
UNKNOWN
Raj loves to listen to songs in his free time. It’s his birthday tomorrow and his friend Neelansh wants his gift to be the most unique. Being great at making music, he decides to produce a song for him. However, Raj likes songs according to their beauty. He determines the beauty of the song as the number of times all the octave musical tones are completed in ascending order. He begins with a jumbled tone of length N and numbers each octave tone as 1,2,3….8. Neelansh wants to maximize the beauty of the song but since he uses the trial version of the software, - He cannot change the size of N. - He cannot introduce any new tone, but can choose any two tones and swap their positions However, Neelansh just received a mail that he needs to submit all his pending assignments by tomorrow. He has tons of assignments left to do, but he doesn’t want to spoil the idea of his gift. Can you help him? -----INPUT----- - The first line contains a single integer T- the number of test cases - The first line of each test case contains a single integer N- the length of the song - The second line contains N- space separated integers ai, ai+1,.....aN -----OUTPUT----- For each test case, print a single line containing one integer- the maximum possible beauty of the song -----CONSTRAINTS----- 1<=T<=102 1<=N<=105 1<=a<=8 -----EXAMPLE INPUT----- 2 8 1 2 3 4 5 6 7 8 16 1 2 1 2 3 3 4 4 5 5 6 6 7 8 7 8 -----EXAMPLE OUTPUT----- 1 2
["for _ in range(int(input())):\r\n n = int(input())\r\n ar = list(map(int,input().split()))\r\n d = {}\r\n for ele in ar:\r\n if ele in d:\r\n d[ele] += 1\r\n else:\r\n d[ele] = 1\r\n m = 99999\r\n count = 0\r\n for ele in d:\r\n count+=1\r\n if m>d[ele]:\r\n m = d[ele]\r\n if count!=8:\r\n print(0)\r\n else:\r\n print(m)\r\n", "for _ in range(int(input())):\n n = int(input())\n a = list(map(int, input().split()))\n dic = {}\n for i in range(1,9):\n dic[i] = a.count(i)\n ans = min(dic.values())\n print(ans)\n \n \n ", "# cook your dish here\ntry:\n for _ in range(int(input())):\n n=input()\n arr=list(map(int,input().split()))\n s=[1,2,3,4,5,6,7,8]\n mini=100000\n for x in s:\n coun=arr.count(x)\n if coun<mini:\n mini=coun\n print(mini)\nexcept:\n pass", "# cook your dish here\nfrom collections import Counter\nimport string\nimport math\nimport sys\ndef array_int():\n return [int(i) for i in sys.stdin.readline().split()]\ndef vary(arrber_of_variables):\n if arrber_of_variables==1:\n return int(sys.stdin.readline())\n if arrber_of_variables>=2:\n return list(map(int,sys.stdin.readline().split())) \ndef makedict(var):\n return dict(Counter(var))\n# i am noob wanted to be better and trying hard for that\ndef printDivisors(n): \n divisors=[] \n # Note that this loop runs till square root \n i = 1\n while i <= math.sqrt(n): \n \n if (n % i == 0) : \n \n # If divisors are equal, print only one \n if (n//i == i) : \n divisors.append(i) \n else : \n # Otherwise print both \n divisors.extend((i,n//i)) \n i = i + 1\n return divisors\n\ndef countTotalBits(num):\n \n # convert number into it's binary and \n # remove first two characters 0b.\n binary = bin(num)[2:]\n return(len(binary))\nfor _ in range(vary(1)):\n n=vary(1)\n num=array_int()\n nt=makedict(num)\n st=set((1,2,3,4,5,6,7,8))\n for i in nt:\n if i in st:\n st.remove(i)\n if len(st)!=0:\n print(0)\n else:\n mini=float('inf')\n for i in nt:\n if nt[i]<mini:\n mini=nt[i]\n \n print(mini)\n\n\n\n \n \n \n \n \n \n\n\t\n\n\n\n \n\n\n\n\n\n\n \n \n\n \n \n\n\n \n\n \n\n\n\n \n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n \n\n \n \n\n\n\n\n\n \n \n \n", "T = int(input())\n\nfor case in range(T):\n n = int(input())\n a = list(map(int, input().split()))\n time = []\n for i in range(1, 9):\n time.append(a.count(i))\n \n print(min(time))", "for _ in range(int(input())):\r\n n=int(input())\r\n a=list(map(int,input().split()))\r\n b=[0 for x in range(11)]\r\n for i in a:\r\n b[i]+=1\r\n ans=b[1]\r\n for i in range(1,9):\r\n ans=min(ans,b[i])\r\n print(ans)", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n arr = list(map(int, input().split()))\n\n arr_c = [arr.count(i) for i in range(1, 9)]\n print(min(arr_c))", "for _ in range(int(input())):\r\n n = int(input())\r\n l = list(map(int,input().split()))\r\n d = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0}\r\n\r\n for i in l:\r\n d[i]+=1\r\n\r\n print(min(d.values()))\r\n\r\n \r\n \r\n", "for j in range(int(input())):\r\n n=int(input())\r\n nums=input().strip()\r\n octave=[]\r\n for i in set(nums):\r\n octave.append(nums.count(i))\r\n octave.sort()\r\n if len(set(nums))==9:\r\n print(octave[0])\r\n else:\r\n print(0)", "for j in range(int(input())):\r\n n=int(input())\r\n nums=input().strip()\r\n octave=[]\r\n for i in set(nums):\r\n octave.append(nums.count(i))\r\n octave.sort()\r\n if len(set(nums))==9:\r\n print(octave[0])\r\n else:\r\n print(0)", "try:\r\n for z in range(int(input())):\r\n n=int(input())\r\n a=list(map(int,input().split()))\r\n z=max(a)\r\n b=[]\r\n for i in range(z):\r\n b.append(a.count(i+1))\r\n print(min(b))\r\nexcept:\r\n pass", "# cook your dish here\nt=int(input())\nfor test in range(t):\n n=int(input())\n mydict={}\n arr=list(map(int,input().strip().split()))\n minimum=9999999\n for i in range(1,9):\n mydict[i]=arr.count(i)\n if mydict[i]<minimum:\n minimum=mydict[i]\n print(minimum)\n ", "for _ in range(int(input())):\r\n n, val = int(input()), list(map(int,input().split()))\r\n total_ct = 0\r\n counts = []\r\n for i in range(1,9):\r\n counts.append(val.count(i))\r\n total_ct = min(counts)\r\n print(total_ct)", "T = int(input())\nfor _ in range(T):\n N = int(input())\n a = sorted(list(map(int, input().split())))\n count = 0\n b = []\n for i in range(1, 9):\n b.append(a.count(i))\n print(min(b))\n \n", "n = int(input())\n\nfor _ in range(n):\n l = int(input())\n t = map(int,input().split())\n accumm = [0]*8\n for i in t:\n accumm[i-1] +=1\n print(min(accumm))\n", "from collections import Counter\r\nfor _ in range(int(input())):\r\n n = int(input())\r\n cnt = Counter()\r\n l = list(map(int,input().split()))\r\n for i in l:\r\n cnt[i]+=1\r\n if len(cnt.keys())==8:\r\n print(min(set(cnt.values()))) \r\n else:\r\n print(0)", "for i in range(int(input())):\r\n x=int(input())\r\n s=list(input())\r\n ans=[]\r\n for j in range(1,9):\r\n ans.append(s.count(str(j)))\r\n\r\n print(min(ans))\r\n\r\n\r\n", "for _ in range(int(input())):\r\n\tn = int(input())\r\n\tarr = [int(x) for x in input().split()]\r\n\tres = [0]*8\r\n\tfor i in range(8):\r\n\t\tres[i] = arr.count(i+1)\r\n\tprint(min(res))", "# cook your dish here\nt = int(input())\nwhile(t):\n n = int(input())\n notes = [int(x) for x in input().split()]\n count = []\n for i in range(1, 9):\n count.append(notes.count(i))\n print(min(count))\n t-= 1\n", "t=int(input())\nfor _ in range(t):\n n=int(input())\n a=list(map(int,input().split()))\n nums=[0,0,0,0,0,0,0,0]\n for i in a:\n nums[i-1]+=1\n if(min(nums)==0):\n print(0)\n else:\n print(min(nums))# cook your dish here\n", "for _ in range(int(input())):\r\n n = int(input())\r\n a = list(map(int, input().split()))\r\n d = {i: 0 for i in range(1, 9)}\r\n for i in a:\r\n d[i] = d[i] + 1\r\n print(min(d.values()))\r\n", "for _ in range(int(input())):\r\n n=int(input())\r\n s=input()\r\n d={}\r\n for i in s:\r\n try:\r\n d[i]+=1\r\n except:\r\n d[i]=1\r\n ans=1000000000\r\n for i in range(1,9):\r\n x=str(i)\r\n try:\r\n ans=min(ans,d[x])\r\n except:\r\n ans=0\r\n print(ans)", "t = int(input())\nwhile(t):\n n = int(input())\n notes = [int(x) for x in input().split()]\n count = []\n for i in range(1,9):\n count.append(notes.count(i))\n print(min(count))\n t -= 1", "for _ in range(int(input())):\n n=int(input())\n l=[int(c) for c in input().split()]\n t=[0 for i in range(8)]\n for i in l:\n t[i-1]+=1\n print(min(t))\n \n# cook your dish here\n"]
{"inputs": [["2", "8", "1 2 3 4 5 6 7 8", "16", "1 2 1 2 3 3 4 4 5 5 6 6 7 8 7 8"]], "outputs": [["1", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
7,645
d573f7fb6c7f7808a30110c5d361f48e
UNKNOWN
You are given a sequence $A_1, A_2, \ldots, A_N$. You may perform the following operation an arbitrary number of times (including zero): choose two adjacent elements of this sequence, i.e. $A_i$, $A_{i+1}$ for some valid $i$, and swap them. However, for each valid $i$, it is not allowed to choose $A_i$ (the element with the index $i$, regardless of its value at any point in time) more than once in total during this process. Find the maximum of the sum $S = \sum_{i=1}^N A_i \cdot i$. -----Input----- - The first line of the input contains a single 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 integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the maximum possible value of $S$. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10^5$ - $1 \le A_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $10^6$ -----Subtasks----- Subtask #1 (50 points): - $N \le 20$ - the sum of $N$ over all test cases does not exceed $200$ Subtask #2 (50 points): original constraints -----Example Input----- 2 4 2 1 4 3 4 7 6 3 2 -----Example Output----- 30 39 -----Explanation----- Example case 1: Swap the first and second element of the initial sequence. Then, swap the third and fourth element of the resulting sequence. The final sequence $A$ is $(1, 2, 3, 4)$. Example case 2: Swap the second and third element to make the sequence $(7, 3, 6, 2)$.
["# cook your dish here\n# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n l=list(map(int, input().split()))\n l.insert(0, 0)\n l1=[0]*(n+1)\n l1[1]=l[1]\n for i in range(2, n+1):\n l1[i]=max(l1[i-1]+l[i]*i, l1[i-2]+l[i-1]*i+l[i]*(i-1))\n \n print(l1[-1]) ", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n l=list(map(int, input().split()))\n l.insert(0, 0)\n l1=[0]*(n+1)\n l1[1]=l[1]\n for i in range(2, n+1):\n l1[i]=max(l1[i-1]+l[i]*i, l1[i-2]+l[i-1]*i+l[i]*(i-1))\n \n print(l1[-1]) ", "# cook your dish here\n# cook your dish here\nfor t in range(int(input())):\n n = int(input())\n a = list(map(int,input().split()))\n dp = [0 for i in range(n+1)]\n dp[1] = a[0]\n for i in range(2,n+1):\n dp[i] = max(dp[i-2] + a[i-2]*(i) + a[i-1]*(i-1), dp[i-1] + a[i-1]*(i))\n print(dp[n])", "for j in range(int(input())):\n n = int(input())\n x = list(map(int,input().split()))\n aman = [0,x[0]]\n for i in range(2,n+1):\n aman.append(max(aman[i-1]+x[i-1]*i,aman[i-2]+x[i-1]*(i-1)+x[i-2]*(i)))\n print(aman[-1])", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n dp = [ 0 for i in range(n+1)]\n dp[1] = l[0]\n \n for i in range(2,n+1):\n \n dp[i] = max(dp[i-1] + i*l[i-1], dp[i-2] + i*l[i-2] + (i-1)*l[i-1])\n \n print(dp[n])", "for _ in range(int(input())):\n \n n = int(input())\n l = list(map(int, input().split()))\n \n \n dp = [ 0 for i in range(n+1)]\n dp[1] = l[0]\n \n for i in range(2,n+1):\n \n dp[i] = max(dp[i-1] + i*l[i-1], dp[i-2] + i*l[i-2] + (i-1)*l[i-1])\n \n print(dp[n])\n \n", "# cook your dish here\n#SFRV\nfor _ in range(int(input())):\n n = int(input())\n l = list(map(int,input().rstrip().split()))\n dp = [0]*(n+1)\n for i in range(n+1):\n if i==0:\n dp[i]=0\n elif i==1:\n dp[i] = l[i-1]\n else:\n dp[i] = max(dp[i-1] + l[i-1]*i, dp[i-2] + l[i-1]*(i-1) + l[i-2]*i)\n\n print(dp[n])", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n l = list(map(int,input().rstrip().split()))\n dp = [0]*(n+1)\n\n for i in range(n+1):\n if i==0:\n dp[i]=0\n elif i==1:\n dp[i] = l[i-1]\n else:\n ##dekho swap kare ya nahi but prev wala\n dp[i] = max(dp[i-1] + l[i-1]*i, dp[i-2] + l[i-1]*(i-1) + l[i-2]*i)\n\n print(dp[n])", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n a = list(map(int,input().split()))\n dp = [0,a[0]]\n for i in range(2,n+1):\n dp.append(max(dp[i-1]+a[i-1]*i,dp[i-2]+a[i-1]*(i-1)+a[i-2]*(i)))\n print(dp[-1])", "for t in range(int(input())):\n n = int(input())\n li = [0] + list(map(int, input().split()))\n \n ans = [0, li[1]]\n for i in range(2,n+1):\n ans += [max(ans[i-1]+li[i]*i, ans[i-2]+li[i-1]*(i)+li[i]*(i-1))]\n print(ans[-1])", "# cook your dish here\nfor t in range(int(input())):\n n = int(input())\n a = list(map(int,input().split()))\n dp = [0 for i in range(n+1)]\n \n dp[1] = a[0]\n #print(dp)\n for i in range(2,n+1):\n dp[i] = max(dp[i-2] + a[i-2]*(i) + a[i-1]*(i-1), dp[i-1] + a[i-1]*(i))\n #print('Pass {}: {}'.format(i,dp))\n print(dp[n])", "for _ in range(int(input())):\n n = int(input())\n a = list(map(int,input().split()))\n dp = [0]*n\n dp[0] = a[0]\n dp[1] = max(dp[0]+a[1]*2,a[1]*1+a[0]*2)\n for i in range(2,n):\n dp[i] = max(dp[i-1]+a[i]*(i+1), dp[i-2]+a[i-1]*(i+1)+a[i]*i)\n print(dp[-1])", "def function(n,arr,i,dp) :\n if i >= n-1 :\n sum = 0\n pointer = 1\n s = ''\n #print('arr:',arr)\n for k in arr :\n sum+=(k*pointer)\n pointer+=1\n s+=str(k)\n dp[s] = sum\n return sum\n s = ''\n for l in arr :\n s+=str(l)\n if dp.get(s,-1) == -1 :\n val = function(n,arr,i+1,dp)\n else :\n val = dp[s]\n for j in range(i,n-1) :\n arr[j],arr[j+1] = arr[j+1],arr[j]\n s = ''\n for l in arr :\n s+=str(l)\n if dp.get(s,-1) == -1 :\n val1 = function(n,arr,j+2,dp) \n else :\n val1 = dp[s]\n val = val if val > val1 else val1\n arr[j],arr[j+1] = arr[j+1],arr[j]\n return val\n\ntimes = int(input())\nwhile times > 0 :\n times-=1\n n = int(input())\n arr = [int(i) for i in input().split()]\n dp = {}\n print(function(n,arr,0,dp))", "for t in range(int(input())):\n n = int(input())\n a = list(map(int,input().split(\" \")))\n k = n + 1\n dp = [0]*k\n dp[1] = a[0]\n \n for i in range(2,n+1):\n dp[i] = max( dp[i-1] + a[i-1]*i , dp[i-2] + a[i-2]*i + a[i-1]*(i-1) )\n \n print(dp[n])"]
{"inputs": [["2", "4", "2 1 4 3", "4", "7 6 3 2"]], "outputs": [["30", "39"]]}
INTERVIEW
PYTHON3
CODECHEF
4,355
d1eb85c4a8b35acf7d294b6206c9a059
UNKNOWN
Mr. Wilson was planning to record his new Progressive Rock music album called "Digits. Cannot. Separate". Xenny and PowerShell, popular pseudo-number-theoreticists from the Land of Lazarus were called by him to devise a strategy to ensure the success of this new album. Xenny and Powershell took their Piano Lessons and arrived at the Studio in different Trains. Mr. Wilson, creative as usual, had created one single, long music track S. The track consisted of N musical notes. The beauty of each musical note was represented by a decimal digit from 0 to 9. Mr. Wilson told them that he wanted to create multiple musical tracks out of this long song. Since Xenny and Powershell were more into the number theory part of music, they didn’t know much about their real workings. Mr. Wilson told them that a separator could be placed between 2 digits. After placing separators, the digits between 2 separators would be the constituents of this new track and the number formed by joining them together would represent the Quality Value of that track. He also wanted them to make sure that no number formed had greater than M digits. Mr. Wilson had Y separators with him. He wanted Xenny and PowerShell to use at least X of those separators, otherwise he would have to ask them to Drive Home. Xenny and PowerShell knew straight away that they had to put place separators in such a way that the Greatest Common Divisor (GCD) of all the Quality Values would eventually determine the success of this new album. Hence, they had to find a strategy to maximize the GCD. If you find the maximum GCD of all Quality Values that can be obtained after placing the separators, Xenny and PowerShell shall present you with a Porcupine Tree. Note: - You can read about GCD here. - Greatest Common Divisor of 0 and 0 is defined as 0. -----Input----- The first line of input consists of a single integer T - the number of testcases. Each test case is of the following format: First line contains a single integer N - the length of the long musical track. Second line contains the string of digits S. Third line contains 3 space-separated integers - M, X and Y - the maximum number of digits in a number, the minimum number of separators to be used and the maximum number of separators to be used. -----Output----- For each testcase, output a single integer on a new line - the maximum GCD possible after placing the separators. -----Constraints----- Subtask 1: 20 points - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 18 - 1 ≤ M ≤ 2 - 1 ≤ X ≤ Y ≤ (N - 1) Subtask 2: 80 points - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 300 - 1 ≤ M ≤ 10 - 1 ≤ X ≤ Y ≤ (N - 1) For both Subtask 1 and Subtask 2: - 1 ≤ X ≤ Y ≤ (N - 1) - M*(Y+1) ≥ N - S may contain leading 0s. -----Example-----Input: 2 3 474 2 1 1 34 6311861109697810998905373107116111 10 4 25 Output: 2 1 -----Explanation----- Test case 1. Since only 1 separator can be placed, we can only have 2 possibilities: a. 4 | 74 The GCD in this case is 2. b. 47 | 4 The GCD in this case is 1. Hence, the maximum GCD is 2. Test case 2. One of the optimal partitions is: 63|118|61|109|69|78|109|98|90|53|73|107|116|111 Bonus: Decode the above partition to unlock a hidden treasure.
["\n\n\nt = int(input())\n\ndef gcd (a, b):\n if (b == 0): return a\n return gcd (b, a % b)\n\ndp = {}\n\ndef solve (p, k, g, s, m, x, y, n):\n if ((p, k, g) in dp): return dp[(p, k, g)];\n \n ans = 0\n\n if (p == n): \n if k >= x and k <= y:\n ans = g\n else:\n ans = 0 \n else: \n for i in range (p, n):\n \n if (i - p + 1 > m): break\n \n temp = solve (i + 1, k + 1, gcd(g, int(s[p:i + 1])), s, m, x, y, n)\n if (temp > ans):\n ans = temp\n \n dp[(p, k, g)] = ans\n return ans\n \n\nwhile t != 0:\n dp = {}\n t -= 1\n n = int(input())\n s = input()\n m, x, y = list(map (int, input().split()))\n x += 1\n y += 1\n \n print(solve (0, 0, 0, s, m, x, y, n))\n \n"]
{"inputs": [["2", "3", "474", "2 1 1", "34", "6311861109697810998905373107116111", "10 4 25"]], "outputs": [["2", "1"]]}
INTERVIEW
PYTHON3
CODECHEF
689
45ac45554cf2cebbb861018faad35fa2
UNKNOWN
In an array, a $block$ is a maximal sequence of identical elements. Since blocks are maximal, adjacent blocks have distinct elements, so the array breaks up into a series of blocks. For example, given the array $[3, 3, 2, 2, 2, 1, 5, 8, 4, 4]$, there are 6 blocks: $[3, 3], [2, 2, 2], [1], [5], [8], [4, 4]$ . In this task, you are given two arrays, $A$ (of length $n$), and $B$ (of length $m$), and a number $K$. You have to interleave $A$ and $B$ to form an array $C$ such that $C$ has $K$ blocks. Each way of interleaving $A$ and $B$ can be represented as a $0-1$ array $X$ of length $n+m$ in which $X[j]$ is $0$ if $C[j]$ came from $A$ and $X[j]$ is $1$ if $C[j]$ came from $B$. A formal description of the interleaving process is given at the end. For example, if $A = [1, 3]$ and $B = [3, 4]$, there are $6$ ways of interleaving $A$ and $B$. With each interleaving $X$ of $A$ and $B$, we also count the number of blocks in the resulting interleaved array $C$. The descriptions of the interleavings, $X$, and the outcomes, $C$, are given below. - $X = [0, 0, 1, 1]$, which corresponds to $C = [1, 3, 3, 4]$, $3$ blocks. - $X = [0, 1, 0, 1]$, which corresponds to $C = [1, 3, 3, 4]$, $3$ blocks. - $X = [0, 1, 1, 0]$, which corresponds to $C = [1, 3, 4, 3]$ , $4$ blocks. - $X = [1, 0, 0, 1]$, which corresponds to $C = [3, 1, 3, 4]$, $4$ blocks. - $X = [1, 0, 1, 0]$, which corresponds to $C = [3, 1, 4, 3]$, $4$ blocks. - $X = [1, 1, 0, 0]$, which corresponds to $C = [3, 4, 1, 3]$, $4$ blocks. Observe that different interleavings $X$ may produce the same array $C$, such as the first two interleavings in the example above. Your task is the following. Given arrays $A$ and $B$ and a number $K$, find the number of different interleavings $X$ of $A$ and $B$ that produce an output array $C$ with exactly $K$ blocks. Note that we are counting the number of interleavings, not the number of different output arrays after interleaving. For instance, if the same output array C is produced via 2 different interleavings, it gets counted twice. Since the answer might be large, print the answer modulo $10^8 + 7$. Here is a formal definition of the interleaving process: Suppose $A = A_1, A_2, ..., A_n$ and $B = B_1, B_2, ..., B_m$. Then, the process of generating an interleaving $C$ can be described using an array $X$ of size $n + m$, with exactly $n$ $0's$ and $m$ $1's$. Suppose we have such an array $X = X_1, X_2, ..., X_{n+m}$. Using this array $X$, we create the output array $C = C_1, C_2, ..., C_{n+m}$, using the following algorithm: i = 0, j = 0 while( (i+j)<(n+m) ) if(X[i+j+1] == 0) C[i+j+1] = A[i+1] i = i+1 else C[i+j+1] = B[j+1] j = j+1 Thus if the $X$ value is $0$, we pick the next available element from $A$ into $C$, and if it is $1$, we pick from $B$ instead. This creates an interleaving of the arrays $A$ and $B$. -----Input Format:----- - The first line contains a single integer, $T$, which is the number of testcases. The description of each testcase follows. - The first line of each testcase contains three integers: $n$, $m$, and $K$, which denote the size of array $A$, the size of array $B$, and the required number of blocks in $C$, respectively. - The next line contains $n$ integers, which represent the array $A$. - The next line contains $m$ integers, which represent the array $B$. -----Output Format:----- - You should print the answer in a new line for each testcase, which should be the number of valid interleaving arrays $X$ which correspond to an output array $C$ with $K$ blocks, modulo $10^8 + 7$. -----Constraints:----- - $1 \leq T \leq 10$ - $1 \leq n \leq 100$ - $1 \leq m \leq 100$ - $1 \leq K \leq n+m$ - $0 \leq A_i, B_j \leq 10^9$ -----Subtasks:----- - Subtask 1: 10% points: $m = 1$ - Subtask 2: 30% points: $0 \leq A_i, B_j \leq 1$ - Subtask 3: 60% points: Original constraints. -----Sample Input:----- 5 2 2 4 1 3 3 4 2 2 3 1 3 3 4 2 2 2 1 3 3 4 2 2 4 4 7 8 5 2 2 2 4 7 8 5 -----Sample Output:----- 4 2 0 6 0 -----Explanation:----- - The first three testcases correspond to the example given in the problem statement. Of the $6$ interleavings, $4$ produce outputs with $4$ blocks and $2$ produce outputs with $3$ blocks. Hence, for $K = 4$, the answer is $4$, for $K = 3$, the answer is $2$, and for $K = 2$, the answer is $0$. - The fourth and fifth testcases have $A = [4, 7]$ and $B = [8, 5]$. Here are the $6$ interleavings of these two arrays. - $X = [0, 0, 1, 1]$, which corresponds to $C= [4, 7, 8, 5]$, $4$ blocks. - $X = [0, 1, 0, 1]$, which corresponds to $C= [4, 8, 7, 5]$, $4$ blocks. - $X = [0, 1, 1, 0]$, which corresponds to $C= [4, 8, 5, 7]$, $4$ blocks. - $X = [1, 0, 0, 1]$, which corresponds to $C= [8, 4, 7, 5]$, $4$ blocks. - $X = [1, 0, 1, 0]$, which corresponds to $C= [8, 4, 5, 7]$, $4$ blocks. - $X = [1, 1, 0, 0]$, which corresponds to $C= [8, 5, 4, 7]$, $4$ blocks. All $6$ interleavings produce outputs with $4$ blocks, so for $K = 4$ the answer is $6$ and for any other value of $K$, the answer is $0$.
["# cook your dish here\n# cook your dish here\nn=0;m=0;\nA=[];B=[];\nanscount=0;k=0;\n\ndef generate(n,m,l):\n nonlocal anscount\n if(len(l)==n+m):\n X=l\n i,j = 0,0\n C=[0 for t in range(n+m)]\n while((i+j)<(n+m)):\n if(X[i+j] == 0):\n C[i+j] = A[i]\n i = i+1\n else:\n C[i+j] = B[j]\n j = j+1\n ans = len(C)\n for i in range(1,len(C)):\n if(C[i]==C[i-1]):\n ans-=1\n if(ans==k):\n anscount+=1\n else:\n if(l.count(1)<m):\n generate(n,m,l+[1])\n if(l.count(0)<n):\n generate(n,m,l+[0])\n else:\n if(l.count(0)<n):\n generate(n,m,l+[0])\nfor _ in range(int(input())):\n anscount=0\n n,m,k=list(map(int,input().split()))\n A=list(map(int,input().split()))\n B=list(map(int,input().split()))\n generate(n,m,[])\n print(anscount)\n", "def find_interleavings_2(a, b, i, j):\n try:\n return lev_memo[(i, j)], nbs_memo[(i, j)]\n except KeyError:\n pass\n to_return = []\n nbs = []\n if i == len(a) and j == len(b):\n return [[]], [0]\n elif i == len(a):\n leaves, nbs1 = find_interleavings_2(a, b, i, j + 1)\n for ind in range(len(leaves)):\n leaf = leaves[ind]\n to_return.append([b[j]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == b[j]:\n nbs.append(nbs1[ind])\n else:\n nbs.append(nbs1[ind] + 1)\n elif j == len(b):\n leaves, nbs1 = find_interleavings_2(a, b, i + 1, j)\n for ind in range(len(leaves)):\n leaf = leaves[ind]\n to_return.append([a[i]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == a[i]:\n nbs.append(nbs1[ind])\n else:\n nbs.append(nbs1[ind] + 1)\n else:\n leaves, nbs1 = find_interleavings_2(a, b, i, j + 1)\n for ind in range(len(leaves)):\n leaf = leaves[ind]\n to_return.append([b[j]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == b[j]:\n nbs.append(nbs1[ind])\n else:\n nbs.append(nbs1[ind] + 1)\n\n leaves2, nbs2 = find_interleavings_2(a, b, i + 1, j)\n for ind in range(len(leaves2)):\n leaf = leaves2[ind]\n to_return.append([a[i]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == a[i]:\n nbs.append(nbs2[ind])\n else:\n nbs.append(nbs2[ind] + 1)\n\n lev_memo[(i, j)] = to_return\n nbs_memo[(i, j)] = nbs\n to_remove_lev = []\n to_remove_nbs = []\n for nbi in range(len(nbs)):\n if nbs[nbi] > k:\n to_remove_lev.append(to_return[nbi])\n to_remove_nbs.append(nbs[nbi])\n\n for tri in to_remove_lev:\n to_return.remove(tri)\n for tri in to_remove_nbs:\n nbs.remove(tri)\n return to_return, nbs\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n lev_memo = dict()\n nbs_memo = dict()\n ils, numBlo = find_interleavings_2(a, b, 0, 0)\n ils = [tuple(x) for x in ils]\n # print(ils, numBlo)\n memo = dict()\n for c in numBlo:\n if c == k:\n res += 1\n\n print(res)\n", "import math\n\n\ndef find_interleavings_2(a, b, i, j):\n try:\n return lev_memo[(i, j)], nbs_memo[(i, j)]\n except KeyError:\n pass\n to_return = []\n nbs = []\n if i == len(a) and j == len(b):\n return [[]], [0]\n elif i == len(a):\n leaves, nbs1 = find_interleavings_2(a, b, i, j + 1)\n for ind in range(len(leaves)):\n leaf = leaves[ind]\n to_return.append([b[j]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == b[j]:\n nbs.append(nbs1[ind])\n else:\n nbs.append(nbs1[ind] + 1)\n elif j == len(b):\n leaves, nbs1 = find_interleavings_2(a, b, i + 1, j)\n for ind in range(len(leaves)):\n leaf = leaves[ind]\n to_return.append([a[i]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == a[i]:\n nbs.append(nbs1[ind])\n else:\n nbs.append(nbs1[ind] + 1)\n else:\n leaves, nbs1 = find_interleavings_2(a, b, i, j + 1)\n for ind in range(len(leaves)):\n leaf = leaves[ind]\n to_return.append([b[j]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == b[j]:\n nbs.append(nbs1[ind])\n else:\n nbs.append(nbs1[ind] + 1)\n\n leaves2, nbs2 = find_interleavings_2(a, b, i + 1, j)\n for ind in range(len(leaves2)):\n leaf = leaves2[ind]\n to_return.append([a[i]] + leaf)\n if len(leaf) == 0:\n nbs.append(nbs1[ind] + 1)\n elif leaf[0] == a[i]:\n nbs.append(nbs2[ind])\n else:\n nbs.append(nbs2[ind] + 1)\n\n lev_memo[(i, j)] = to_return\n nbs_memo[(i, j)] = nbs\n return to_return, nbs\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n lev_memo = dict()\n nbs_memo = dict()\n ils, numBlo = find_interleavings_2(a, b, 0, 0)\n ils = [tuple(x) for x in ils]\n #print(ils, numBlo)\n memo = dict()\n for c in numBlo:\n if c == k:\n res += 1\n\n print(res)\n", "import math\n\n\ndef find_interleavings_2(a, b, i, j):\n try:\n return lev_memo[(i, j)]\n except KeyError:\n pass\n to_return = []\n if i == len(a) and j == len(b):\n return [[]]\n elif i == len(a):\n for leaf in find_interleavings_2(a, b, i, j + 1):\n to_return.append([b[j]] + leaf)\n elif j == len(b):\n for leaf in find_interleavings_2(a, b, i + 1, j):\n to_return.append([a[i]] + leaf)\n else:\n for leaf in find_interleavings_2(a, b, i, j + 1):\n to_return.append([b[j]] + leaf)\n for leaf in find_interleavings_2(a, b, i + 1, j):\n to_return.append([a[i]] + leaf)\n lev_memo[(i, j)] = to_return\n return to_return\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n lev_memo = dict()\n ils = find_interleavings_2(a, b, 0, 0)\n ils = [tuple(x) for x in ils]\n # print(ils)\n memo = dict()\n for c in ils:\n try:\n res += memo[c]\n continue\n except KeyError:\n pass\n num_blocks = 0\n prev = math.inf\n for i in c:\n if i != prev:\n num_blocks += 1\n prev = i\n # num_blocks += 1\n if num_blocks == k:\n res += 1\n memo[c] = 1\n else:\n memo[c] = 0\n\n print(res)\n", "import math\n\n\ndef find_interleavings(a, b):\n c = list()\n\n def interleaf(i, j, l):\n if i == len(a) and j == len(b):\n c.append(tuple(l))\n elif i == len(a):\n interleaf(i, j + 1, l + [b[j]])\n elif j == len(b):\n interleaf(i + 1, j, l + [a[i]])\n else:\n interleaf(i + 1, j, l + [a[i]])\n interleaf(i, j + 1, l + [b[j]])\n\n interleaf(0, 0, [])\n return c\n\n\ndef find_interleavings_2(a, b, i, j):\n to_return = []\n if i == len(a) and j == len(b):\n return [[]]\n elif i == len(a):\n for leaf in find_interleavings_2(a, b, i, j + 1):\n to_return.append([b[j]] + leaf)\n elif j == len(b):\n for leaf in find_interleavings_2(a, b, i + 1, j):\n to_return.append([a[i]] + leaf)\n else:\n for leaf in find_interleavings_2(a, b, i, j + 1):\n to_return.append([b[j]] + leaf)\n for leaf in find_interleavings_2(a, b, i + 1, j):\n to_return.append([a[i]] + leaf)\n\n return to_return\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n ils = find_interleavings_2(a, b, 0, 0)\n ils = [tuple(x) for x in ils]\n # print(ils)\n memo = dict()\n for c in ils:\n try:\n res += memo[c]\n continue\n except KeyError:\n pass\n num_blocks = 0\n prev = math.inf\n for i in c:\n if i != prev:\n num_blocks += 1\n prev = i\n # num_blocks += 1\n if num_blocks == k:\n res += 1\n memo[c] = 1\n else:\n memo[c] = 0\n\n print(res)\n", "import math\n\n\ndef find_interleavings(a, b):\n c = list()\n\n def interleaf(i, j, l):\n if i == len(a) and j == len(b):\n c.append(tuple(l))\n elif i == len(a):\n interleaf(i, j + 1, l + [b[j]])\n elif j == len(b):\n interleaf(i + 1, j, l + [a[i]])\n else:\n interleaf(i + 1, j, l + [a[i]])\n interleaf(i, j + 1, l + [b[j]])\n\n interleaf(0, 0, [])\n return c\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n ils = find_interleavings(a, b)\n # print(ils)\n memo = dict()\n for c in ils:\n try:\n res += memo[c]\n continue\n except KeyError:\n pass\n num_blocks = 0\n prev = math.inf\n for i in c:\n if i != prev:\n num_blocks += 1\n prev = i\n # num_blocks += 1\n if num_blocks == k:\n res += 1\n memo[c] = 1\n else:\n memo[c] = 0\n\n print(res)\n", "import math\n\n\ndef find_interleavings(a, b):\n c = list()\n\n def interleaf(i, j, l):\n if i == len(a) and j == len(b):\n c.append(l)\n elif i == len(a):\n interleaf(i, j + 1, l + [b[j]])\n elif j == len(b):\n interleaf(i + 1, j, l + [a[i]])\n else:\n interleaf(i + 1, j, l + [a[i]])\n interleaf(i, j + 1, l + [b[j]])\n\n interleaf(0, 0, [])\n return c\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n ils = find_interleavings(a, b)\n #print(ils)\n for c in ils:\n num_blocks = 0\n prev = math.inf\n for i in c:\n if i != prev:\n num_blocks += 1\n prev = i\n # num_blocks += 1\n if num_blocks == k:\n res += 1\n print(res)\n", "import math\n\n\ndef find_interleavings(a, b):\n c = list()\n\n def interleaf(i, j, l):\n if i == len(a) and j == len(b):\n c.append(l)\n elif i == len(a):\n interleaf(i, j + 1, l + [b[j]])\n elif j == len(b):\n interleaf(i + 1, j, l + [a[i]])\n else:\n interleaf(i + 1, j, l + [a[i]])\n interleaf(i, j + 1, l + [b[j]])\n\n interleaf(0, 0, [])\n return c\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split(\" \")))\n a = list(map(int, input().split(\" \")))\n b = list(map(int, input().split(\" \")))\n res = 0\n\n ils = find_interleavings(a, b)\n #print(ils)\n for c in ils:\n num_blocks = 0\n prev = math.inf\n for i in c:\n if i != prev:\n num_blocks += 1\n prev = i\n # num_blocks += 1\n if num_blocks == k:\n res += 1\n print(res)\n", "# cook your dish here\nn=0;m=0;\nA=[];B=[];\nanscount=0;k=0;\n\ndef generate(n,m,l):\n nonlocal anscount\n if(len(l)==n+m):\n X=l\n i,j = 0,0\n C=[0 for t in range(n+m)]\n while((i+j)<(n+m)):\n if(X[i+j] == 0):\n C[i+j] = A[i]\n i = i+1\n else:\n C[i+j] = B[j]\n j = j+1\n ans = len(C)\n for i in range(1,len(C)):\n if(C[i]==C[i-1]):\n ans-=1\n if(ans==k):\n anscount+=1\n else:\n if(l.count(1)<m):\n generate(n,m,l+[1])\n if(l.count(0)<n):\n generate(n,m,l+[0])\n else:\n if(l.count(0)<n):\n generate(n,m,l+[0])\nfor _ in range(int(input())):\n anscount=0\n n,m,k=list(map(int,input().split()))\n A=list(map(int,input().split()))\n B=list(map(int,input().split()))\n generate(n,m,[])\n print(anscount)\n", "# cook your dish here\nn=0;m=0;\nA=[];B=[];\nanscount=0;k=0;\n\ndef generate(n,m,l):\n nonlocal anscount\n if(len(l)==n+m):\n X=l\n i,j = 0,0\n C=[0 for t in range(n+m)]\n while((i+j)<(n+m)):\n if(X[i+j] == 0):\n C[i+j] = A[i]\n i = i+1\n else:\n C[i+j] = B[j]\n j = j+1\n ans = len(C)\n for i in range(1,len(C)):\n if(C[i]==C[i-1]):\n ans-=1\n if(ans==k):\n anscount+=1\n else:\n if(l.count(1)<m):\n generate(n,m,l+[1])\n if(l.count(0)<n):\n generate(n,m,l+[0])\n else:\n if(l.count(0)<n):\n generate(n,m,l+[0])\nfor _ in range(int(input())):\n anscount=0\n n,m,k=list(map(int,input().split()))\n A=list(map(int,input().split()))\n B=list(map(int,input().split()))\n generate(n,m,[])\n print(anscount)\n", "# cook your dish here\nn=0;m=0;\nA=[];B=[];\nanscount=0;k=0;\n\ndef generate(n,m,l):\n nonlocal anscount\n if(len(l)==n+m):\n X=l\n i,j = 0,0\n C=[0 for t in range(n+m)]\n while((i+j)<(n+m)):\n if(X[i+j] == 0):\n C[i+j] = A[i]\n i = i+1\n else:\n C[i+j] = B[j]\n j = j+1\n ans = len(C)\n for i in range(1,len(C)):\n if(C[i]==C[i-1]):\n ans-=1\n if(ans==k):\n anscount+=1\n else:\n if(l.count(1)<m):\n generate(n,m,l+[1])\n if(l.count(0)<n):\n generate(n,m,l+[0])\n else:\n if(l.count(0)<n):\n generate(n,m,l+[0])\nfor _ in range(int(input())):\n anscount=0\n n,m,k=list(map(int,input().split()))\n A=list(map(int,input().split()))\n B=list(map(int,input().split()))\n generate(n,m,[])\n print(anscount)\n", "# cook your dish here\nn=0;m=0;\nA=[];B=[];\nanscount=0;k=0;\n\ndef generate(n,m,l):\n nonlocal anscount\n if(len(l)==n+m):\n X=l\n i,j = 0,0\n C=[0 for t in range(n+m)]\n while((i+j)<(n+m)):\n if(X[i+j] == 0):\n C[i+j] = A[i]\n i = i+1\n else:\n C[i+j] = B[j]\n j = j+1\n ans = len(C)\n for i in range(1,len(C)):\n if(C[i]==C[i-1]):\n ans-=1\n if(ans==k):\n anscount+=1\n else:\n if(l.count(1)<m):\n generate(n,m,l+[1])\n if(l.count(0)<n):\n generate(n,m,l+[0])\n else:\n if(l.count(0)<n):\n generate(n,m,l+[0])\nfor _ in range(int(input())):\n anscount=0\n n,m,k=list(map(int,input().split()))\n A=list(map(int,input().split()))\n B=list(map(int,input().split()))\n generate(n,m,[])\n print(anscount)\n", "# cook your dish here\nmod = 10 ** 8 + 7\n\ndef dic_add(dic, k1, v):\n if k1 <= k:\n if k1 in dic:\n dic[k1] = (dic[k1]+v) % mod\n else:\n dic[k1] = v\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split()))\n a_l = list(map(int, input().split()))\n b_l = list(map(int, input().split()))\n\n # 0: m end, 1: n end\n f_dp = [[[{} for _ in range(n+1)] for _ in range(m+1)] for _ in range(2)]\n\n f_dp[0][1][0] = {1: 1}\n f_dp[1][0][1] = {1: 1}\n dif = 1\n for j in range(1, m):\n if b_l[j] != b_l[j - 1]:\n dif += 1\n f_dp[0][j+1][0] = {dif: 1}\n\n dif = 1\n for i in range(1, n):\n if a_l[i] != a_l[i - 1]:\n dif += 1\n f_dp[1][0][i+1] = {dif: 1}\n\n for i in range(1, n + 1):\n for j in range(1, m + 1):\n # m end\n if j -2 >= 0 and b_l[j-1] != b_l[j - 2]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[0][j - 1][i].items()):\n dic_add(f_dp[0][j][i], kk + addi, vv)\n \n if b_l[j-1] != a_l[i-1]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[1][j-1][i].items()):\n dic_add(f_dp[0][j][i], kk + addi, vv)\n \n # n end\n if i -2 >= 0 and a_l[i-1] != a_l[i - 2]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[1][j][i-1].items()):\n dic_add(f_dp[1][j][i], kk + addi, vv)\n \n if b_l[j-1] != a_l[i-1]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[0][j][i-1].items()):\n dic_add(f_dp[1][j][i], kk + addi, vv)\n\n ans = 0\n if k in f_dp[0][m][n]:\n ans += f_dp[0][m][n][k]\n if k in f_dp[1][m][n]:\n ans += f_dp[1][m][n][k]\n print(ans%mod)\n", "# cook your dish here\nmod = 10 ** 8 + 7\n\ndef dic_add(dic, k1, v):\n if k1 <= k:\n if k1 in dic:\n dic[k1] = (dic[k1]+v) % mod\n else:\n dic[k1] = v\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split()))\n a_l = list(map(int, input().split()))\n b_l = list(map(int, input().split()))\n\n # 0: m end, 1: n end\n f_dp = [[[{} for _ in range(n+1)] for _ in range(m+1)] for _ in range(2)]\n\n f_dp[0][1][0] = {1: 1}\n f_dp[1][0][1] = {1: 1}\n dif = 1\n for j in range(1, m):\n if b_l[j] != b_l[j - 1]:\n dif += 1\n f_dp[0][j+1][0] = {dif: 1}\n\n dif = 1\n for i in range(1, n):\n if a_l[i] != a_l[i - 1]:\n dif += 1\n f_dp[1][0][i+1] = {dif: 1}\n\n for i in range(1, n + 1):\n for j in range(1, m + 1):\n # m end\n if j -2 >= 0 and b_l[j-1] != b_l[j - 2]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[0][j - 1][i].items()):\n dic_add(f_dp[0][j][i], kk + addi, vv)\n \n if b_l[j-1] != a_l[i-1]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[1][j-1][i].items()):\n dic_add(f_dp[0][j][i], kk + addi, vv)\n \n # n end\n if i -2 >= 0 and a_l[i-1] != a_l[i - 2]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[1][j][i-1].items()):\n dic_add(f_dp[1][j][i], kk + addi, vv)\n \n if b_l[j-1] != a_l[i-1]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[0][j][i-1].items()):\n dic_add(f_dp[1][j][i], kk + addi, vv)\n\n ans = 0\n if k in f_dp[0][m][n]:\n ans += f_dp[0][m][n][k]\n if k in f_dp[1][m][n]:\n ans += f_dp[1][m][n][k]\n print(ans%mod)\n", "# cook your dish here\nmod = 10 ** 8 + 7\n\ndef dic_add(dic, k1, v):\n if k1 <= k:\n if k1 in dic:\n dic[k1] = (dic[k1]+v) % mod\n else:\n dic[k1] = v\n\n\nfor _ in range(int(input())):\n n, m, k = list(map(int, input().split()))\n a_l = list(map(int, input().split()))\n b_l = list(map(int, input().split()))\n\n # 0: m end, 1: n end\n f_dp = [[[{} for _ in range(n+1)] for _ in range(m+1)] for _ in range(2)]\n\n f_dp[0][1][0] = {1: 1}\n f_dp[1][0][1] = {1: 1}\n dif = 1\n for j in range(1, m):\n if b_l[j] != b_l[j - 1]:\n dif += 1\n f_dp[0][j+1][0] = {dif: 1}\n\n dif = 1\n for i in range(1, n):\n if a_l[i] != a_l[i - 1]:\n dif += 1\n f_dp[1][0][i+1] = {dif: 1}\n\n for i in range(1, n + 1):\n for j in range(1, m + 1):\n # m end\n if j -2 >= 0 and b_l[j-1] != b_l[j - 2]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[0][j - 1][i].items()):\n dic_add(f_dp[0][j][i], kk + addi, vv)\n \n if b_l[j-1] != a_l[i-1]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[1][j-1][i].items()):\n dic_add(f_dp[0][j][i], kk + addi, vv)\n \n # n end\n if i -2 >= 0 and a_l[i-1] != a_l[i - 2]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[1][j][i-1].items()):\n dic_add(f_dp[1][j][i], kk + addi, vv)\n \n if b_l[j-1] != a_l[i-1]:\n addi = 1\n else:\n addi = 0\n for kk, vv in list(f_dp[0][j][i-1].items()):\n dic_add(f_dp[1][j][i], kk + addi, vv)\n\n ans = 0\n if k in f_dp[0][m][n]:\n ans += f_dp[0][m][n][k]\n if k in f_dp[1][m][n]:\n ans += f_dp[1][m][n][k]\n print(ans)\n"]
{"inputs": [["5", "2 2 4", "1 3", "3 4", "2 2 3", "1 3", "3 4", "2 2 2", "1 3", "3 4", "2 2 4", "4 7", "8 5", "2 2 2", "4 7", "8 5"]], "outputs": [["4", "2", "0", "6", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
17,948
8b10d41ea57a99fb4a853d0ca1f7e41e
UNKNOWN
Patlu has recently got a new problem based on Pallindromes. A Pallindrome is a number that is same from front and back, example $121$ is pallindrome but $123$ is not . He wants to calculate sum of all $N$ digit number which are Pallindromic in nature and divisible by $9$ and does not contain any zero in their decimal representation. As the answer can be very large so print the sum modulo $10^9 + 7$. -----Input:----- - First line of input contain $T$, number of testcases. Then the testcases follow. - Each testcase contains single line of input , one integer $N$. -----Output:----- - For each testcase, output in a single line answer having $N$ digits pallindromic string. -----Constraints----- - $1\leq T \leq 100$ - $1\leq N \leq 10^5$ -----Sample Input:----- 2 1 2 -----Sample Output:----- 9 99
["def getsum(N):\n\tif N==1:\n\t\treturn 9\n\tif N==2:\n\t\treturn 99\n\ts = \"\"\n\tfor i in range(0,N):\n\t\ts = s+'5'\n\ts = int(s)\n\tif N%2==0:\n\t\ts = s*pow(9,N//2-1)\n\telse:\n\t\ts = s*pow(9,N//2)\n\treturn s%(pow(10,9)+7)\n\ndef main():\n\tt = int(input())\n\tfor _ in range(0,t):\n\t\tN = int(input())\n\t\tresult = getsum(N)\n\t\tprint(result)\ndef __starting_point():\n\tmain()\n\n__starting_point()"]
{"inputs": [["2", "1", "2"]], "outputs": [["9", "99"]]}
INTERVIEW
PYTHON3
CODECHEF
414
2cee3653b5cdefc2ebc3e51de9d13536
UNKNOWN
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy buildings placed in a row and numbered from left to right strating from 0. Each building i (except the first and the last) has exactly two adjacent buildings with indices i-1 and i+1. The first and the last buildings have just a single adjacent building. Some of the buildings contain bombs. When bomb explodes in some building it destroys it and all adjacent to it buildings. You are given the string S of length N, where Si is 1 if the i-th building contains bomb, 0 otherwise. Find for the Little Elephant the number of buildings that will not be destroyed after all bombs explode. Please note that all bombs explode simultaneously. -----Input----- The first line contains single integer T - the number of test cases. T test cases follow. The first line of each test case contains the single integer N - the number of buildings. The next line contains the string S of length N consisted only of digits 0 and 1. -----Output----- In T lines print T inetgers - the answers for the corresponding test cases. -----Constraints----- 1 <= T <= 100 1 <= N <= 1000 -----Example----- Input: 3 3 010 5 10001 7 0000000 Output: 0 1 7
["import sys\nT = int(sys.stdin.readline().strip())\nfor t in range(T):\n sys.stdin.readline().strip()\n st = '0'+sys.stdin.readline().strip()+'0'\n res = 0\n for i in range(1,len(st)-1):\n if st[i] == st[i-1] == st[i+1] == '0':\n res+=1\n print(res)\n", "t = int( input() )\n\nwhile t > 0:\n n = int( input() )\n s = input()\n c = 0\n z = 1\n \n for i in range( n ):\n if s[i] == '0':\n z += 1\n continue\n \n if z > 2:\n c += z - 2\n \n z = 0\n \n if z > 1:\n c += z - 1\n\n t -= 1\n print(c) ", "t = int(input())\nfor i in range(0, t):\n n = int(input())\n a = input()\n b = [1]*n\n for i in range(0, n):\n if a[i] == '1':\n b[i] = 0\n if i > 0:\n b[i - 1] = 0\n if i < n - 1:\n b[i + 1] = 0\n ans = 0\n for i in range(0, n):\n if b[i] == 1:\n ans += 1\n print(ans)", "inputs = int(input())\nwhile inputs > 0:\n length = int(input())\n building = input()\n buildings = []\n if length != 0:\n for i in range(0,length):\n buildings.append(int(building[i]))\n if buildings[0] == 1:\n buildings[0] = 2\n if length > 1:\n if buildings[1] == 0:\n buildings[1] = 2\n for i in range(1, length-1):\n if buildings[i] == 1:\n buildings[i] = 2\n if buildings[i+1] == 0:\n buildings[i+1] = 2\n if buildings[i-1] == 0:\n buildings[i-1] = 2\n if buildings[length-1] == 1:\n buildings[length-1] = 2\n if buildings[length-2] == 0:\n buildings[length-2] = 2\n print(buildings.count(0))\n else:\n print(0)\n inputs = inputs - 1\n", "def not_destroyed(x, n):\n for j in range(n):\n if x[j] == '1':\n x[j] = '2'\n if j-1 >= 0:\n x[j-1] = '2'\n if j+1 < n and x[j+1] != '1':\n x[j+1] = '2'\n return x.count('0')\n \n\nT = int(input())\nfor i in range(T):\n L = []\n N = int(input())\n S = input()\n for j in range(N):\n L.append(S[j])\n n = not_destroyed(L, N)\n print(n)\n", "def main():\n T = int(input())\n for test in range(T):\n N = int(input())\n buildingList = input().strip()\n totalStanding = N\n for index, building in enumerate(buildingList):\n if building == '1' or \\\n (index > 0 and buildingList[index-1] == '1') or \\\n (index < N-1 and buildingList[index+1] == '1'):\n totalStanding -= 1\n print(totalStanding)\n\n\ndef __starting_point():\n main()\n\n__starting_point()", "def main():\n T = int(input())\n for test in range(T):\n N = int(input())\n buildingList = input().strip()\n totalStanding = N\n for index, building in enumerate(buildingList):\n if building == '1' or \\\n (index > 0 and buildingList[index-1] == '1') or \\\n (index < N-1 and buildingList[index+1] == '1'):\n totalStanding -= 1\n print(totalStanding)\n\n\ndef __starting_point():\n main()\n\n__starting_point()", "t = int(input())\nfor nTest in range(t):\n n = int(input())\n s = input().strip()\n ns = int(s)\n ns = ns + ns//10 + 10*ns\n \n ss = str(ns)[-n:]\n print(ss.count(\"0\") + n-len(ss))\n\n", "t = int(input())\nfor nTest in range(t):\n n = int(input())\n #s = list(raw_input().strip())\n s = input().strip()\n ns = int(s) + int(s[1:]+'0') + int('0'+s[:-1])\n ss = str(ns)\n print(ss.count(\"0\") + len(s)-len(ss))\n## answer = 0\n## for i in range(n):\n## if (i==0 or s[i-1]=='0') and (s[i]=='0') and (i==n-1 or s[i+1]=='0'):\n## answer +=1\n## print answer\n", "t = int(input())\nfor nTest in range(t):\n n = int(input())\n s = list(input().strip())\n answer = 0\n for i in range(n):\n if (i==0 or s[i-1]=='0') and (s[i]=='0') and (i==n-1 or s[i+1]=='0'):\n answer +=1\n print(answer)\n", "cases = int(input())\nfor case in range (cases):\n length = int(input())\n buildings = [1]*length\n bombs = input()\n for i in range(length):\n if (bombs[i]==\"1\"):\n buildings[i] = 0\n if (i!=0):\n buildings[i-1] = 0\n if (i!=length-1):\n buildings[i+1] = 0\n i = i+2\n print(buildings.count(1))\n", "x=int(input())\nlistk=[]\nfor i in range(0,x):\n len_s=int(input())\n build=input()\n list1=[]\n for e in build:\n list1.append(e)\n for i in range(0,len_s):\n \n if list1[i]=='1':\n \n list1[i]=='R'\n if (i-1)>=0:\n list1[i-1]='R'\n if i!=(len_s-1):\n if list1[i+1]:\n if list1[i+1]!='1':\n list1[i+1]='R'\n sum=0\n for e in list1:\n if e=='0':\n sum=sum+1\n listk.append(sum)\nfor e in listk:\n print(e)\n \n \n", "def main():\n T = int(input())\n for i in range(T):\n N = int(input())\n S = input()\n R = {}\n for i in range(N):\n if S[i] == '1':\n R[i-1] = 2\n R[i] = 2\n R[i+1] = 2\n R[-1] = 0\n R[N] = 0\n print(N - list(R.values()).count(2))\n\n\n\ndef __starting_point():\n main()\n\n\n\n__starting_point()", "#!/usr/bin/env python\n\ndef process(N, S):\n C = 0\n for i in range(1, N + 1):\n if S[i-1] or S[i] or S[i+1]:\n C += 1\n return N - C\n\ndef main():\n T = int(input().strip())\n for t in range(T):\n N = int(input().strip())\n S = [0] + list(map(int, input().strip())) + [0]\n print(process(N, S))\n\nmain()\n\n", "N = int(input())\nfor i in range(0,N):\n inp = int(input())\n inpu = input()\n input_list = []\n for j in range(0,inp):\n input_list+=[inpu[j]]\n li = []\n if (input_list[0] == \"1\"):\n li+=[0,1]\n if (input_list[inp-1] == \"1\"):\n li+=[inp-1,inp-2]\n for k in range(1,inp-1):\n if (input_list[k] == \"1\"):\n li+=[k-1,k,k+1]\n li = list(set(li))\n t = inp- len(li)\n if t<0:\n print(\"0\")\n else:\n print(t)", "t=int(input())\nfor i in range(t):\n N=int(input())\n word=input()\n hit=[0]*N\n for x in range(N):\n if word[x]=='1':\n hit[x]=1\n if x>0: hit[x-1]=1\n if x<N-1: hit[x+1]=1\n print(hit.count(0))\n", "t=int(input())\nfor i in range(t):\n N=int(input())\n word=input()\n hit=[0]*N\n for x in range(N):\n if word[x]=='1':\n hit[x]=1\n if x>0: hit[x-1]=1\n if x<N-1: hit[x+1]=1\n print(hit.count(0))\n", "for _ in range(int(input())):\n n=int(input())\n s=input().strip()\n \n ans=n\n \n for i in range(n):\n if s[i]=='1':\n ans-=1\n elif i!=0 and s[i-1]=='1':\n ans-=1\n elif i!=n-1 and s[i+1]=='1':\n ans-=1\n \n print(ans)"]
{"inputs": [["3", "3", "010", "5", "10001", "7", "0000000"]], "outputs": [["0", "1", "7"]]}
INTERVIEW
PYTHON3
CODECHEF
6,012
79d57eaef6c96c6996a4c3255a89a6cd
UNKNOWN
Vlad enjoys listening to music. He lives in Sam's Town. A few days ago he had a birthday, so his parents gave him a gift: MP3-player! Vlad was the happiest man in the world! Now he can listen his favorite songs whenever he wants! Vlad built up his own playlist. The playlist consists of N songs, each has a unique positive integer length. Vlad likes all the songs from his playlist, but there is a song, which he likes more than the others. It's named "Uncle Johny". After creation of the playlist, Vlad decided to sort the songs in increasing order of their lengths. For example, if the lengths of the songs in playlist was {1, 3, 5, 2, 4} after sorting it becomes {1, 2, 3, 4, 5}. Before the sorting, "Uncle Johny" was on K-th position (1-indexing is assumed for the playlist) in the playlist. Vlad needs your help! He gives you all the information of his playlist. Your task is to find the position of "Uncle Johny" in the sorted playlist. -----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 one integer N denoting the number of songs in Vlad's playlist. The second line contains N space-separated integers A1, A2, ..., AN denoting the lenghts of Vlad's songs. The third line contains the only integer K - the position of "Uncle Johny" in the initial playlist. -----Output----- For each test case, output a single line containing the position of "Uncle Johny" in the sorted playlist. -----Constraints----- 1 ≤ T ≤ 1000 1 ≤ K ≤ N ≤ 100 1 ≤ Ai ≤ 109 -----Example----- Input: 3 4 1 3 4 2 2 5 1 2 3 9 4 5 5 1 2 3 9 4 1 Output: 3 4 1 -----Explanation----- In the example test there are T=3 test cases. Test case 1 In the first test case N equals to 4, K equals to 2, A equals to {1, 3, 4, 2}. The answer is 3, because {1, 3, 4, 2} -> {1, 2, 3, 4}. A2 now is on the 3-rd position. Test case 2 In the second test case N equals to 5, K equals to 5, A equals to {1, 2, 3, 9, 4}. The answer is 4, because {1, 2, 3, 9, 4} -> {1, 2, 3, 4, 9}. A5 now is on the 4-th position. Test case 3 In the third test case N equals to 5, K equals to 1, A equals to {1, 2, 3, 9, 4}. The answer is 1, because {1, 2, 3, 9, 4} -> {1, 2, 3, 4, 9}. A1 stays on the 1-th position. -----Note----- "Uncle Johny" is a real song performed by The Killers.
["# cook your dish here\nt=int(input())\nfor i in range(t):\n n=int(input())\n nums=list(map(int,input().split()))\n k=int(input())\n an=nums[k-1]\n cn=0\n for i in range(n):\n if(nums[i]<an):\n cn+=1\n\n print(cn+1)\n", "# cook your dish here\nT = int(input())\nfor i in range(1,T+1):\n N = int(input())\n x = list(map(int, input().split()))[:N]\n K = int(input())\n val = x[K-1]\n y = sorted(x)\n z = y.index(val)\n print(z+1)", "# cook your dish here\nT = int(input())\nfor i in range(1,T+1):\n N = int(input())\n x = list(map(int, input().split()))[:N]\n K = int(input())\n val = x[K-1]\n y = sorted(x)\n z = y.index(val)\n print(z+1)", "def position():\n size = int(input())\n l = list(map(int, input().split()))\n pos = int(input())\n key = l[pos-1]\n sortArray = sorted(l)\n for i in range(size):\n if(sortArray[i] == key):\n return i+1\nfor _ in range(int(input())):\n print(position())", "for i in range(int(input())):\r\n n = int(input())\r\n l = list(map(int,input().split()))\r\n k = int(input())\r\n a = l[k-1]\r\n l.sort()\r\n print(l.index(a) + 1)", "# cook your dish here\nt=int(input())\nfor i in range(t) :\n n=int(input())\n str=list(map(int,input().split()))\n t=int(input())\n x=str[t-1]\n str.sort()\n print(str.index(x)+1)\n ", "for case in range(int(input())):\n n = int(input())\n lengths = list(map(int,input().split()))\n k = int(input())\n uncle_song_len = lengths[k-1]\n lengths.sort()\n print(lengths.index(uncle_song_len)+1)\n ", "t = int(input())\nfor test in range(t):\n n = int(input())\n a = list(map(int, input().split()))\n k = int(input())\n k -= 1\n element = a[k]\n a.sort()\n element_index = a.index(element)\n element_index += 1\n print(element_index)", "t=int(input())\r\nfor i in range(t):\r\n num=int(input())\r\n lst=list(map(int,input().split()))\r\n pos=int(input())\r\n x=lst[pos-1]\r\n lst.sort()\r\n print(lst.index(x)+1)", "for _ in range(int(input())):\n num=int(input())\n lst=list(map(int,input().split()))\n n=int(input())\n a=lst[n-1]\n lst.sort()\n for i in range(num):\n if a==lst[i]:\n print(i+1)\n break# cook your dish here\n", "import sys\n\n\ndef johnny(a,k):\n n = len(a)\n a = sorted(a)\n l = 0\n h = n-1\n while l <= h:\n mid = (l+h)//2\n if a[mid] == k:\n return mid + 1\n if a[mid] > k:\n h = mid-1\n else:\n l = mid+1\n return -1\n\n\n\nt = int(input())\nwhile t:\n n = int(input())\n a = [int(k) for k in input().split()]\n k = int(input())\n x = a[k-1]\n ans = johnny(a,x)\n print(ans)\n t -= 1\n# cook your dish here\n", "# cook your dish here\ntestCases = int(input())\nfor time in range(1,testCases+1):\n length = int(input())\n numList = [int(item) for item in input().split()]\n k = int(input())\n mark = numList[k-1]\n numList.sort()\n print((numList.index(mark))+1)\n \n ", "for _ in range(int(input())):\n input()\n arr = list(map(int, input().split()))\n x = arr[int(input())-1]\n ans = 1\n for n in arr:\n if n < x: ans += 1\n print(ans)", "# cook your dish here\ntry:\n lt = []\n for _ in range(int(input())):\n N = int(input())\n N_lt = list(map(int, input().split()))\n K = N_lt[int(input()) - 1]\n N_lt = sorted(N_lt)\n lt.append(N_lt.index(K))\n for i in lt:\n print(i+1)\nexcept:\n pass", "t=int(input())\nfor _ in range(t):\n n=int(input())\n a=list(map(int,input().split()))\n k=int(input())\n c=a[k-1]\n a.sort()\n for i in range(n):\n if a[i]==c:\n print(i+1)\n break\n"]
{"inputs": [["3", "4", "1 3 4 2", "2", "5", "1 2 3 9 4", "5", "5", "1 2 3 9 4 ", "1", "", ""]], "outputs": [["3", "4", "1"]]}
INTERVIEW
PYTHON3
CODECHEF
3,891
e1617e0c638a757211d712cabda2c935
UNKNOWN
Little Praneet loves experimenting with algorithms and has devised a new algorithm. The algorithm is performed on an integer as follows: - if the rearmost digit is $0$, he will erase it. - else, he will replace the rearmost digit $d$ with $d-1$. If a point comes when the integer becomes $0$, the algorithm stops. You are given an integer $n$. Praneet will perform the algorithm on it $a$ times. You have to print the result after $a$ operations. -----Input:----- - The first and only line of input contains two integers $n$ — initial number, and $a$ —the number of operations. -----Output:----- - Print one integer — the result of performing the algorithm on $n$ $a$ times. -----Constraints----- - $2 \leq n \leq 10^9$ - $1 \leq a \leq 50$ -----Sample Input 1----- 1001 2 -----Sample Input 2----- 5 2 -----Sample Output 1----- 100 -----Sample Output 2----- 3 -----Explanation----- - In the first example, the transformation is as follows: $1001->1000->100$. - In the second example, the transformation is as follows: $5->4->3$.
["# cook your dish here\nn,a=map(int,input().split())\nfor i in range(a):\n if(n%10==0):\n n=n//10\n else:\n n=n-1\n \nprint(n)", "# cook your dish here\na,b=map(int,input().split())\nfor i in range(b):\n r=a%10\n n=a//10\n if(r==0):\n a=n\n else:\n a=a-1\nprint(a)", "#q1\nn,x=list(map(int,input().split()))\nfor i in range(x):\n if(n==0):\n break\n if(n%10==0):\n n=n//10\n else:\n n-=1\nprint(n)\n\n", "# cook your dish here\nm , n = input().split()\nfor i in range(int(n)):\n if m[-1] == '0':\n m = m[:len(m)-1]\n else:\n b = int(m[-1])-1\n m = m[:len(m)-1]+str(b)\nprint(int(m))\n", "n,a=list(map(int,input().split()))\nn=list(map(int,str(n)))\nfor i in range(a):\n if(n[-1]==0):\n n.pop()\n else:\n n[-1]-=1\n if(len(n)==0):\n break\nif(len(n)==1):\n print(int(n[0]))\nelif(len(n)==0):\n print(0)\nelse:\n n=list(map(str,n))\n print(int(\"\".join(n)))\n", "n,a=map(int,input().split())\nwhile a>0:\n if n%10==0:\n n=n//10\n else:\n n=n-1\n a=a-1\nprint(n)", "# cook your dish here\nn,k=map(int,input().split())\nwhile n!=1 and k:\n if n%10==0:\n n//=10\n else:\n n-=1\n k-=1\nprint(n)", "\n#def solve():\n \n'''while test:\n solve()\n test-=1'''\n\nn=[int(i) for i in input().split()] \nx=n[1]\nn=n[0]\nfor i in range(x):\n if n==0:\n break\n if n%10==0:\n n=n//10\n continue\n n=n-1\nprint(n) ", "# cook your dish here\nn,a=list(map(int,input().split()))\nfor _ in range(a):\n if n == 0:\n break\n elif str(n)[-1] == \"0\":\n n = n//10\n else:\n n = n-1\nprint(str(n)+\"\\n\")\n", "# cook your dish here\nn,op = map(int,input().split())\nfor i in range(op):\n if n%10 ==0:\n n =n//10\n else:\n n =n-1\nprint(n)", "n,a =input().split()\na=int(a)\ns=str(n)\n#print(type(s[len(s)-1]))\nwhile(a>0):\n if(s[len(s)-1]==\"0\"):\n s=s[:len(s)-1]\n else:\n s1=str((int(s[len(s)-1]))-1)\n s=s[:len(s)-1]\n s=s+s1\n a=a-1\nprint(int(s))\n \n", "def chef(a,n):\n while n>0:\n if a[-1]==\"0\":\n a=a[:-1]\n n-=1\n else:\n a=str(int(a)-1)\n n-=1\n return a\nb=list(map(str,input().split()))\nprint(chef(b[0],int(b[1])))", "n, a = map(int,input().split())\nfor j in range(a):\n if n%10 == 0:\n n /= 10\n else:\n n -= 1\nprint(int(n))", "n,a=map(int,input().split())\ni=0\nwhile i<a and n>0:\n if n%10==0:\n n=n//10\n else:\n n-=1\n i+=1\nprint(n)", "# cook your dish here\nn,a=list(map(int,input().split()))\nwhile a!=0:\n if n%10==0:\n n=n//10\n else:\n n-=1\n a-=1\nprint(n)\n", "# cook your dish here\n(n,a)=map(int,input().split(' '))\nwhile a:\n if n%10==0:\n n=n//10\n else:\n n-=1\n a-=1\nprint(n)", "na = list(map(int,input().split()))\nn = na[0]\na = na[1]\n\nn = str(n)\nn = list(map(int,n))\nfor i in range(a):\n if n[-1] == 0:\n n.pop()\n else:\n n[-1] -= 1\n\nprint(int(''.join(list(map(str,n)))))\n", "# cook your dish here\ntry:\n n,k=map(int,input().split(\" \"))\n while(k>0):\n if(n%10==0):\n n=n//10\n else:\n n=n-1\n k=k-1\n if(n==0):\n break\n \n print(n)\nexcept EOFError as e:\n print(e)", "# cook your dish here\nn,a=map(int,input().split())\nfor i in range(a):\n if n!= 0:\n r=n%10\n n=n//10\n if r!=0:\n r-=1\n n=n*10+r\nprint(int(n))", "(n, a) = list(map(int, input().strip().split()))\nfor i in range(a):\n if(n == 0):\n print(n)\n break\n if(n%10 == 0):\n n = n//10\n else:\n n -= 1\nprint(n)\n \n", "try:\n n,a=map(int,input().split())\n while a>0:\n if n%10==0:\n n//=10\n else:\n n-=1\n a-=1\n print(n)\nexcept:\n pass", "# cook your dish here\nab=input().split(' ')\na=ab[0]\nb=int(ab[1])\naa=[]\nfor x in str(a):\n aa.append(int(x))\nwhile b>0:\n \n if len(aa)==1 and aa[0]==0:\n break\n else:\n if aa[-1]==0:\n \n del(aa[-1])\n \n else:\n \n aa[-1]=int(aa[-1])-1\n b-=1 \nfor x in aa:\n print(x,end='')\n", "n,a=map(int,input().split(' '))\nfor i in range(a):\n if n%10==0:\n n//=10\n else:\n n-=1\nprint(n)", "# cook your dish here\nn,a = map(int,input().split())\nwhile(a):\n a=a-1\n if n%10==0:\n n=n//10\n else:\n n=n-1\n if n==0:\n break\nprint(n)"]
{"inputs": [["1001 2", "Sample Input 2", "5 2"]], "outputs": [["100", "Sample Output 2", "3"]]}
INTERVIEW
PYTHON3
CODECHEF
3,998
9e12d24deb5e26ac7c994ad3e9752f5a
UNKNOWN
Chef has just learned a new data structure - Fenwick tree. This data structure holds information about array of N elements and can process two types of operations: - Add some value to ith element of the array - Calculate sum of all elements on any prefix of the array Both operations take O(log N) time. This data structure is also well known for its low memory usage. To be more precise, it needs exactly the same amount of memory as that of array. Given some array A, first we build data structure in some other array T. Ti stores the sum of the elements Astart, Astart + 1, ..., Ai. Index start is calculated with formula start = Fdown(i) = (i & (i + 1)). Here "&" denotes bitwise AND operation. So, in order to find a sum of elements A0, A1, ..., AL you start with index L and calculate sum of TL + TFdown(L)-1 + TFdown(Fdown(L)-1)-1 + ... + TFdown(Fdown(...(Fdown(L)-1)-1)-1. Usually it is performed with cycle that goes from L down to 0 with function Fdown and sums some elements from T. Chef wants to verify that the time complexity to calculate sum of A0, A1, A2, ..., AL is O(log L). In order to do so, he wonders how many times he has to access array T to calculate this sum. Help him to find this out. Since Chef works with really big indices. The value of L can be very large and is provided to you in binary representation as concatenation of strings L1, L2 repeated N times and string L3. -----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 only line of each test case contains three non-empty strings L1, L2, L3 and an integer N. Strings will contain only characters 0 and 1. To obtain binary representation of index L concatenate L1 with L2 repeated N times and with L3. You are guaranteed that the index will be positive. -----Output----- For each test case, output a single line containing number of times Fenwick tree data structure will access array T in order to compute sum of A0, A1, A2, ..., AL. -----Constraints----- - 1 ≤ T ≤ 300 - 1 ≤ Length(Li) ≤ 1000 - 1 ≤ N ≤ 106 -----Subtasks----- - Subtask #1 (20 points): |L1| + |L2| * N + |L3| ≤ 60 - Subtask #2 (30 points): 1 ≤ T ≤ 30, 1 ≤ N ≤ 100 - Subtask #3 (50 points): No additional constraints -----Example----- Input: 4 001 100 011 4 1000 1101 100 3 1010 001 101 4 010 101 000 4 Output: 6 12 8 10
["t=int(input())\ncount=[]\n\nfor i in range(t) :\n s = input()\n a,b,c,n = s.split()\n n=int(n)\n d = int(a+b*n+c,2)\n count.append(0)\n while(d>0) :\n d=(d&(d+1))-1\n count[i]+=1\n\n\nfor i in range(t) :\n print(count[i])\n"]
{"inputs": [["4", "001 100 011 4", "1000 1101 100 3", "1010 001 101 4", "010 101 000 4"]], "outputs": [["6", "12", "8", "10"]]}
INTERVIEW
PYTHON3
CODECHEF
229
439b44535007d1ac0c7d7ee7f39af1e4
UNKNOWN
Chef is operating a slush machine. The machine produces slush drinks with $M$ flavors (numbered $1$ through $M$); for each valid $i$, the maximum number of drinks with flavour $i$ the machine can produce is $C_i$. Chef expects $N$ customers to come buy slush drinks today. The customers are numbered $1$ through $N$ in the order in which they buy the drinks. For each valid $i$, the favorite flavour of the $i$-th customer is $D_i$ and this customer is willing to pay $F_i$ units of money for a drink with this flavour, or $B_i$ units of money for a drink with any other flavuor. Whenever a customer wants to buy a drink: - if it is possible to sell this customer a drink with their favourite flavour, Chef must sell them a drink with this flavour - otherwise, Chef must sell this customer a drink, but he may choose its flavour Chef wants to make the maximum possible profit. He is asking you to help him decide the flavours of the drinks he should sell to the customers in order to maximise the profit. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - The second line contains $M$ space-separated integers $C_1, C_2, \ldots, C_M$. - $N$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $D_i$, $F_i$ and $B_i$. -----Output----- For each test case, print two lines: - The first of these lines should contain a single integer — the maximum profit. - The second line should contain $N$ space-separated integers denoting the flavours of the drinks Chef should sell, in this order. If there are multiple solutions, you may find any one. -----Constraints----- - $1 \le T \le 1,000$ - $2 \le N, M \le 10^5$ - $1 \le D_i \le M$ for each valid $i$ - $1 \le C_i \le N$ for each valid $i$ - $1 \le B_i < F_i \le 10^9$ for each valid $i$ - $C_1+C_2+\ldots+C_M \ge N$ - the sum of $N$ over all test cases does not exceed $10^6$ - the sum of $M$ over all test cases does not exceed $10^6$ -----Example Input----- 1 5 3 1 2 3 2 6 3 2 10 7 2 50 3 1 10 5 1 7 4 -----Example Output----- 33 2 2 3 1 3
["t=int(input())\nfor i in range(t):\n n,m=list(map(int,input().split()))\n l=[0]+list(map(int,input().split()))\n s=0\n c=1\n m1=[]\n for i in range(n):\n d,f,b=list(map(int,input().split()))\n if(l[d]>0):\n m1.append(d)\n s+=f\n l[d]-=1\n else:\n m1.append(0)\n s+=b\n for i in range(n):\n if(m1[i]==0):\n for j in range(c,m+1):\n if(l[j]>0):\n m1[i]=j\n l[j]-=1\n c=j\n break\n print(s)\n print(*m1)\n", "t=int(input())\nfor i in range(t):\n n,m=list(map(int,input().split()))\n l=[0]+list(map(int,input().split()))\n s=0\n c=1\n m1=[]\n for i in range(n):\n d,f,b=list(map(int,input().split()))\n if(l[d]>0):\n m1.append(d)\n s+=f\n l[d]-=1\n else:\n m1.append(0)\n s+=b\n for i in range(n):\n if(m1[i]==0):\n for j in range(c,m+1):\n if(l[j]>0):\n m1[i]=j\n l[j]-=1\n c=j\n break\n print(s)\n print(*m1)\n", "# cook your dish here\nfor _ in range(int(input())):\n n,m=list(map(int, input().split()))\n c=list(map(int, input().split()))\n l=[0]*n \n total=0\n \n for i in range(n):\n d,f,b=list(map(int, input().split()))\n if c[d-1]>0:\n c[d-1]-=1 \n total+=f \n l[i]=d \n \n else:\n total+=b \n\n t=m-1 \n print(total) \n for i in l:\n if i>0:\n print(i, end=' ')\n \n else:\n while True:\n if c[t]>0:\n print(t+1, end=' ')\n c[t]-=1\n if c[t]==0:\n t-=1 \n break\n \n else:\n t-=1 \n \n print('') ", "# cook your dish here\nfor _ in range(int(input())):\n n,m=list(map(int, input().split()))\n c=list(map(int, input().split()))\n l = []\n l1=[0]*n \n total=0\n \n for i in range(n):\n d,f,b=list(map(int, input().split()))\n if c[d-1]>0:\n c[d-1]-=1 \n total+=f \n l1[i]=d \n \n else:\n total+=b \n l.append(b)\n \n t=m-1 \n print(total) \n for i in l1:\n if i>0:\n print(i, end=' ')\n \n else:\n while True:\n if c[t]>0:\n print(t+1, end=' ')\n c[t]-=1\n if c[t]==0:\n t-=1 \n break\n \n else:\n t-=1 \n \n print('') ", "t=int(input())\nfor i in range(t):\n n,m=list(map(int,input().split()))\n l=[0]+list(map(int,input().split()))\n s=0\n c=0\n m1=[]\n for i in range(n):\n d,f,b=list(map(int,input().split()))\n if(l[d]>0):\n m1.append(d)\n s+=f\n l[d]-=1\n else:\n m1.append(0)\n s+=b\n for i in range(n):\n if(m1[i]==0):\n for j in range(c,m+1):\n if(l[j]>0):\n m1[i]=j\n l[j]-=1\n c=j\n break\n print(s)\n print(*m1)\n \n \n \n \n \n \n", "t = int(input())\nfor _ in range(t):\n n,m = map(int,input().split())\n flav = list(map(int,input().split()))\n ans = []\n res = 0\n k = 0\n for i in range(n):\n d,f,b = map(int,input().split())\n if flav[d-1] == 0:\n res += b\n ans.append(-1)\n k+=1\n else:\n flav[d-1]-=1\n res += f\n ans.append(d)\n l = []\n for i in range(m):\n while flav[i]!=0 and k!=0:\n flav[i]-=1;\n k-=1\n l.append(i+1)\n \n print(res)\n c = 0\n for i in range(n):\n if ans[i] == -1:\n print(l[c],end =' ')\n c+=1\n else:\n print(ans[i],end=' ')\n print()", "for _ in range(int(input())):\n \n n, m = list(map(int,input().split()))\n \n l= list(map(int,input().split()))\n \n k=[0]*n\n ans=0\n \n for i in range(n):\n \n d,f,b = list(map(int,input().split()))\n \n if l[d-1]!=0:\n l[d-1]-=1\n k[i]=d\n ans+=f\n else:\n ans+=b\n j=0 \n for i in range(m):\n \n if l[i]!=0:\n \n while(j<n and l[i]>0):\n \n if k[j]==0:\n l[i]-=1\n k[j]=i+1\n j+=1\n \n print(ans)\n print(*k)\n \n \n \n \n", "for _ in range(int(input())):\n \n n, m = list(map(int,input().split()))\n \n l= list(map(int,input().split()))\n \n k=[0]*n\n ans=0\n \n for i in range(n):\n \n d,f,b = list(map(int,input().split()))\n \n if l[d-1]!=0:\n l[d-1]-=1\n k[i]=d\n ans+=f\n else:\n ans+=b\n j=0 \n for i in range(m):\n \n if l[i]!=0:\n \n while(j<n and l[i]>0):\n \n if k[j]==0:\n l[i]-=1\n k[j]=i+1\n j+=1\n \n print(ans)\n print(*k)\n \n \n \n \n \n \n \n \n \n \n \n \n", "for _ in range(int(input())):\n \n n, m = list(map(int,input().split()))\n \n l= list(map(int,input().split()))\n \n k=[0]*n\n ans=0\n \n for i in range(n):\n \n d,f,b = list(map(int,input().split()))\n \n if l[d-1]!=0:\n l[d-1]-=1\n k[i]=d\n ans+=f\n else:\n ans+=b\n j=0 \n for i in range(m):\n \n if l[i]!=0:\n \n while(j<n and l[i]>0):\n \n if k[j]==0:\n l[i]-=1\n k[j]=i+1\n j+=1\n \n print(ans)\n print(*k)\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n", "for _ in range(int(input())):\n \n n, m = list(map(int,input().split()))\n \n l= list(map(int,input().split()))\n \n k=[0]*n\n ans=0\n \n for i in range(n):\n \n d,f,b = list(map(int,input().split()))\n \n if l[d-1]!=0:\n l[d-1]-=1\n k[i]=d\n ans+=f\n else:\n ans+=b\n j=0 \n for i in range(m):\n \n if l[i]!=0:\n \n while(j<n and l[i]>0):\n \n if k[j]==0:\n l[i]-=1\n k[j]=i+1\n j+=1\n \n print(ans)\n print(*k)\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n", "for _ in range(int(input())):\n n,m=map(int,input().split())\n drinks=list(map(int,input().split()))\n output=0\n remain=[]\n olist=[0]*n \n for i in range(n):\n demand,goodp,badp=map(int,input().split())\n if drinks[demand-1]!=0:\n drinks[demand-1]-= 1 \n olist[i]=demand \n output+=goodp \n else:\n remain.append(i)\n output+=badp \n rj=0\n for i in range(len(drinks)):\n if drinks[i]==0:\n continue\n while(drinks[i]>0):\n if rj==len(remain):\n break\n olist[remain[rj]]=i+1\n rj+=1 \n drinks[i]-=1\n if rj==len(remain):\n break\n \n print(output)\n for i in olist:\n print(i,end=\" \")\n print()", "# cook your dish here\nfor _ in range(int(input())):\n n,m=list(map(int,input().split()))\n c=list(map(int,input().split()))\n d={}\n for i in range(len(c)):\n d[i+1]=c[i]\n ans=[]\n profit=0\n r_1=0\n for i in range(n):\n t_1,t_2,t_3=list(map(int,input().split()))\n if d[t_1]>0:\n profit+=t_2\n d[t_1]-=1\n ans.append(t_1)\n else:\n profit+=t_3\n r_1+=1\n ans.append('')\n print(profit)\n rem=[]\n for i in d:\n if d[i]>0:\n rem+=[i]*d[i]\n d[i]=0\n if len(rem)>=r_1:\n break\n j=0\n for i in range(len(ans)):\n if ans[i]=='':\n ans[i]=rem[j]\n j+=1\n print(*ans)\n", "for _ in range(int(input())):\n n,m=list(map(int,input().split()))\n c=list(map(int,input().split()))\n d={}\n for i in range(len(c)):\n d[i+1]=c[i]\n ans=[]\n profit=0\n r_1=0\n for i in range(n):\n t_1,t_2,t_3=list(map(int,input().split()))\n if d[t_1]>0:\n profit+=t_2\n d[t_1]-=1\n ans.append(t_1)\n else:\n profit+=t_3\n r_1+=1\n ans.append('')\n print(profit)\n rem=[]\n for i in d:\n if d[i]>0:\n rem+=[i]*d[i]\n d[i]=0\n if len(rem)>=r_1:\n break\n j=0\n for i in range(len(ans)):\n if ans[i]=='':\n ans[i]=rem[j]\n j+=1\n print(*ans)\n", "t=int(input())\nwhile(t>0):\n n,k=list(map(int,input().split()))\n l=[0]+list(map(int,input().split()))\n s=0\n m=[]\n for i in range(n):\n d,f,b=list(map(int,input().split()))\n if(l[d]>0):\n l[d]-=1\n s+=f\n m.append(d)\n else:\n s+=b\n m.append(0)\n c=0\n for i in range(n):\n if(m[i]==0):\n for j in range(c,k+1):\n if(l[j]>0):\n c=j\n m[i]=j\n l[j]-=1\n break\n print(s) \n print(*m)\n t-=1\n", "for _ in range(int(input())):\n n,m=map(int,input().split())\n c=list(map(int,input().split()))\n l=[]\n ans=0\n for z in range(n):\n d,f,b=map(int,input().split())\n l.append([d,f,b])\n v=[0]*len(l)\n res=[0]*n\n a=[]\n for j,i in enumerate(l):\n if(c[i[0]-1]>0):\n c[i[0]-1]-=1 \n ans+=i[1]\n v[j]=1\n res[j]=i[0]\n s=len(l)\n lc=len(c)\n count=0\n for i in range(s):\n if(v[i]==0):\n ans+=l[i][2]\n count+=1 \n k=[]\n for i in range(lc):\n if(c[i]>0):\n while(c[i]>0 and count>0):\n c[i]-=1 \n count-=1 \n k.append(i+1)\n kl=len(res)\n for i in range(kl):\n if(res[i]==0):\n res[i]=k.pop()\n print(ans) \n for i in res:\n print(i,end=' ')\n print()", "for _ in range(int(input())):\n n,m=map(int,input().split())\n c=list(map(int,input().split()))\n l=[]\n ans=0\n for z in range(n):\n d,f,b=map(int,input().split())\n l.append([d,f,b])\n v=[0]*len(l)\n res=[0]*n\n a=[]\n for j,i in enumerate(l):\n if(c[i[0]-1]>0):\n c[i[0]-1]-=1 \n ans+=i[1]\n v[j]=1\n res[j]=i[0]\n s=len(l)\n lc=len(c)\n count=0\n for i in range(s):\n if(v[i]==0):\n ans+=l[i][2]\n count+=1 \n k=[]\n for i in range(lc):\n if(c[i]>0):\n while(c[i]>0 and count>0):\n c[i]-=1 \n count-=1 \n k.append(i+1)\n kl=len(res)\n for i in range(kl):\n if(res[i]==0):\n res[i]=k.pop()\n print(ans) \n for i in res:\n print(i,end=' ')\n print()", "# cook your dish here\ntest=int(input())\nfor _ in range(test):\n n,m=map(int,input().split(\" \"))\n c=[int(j) for j in input().split(\" \")]\n revenue=0\n finallist=[]\n remlist=[]\n p=0\n customer=[]\n for j in range(n):\n customer.append([int(j) for j in input().split(\" \")])\n if c[customer[j][0]-1]>0:\n revenue += customer[j][1]\n finallist.append(customer[j][0])\n c[customer[j][0]-1]-=1\n else:\n remlist.append(p)\n p+=1\n\n k=0\n while len(remlist):\n if c[k]>0:\n revenue+=customer[remlist[0]][2]\n finallist.insert(remlist[0],k+1)\n c[k]-=1\n remlist.remove(remlist[0])\n else:\n k+=1\n print(revenue)\n print(*finallist, sep = \" \")", "t=int(input())\nfor i in range(t):\n # n = number of customers, m = number of flavors\n n , m = map(int,input().split(\" \"))\n # c for number of each flavor\n c = [int(j) for j in input().split(\" \")]\n # customer for customer preference data\n\n revenue = 0\n finallist = []\n remlist = []\n p=0\n\n customer = []\n for j in range(n):\n customer.append([int(j) for j in input().split(\" \")])\n if c[customer[j][0]-1]>0:\n revenue += customer[j][1]\n finallist.append(customer[j][0])\n c[customer[j][0]-1]-=1\n else:\n remlist.append(p)\n p+=1\n\n k=0\n while len(remlist):\n if c[k]>0:\n revenue += customer[remlist[0]][2]\n finallist.insert(remlist[0],k+1)\n c[k]-=1\n remlist.remove(remlist[0])\n else:\n k+=1\n \n print(revenue)\n print(*finallist, sep = \" \")", "# cook your dish here\nfor _ in range(int(input())):\n n,m=map(int,input().split())\n l=[0]*(m+1)\n assign=[0]*(n+1)\n s=0\n k=list(map(int,input().split()))\n for i in range(m):\n l[i+1]=k[i]\n for i in range(n):\n d,f,b=map(int,input().split())\n if(l[d]>0):\n l[d]-=1\n s+=f\n assign[i]=d\n else:\n s+=b\n j=0\n while(l[j]==0):\n j+=1\n for i in range(n):\n if(assign[i]==0):\n if(l[j]!=0):\n l[j]-=1\n assign[i]=j\n else:\n while(l[j]==0):\n j+=1\n l[j]-=1\n assign[i]=j\n print(s)\n for i in range(n):\n print(assign[i],end=\" \")\n print()\n ", "t=int(input())\nfor i in range(t):\n n,m=[int(x) for x in input().split()]\n arr=[]\n amt=[int(x) for x in input().split()]\n cost=0\n brr=[0 for i in range(n)]\n for j in range(n):\n arr.append([int(y) for y in input().split()])\n if(amt[arr[j][0]-1]>0):\n amt[arr[j][0]-1]-=1\n cost+=arr[j][1]\n brr[j]=arr[j][0]\n r=0\n for j in range(n):\n if(brr[j]==0):\n for k in range(r,m):\n if(amt[k]>0):\n r=k\n amt[k]-=1\n cost+=arr[j][2]\n brr[j]=k+1\n break\n print(cost)\n for j in range(n-1):\n print(brr[j],end=\" \")\n print(brr[-1])\n", "t=int(input())\nfor i in range(t):\n n,m=[int(x) for x in input().split()]\n arr=[]\n amt=[int(x) for x in input().split()]\n cost=0\n brr=[0 for i in range(n)]\n for j in range(n):\n arr.append([int(y) for y in input().split()])\n if(amt[arr[j][0]-1]>0):\n amt[arr[j][0]-1]-=1\n cost+=arr[j][1]\n brr[j]=arr[j][0]\n r=0\n for j in range(n):\n if(brr[j]==0):\n for k in range(r,m):\n if(amt[k]>0):\n r=k\n amt[k]-=1\n cost+=arr[j][2]\n brr[j]=k+1\n break\n print(cost)\n for j in range(n-1):\n print(brr[j],end=\" \")\n print(brr[-1])\n", "t=int(input())\nfor i in range(t):\n n,m=[int(x) for x in input().split()]\n amt=[int(x) for x in input().split()]\n cost=0\n brr=[0 for x in range(n)]\n crr=[]\n for j in range(n):\n d,f,b=[int(x) for x in input().split()]\n if(amt[d-1]>0):\n amt[d-1]-=1\n cost+=f\n brr[j]=d\n else:\n crr.append(j)\n cost+=b\n r=0\n for j in crr:\n for k in range(r,m):\n if(amt[k]>0):\n r=k\n amt[k]-=1\n brr[j]=k+1\n break\n print(cost)\n for j in range(n-1):\n print(brr[j],end=\" \")\n print(brr[-1])\n", "t=int(input())\nfor i in range(t):\n d=[]\n f=[]\n b=[]\n profit=0\n \n n,m=[int(x) for x in input().split()]\n\n order=[0 for j in range(n)]\n flav=[int(x) for x in input().split()]\n for j in range(n):\n inp=[int(x) for x in input().split()]\n d.append(inp[0])\n f.append(inp[1])\n b.append(inp[2])\n\n for j in range(n):\n if(flav[d[j]-1])>0:\n profit=profit+f[j]\n flav[d[j]-1]-=1\n order[j]=d[j]\n flag=0\n for j in range(n):\n if(order[j]==0):\n for k in range(flag,n):\n if(flav[flag]>0):\n order[j]=flag+1\n flav[flag]-=1\n profit+=b[j]\n break\n else:\n flag+=1\n\n print(profit)\n for k in range(len(order)-1):\n print(order[k],end=\" \")\n print(order[-1])\n"]
{"inputs": [["1", "5 3", "1 2 3", "2 6 3", "2 10 7", "2 50 3", "1 10 5", "1 7 4"]], "outputs": [["33", "2 2 3 1 3"]]}
INTERVIEW
PYTHON3
CODECHEF
13,185
57b1f2dbc72988fcd0da36554f61632f
UNKNOWN
Today, puppy Tuzik is going to a new dog cinema. He has already left his home and just realised that he forgot his dog-collar! This is a real problem because the city is filled with catchers looking for stray dogs. A city where Tuzik lives in can be considered as an infinite grid, where each cell has exactly four neighbouring cells: those sharing a common side with the cell. Such a property of the city leads to the fact, that the distance between cells (xA, yA) and (xB, yB) equals |xA - xB| + |yA - yB|. Initially, the puppy started at the cell with coordinates (0, 0). There are N dog-catchers located at the cells with the coordinates (xi, yi), where 1 ≤ i ≤ N. Tuzik's path can be described as a string S of M characters, each of which belongs to the set {'D', 'U', 'L', 'R'} (corresponding to it moving down, up, left, and right, respectively). To estimate his level of safety, Tuzik wants to know the sum of the distances from each cell on his path to all the dog-catchers. You don't need to output this sum for the staring cell of the path (i.e. the cell with the coordinates (0, 0)). -----Input----- The first line of the input contains two integers N and M. The following N lines contain two integers xi and yi each, describing coordinates of the dog-catchers. The last line of the input contains string S of M characters on the set {'D', 'U', 'L', 'R'}. - 'D' - decrease y by 1 - 'U' - increase y by 1 - 'L' - decrease x by 1 - 'R' - increase x by 1 -----Output----- Output M lines: for each cell of the path (except the starting cell), output the required sum of the distances. -----Constraints----- - 1 ≤ N ≤ 3 ✕ 105 - 1 ≤ M ≤ 3 ✕ 105 - -106 ≤ xi, yi ≤ 106 -----Example----- Input: 2 3 1 2 0 1 RDL Output: 4 6 6 -----Explanation----- Initially Tuzik stays at cell (0, 0). Let's consider his path: - Move 'R' to the cell (1, 0). Distance to the catcher (1, 2) equals 2, distance to the catcher (0, 1) equals 2, so the total distance equals 4 - Move 'D' to the cell (1, -1). Distance to the catcher (1, 2) equals 3, distance to the catcher (0, 1) equals 3, so the total distance equals 6 - Move 'L' to the cell (0, -1). Distance to the catcher (1, 2) equals 4, distance to the catcher (0, 1) equals 2, so the total distance equals 6
["# cook your dish here\nfrom sys import stdin,stdout\na,b=list(map(int,stdin.readline().split()))\nleft=[]\ntop=[]\nfor i in range(a):\n c,d=list(map(int,stdin.readline().split()))\n left.append(c)\n top.append(d)\nleft.sort()\ntop.sort()\nfrom bisect import bisect_right as br\nfrom bisect import bisect_left as bl\nrow=0\ncol=0\ntotal=0\ncons_x=0\ncons_y=0\nfor i in range(len(left)):\n cons_x+=(abs(left[i]))\n cons_y+=(abs(top[i]))\ntotal=cons_x+cons_y\ncc=stdin.readline().rstrip()\nfor i in cc:\n if i==\"R\":\n kk=br(left,col)\n cons_x=(cons_x+kk-(a-kk))\n col+=1\n if i==\"L\":\n kk=bl(left,col)\n cons_x=(cons_x+(a-kk)-kk)\n col-=1\n if i==\"U\":\n kk=br(top,row)\n cons_y=(cons_y+kk-(a-kk))\n row+=1\n if i==\"D\":\n kk=bl(top,row)\n cons_y=(cons_y+(a-kk)-kk)\n row-=1\n stdout.write(str(cons_x+cons_y))\n stdout.write(\"\\n\")\n \n\n"]
{"inputs": [["2 3", "1 2", "0 1", "RDL"]], "outputs": [["4", "6", "6"]]}
INTERVIEW
PYTHON3
CODECHEF
860
27d0a69336ca3e336549148f45d02850
UNKNOWN
Pied Piper is a startup company trying to build a new Internet called Pipernet. Currently, they have $A$ users and they gain $X$ users everyday. There is also another company called Hooli, which has currently $B$ users and gains $Y$ users everyday. Whichever company reaches $Z$ users first takes over Pipernet. In case both companies reach $Z$ users on the same day, Hooli takes over. Hooli is a very evil company (like E-Corp in Mr. Robot or Innovative Online Industries in Ready Player One). Therefore, many people are trying to help Pied Piper gain some users. Pied Piper has $N$ supporters with contribution values $C_1, C_2, \ldots, C_N$. For each valid $i$, when the $i$-th supporter contributes, Pied Piper gains $C_i$ users instantly. After contributing, the contribution value of the supporter is halved, i.e. $C_i$ changes to $\left\lfloor C_i / 2 \right\rfloor$. Each supporter may contribute any number of times, including zero. Supporters may contribute at any time until one of the companies takes over Pipernet, even during the current day. Find the minimum number of times supporters must contribute (the minimum total number of contributions) so that Pied Piper gains control of Pipernet. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains six space-separated integers $N$, $A$, $B$, $X$, $Y$ and $Z$. - The second line contains $N$ space-separated integers $C_1, C_2, \ldots, C_N$ — the initial contribution values. -----Output----- For each test case, if Hooli will always gain control of Pipernet, print a single line containing the string "RIP" (without quotes). Otherwise, print a single line containing one integer — the minimum number of times supporters must contribute. -----Constraints----- - $1 \le T \le 10$ - $1 \le N \le 10^5$ - $1 \le A, B, X, Y, Z \le 10^9$ - $A, B < Z$ - $0 \le C_i \le 10^9$ for each valid $i$ -----Example Input----- 3 3 10 15 5 10 100 12 15 18 3 10 15 5 10 100 5 5 10 4 40 80 30 30 100 100 100 100 100 -----Example Output----- 4 RIP 1 -----Explanation----- Example case 1: After $8$ days, Pied Piper will have $50$ users and Hooli will have $95$ users. Then, if each supporter contributes once, Pied Piper will also have $95$ users. After that, they still need $5$ more users, so supporter $3$ can contribute again, with $18/2 = 9$ more users. So the answer will be $4$. Example case 2: There is no way to beat Hooli.
["# cook your dish here\nimport heapq as hq\nfrom math import floor\nfor _ in range(int(input())):\n n,a,b,x,y,z=map(int,input().split())\n arr=[-int(i) for i in input().split()]\n days=((z-b-1)//y)\n ans=0\n hq.heapify(arr)\n curr=a+days*x\n while curr<z :\n u=hq.heappop(arr)\n u=-u\n if u==0 :\n break\n else:\n curr+=u\n ans+=1\n hq.heappush(arr,-(u//2))\n if curr>=z:\n print(ans)\n else:\n print(\"RIP\")", "# cook your dish here\nimport heapq as hq\nfrom math import floor\nfor _ in range(int(input())):\n n,a,b,x,y,z=list(map(int,input().split()))\n arr=[-int(i) for i in input().split()]\n days=((z-b-1)//y)\n \n\n ans=0\n\n hq.heapify(arr)\n curr=a+days*x\n\n while curr<z :\n\n u=hq.heappop(arr)\n u=-u\n if u==0 :\n break\n else:\n curr+=u\n ans+=1\n hq.heappush(arr,-(u//2))\n\n if curr>=z:\n\n print(ans)\n else:\n print(\"RIP\")\n\n\n\n\n\n", "# cook your dish here\nimport heapq as hq\nfrom math import floor\nfor _ in range(int(input())):\n n,a,b,x,y,z=list(map(int,input().split()))\n arr=[-int(i) for i in input().split()]\n days=floor((z-b-1)/y)\n \n\n ans=0\n\n hq.heapify(arr)\n curr=a+days*x\n\n while curr<z :\n\n u=hq.heappop(arr)\n u=-u\n if u==0 :\n break\n else:\n curr+=u\n ans+=1\n hq.heappush(arr,-(u//2))\n\n if curr>=z:\n\n print(ans)\n else:\n print(\"RIP\")\n\n\n\n\n\n", "# cook your dish here\nimport heapq as hq\nfrom math import floor\nfor _ in range(int(input())):\n n,a,b,x,y,z=list(map(int,input().split()))\n arr=[-int(i) for i in input().split()]\n days=floor((z-b)/y)\n if b+y*days==z:\n z+=1\n\n\n ans=0\n\n hq.heapify(arr)\n curr=a+days*x\n\n while curr<z :\n\n u=hq.heappop(arr)\n u=-u\n if u==0 :\n break\n else:\n curr+=u\n ans+=1\n hq.heappush(arr,-(u//2))\n\n if curr>=z:\n\n print(ans)\n else:\n print(\"RIP\")\n\n\n\n\n\n", "# cook your dish here\nimport heapq\nfrom math import ceil, floor\nfor _ in range(int(input())):\n n, a, b, x, y, z = map(int,input().split())\n s = [-int(i) for i in input().split()]\n days = floor((z-b)/y)\n current = a+days*x\n if b+days*y==z:\n z+=1\n count = 0\n heapq.heapify(s)\n\n while current<z:\n ret = heapq.heappop(s)\n ret = -ret\n\n if ret == 0:\n break\n else:\n current+=ret\n heapq.heappush(s,-(ret//2))\n #heapq.heapify(s)\n count+=1\n print(count) if current>=z else print(\"RIP\")", "# cook your dish here\nimport heapq\nfrom math import ceil, floor\nfor _ in range(int(input())):\n n, a, b, x, y, z = map(int,input().split())\n s = [-int(i) for i in input().split()]\n days = floor((z-b)/y)\n current = a+days*x\n if b+days*y==z:\n z+=1\n count = 0\n heapq.heapify(s)\n\n while current<z:\n ret = heapq.heappop(s)\n ret = -ret\n\n if ret == 0:\n break\n else:\n current+=ret\n heapq.heappush(s,-(ret//2))\n #heapq.heapify(s)\n count+=1\n print(count) if current>=z else print(\"RIP\")", "# cook your dish here\nimport heapq\nimport math\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= list(map(int,input().split()))\n t=math.ceil((z-b)/y)\n sumy= a+ x*(t-1) \n if (sum(cntrbtns)*2 +sumy) < z:\n print(\"RIP\")\n continue\n c=0\n for i in range(n):\n cntrbtns[i]= - cntrbtns[i]\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sumy>=z:\n print(c)\n break\n ele=heapq.heappushpop(cntrbtns,math.ceil(ele/2))\n if ele==0:\n print(\"RIP\")\n break\n sumy -=ele \n c+=1 \n \n\n", "# cook your dish here\nimport heapq\nimport math\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= list(map(int,input().split()))\n t=math.ceil((z-b)/y)\n sumy= a+ x*(t-1) \n if (sum(cntrbtns)*2 +sumy) < z:\n print(\"RIP\")\n continue\n c=0\n for i in range(n):\n cntrbtns[i]= - cntrbtns[i]\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sumy>=z:\n print(c)\n break\n ele=heapq.heappushpop(cntrbtns,math.ceil(ele/2))\n if ele==0:\n print(\"RIP\")\n break\n sumy -=ele \n c+=1 \n \n\n", "# cook your dish here\nimport heapq\nfrom math import ceil\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= [-(int(x)) for x in input().split()]\n \n if b>=z :\n print(\"RIP\")\n continue\n sum= a+ x*(ceil((z-b)/y)-1) \n c=0\n heapq.heapify(cntrbtns)\n ele=0\n while sum<z and any(cntrbtns):\n ele=-heapq.heappop(cntrbtns)\n sum+=ele\n heapq.heappush(cntrbtns,-(ele>>1))\n c+=1 \n print([\"RIP\",c][sum>=z])\n", "# cook your dish here\nimport heapq\nfrom math import ceil\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= [-(int(x)) for x in input().split()]\n \n if b>=z :\n print(\"RIP\")\n continue\n sum= a+ x*(ceil((z-b)/y)-1) \n c=0\n heapq.heapify(cntrbtns)\n ele=0\n while sum<z and any(cntrbtns):\n ele=-heapq.heappushpop(cntrbtns,-(ele>>1))\n sum+=ele \n c+=1 \n print([\"RIP\",c][sum>=z])\n", "# cook your dish here\nimport heapq\nfrom math import ceil\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= [-(int(x)) for x in input().split()]\n \n if b>=z :\n print(\"RIP\")\n continue\n sum= a+ x*(ceil((z-b)/y)-1) \n c=0\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sum>=z:\n print(c)\n break\n ele=-ele\n ele=heapq.heappushpop(cntrbtns,-(ele>>1))\n if ele==0:\n print(\"RIP\")\n break\n sum-=ele \n c+=1 \n \n", "# cook your dish here\nimport heapq\nfrom math import ceil\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= [-(int(x)) for x in input().split()]\n \n if b>=z :\n print(\"RIP\")\n continue\n t=ceil((z-b)/y)\n sum= a+ x*(t-1) \n c=0\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sum>=z:\n print(c)\n break\n ele=-ele\n ele=heapq.heappushpop(cntrbtns,-(ele>>1))\n if ele==0:\n print(\"RIP\")\n break\n sum-=ele \n c+=1 \n \n", "import heapq\nfrom math import ceil\nfor _ in range(int(input())):\n n,a,b,x,y,z =[int(i) for i in input().split()]\n A=[-int(i) for i in input().split()]\n heapq.heapify(A)\n days=ceil((z-b)/y)\n pig=z-a\n if(ceil((pig+2*sum(A))/x)>=days):\n print(\"RIP\")\n else:\n ans=0\n while(ceil(pig/x)>=days and any(A)):\n use=-heapq.heappop(A)\n pig-=use\n heapq.heappush(A,-(use>> 1))\n ans+=1\n print([\"RIP\",ans][ceil(pig/x)<days])\n", "# cook your dish here\nimport heapq\nimport math\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= [-(int(x)) for x in input().split()]\n \n if b>=z :\n print(\"RIP\")\n continue\n t=math.ceil((z-b)/y)\n sum= a+ x*(t-1) \n c=0\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sum>=z:\n print(c)\n break\n ele=-ele\n ele=heapq.heappushpop(cntrbtns,-(ele>>1))\n if ele==0:\n print(\"RIP\")\n break\n sum-=ele \n c+=1 \n \n", "from collections import deque\nimport math\ndef solve(nums, d,z):\n q1 = deque(sorted(nums, reverse=True))\n q2 = deque()\n c=0\n if d>=z:\n return c\n while True:\n if len(q1)==0 and len(q2)==0:\n break\n if not q2 or q1[0] > q2[0]:\n ele = q1.popleft()\n c+=1\n d+=ele\n if d>=z:\n return c\n ele = ele // 2\n if ele>=1:\n q2.append(ele)\n else:\n ele = q2.popleft()\n c+=1\n d+=ele\n if d>=z:\n return c\n ele = ele // 2\n if ele>=2:\n q2.append(ele)\n \n if not q1:\n q1, q2 = q2, q1\n if d>=z:\n return c\n return 'RIP'\n \n \nt=int(input())\nfor _ in range(t):\n n,a,b,x,y,z=list(map(int,input().split()))\n nums = list(map(int, input().split()))\n queries = set([])\n c=z-b\n c=math.ceil(c/y)-1\n d=a+x*c\n if d>=z:\n print(0)\n else:\n \n \n print(solve(nums,d,z))\n\n", "# cook your dish here\nfrom collections import deque\n\nimport math\ndef solve(nums, d,z):\n q1 = deque(sorted(nums, reverse=True))\n q2 = deque()\n c=0\n if d>=z:\n return c\n while True:\n if len(q1)==0 and len(q2)==0:\n break\n if not q2 or q1[0] > q2[0]:\n ele = q1.popleft()\n c+=1\n d+=ele\n if d>=z:\n return c\n ele = ele // 2\n if ele>=1:\n q2.append(ele)\n else:\n ele = q2.popleft()\n c+=1\n d+=ele\n if d>=z:\n return c\n ele = ele // 2\n if ele>=2:\n q2.append(ele)\n \n if not q1:\n q1, q2 = q2, q1\n if d>=z:\n return c\n return 'RIP'\n \n \nt=int(input())\nfor _ in range(t):\n n,a,b,x,y,z=list(map(int,input().split()))\n nums = list(map(int, input().split()))\n queries = set([])\n c=z-b\n c=math.ceil(c/y)-1\n d=a+x*c\n if d>=z:\n print(0)\n else:\n \n \n print(solve(nums,d,z))\n\n", "# cook your dish here\nimport heapq\nimport math\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= [-(int(x)) for x in input().split()]\n \n if b>=z :\n print(\"RIP\")\n continue\n t=math.ceil((z-b)/y)\n sum= a+ x*(t-1) \n c=0\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sum>=z:\n print(c)\n break\n ele=-ele\n ele=heapq.heappushpop(cntrbtns,-(ele//2))\n if ele==0:\n print(\"RIP\")\n break\n sum-=ele \n c+=1 \n \n", "# cook your dish here\nimport heapq\nimport math\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= list(map(int,input().split()))\n \n if b>=z :\n print(\"RIP\")\n continue\n t=math.ceil((z-b)/y)\n sum= a+ x*(t-1) \n c=0\n for i in range(n):\n cntrbtns[i]= - cntrbtns[i]\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sum>=z:\n print(c)\n break\n ele=-ele\n ele=heapq.heappushpop(cntrbtns,-(ele//2))\n if ele==0:\n print(\"RIP\")\n break\n sum-=ele \n c+=1 \n \n", "# cook your dish here\nimport heapq\nimport math\nfor i in range(int(input())):\n n,a,b,x,y,z = list(map(int,input().split()))\n cntrbtns= list(map(int,input().split()))\n \n if b>=z :\n print(\"RIP\")\n continue\n t=math.ceil((z-b)/y)\n sum= a+ x*(t-1) \n c=0\n for i in range(n):\n cntrbtns[i]= - cntrbtns[i]\n heapq.heapify(cntrbtns)\n ele=0\n while True:\n if sum>=z:\n print(c)\n break\n ele=heapq.heappushpop(cntrbtns,math.ceil(ele/2))\n if ele==0:\n print(\"RIP\")\n break\n sum-=ele \n c+=1 \n \n", "# cook your dish here\nimport heapq\nfrom math import ceil\n\nt= int(input())\nfor k in range(t):\n n, a, b, x, y, z = list(map(int,input().split()))\n c = list(map(int,input().split()))\n cont = [ -int(x) for x in c]\n \n days = int((z-b) / y )\n \n p_user = days * x + a\n \n need = z - p_user\n flag = 0\n \n heapq.heapify(cont) \n count = 0\n if ceil(((z-a) - (2*sum(c)))/x) >= days + 1:\n print(\"RIP\")\n \n \n elif p_user > z:\n print(0)\n else:\n while( p_user < z):\n \n key = heapq.heappop(cont)\n if key ==0:\n flag = 1 \n break\n count += 1\n p_user += -key\n #print(need,key,cont)\n heapq.heappush(cont,int(key/2))\n \n if flag == 0:\n print(count)\n else :\n print(\"RIP\")\n \n \n \n \n \n \n \n", "#\n\nimport heapq\nfrom math import ceil\n\nfor _ in range(int(input())):\n N, A, B, X, Y, Z = map(int, input().split())\n C = list(map(int, input().split()))\n heap = [-contrib for contrib in C]\n heapq.heapify(heap)\n \n hooli_days = ceil((Z-B)/Y)\n piper_init_gap = Z - A\n \n if ceil((piper_init_gap-2*sum(C))/X) >= hooli_days:\n print('RIP')\n else:\n num_contribs = 0\n while ceil(piper_init_gap/X) >= hooli_days and any(heap):\n contrib = -heapq.heappop(heap)\n piper_init_gap -= contrib\n heapq.heappush(heap, -(contrib >> 1))\n num_contribs += 1\n if ceil(piper_init_gap/X) < hooli_days:\n print(num_contribs)\n else:\n print('RIP')", "import heapq\nfrom math import ceil\n\n\nfor _ in range(int(input())):\n n, a, b, x, y, z = list(map(int, input().split()))\n c = [-int(i) for i in input().split()]\n heapq.heapify(c)\n days = ceil((z - b) / y)\n pig = z-a\n if ceil((pig + 2 * sum(c)) / x) >= days:\n print(\"RIP\")\n else:\n ans = 0\n while ceil(pig / x) >= days and any(c):\n use = -heapq.heappop(c)\n pig -= use\n heapq.heappush(c, -(use >> 1))\n ans += 1\n if ceil(pig / x) >= days:\n print(\"RIP\")\n else:\n print(ans)\n", "import heapq as heap\n\nfor t in range(int(input())):\n n, a, b, x, y, z = map(int,input().split( ))\n h = list(map(int,input().split( )))\n for i in range(n):\n h[i] *= -1\n heap.heapify(h)\n extra = (z - b - 1) // y\n a += x * extra\n b += y * extra\n ans = 0\n while a < z and h:\n maximum = -1 * heap.heappop(h)\n a += maximum\n ans += 1\n maximum //= 2\n if maximum:\n heap.heappush(h,-1 * maximum)\n if a < z:\n print(\"RIP\")\n else:\n print(ans)"]
{"inputs": [["3", "3 10 15 5 10 100", "12 15 18", "3 10 15 5 10 100", "5 5 10", "4 40 80 30 30 100", "100 100 100 100"]], "outputs": [["4", "RIP", "1"]]}
INTERVIEW
PYTHON3
CODECHEF
12,397
069eb79b8917dd3fc69cc30feda03fe2
UNKNOWN
Monisha likes to paint. She has painted $N$ paintings (numbered $1$ through $N$) and wants to choose some subset of these paintings for an exhibition. For each valid $i$, the $i$-th painting has beauty $b_i$ and the probability that it will be displayed at the exhibition is $p_i$. Each painting is chosen or excluded from the exhibition independently randomly. The beauty of the resulting exhibition is the bitwise XOR of the beauties of all displayed paintings. If no paintings are displayed, the XOR is $0$. Monisha wants to find out how beautiful her exhibition is going to be. Help her compute the expected value of the beauty of the exhibition. -----Input----- - The first line of the input contains a single 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 integer $N$. - The second line contains $N$ space-separated integers $b_1, b_2, \ldots, b_N$. - The third line contains $N$ space-separated real numbers $p_1, p_2, \ldots, p_N$. Each of these numbers is given with at most five digits after the decimal point. -----Output----- For each test case, print a single line containing one real number — the expected beauty of the exhibition. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$. -----Constraints----- - $1 \le N \le 10^5$ - $0 \le b_i \le 10^9$ for each valid $i$ - $0 \le p_i \le 1$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $4 \cdot 10^5$ -----Example Input----- 2 3 5 6 2 1.0 0.0 0.5 4 2 2 2 2 0.5 0.5 0.5 0.5 -----Example Output----- 6.000000000000000 1.000000000000000 -----Explanation----- Example case 1: The first painting must be displayed at the exhibition, the second one must be excluded. If the third painting is displayed (with probability $0.5$), the beauty is $5 \oplus 2 = 7$; if it is excluded (with probability $0.5$), the beauty is $5$. The expected beauty is $0.5 \cdot 5 + 0.5 \cdot 7 = 6$. Example case 2: If there is an odd number of paintings at the exhibition, the beauty is $2$; this has a probability of $0.5$. If there is an even number of paintings, the beauty is $0$ (with probability $0.5$), so the expected beauty is $0.5 \cdot 0 + 0.5 \cdot 2 = 1$.
["# cook your dish here\nt=int(input())\nwhile(t>0):\n n=int(input())\n b=[int(x) for x in input().split()]\n p=[float(x) for x in input().split()]\n s=[0]*(10)\n yet=2\n mx=0\n for i in range(n):\n st=bin(b[i])\n rng=len(st)-2\n if(rng+2>yet):\n for ml in range(rng+2-yet):\n s.append(0)\n if(rng>mx):\n mx=rng\n for k in range(2,rng+2):\n if(st[k]=='1'):\n s[rng-k+1]=(s[rng-k+1]*(1-p[i]))+((1-s[rng-k+1])*(p[i]))\n # else:\n # s[k-2]=(s[k-2]*1)\n # print(s)\n # print(mx)\n mult=1\n ans=0\n for i in range(0,mx):\n ans+=mult*s[i]\n mult=mult*2\n print(\"%.16f\" % ans)\n t-=1", "# cook your dish here\nt=int(input())\nwhile t:\n x=0\n n=int(input())\n b=list(map(int,input().strip().split()))\n p=list(map(float,input().strip().split()))\n for i in range(30):\n pb=0.0\n for j in range(n):\n if b[j]&(1<<i):\n pb=pb*(1-p[j])+(1-pb)*p[j]\n x+=(1<<i)*pb\n print(x)\n t-=1", "for _ in range(int(input())):\n n = int(input())\n B = [int(x) for x in input().split()]\n P = [float(x) for x in input().split()]\n tl = [0]*30\n for b,p in zip(B,P):\n bs = bin(b)[2:][::-1]\n for i in range(len(bs)):\n if bs[i]=='1':\n tl[29-i] = (1-tl[29-i])*p + (1-p)*tl[29-i]\n ans = 0\n for i in range(29,-1,-1):\n ans += (2**(29-i))*tl[i]\n print(ans)", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n xl = [int(x) for x in input().split()]\n pl = [float(x) for x in input().split()]\n tl = [0]*30\n for x,p in zip(xl,pl):\n bs = bin(x)[2:][::-1]\n for i in range(len(bs)):\n if bs[i]=='1':\n tl[i] = (1-tl[i])*p + (1-p)*tl[i]\n ans = 0\n for i in range(30):\n ans += (2**i)*tl[i]\n print(ans)", "for _ in range(int(input())):\n n=int(input())\n xl=[int(x) for x in input().split()]\n pl=[float(x) for x in input().split()]\n tl=[0]*30\n for x,p in zip(xl,pl):\n bs=bin(x)[2:][::-1]\n for i in range(len(bs)):\n if bs[i]=='1':\n tl[i]=(1-tl[i])*p+(1-p)*tl[i]\n ans=0\n for i in range(30):\n ans+=(2**i)*tl[i]\n print(ans)", "# cook your dish here\nk = 30 \nfor _ in range(int(input())):\n n = int(input())\n l1 = list(map(int, input().split()))\n l2 = list(map(float, input().split()))\n ans = 0\n for i in range(k):\n p = 0\n for j in range(n):\n if(l1[j] & (1<<i)):\n p = p*(1-l2[j])+(1-p)*l2[j]\n ans += p*(1<<i)\n \n print(\"%.15f\"%ans)", "from bisect import bisect_right as br\nfrom bisect import bisect_left as bl\nfrom collections import *\nfrom itertools import *\nimport functools\nimport sys\nfrom math import *\nfrom decimal import *\nfrom copy import *\ngetcontext().prec = 30\nMAX = sys.maxsize\nMAXN = 10**6+10\nMOD = 10**9+7\ndef isprime(n):\n n = abs(int(n))\n if n < 2:\n return False\n if n == 2: \n return True \n if not n & 1: \n return False\n for x in range(3, int(n**0.5) + 1, 2):\n if n % x == 0:\n return False\n return True\n\ndef mhd(a,b):\n return abs(a[0]-b[0])+abs(b[1]-a[1])\n\ndef numIN(x = \" \"):\n return(map(int,sys.stdin.readline().strip().split(x)))\n\ndef charIN(x= ' '):\n return(sys.stdin.readline().strip().split(x))\n\ndef arrIN():\n return list(numIN())\n\ndef dis(x,y):\n a = y[0]-x[0]\n b = x[1]-y[1]\n return (a*a+b*b)**0.5\n\ndef lgcd(a):\n g = a[0]\n for i in range(1,len(a)):\n g = math.gcd(g,a[i])\n return g\n\ndef ms(a):\n msf = -MAX\n meh = 0\n st = en = be = 0\n for i in range(len(a)):\n meh+=a[i]\n if msf<meh:\n msf = meh\n st = be\n en = i\n if meh<0:\n meh = 0\n be = i+1\n return msf,st,en\n\ndef flush():\n return sys.stdout.flush()\n\n'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\u00a0\u00a0\u00a0\u00a0'''\n\nfor _ in range(int(input())):\n n = int(input())\n a = arrIN()\n p = list(map(float,input().split()))\n dp = [0]*32\n for i in range(32):\n for j in range(n):\n if ((a[j]) & (1<<i)):\n dp[i] = (1-p[j])*dp[i]+(1-dp[i])*p[j]\n ans = 0\n for i in range(32):\n ans+=dp[i]*(1<<i)\n print(ans)", "T=int(input())\nfor i in range(0,T):\n N=int(input())\n b=[int(x) for x in input().split()]\n p=[float(x) for x in input().split()]\n arr=[0]*32\n for j in range(0,len(b)):\n st=bin(b[j])\n st=st[2:]\n #print(st)\n for k in range(1,len(st)+1):\n if(st[-k]=='1'):\n arr[k-1]=((1-arr[k-1])*p[j])+((1-p[j])*arr[k-1])\n #for k in range(len(st)-1,-1,-1):\n #if(st[k]=='1'):\n #arr[N-k-1]=(arr[N-k-1]*(1-p[j]))+((1-arr[N-k-1])*p[j])\n #print(N-k,arr)\n #print(arr)\n\n exp=0\n xr=1\n for j in range(0,len(arr)):\n exp=exp+(xr*arr[j])\n xr=xr*2\n\n print(exp)\n\n\n", "# cook your dish here\nl=[1]\nfor k in range(31):\n l.append(l[-1]*2)\ndef convert(s):\n while len(s)!=32:\n s='0'+s\n ans=\"\"\n for i in s:\n ans=i+ans\n return ans\ndef main():\n for _ in range(int(input())):\n n=int(input())\n val=list(map(int,input().split()))\n p=list(map(float,input().split()))\n res=0\n for i in range(32):\n x=0\n for j in range(n):\n if val[j] & (1<<i):\n x=(p[j]*(1-x))+(1-p[j])*(x)\n res+=(l[i]*x)\n print(\"%.6f\"%res)\ndef __starting_point():\n main()\n \n\n__starting_point()", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n b=list(map(float,input().split()))\n ans=0\n for i in range(30):\n temp=0\n for j in range(n):\n if (a[j]&(1<<i)):\n temp=temp*(1-b[j])+(1-temp)*b[j]\n ans+=temp*(1<<i)\n print(ans)", "\nt = int(input())\nwhile(t>0 ):\n n= int(input())\n #b = [0]*n;\n B=list(map(int,input().strip().split(' ')))\n P=list(map(float,input().strip().split(' ')))\n l = [0]*32\n ans = 0 \n cur_bit = 1\n for i in range(0,31):\n # print(cur_bit,end =\" \")\n cur_prob = 0.0\n for j in range(0,len(B)):\n if(cur_bit&B[j]):\n cur_prob = cur_prob*(1-P[j]) + (1-cur_prob)*P[j]\n cur_bit = cur_bit << 1\n # print(cur_prob)\n ans = ans + cur_prob* (2**i)\n\n print(ans)\n\n\n t-=1;", "t=int(input())\nwhile(t>0):\n n=int(input())\n b=[int(x) for x in input().split()]\n p=[float(x) for x in input().split()]\n s=[0]*(10)\n yet=2\n mx=0\n for i in range(n):\n st=bin(b[i])\n rng=len(st)-2\n if(rng+2>yet):\n for ml in range(rng+2-yet):\n s.append(0)\n if(rng>mx):\n mx=rng\n for k in range(2,rng+2):\n if(st[k]=='1'):\n s[rng-k+1]=(s[rng-k+1]*(1-p[i]))+((1-s[rng-k+1])*(p[i]))\n # else:\n # s[k-2]=(s[k-2]*1)\n # print(s)\n # print(mx)\n mult=1\n ans=0\n for i in range(0,mx):\n ans+=mult*s[i]\n mult=mult*2\n print(\"%.16f\" % ans)\n t-=1", "for _ in range(int(input())):\n n = int(input())\n xl = [int(x) for x in input().split()]\n pl = [float(x) for x in input().split()]\n tl = [0]*30\n for x,p in zip(xl,pl):\n bs = bin(x)[2:][::-1]\n for i in range(len(bs)):\n if bs[i]=='1':\n tl[i] = (1-tl[i])*p + (1-p)*tl[i]\n ans = 0\n for i in range(30):\n ans += (2**i)*tl[i]\n print(ans)"]
{"inputs": [["2", "3", "5 6 2", "1.0 0.0 0.5", "4", "2 2 2 2", "0.5 0.5 0.5 0.5"]], "outputs": [["6.000000000000000", "1.000000000000000"]]}
INTERVIEW
PYTHON3
CODECHEF
6,721
88fb55c640a4ad5708428991926bcf04
UNKNOWN
Chef wants you to distribute candies among $N$ kids who are sitting in a circle. However, he wants to make some kids jealous of others. Thus, he wants you to distribute candies in such a way that there is a difference of at least $K$ candies between two adjacent kids. Given the value of $N$ and $K$, you need to find the minimum number of candies you need to satisfy the given conditions, such that, each kid gets at least one candy. -----Input:----- - First line will contain $T$, the number of testcases. Then the testcases follow. - The only line of each testcase contains two space-separated integers $N$ and $K$. -----Output:----- For each test case, print a single line containing one integer ― the number of candies you need. -----Constraints----- - $1 \leq T \leq 10^6$ - $2 \leq N \leq 10^3$ - $0 \leq K \leq 10^4$ -----Sample Input:----- 1 2 1 -----Sample Output:----- 3 -----EXPLANATION:----- The minimum number of candies required is $3$. One kid needs to have $1$ candy and the other needs to have $2$ candy to have a difference of $1$ candy between them.
["# cook your dish here\nfor _ in range(int(input())):\n n,k = [int(v) for v in input().split()]\n ans = (n//2)*(k+2)\n if n%2 == 0:\n ans = ans\n else:\n ans += 1 + 2*k\n \n print(ans)", "t=int(input())\r\nl=[]\r\nfor i in range(t):\r\n n,k=map(int,input().split())\r\n can=0\r\n if n%2==0:\r\n can=(n//2)+(n-(n//2))*(k+1)\r\n else:\r\n can=(n//2)+1+(n-(n//2)-1)*(k+1)+2*k\r\n l.append(can)\r\nfor x in range(t):\r\n print(l[x])", "t=int(input())\nfor i in range(t):\n m,l=list(map(int,input().split()))\n if m==1:\n print(1)\n else:\n s=(2+l)*(m//2)\n if m%2!=0:\n s+=1+(2*l)\n print(s) \n \n \n", "t=int(input())\r\n\r\nr=[]\r\nfor each in range(t):\r\n n,k=list(map(int,input().split()))\r\n\r\n if n%2==0:\r\n r.append(n+n*k//2)\r\n else:\r\n n=n-1\r\n r.append(n+n*k//2+1+2*k)\r\n\r\nfor each in r:\r\n print(each)\r\n", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n n,k=map(int,input().split())\n if n==1:\n print(1)\n else:\n r=(2+k)*(n//2)\n if n%2!=0:\n r+=1+2*k\n print(r)", "# cook your dish here\n# cook your code here\nfor _ in range(int(input())):\n n,k = map(int,input().split())\n print( (n//2)*(k+2) + (n%2)*(2*k+1)) ", "t = int(input())\nwhile t != 0:\n n, k = list(map(int, input().split()))\n \n if n% 2 == 0 :\n even = n/2 \n odd = n/2 * (k+1)\n# print(\"even\",even,\"odd\",odd)\n else:\n odd = (n-1)/2 * (k+1)\n even = (n-1)/2 + (1 + 2*k)\n# print(\"even\",even,\"odd\",odd)\n total = even + odd\n print(int(total))\n \n t -= 1", "# cook your dish here\nfor _ in range(int(input())):\n n,k=map(int,input().split())\n if n&1 :\n print(n//2*(k+2)+2*k+1)\n else:\n print(n//2*(k+2))", "# cook your dish here\nfor _ in range(int(input())):\n n,k=map(int,input().split())\n if n&1 :\n print(n//2*(k+2)+2*k+1)\n else:\n print(n//2*(k+2))", "for _ in range(int(input())):\r\n n,k=map(int,input().split())\r\n if(n%2==0):\r\n ans=n+(n//2)*k\r\n print(ans)\r\n else:\r\n n=n-1\r\n ans=n+(n//2)*k+1+2*k\r\n print(ans)", "for i in range(int(input())):\n n,k=map(int,input().split())\n if k==0:\n print(n)\n else:\n if n%2==0:\n s=n//2 + (n//2)*(k+1)\n print(s)\n else:\n s=n//2 + 2*(k+1) + (n//2)*(k+1)\n print(s-1)", "# cook your dish here\nt = int(input())\nfor z in range(t):\n n,k = list(map(int,input().split()))\n if n%2==0:\n print(n+(k*n//2))\n else:\n print(n+((((n-1)//2)+2)*k))\n \n", "# cook your dish here\n#code\nt=int(input())\nwhile(t>0):\n t-=1\n n,k=map(int,input().split())\n ans=(n//2)*(k+2)+(n%2)*(2*k+1);\n print(ans);", "# cook your dish here\nfor _ in range(int(input())):\n n, k = map(int, input().split())\n if n % 2 == 0:\n print(n + ((n // 2) * k))\n else:\n if n == 3:\n print(n + (3 * k))\n else:\n print(n + (3 * k) + ((n - 3)//2 * k))", "t=int(input())\nwhile t>0:\n n,k=map(int,input().split())\n if n%2==0:\n ans=(n//2)*(2+k)\n else:\n ans=((n//2)*(2+k))+(2*k+1)\n print(int(ans))\n t-=1", "from sys import stdin\ntry:\n for _ in range(int(stdin.readline())):\n n,k=map(int,stdin.readline().rstrip().split())\n if n%2==0:\n candy=((k+2)*n)//2\n elif n==3:\n candy=(3+3*k)\n else:\n candy=((k+2)*(n-1))//2+(2*k+1)\n print(candy)\nexcept:\n pass", "from sys import stdin\ntry:\n for _ in range(int(stdin.readline())):\n n,k=map(int,stdin.readline().rstrip().split())\n if n%2==0:\n candy=((k+2)*n)//2\n elif n==3:\n candy=(3+3*k)\n else:\n candy=((k+2)*(n-1))//2+(2*k+1)\n print(candy)\nexcept:\n pass", "from sys import stdin\ntry:\n for _ in range(int(stdin.readline())):\n n,k=map(int,stdin.readline().rstrip().split())\n if n%2==0:\n candy=((k+2)*n)//2\n elif n==3:\n candy=(3+3*k)\n else:\n candy=((k+2)*(n-1))//2+(2*k+1)\n print(candy)\nexcept:\n pass", "for _ in range(int(input())):\r\n n,k=map(int,input().split())\r\n s = 0\r\n if n%2==0:\r\n s = (int)((n / 2) * (k + 2))\r\n else:\r\n s = (int)(((n-1) / 2) * (k + 2)) + ((2*k)+1)\r\n print(s)", "t=int(input())\nl=[]\nwhile(t):\n q=input()\n l=q.split()\n n=int(l[0])\n k=int(l[1])\n if(n%2==0):\n print(int(n/2+n/2*(1+k)))\n else:\n print(int(n//2+n//2*(1+k)+1+2*k))\n t=t-1", "for _ in range(int(input())):\n n,k=map(int,input().split())\n if k==0:\n print(n)\n elif n%2==0:\n print((k+1)*n//2+n//2)\n else:\n print((k+1)*(n//2)+n//2+2*k+1)", "for _ in range(int(input())):\r\n n, k = list(map(int, input().split()))\r\n\r\n if n % 2 == 0:\r\n print(k * (n // 2) + n)\r\n\r\n if n % 2 == 1:\r\n print(k * (n // 2) + 2 * k + n)\r\n", "n=int(input())\r\nfor i in range(n):\r\n ans=0\r\n t,k=map(int,input().split())\r\n ans+=(k+2)*(t//2)\r\n if t%2==1:\r\n ans+=1+2*k\r\n print(ans)"]
{"inputs": [["1", "2 1"]], "outputs": [["3"]]}
INTERVIEW
PYTHON3
CODECHEF
5,450
46b746160d8a9e2a1ff1c0abc6fd1e67
UNKNOWN
After the hundred years of war started by the Fire Nation, its time for the Water Tribes to bring it to an end. Avatar asked Sokka to go to the Southern WarZone from The Northern WarZone and gave him some money in a bag for his journey. Sokka has the World Map to guide him during his journey . The World Map is described as a $N$x$N$ grid where the Northern WarZone is denoted by $(0,0)$ and the Southern WarZone is denoted by $(N-1,N-1)$. Each location on the world map is denoted in a similar way by two integers r and c. For each location having: - $r = c$ denotes neutral land ( $(0,0)$ and $(N-1,N-1)$ also come under this category) - $r < c$ denotes the land under the rule of Water Tribes - $r > c$ denotes the land under the rule of Fire Kingdom Being wise Sokka travels only from one location to any other valid location (by valid location we mean a location which exists on the world map grid i.e. for that location $0 \leq r < N$ and $0 \leq c < N$ ) just to the right $(r,c+1)$ or below $(r+1,c)$ the current location randomly. Due to the times of war , Sokka has to pay one coin each time he transitions from one nation to another. Here a transition is counted when Sokka is in Water Tribe land and moves to Fire Nation Land crossing the neutral land or vice versa .The first move is obviously never counted as a transition. Moreover , the coin is to be payed exactly once for one such transition (eg. if he makes this transition k times he has to pay k coins). The initial number of coins Sokka has is $2*N$. The probability that the coins he has when he reaches his destination is lesser than the number of coins he started his journey with can be expressed as a fraction $P/Q$, where P and Q are integers $(P \geq 0, Q > 0)$ and Q is co-prime with $(10^9)+7$. You should compute $P/Q$ modulo $(10^9)+7$ for $T$ values of $N$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, one integer $N$, the size of the world map grid. -----Output:----- For each testcase, output in a single line , the Probability modulo (10^9)+7. -----Constraints----- - $1 \leq T \leq 100000$ - $2 \leq N \leq 10^9$ -----Sample Input:----- 2 5 2 -----Sample Output:----- 200000002 0 -----EXPLANATION:----- For N=2, there is no possible path in which Sokka has to spend money, so the final number of coins in the bag is not lesser than the initial amount.
["m=1000000007\r\ndef gcd(a, b): \r\n if (a == 0): \r\n return b \r\n return gcd(b % a, a)\r\ndef modexp(x, n): \r\n if (n == 0) : \r\n return 1\r\n elif (n % 2 == 0) : \r\n return modexp((x * x) % m, n // 2) \r\n else : \r\n return (x * modexp((x * x) % m, \r\n (n - 1) / 2) % m)\r\ndef getFractionModulo(a, b): \r\n c = gcd(a, b)\r\n a = a // c \r\n b = b // c \r\n d = modexp(b, m - 2) \r\n ans = ((a % m) * (d % m)) % m\r\n return ans\r\nt=int(input())\r\nfor i in range(t):\r\n n=int(input())\r\n n=n-1\r\n print(getFractionModulo(n-1,n+1))\r\n"]
{"inputs": [["2", "5", "2"]], "outputs": [["200000002", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
648
06fba60dbb7e4c250482ebf54078b048
UNKNOWN
Bandwidth of a matrix A is defined as the smallest non-negative integer K such that A(i, j) = 0 for |i - j| > K. For example, a matrix with all zeros will have its bandwith equal to zero. Similarly bandwith of diagonal matrix will also be zero. For example, for the below given matrix, the bandwith of this matrix is 2. 1 0 0 0 1 1 1 1 0 Bandwidth of the below matrix is 1. Bandwidth of the below matrix is 2. Bandwidth of the below matrix is also 2. You will be a given a binary matrix A of dimensions N × N. You are allowed to make following operation as many times as you wish (possibly zero or more). In a single operation, you can swap any two entries of the matrix. Your aim is to minimize the bandwidth of the matrix. Find the minimum bandwidth of the matrix A you can get after making as many operations of above type as you want. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follow. First line of each test case contains an integer N denoting the height/width of the matrix. Next N lines of each test case contain N space separated binary integers (either zero or one) corresponding to the entries of the matrix. -----Output----- For each test case, output a single integer corresponding to the minimum bandwidth that you can obtain. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 500 - 0 ≤ A(i, j) ≤ 1 -----Subtasks----- - Subtask #1 (40 points) : 1 ≤ N ≤ 100 - Subtask #2 (60 points) : original constraints -----Example----- Input: 6 2 0 0 0 0 2 1 0 0 1 2 1 0 1 0 2 1 0 1 1 3 1 0 0 0 1 1 1 1 0 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output: 0 0 0 1 1 3 -----Explanation----- Example case 1. The bandwidth of a matrix will all zero entries will be zero. This is the minimum bandwidth you can get, so there is no need of performing any swap operation. Example case 2. The bandwidth of a diagonal matrix will also be zero. Example case 3. You can make the given matrix a diagonal matrix by swapping A(2, 1) and A(2, 2), which will have zero bandwidth. Example case 4. You can not make swaps in any way that can reduce the bandwidth of this matrix. Bandwidth of this matrix is equal to 1, which is the minimum bandwidth that you can get. Example case 5. Bandwidth of the given matrix is 2. You can make it equal to be 1 by swapping A(3, 1) and A(3, 3), i.e. the matrix after the operation will look like 1 0 0 0 1 1 0 1 1 The bandwidth of this matrix is 1. Example case 6. The swap operations won't have any effect on the matrix. Its bandwidth is equal to 3.
["t = int(input())\r\nfor i in range(t):\r\n n = int(input())\r\n A = []\r\n for i in range(0, n):\r\n A.append([int(i) for i in input().split()])\r\n ones = sum([sum(i) for i in A])\r\n compare = n\r\n ans = 0\r\n for i in range(0, n):\r\n if ones <= compare:\r\n ans = i\r\n break\r\n compare += 2*(n-1-i)\r\n print(ans)", "# cook your dish here\ntry:\n \n for i in range(int(input())):\n n = int(input())\n A = []\n for i in range(0, n):\n A.append([int(i) for i in input().split()])\n ones = sum([sum(i) for i in A])\n compare = n\n ans = 0\n for i in range(0, n):\n if ones <= compare:\n ans = i\n break\n compare += 2*(n-1-i)\n print(ans)\nexcept:\n pass", "# cook your dish here\nT = int(input())\nfor i in range(T):\n n = int(input())\n A = []\n for i in range(0, n):\n A.append([int(i) for i in input().split()])\n ones = sum([sum(i) for i in A])\n compare = n\n ans = 0\n for i in range(0, n):\n if ones <= compare:\n ans = i\n break\n compare += 2*(n-1-i)\n print(ans)", "# cook your dish here\n#problem code: BANDMATR, language: python3.6\nfor __ in range(int(input())):\n a = []\n n = int(input())\n for i in range(n):\n a.append(list(map(int,input().split())))\n count=0\n for i in range(n):\n for j in range(n): \n if a[i][j]==0:\n count+=1\n temp = n-1\n for k in range(n-1):\n arb = int((n-1-k)*(n - k))\n if count >= arb:\n temp = k\n break\n print(temp) ", "# cook your dish here\ntry:\n T=int(input())\n nsw=[]\n for _ in range(T):\n N=int(input())\n a=[]\n for __ in range(N): \n a+=list(map(int, input().rstrip().split()))\n count=0\n for ___ in a:\n if 1==___:\n count+=1\n count=count-N\n ans=0;\n while count>0:\n ans+=1\n count=count-(2*(N-1))\n N=N-1\n \n nsw+=[ans]\n print('\\n'.join(map(str,nsw)))\nexcept EOFError:\n pass", "try:\n t = int(input())\n for _ in range(t):\n N = int(input())\n c = 0\n p = N-1\n a = [100 for i in range(N)]\n a[0] = N \n for i in range(1,N):\n a[i] = a[i-1]+2*p \n p-=1 \n for j in range(N):\n row = input().split(' ')\n for k in range(N):\n if int(row[k]) == 1:\n c+=1 \n for i in range(N):\n if c <= a[i]:\n print(i)\n break\nexcept:\n pass\n \n \n", "#problem code: BANDMATR, language: python3.6\r\nfor __ in range(int(input())):\r\n a = []\r\n n = int(input())\r\n for i in range(n):\r\n a.append(list(map(int,input().split())))\r\n count=0\r\n for i in range(n):\r\n for j in range(n): \r\n if a[i][j]==0:\r\n count+=1\r\n temp = n-1\r\n for k in range(n-1):\r\n arb = int((n-1-k)*(n - k))\r\n if count >= arb:\r\n temp = k\r\n break\r\n print(temp)", "# cook your dish here\ntry:\n T=int(input())\n nsw=[]\n for _ in range(T):\n N=int(input())\n a=[]\n for __ in range(N): \n a+=list(map(int, input().rstrip().split()))\n count=0\n for ___ in a:\n if 1==___:\n count+=1\n count=count-N\n ans=0;\n while count>0:\n ans+=1\n count=count-(2*(N-1))\n N=N-1\n \n nsw+=[ans]\n print('\\n'.join(map(str,nsw)))\nexcept EOFError:\n pass", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n n=int(input())\n a=[]\n for i in range(n):\n lis=[int(inp) for inp in input().split()]\n a.append(lis)\n zer_count=0\n for i in range(n):\n for j in range(n):\n if a[i][j]==0:\n zer_count+=1\n count=0\n while count<=n:\n zer_count-=2*(count+1)\n if zer_count<0:\n break\n else:\n count+=1\n print(n-1-count)", "# cook your dish here\nt=int(input())\nwhile t>0 :\n n=int(input())\n l=[list(map(int,input().split())) for i in range(n)]\n c=0 \n for i in l :\n c+=i.count(0)\n c=c//2 \n i=1 \n while c>=0 :\n c-=i\n i+=1\n print(n-i+1)\n t-=1", "# cook your dish here\nT = int(input())\nfor i in range(T):\n n = int(input())\n A = []\n for i in range(0, n):\n A.append([int(i) for i in input().split()])\n ones = sum([sum(i) for i in A])\n compare = n\n ans = 0\n for i in range(0, n):\n if ones <= compare:\n ans = i\n break\n compare += 2*(n-1-i)\n print(ans)", "# cook your dish here\nT = int(input())\nfor i in range(T):\n n = int(input())\n A = []\n for i in range(0, n):\n A.append([int(i) for i in input().split()])\n ones = sum([sum(i) for i in A])\n compare = n\n ans = 0\n for i in range(0, n):\n if ones <= compare:\n ans = i\n break\n compare += 2*(n-1-i)\n print(ans)", "for _ in range(int(input())):\r\n n = int(input())\r\n z = 0\r\n for _ in range(n):\r\n z += input().strip().split(' ').count('0')\r\n \r\n k = 2\r\n r = n - 1\r\n cs = k\r\n while cs <= z:\r\n r -= 1\r\n k += 2\r\n cs += k\r\n print(r)\r\n", "for _ in range(int(input())):\r\n n = int(input())\r\n d = {}\r\n i = 0\r\n while (i < n):\r\n j = 0\r\n o = []\r\n o = list(map(int, input().split()))\r\n while (j < len(o)):\r\n x = o[j]\r\n d[(i, j)] = x\r\n j = j + 1\r\n\r\n i = i + 1\r\n\r\n do = {}\r\n dz = {}\r\n\r\n i = 0\r\n while(i < n):\r\n j = 0\r\n while(j < n):\r\n e = d[(i,j)]\r\n ri = i + 1\r\n rj = j + 1\r\n y = abs(ri - rj)\r\n if e == 1:\r\n if y in do:\r\n do[y] = do[y]+1\r\n\r\n else:\r\n do[y] = 1\r\n\r\n else:\r\n if y in dz:\r\n dz[y] = dz[y] + 1\r\n\r\n else:\r\n dz[y] = 1\r\n\r\n j = j+1\r\n\r\n i = i+1\r\n\r\n lo = sorted(do.keys())\r\n lz = sorted(dz.keys())\r\n lo = lo[::-1]\r\n #print(lo)\r\n #print(lz)\r\n #print(do)\r\n #print(dz)\r\n\r\n i = 0\r\n j = 0\r\n if i == len(lo):\r\n print(0)\r\n continue\r\n\r\n if j == len(lz):\r\n print(lo[i])\r\n continue\r\n\r\n while((lo[i] != lz[j]) and (lo[i] > lz[j])):\r\n ov = lo[i]\r\n zv = lz[j]\r\n #print(ov,zv,3)\r\n do[ov] = do[ov]-1\r\n dz[zv] = dz[zv]-1\r\n\r\n if do[ov] == 0:\r\n i = i+1\r\n if i == len(lo):\r\n break\r\n\r\n if dz[zv] == 0:\r\n j = j+1\r\n if j == len(lz):\r\n break\r\n\r\n if i != len(lo):\r\n print(lo[i])\r\n\r\n else:\r\n print(lz[j])", "for _ in range(int(input())):\r\n n = int(input())\r\n d = {}\r\n i = 0\r\n while (i < n):\r\n j = 0\r\n o = []\r\n o = list(map(int, input().split()))\r\n while (j < len(o)):\r\n x = o[j]\r\n d[(i, j)] = x\r\n j = j + 1\r\n\r\n i = i + 1\r\n\r\n do = {}\r\n dz = {}\r\n\r\n i = 0\r\n while(i < n):\r\n j = 0\r\n while(j < n):\r\n e = d[(i,j)]\r\n ri = i + 1\r\n rj = j + 1\r\n y = abs(ri - rj)\r\n if e == 1:\r\n if y in do:\r\n do[y] = do[y]+1\r\n\r\n else:\r\n do[y] = 1\r\n\r\n else:\r\n if y in dz:\r\n dz[y] = dz[y] + 1\r\n\r\n else:\r\n dz[y] = 1\r\n\r\n j = j+1\r\n\r\n i = i+1\r\n\r\n lo = sorted(do.keys())\r\n lz = sorted(dz.keys())\r\n lo = lo[::-1]\r\n #print(lo)\r\n #print(lz)\r\n #print(do)\r\n #print(dz)\r\n\r\n i = 0\r\n j = 0\r\n if i == len(lo):\r\n print(0)\r\n continue\r\n\r\n if j == len(lz):\r\n print(lo[i])\r\n continue\r\n\r\n while((lo[i] != lz[j]) and (lo[i] > lz[j])):\r\n ov = lo[i]\r\n zv = lz[j]\r\n #print(ov,zv,3)\r\n do[ov] = do[ov]-1\r\n dz[zv] = dz[zv]-1\r\n\r\n if do[ov] == 0:\r\n i = i+1\r\n if i == len(lo):\r\n break\r\n\r\n if dz[zv] == 0:\r\n j = j+1\r\n if j == len(lz):\r\n break\r\n\r\n print(lo[i])", "for _ in range(int(input())):\r\n n = int(input())\r\n d = {}\r\n i = 0\r\n while (i < n):\r\n j = 0\r\n o = []\r\n o = list(map(int, input().split()))\r\n while (j < len(o)):\r\n x = o[j]\r\n d[(i, j)] = x\r\n j = j + 1\r\n\r\n i = i + 1\r\n\r\n do = {}\r\n dz = {}\r\n\r\n i = 0\r\n while(i < n):\r\n j = 0\r\n while(j < n):\r\n e = d[(i,j)]\r\n ri = i + 1\r\n rj = j + 1\r\n y = abs(ri - rj)\r\n if e == 1:\r\n if y in do:\r\n do[y] = do[y]+1\r\n\r\n else:\r\n do[y] = 1\r\n\r\n else:\r\n if y in dz:\r\n dz[y] = dz[y] + 1\r\n\r\n else:\r\n dz[y] = 1\r\n\r\n j = j+1\r\n\r\n i = i+1\r\n\r\n lo = sorted(do.keys())\r\n lz = sorted(dz.keys())\r\n lo = lo[::-1]\r\n #print(lo)\r\n #print(lz)\r\n #print(do)\r\n #print(dz)\r\n if len(lo) == 0:\r\n print(0)\r\n continue\r\n\r\n if len(lz) == 0:\r\n print(lo[0])\r\n continue\r\n\r\n i = 0\r\n j = 0\r\n while((lo[i] != lz[j]) and (lo[i] > lz[j])):\r\n ov = lo[i]\r\n zv = lz[j]\r\n #print(ov,zv,3)\r\n do[ov] = do[ov]-1\r\n dz[zv] = dz[zv]-1\r\n\r\n if do[ov] == 0:\r\n i = i+1\r\n if i == len(lo):\r\n break\r\n\r\n if dz[zv] == 0:\r\n j = j+1\r\n if j == len(lz):\r\n break\r\n\r\n print(lo[i])", "def __starting_point():\r\n\r\n for _ in range(int(input())):\r\n res = 0\r\n n = int(input())\r\n m = []\r\n for i in range(n):\r\n m.append(list(map(int, input().split())))\r\n zeroes = 0\r\n for a in m:\r\n for el in a:\r\n if el == 0:\r\n zeroes = zeroes + 1\r\n for i in range(n):\r\n if i < n-1:\r\n x = i * 2 + 2\r\n else:\r\n x = n\r\n if x > zeroes:\r\n res = i\r\n break\r\n elif x == zeroes:\r\n res = i + 1\r\n break\r\n else:\r\n zeroes = zeroes - x\r\n if res == n:\r\n res = res - 1\r\n print(n-1-res)\r\n\r\n\n__starting_point()", "# cook your dish here\n\nt = int(input())\n\nfact = []\nfor i in range(500):\n fact.append((i*(i+1))//2)\n#print(fact)\n \n \ndef search(fact, n, low, high, key):\n mid = (low + high)//2\n #print(low, high, mid, fact[mid], fact[mid+1], fact)\n if fact[mid] == key:\n return mid\n elif fact[mid] < key and mid == n-1:\n return mid\n elif fact[mid] < key and fact[mid+1] > key:\n return mid\n elif fact[mid] < key and fact[mid+1] == key:\n return mid+1\n elif fact[mid] < key:\n return search(fact, n, mid+1, high, key)\n else:\n return search(fact, n, low, mid-1, key)\n\nfor _ in range(t):\n n = int(input())\n \n M = [[int(a) for a in input().strip().split()] for i in range(n)]\n \n count = 0\n for i in range(n):\n for j in range(n):\n count = count + 1 if M[i][j] == 0 else count\n \n i = search(fact, len(fact), 0, len(fact), count//2)\n #print(count, i, fact[i], n, n-i-1)\n print(0 if count >= fact[n-1]*2 else n-i-1)\n \n", "for _ in range(int(input().strip())):\r\n\tn = int(input().strip())\r\n\ttotal_ones = 0\r\n\tfor _ in range(n):\r\n\t\ttotal_ones += sum(list(map(int, input().strip().split())))\r\n\ti = 0\r\n\ttotal_ones -= n\r\n\twhile total_ones>0:\r\n\t\ti += 1\r\n\t\ttotal_ones -= 2*(n-i)\r\n\tprint(i)", "import itertools\nfor _ in range(int(input())):\n N = int(input());A = list()\n for i in range(N):\n B = list(map(int,input().split(\" \")))\n A.append(B)\n C = list(itertools.chain.from_iterable(A))\n Z = C.count(0);temp = N-1\n for i in range(N-1):\n y = int((N-1-i)*(N - i))\n if Z >= y:\n temp = i\n break\n print(temp)", "from sys import stdin\n\nT = int(stdin.readline())\n\nfor i in range(0, T):\n n = int(stdin.readline())\n A = []\n for i in range(0, n):\n A.append([int(i) for i in stdin.readline().split(' ')])\n ones = sum([sum(i) for i in A])\n compare = n\n ans = 0\n for i in range(0, n):\n if ones <= compare:\n ans = i\n break\n compare += 2*(n-1-i)\n print(ans)", "from math import sqrt\r\nfor _ in range(int(input())):\r\n n=int(input())\r\n l=[0]*n\r\n c=0\r\n for i in range(n):\r\n l[i]=list(map(int,input().split()))\r\n c=c+l[i].count(0)\r\n q=c//2\r\n a=(-1+sqrt(1+8*q))//2\r\n print(int(n-a-1))", "# cook your dish here\ndef f(n, c):\n\t# print(\"f\", n, c)\n\tif c <= n:\n\t\treturn 0\n\tif c == n * n:\n\t\treturn n-1\n\te = n-1\n\ts = n\n\twhile c > s:\n\t\ts += 2 * e\n\t\te -= 1\n\treturn n-1-e\n# hat = n * (n+1) // 2\n# if c <= hat:\n# \treturn 0\n# return\n\nt = int(input())\nfor it in range(t):\n\tn = int(input())\n\tc = 0\n\tfor i in range(n):\n\t\ta = input().split()\n\t\tc += a.count('1')\n\tprint(f(n, c))\n", "def f(n, c):\r\n\t# print(\"f\", n, c)\r\n\tif c <= n:\r\n\t\treturn 0\r\n\tif c == n * n:\r\n\t\treturn n-1\r\n\te = n-1\r\n\ts = n\r\n\twhile c > s:\r\n\t\ts += 2 * e\r\n\t\te -= 1\r\n\treturn n-1-e\r\n# hat = n * (n+1) // 2\r\n# if c <= hat:\r\n# \treturn 0\r\n# return\r\n\r\nt = int(input())\r\nfor it in range(t):\r\n\tn = int(input())\r\n\tc = 0\r\n\tfor i in range(n):\r\n\t\ta = input().split()\r\n\t\tc += a.count('1')\r\n\tprint(f(n, c))\r\n", "for t in range(int(input())):\r\n n = int(input())\r\n matrix = []\r\n count1 = 0\r\n for i in range(n):\r\n matrix.append(list(map(int, input().split())))\r\n count1 += matrix[-1].count(1)\r\n bandwidth = 0\r\n count1 -= n\r\n for i in range(n-1,0, -1):\r\n if count1 > 0:\r\n count1 -= 2 * i\r\n bandwidth += 1\r\n print(bandwidth)\r\n"]
{"inputs": [["6", "2", "0 0", "0 0", "2", "1 0", "0 1", "2", "1 0", "1 0", "2", "1 0", "1 1", "3", "1 0 0", "0 1 1", "1 1 0", "4", "1 1 1 1", "1 1 1 1", "1 1 1 1", "1 1 1 1", "", ""]], "outputs": [["0", "0", "0", "1", "1", "3"]]}
INTERVIEW
PYTHON3
CODECHEF
15,384
4f029208ac22abad4a46a114eb445eba
UNKNOWN
The Little Elephant loves lucky strings. Everybody knows that the lucky string is a string of digits that contains only the lucky digits 4 and 7. For example, strings "47", "744", "4" are lucky while "5", "17", "467" are not. The Little Elephant has the strings A and B of digits. These strings are of equal lengths, that is |A| = |B|. He wants to get some lucky string from them. For this he performs the following operations. At first he arbitrary reorders digits of A. Then he arbitrary reorders digits of B. After that he creates the string C such that its i-th digit is the maximum between the i-th digit of A and the i-th digit of B. In other words, C[i] = max{A[i], B[i]} for i from 1 to |A|. After that he removes from C all non-lucky digits saving the order of the remaining (lucky) digits. So C now becomes a lucky string. For example, if after reordering A = "754" and B = "873", then C is at first "874" and then it becomes "74". The Little Elephant wants the resulting string to be as lucky as possible. The formal definition of this is that the resulting string should be the lexicographically greatest possible string among all the strings that can be obtained from the given strings A and B by the described process. Notes - |A| denotes the length of the string A. - A[i] denotes the i-th digit of the string A. Here we numerate the digits starting from 1. So 1 ≤ i ≤ |A|. - The string A is called lexicographically greater than the string B if either there exists some index i such that A[i] > B[i] and for each j < i we have A[j] = B[j], or B is a proper prefix of A, that is, |A| > |B| and first |B| digits of A coincide with the corresponding digits of B. -----Input----- The first line of the input contains a single integer T, the number of test cases. T test cases follow. Each test case consists of two lines. The first line contains the string A. The second line contains the string B. -----Output----- For each test case output a single line containing the answer for the corresponding test case. Note, that the answer can be an empty string. In this case you should print an empty line for the corresponding test case. -----Constraints----- 1 ≤ T ≤ 10000 1 ≤ |A| ≤ 20000 |A| = |B| Each character of A and B is a digit. Sum of |A| across all the tests in the input does not exceed 200000. -----Example----- Input: 4 4 7 435 479 7 8 1675475 9756417 Output: 7 74 777744 -----Explanation----- Case 1. In this case the only possible string C we can get is "7" and it is the lucky string. Case 2. If we reorder A and B as A = "543" and B = "749" the string C will be at first "749" and then becomes "74". It can be shown that this is the lexicographically greatest string for the given A and B. Case 3. In this case the only possible string C we can get is "8" and it becomes and empty string after removing of non-lucky digits. Case 4. If we reorder A and B as A = "7765541" and B = "5697714" the string C will be at first "7797744" and then becomes "777744". Note that we can construct any lexicographically greater string for the given A and B since we have only four "sevens" and two "fours" among digits of both strings A and B as well the constructed string "777744".
["\nt = int(input())\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tagts += 1\n\t\t\telse:\n\t\t\t\taseven += 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tafts += 1\n\t\t\telse:\n\t\t\t\tafour += 1\n\t\telse:\n\t\t\taltf += 1\n\t\n\tfor j in b:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tbgts += 1\n\t\t\telse:\n\t\t\t\tbseven += 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tbfts += 1\n\t\t\telse:\n\t\t\t\tbfour += 1\n\t\telse:\n\t\t\tbltf += 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven -= bfts;\n\t\tnseven += bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts -= aseven;\n\t\tnseven += aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven -= afts;\n\t\tnseven += afts;\n\t\tafts = 0;\n\telse:\n\t\tafts -= bseven;\n\t\tnseven += bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven -= bltf;\n\t\tnseven += bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf -= aseven;\n\t\tnseven += aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven -= altf;\n\t\tnseven += altf;\n\t\taltf = 0;\n\telse:\n\t\taltf -= bseven;\n\t\tnseven += bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven -= bfour;\n\t\tnseven += bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour -= aseven;\n\t\tnseven += aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven -= afour;\n\t\tnseven += afour;\n\t\tafour = 0;\n\telse:\n\t\tafour -= bseven;\n\t\tnseven += bseven;\n\t\tbseven = 0;\n\t\n\tnseven += min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour -= bltf;\n\t\tnfour += bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf -= afour;\n\t\tnfour += afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour -= altf;\n\t\tnfour += altf;\n\t\taltf = 0\n\telse:\n\t\taltf -= bfour;\n\t\tnfour += bfour;\n\t\tbfour = 0;\n\t\n\tnfour += min(afour,bfour)\n\t\n\tprint('7'*nseven + '4'*nfour)", "\nt = int(input())\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tagts = agts + 1\n\t\t\telse:\n\t\t\t\taseven = aseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tafts = afts + 1\n\t\t\telse:\n\t\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tbgts = bgts + 1\n\t\t\telse:\n\t\t\t\tbseven = bseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tbfts = bfts + 1\n\t\t\telse:\n\t\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('7'*nseven + '4'*nfour)", "\nt = int(input())\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tagts = agts + 1\n\t\t\telse:\n\t\t\t\taseven = aseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tafts = afts + 1\n\t\t\telse:\n\t\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tbgts = bgts + 1\n\t\t\telse:\n\t\t\t\tbseven = bseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tbfts = bfts + 1\n\t\t\telse:\n\t\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('%s%s' % ('7'*nseven, '4'*nfour))", "\nt = int(input())\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tagts = agts + 1\n\t\t\telse:\n\t\t\t\taseven = aseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tafts = afts + 1\n\t\t\telse:\n\t\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tbgts = bgts + 1\n\t\t\telse:\n\t\t\t\tbseven = bseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tbfts = bfts + 1\n\t\t\telse:\n\t\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('%s%s' % ('7'*nseven, '4'*nfour))", "\nt = int(input())\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tagts = agts + 1\n\t\t\telse:\n\t\t\t\taseven = aseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tafts = afts + 1\n\t\t\telse:\n\t\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tbgts = bgts + 1\n\t\t\telse:\n\t\t\t\tbseven = bseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tbfts = bfts + 1\n\t\t\telse:\n\t\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('7'*nseven + '4'*nfour)", "\nt = int(input())\n\na = []\nb = []\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tagts = agts + 1\n\t\t\telse:\n\t\t\t\taseven = aseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tafts = afts + 1\n\t\t\telse:\n\t\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j >= '7':\n\t\t\tif j > '7':\n\t\t\t\tbgts = bgts + 1\n\t\t\telse:\n\t\t\t\tbseven = bseven + 1\n\t\telif j >= '4':\n\t\t\tif j > '4':\n\t\t\t\tbfts = bfts + 1\n\t\t\telse:\n\t\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('7'*nseven + '4'*nfour)", "\nt = int(input())\n\na = []\nb = []\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j > '7':\n\t\t\tagts = agts + 1\n\t\telif j == '7':\n\t\t\taseven = aseven + 1\n\t\telif j > '4':\n\t\t\tafts = afts + 1\n\t\telif j == '4':\n\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j > '7':\n\t\t\tbgts = bgts + 1\n\t\telif j == '7':\n\t\t\tbseven = bseven + 1\n\t\telif j > '4':\n\t\t\tbfts = bfts + 1\n\t\telif j == '4':\n\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('7'*nseven + '4'*nfour)", "\nt = int(input())\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j > '7':\n\t\t\tagts = agts + 1\n\t\telif j == '7':\n\t\t\taseven = aseven + 1\n\t\telif j > '4':\n\t\t\tafts = afts + 1\n\t\telif j == '4':\n\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j > '7':\n\t\t\tbgts = bgts + 1\n\t\telif j == '7':\n\t\t\tbseven = bseven + 1\n\t\telif j > '4':\n\t\t\tbfts = bfts + 1\n\t\telif j == '4':\n\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tprint('7'*nseven + '4'*nfour)", "\nt = int(input())\n\n# adict = [0]*10;\n# bdict = [0]*10;\n\nfor i in range(0,t):\n\ta = input()\n\tb = input()\n\t\n\t# lena = len(a);\n\t\n\tagts=bgts=afour=bfour=aseven=bseven=altf=bltf=afts=bfts=0;\n\t\n\tfor j in a:\n\t\tif j > '7':\n\t\t\tagts = agts + 1\n\t\telif j == '7':\n\t\t\taseven = aseven + 1\n\t\telif j > '4':\n\t\t\tafts = afts + 1\n\t\telif j == '4':\n\t\t\tafour = afour + 1\n\t\telse:\n\t\t\taltf = altf + 1\n\t\n\tfor j in b:\n\t\tif j > '7':\n\t\t\tbgts = bgts + 1\n\t\telif j == '7':\n\t\t\tbseven = bseven + 1\n\t\telif j > '4':\n\t\t\tbfts = bfts + 1\n\t\telif j == '4':\n\t\t\tbfour = bfour + 1\n\t\telse:\n\t\t\tbltf = bltf + 1\n\t\n\t# for j in range(0,10):\n\t\t# adict[j] = 0;\n\t\t# bdict[j] = 0;\n\t\n\t# for j in range(0,lena):\n\t\t# adict[int(a[j])] = adict[int(a[j])] + 1\n\t\t# bdict[int(b[j])] = bdict[int(b[j])] + 1\n\t\n\t# agts = adict[8] + adict[9]\n\t# bgts = bdict[8] + bdict[9]\n\t\n\t# aseven = adict[7]\n\t# bseven = bdict[7]\n\t\n\t# afts = adict[5] + adict[6]\n\t# bfts = bdict[5] + bdict[6]\n\t\n\t# afour = adict[4]\n\t# bfour = bdict[4]\n\t\n\t# altf = adict[0] + adict[1] + adict[2] + adict[3]\n\t# bltf = bdict[0] + bdict[1] + bdict[2] + bdict[3]\n\t\n\tnseven = 0\n\tnfour = 0\n\t\n\tif aseven > bfts:\n\t\taseven = aseven - bfts;\n\t\tnseven = nseven + bfts;\n\t\tbfts = 0;\n\telse:\n\t\tbfts = bfts - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afts:\n\t\tbseven = bseven - afts;\n\t\tnseven = nseven + afts;\n\t\tafts = 0;\n\telse:\n\t\tafts = afts - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bltf:\n\t\taseven = aseven - bltf;\n\t\tnseven = nseven + bltf;\n\t\tbltf = 0;\n\telse:\n\t\tbltf = bltf - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > altf:\n\t\tbseven = bseven - altf;\n\t\tnseven = nseven + altf;\n\t\taltf = 0;\n\telse:\n\t\taltf = altf - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tif aseven > bfour:\n\t\taseven = aseven - bfour;\n\t\tnseven = nseven + bfour;\n\t\tbfour = 0;\n\telse:\n\t\tbfour = bfour - aseven;\n\t\tnseven = nseven + aseven;\n\t\taseven = 0;\n\t\n\tif bseven > afour:\n\t\tbseven = bseven - afour;\n\t\tnseven = nseven + afour;\n\t\tafour = 0;\n\telse:\n\t\tafour = afour - bseven;\n\t\tnseven = nseven + bseven;\n\t\tbseven = 0;\n\t\n\tnseven = nseven + min(aseven,bseven)\n\t\n\tif afour > bltf:\n\t\tafour = afour - bltf;\n\t\tnfour = nfour + bltf;\n\t\tbltf = 0\n\telse:\n\t\tbltf = bltf - afour;\n\t\tnfour = nfour + afour;\n\t\tafour = 0;\n\t\n\tif bfour > altf:\n\t\tbfour = bfour - altf;\n\t\tnfour = nfour + altf;\n\t\taltf = 0\n\telse:\n\t\taltf = altf - bfour;\n\t\tnfour = nfour + bfour;\n\t\tbfour = 0;\n\t\n\tnfour = nfour + min(afour,bfour)\n\t\n\tif nseven + nfour > 0:\n\t\tprint('7'*nseven + '4'*nfour)\n\telse:\n\t\tprint()", "def lucky10(X, Y):\n\tx, y = {4:0, 7:0}, {4:0, 7:0}\n\tX =list(map(int, X))\n\tY =list(map(int,Y))\n\tx47, y47 = {4:0, 7:0}, {4:0, 7:0}\n\tfor i in X:\n\t\tif i == 4:\n\t\t\tx47[4] += 1\n\t\telif i == 7:\n\t\t\tx47[7] += 1\n\t\telif i < 4:\n\t\t\tx[4] += 1\n\t\telif i<7 and i>4:\n\t\t\tx[7] += 1\n\tfor i in Y:\n\t\tif i == 4:\n\t\t\ty47[4] += 1\n\t\telif i == 7:\n\t\t\ty47[7] += 1\n\t\telif i < 4:\n\t\t\ty[4] += 1\n\t\telif i<7 and i>4:\n\t\t\ty[7] += 1\n\t#print x47, y47, x, y\n\tn=\"\"\n\tif x47[7] <= y[7]:\n\t\tn += '7'*x47[7]\n\t\ty[7] -= x47[7]\n\t\tx47[7] = 0\n\telif x47[7] <= (y[7]+y[4]):\n\t\tn += '7'*x47[7]\n\t\ty[4] -= (x47[7]-y[7])\n\t\ty[7] = 0\n\t\tx47[7] = 0\n\telif x47[7] <= (y[7]+y[4]+y47[4]):\n\t\ty47[4] -= (x47[7]-(y[7]+y[4]))\n\t\tn += '7'*x47[7]\n\t\tx47[7] = 0\n\t\ty[7]=y[4]=0\n\telif x47[7] <= (y[7]+y[4]+y47[4] + y47[7]):\n\t\t'''print \"Debug value:\", (x47[7]-(y[7]+y[4]+y47[4]))\n\t\tprint y[7], \" \", y[4], \" \", y47[4]\n\t\tprint y47[7]'''\n\t\ty47[7] -= (x47[7]-(y[7]+y[4]+y47[4]))\n\t\tn += '7'*x47[7]\n\t\tx47[7] = 0\n\t\ty[7]=y[4]=y47[4]=0\n\t\t#print \"7 left in y\", y47[7]\n\telse:\n\t\tn += '7'*(y[7]+y[4]+y47[4]+y47[7])\t\t\n\t\tx47[7] = x47[7] - (y[7]+y[4]+y47[4]+y47[7])\n\t\ty[7]=y[4]=y47[4]=y47[7]=0\n\n\tif y47[7] <= x[7]:\n\t\tn += '7'*y47[7]\n\t\tx[7] -= y47[7]\n\t\ty47[7] = 0\n\telif y47[7] <= (x[7]+x[4]):\n\t\tn += '7'*y47[7]\n\t\tx[4] -= (y47[7]-x[7])\n\t\tx[7] =0\n\t\ty47[7] = 0\n\telif y47[7] <= (x[7]+x[4]+x47[4]):\t\t\n\t\tx47[4] -= (y47[7]-(x[7]+x[4]))\n\t\tn += '7'*y47[7]\n\t\ty47[7] = 0\n\t\tx[7]=x[4]=0\n\telif y47[7] <= (x[7]+x[4]+x47[4] + x47[7]):\t\t\n\t\tx47[7] -= (y47[7]-(x[7]+x[4]+x47[4]))\n\t\tn += '7'*y47[7]\n\t\ty47[7] = 0\n\t\tx[7]=x[4]=x47[4]=0\n\telse:\n\t\tn += '7'*(x[7]+x[4]+x47[4]+x47[7])\t\t\n\t\ty47[7] -= (x[7]+x[4]+x47[4]+x47[7])\n\t\tx[7]=x[4]=x47[4]=x47[7]=0\n\n\t\t####################4###############\n\tif x47[4] <= y[4]:\n\t\tn += '4'*x47[4]\n\t\ty[4] -= x47[4]\n\t\tx47[4] =0\n\telif x47[4] <= (y[4]+y47[4]):\n\t\tn += '4'*x47[4]\n\t\ty47[4] -= (x47[4]-y[4])\n\t\ty[4] = 0\n\t\tx47[4] = 0\n\telse:\n\t\tn += '4'*(y47[4]+y[4])\t\t\n\t\tx47[4] -= (y47[4]+y[4])\n\t\ty47[4]=y[4]=0\n\n\tif y47[4] <= x[4]:\n\t\tn += '4'*y47[4]\n\t\tx[4] -= y47[4]\n\t\ty47[4] = 0\n\telif y47[4] <= (x[4]+x47[4]):\n\t\tn += '4'*y47[4]\n\t\tx47[4] -= (y47[4]-x[4])\n\t\tx[4] = 0\n\t\ty47[4] = 0\n\telse:\n\t\tn += '4'*(x47[4]+x[4])\t\n\t\ty47[4] -= (x47[4]+x[4])\n\t\tx47[4]=x[4]=0\n\treturn n\ndef io():\n\tt = int(input())\n\tst = \"\"\n\twhile t>0:\n\t\ta = input()\n\t\tb = input()\n\t\tst += lucky10(a, b) + \"\\n\"\n\t\tt-=1\n\tprint(st)\nio()\n", "def inp():\n l = [0]*10\n for c in input():\n l[ord(c)-48] += 1\n return (l[0]+l[1]+l[2]+l[3], l[4], l[5]+l[6], l[7])\n\ndef go():\n a0,a4,a6,a7 = inp()\n b0,b4,b6,b7 = inp()\n r4 = r7 = 0\n m = min(b6, a7); r7 += m; b6 -= m; a7 -= m\n m = min(a6, b7); r7 += m; a6 -= m; b7 -= m\n m = min(b0, a7); r7 += m; b0 -= m; a7 -= m\n m = min(a0, b7); r7 += m; a0 -= m; b7 -= m\n m = min(b4, a7); r7 += m; b4 -= m; a7 -= m\n m = min(a4, b7); r7 += m; a4 -= m; b7 -= m\n m = min(a7, b7); r7 += m; a7 -= m; b7 -= m\n m = min(b0, a4); r4 += m; b0 -= m; a4 -= m\n m = min(a0, b4); r4 += m; a0 -= m; b4 -= m\n m = min(a4, b4); r4 += m; a4 -= m; b4 -= m\n print(\"7\"*r7 + \"4\"*r4)\n\nfor t in range(eval(input())): go()\n", "#!/usr/bin/python\n#http://www.codechef.com/OCT12/problems/LUCKY10\n\nimport sys\n\ndef solve():\n\ta=sys.stdin.readline()\n\tb=sys.stdin.readline()\n\n\tdef arrange(a,A):\n\t\tfor i in a:\n\t\t\tif '0'<=i<='3':A[0]+=1\n\t\t\telif i=='4':\n\t\t\t\tA[1]+=1\n\t\t\telif i=='7':\n\t\t\t\tA[2]+=1\n\t\t\telif '5'<=i<='6':A[3]+=1\n\tA,B=[0]*5,[0]*5\n\tarrange(a,A)\n\tarrange(b,B)\n\n\tret=''\n\tret=(min(A[2],B[3])+min(B[2],A[3]))*'7'\n\n\tA[2],B[2]=max(0,A[2]-B[3]),max(0,B[2]-A[3])\n\tt=min(A[1]+B[1],A[1]+A[0],B[1]+B[0])\n\tA[0]=A[1]+A[0]-t\n\tB[0]=B[1]+B[0]-t\n\tret=ret+(min(A[2],B[0])+min(B[2],A[0]))*'7'\n\tA[2],B[2]=max(0,A[2]-B[0]),max(0,B[2]-A[0])\n\tret=ret+(min(t,A[2])+min(t,B[2]))*'7'\n\tk=min(max(0,t-A[2]),max(0,t-B[2]))\n\n\tA[2],B[2]=max(0,A[2]-t),max(0,B[2]-t)\n\tret=ret+min(A[2],B[2])*'7'+k*'4'\n\n\tprint(ret)\n\treturn \n\n\nt=int(input())\nfor i in range(t):\n\tsolve()\n\n", "#!/usr/bin/python\n#http://www.codechef.com/OCT12/problems/LUCKY10\n\nimport sys\n\ndef solve():\n\ta=sys.stdin.readline()\n\tb=sys.stdin.readline()\n\n\tdef arrange(a,A):\n\t\tfor i in a:\n\t\t\tif '0'<=i<='3':A[0]+=1\n\t\t\telif i=='4':\n\t\t\t\tA[1]+=1\n\t\t\telif i=='7':\n\t\t\t\tA[2]+=1\n\t\t\telif '5'<=i<='6':A[3]+=1\n\tA,B=[0]*5,[0]*5\n\tarrange(a,A)\n\tarrange(b,B)\n\n\tret=''\n\tret=(min(A[2],B[3])+min(B[2],A[3]))*'7'\n\n\tA[2],B[2]=max(0,A[2]-B[3]),max(0,B[2]-A[3])\n\tt=min(A[1]+B[1],A[1]+A[0],B[1]+B[0])\n\tA[0]=A[1]+A[0]-t\n\tB[0]=B[1]+B[0]-t\n\tret=ret+(min(A[2],B[0])+min(B[2],A[0]))*'7'\n\tA[2],B[2]=max(0,A[2]-B[0]),max(0,B[2]-A[0])\n\tret=ret+(min(t,A[2])+min(t,B[2]))*'7'\n\tk=min(max(0,t-A[2]),max(0,t-B[2]))\n\n\tA[2],B[2]=max(0,A[2]-t),max(0,B[2]-t)\n\tret=ret+min(A[2],B[2])*'7'+k*'4'\n\n\tprint(ret)\n\treturn \n\n\nt=int(input())\nfor i in range(t):\n\tsolve()\n\n", "tests=int(input())\nfor t in range(tests):\n\ta=input()\n\tb=input()\n\taSev=a.count('7')\n\taLesS=a.count('5')+a.count('6')\n\taFor=a.count('4')\n\taLess=a.count('3')+a.count('2')+a.count('1')+a.count('0')\n\tbSev=b.count('7')\n\tbLesS=b.count('5')+b.count('6')\n\tbFor=b.count('4')\n\tbLess=b.count('3')+b.count('2')+b.count('1')+b.count('0')\n\tfin7=0\n\tfin4=0\n\tif aSev<=bLesS:\n\t\tfin7+=aSev\n\t\tbLesS-=aSev\n\t\taSev=0\n\telse:\n\t\tfin7+=bLesS\n\t\taSev-=bLesS\n\t\tbLesS=0\n\t\tif aSev<=bLess:\n\t\t\tfin7+=aSev\n\t\t\tbLess-=aSev\n\t\t\taSev=0\n\t\telse:\n\t\t\tfin7+=bLess\n\t\t\taSev-=bLess\n\t\t\tbLess=0\n\t\t\tif aSev<=bFor:\n\t\t\t\tfin7+=aSev\n\t\t\t\tbFor-=aSev\n\t\t\t\taSev=0\n\t\t\telse:\n\t\t\t\tfin7+=bFor\n\t\t\t\taSev-=bFor\n\t\t\t\tbFor=0\n\tif bSev<=aLesS:\n\t\tfin7+=bSev\n\t\taLesS-=bSev\n\t\tbSev=0\n\telse:\n\t\tfin7+=aLesS\n\t\tbSev-=aLesS\n\t\taLesS=0\n\t\tif bSev<=aLess:\n\t\t\tfin7+=bSev\n\t\t\taLess-=bSev\n\t\t\tbSev=0\n\t\telse:\n\t\t\tfin7+=aLess\n\t\t\tbSev-=aLess\n\t\t\taLess=0\n\t\t\tif bSev<=aFor:\n\t\t\t\tfin7+=bSev\n\t\t\t\taFor-=bSev\n\t\t\t\taSev=0\n\t\t\telse:\n\t\t\t\tfin7+=aFor\n\t\t\t\tbSev-=aFor\n\t\t\t\taFor=0\n\tfin7+=min(aSev,bSev)\n\tif aFor<=bLess:\n\t\tfin4+=aFor\n\t\tbLess-=aFor\n\t\taFor=0\n\telse:\n\t\tfin4+=bLess\n\t\taFor-=bLess\n\t\tbLess=0\n\tif bFor<=aLess:\n\t\tfin4+=bFor\n\t\taLess-=bFor\n\t\tbFor=0\n\telse:\n\t\tfin4+=aLess\n\t\tbFor-=aLess\n\t\taLess=0\n\tfin4+=min(aFor,bFor)\n\tprint('7'*fin7+'4'*fin4)\n\t\t\n", "t=int(input())\nfor i in range(t):\n a=input()\n b=input()\n sa,sb,fa,fb,ac1,ac2,bc1,bc2=0,0,0,0,0,0,0,0\n for x in a:\n if x<'4': ac1+=1\n elif x=='4': fa+=1\n elif x<'7': ac2+=1\n elif x=='7': sa+=1\n for x in b:\n if x<'4': bc1+=1\n elif x=='4': fb+=1\n elif x<'7': bc2+=1\n elif x=='7': sb+=1\n\n final7,final4=0,0\n #print sa,fa,ac1,ac2\n #print sb,fb,bc1,bc2\n\n # Check for 7s in A\n if sa:\n x=sa if bc2>sa else bc2\n final7+=x\n sa-=x\n bc2-=x\n x=sa if bc1>sa else bc1\n final7+=x\n sa-=x\n bc1-=x\n if sa:\n x=sa if fb>sa else fb\n final7+=x\n sa-=x\n fb-=x\n if sb:\n x=sb if ac2>sb else ac2\n final7+=x\n sb-=x\n ac2-=x\n x=sb if ac1>sb else ac1\n final7+=x\n sb-=x\n ac1-=x\n if sb:\n x=sb if fa>sb else fa\n final7+=x\n sb-=x\n fa-=x\n if fa:\n x=fa if bc1>fa else bc1\n final4+=x\n fa-=x\n bc1-=x\n if fb:\n x=fb if ac1>fb else ac1\n final4+=x\n fb-=x\n ac1-=x\n final4+=min(fa,fb)\n final7+=min(sa,sb)\n print('7'*final7+'4'*final4)\n \n", "import sys\n\ndef analyze(string):\n\tsevens = 0\n\tlow = 0\n\tmid = 0\n\thigh = 0\n\tfours = 0\n\tfor c in string:\n\t\tif c > '7':\n\t\t\thigh = high + 1\n\t\telif c == '7':\n\t\t\tsevens = sevens +1\n\t\telif c == '4' :\n\t\t\tfours = fours + 1\n\t\telif c < '4':\n\t\t\tlow = low +1 \n\t\telse:\n\t\t\tmid = mid + 1\n\n\treturn (sevens,fours,high,mid,low)\t\n\ndef solve(A,B):\n\tl = len(A)\n\ta = analyze(A)\n\tb = analyze(B)\n\t(a_s, a_f, a_h, a_m, a_l) = a\n\t(b_s, b_f, b_h, b_m, b_l) = b\n\n\tc_s = 0\n\tc_f = 0\n\n\t# Start by removing high values\n\tif a_h > b_h :\n\t\t# Remove high values\n\t\tl = l - (a_h)\n\t\t# Hide mid values under high values\n\t\tb_m = max(b_m - (a_h - b_h),0)\n\telif b_h > a_h :\n\t\t# Remove high values\n\t\tl = l - (b_h)\n\t\t# Hide mid values under high values\n\t\ta_m = max(a_m - (b_h - a_h),0)\n\telse:\t\n\t\tl = l - a_h\n\n\tc_s = a_s + b_s\t\n\n\tl = l - c_s\n\n\tif l <= 0:\n\t\tc_s = c_s + l\n\t\treturn (c_s,c_f)\n\t\n\t# Hide mid values under sevens\n\ta_m = max(a_m - b_s,0)\n\tb_m = max(b_m - a_s,0)\n\t\n# Removing mid values\t\n\n\tl = l - max(a_m,b_m)\n\n\t# Check how many fours can be arranged..\n\n\tc_f = a_f + b_f\n\tl = l - c_f\n\n\tif l <= 0:\n\t\tc_f = c_f + l\n\n\treturn (c_s,c_f)\n\t\t\ndef show(s,f):\n\tprint(s * '7' + f * '4')\n\t\n\nf = sys.stdin\n\nfor test in range(int(f.readline())):\n\tA = f.readline().rstrip()\n\tB = f.readline().rstrip()\n\t(c_s,c_f) = solve(A,B)\n\tshow(c_s,c_f)\n", "t=int(input())\nfor i in range(t):\n a=input()\n b=input()\n sa,sb,fa,fb,ac1,ac2,bc1,bc2=0,0,0,0,0,0,0,0\n for x in a:\n if x<'4': ac1+=1\n elif x=='4': fa+=1\n elif x<'7': ac2+=1\n elif x=='7': sa+=1\n for x in b:\n if x<'4': bc1+=1\n elif x=='4': fb+=1\n elif x<'7': bc2+=1\n elif x=='7': sb+=1\n\n final7,final4=0,0\n #print sa,fa,ac1,ac2\n #print sb,fb,bc1,bc2\n\n # Check for 7s in A\n if sa:\n x=sa if bc2>sa else bc2\n final7+=x\n sa-=x\n bc2-=x\n x=sa if bc1>sa else bc1\n final7+=x\n sa-=x\n bc1-=x\n if sa:\n x=sa if fb>sa else fb\n final7+=x\n sa-=x\n fb-=x\n if sb:\n x=sb if ac2>sb else ac2\n final7+=x\n sb-=x\n ac2-=x\n x=sb if ac1>sb else ac1\n final7+=x\n sb-=x\n ac1-=x\n if sb:\n x=sb if fa>sb else fa\n final7+=x\n sb-=x\n fa-=x\n if fa:\n x=fa if bc1>fa else bc1\n final4+=x\n fa-=x\n bc1-=x\n if fb:\n x=fb if ac1>fb else ac1\n final4+=x\n fb-=x\n ac1-=x\n final4+=min(fa,fb)\n final7+=min(sa,sb)\n print('7'*final7+'4'*final4)\n \n"]
{"inputs": [["4", "4", "7", "435", "479", "7", "8", "1675475", "9756417", "", ""]], "outputs": [["7", "74", "", "777744"]]}
INTERVIEW
PYTHON3
CODECHEF
31,415
b097da0499448a772a0ab2f9e62b0be8
UNKNOWN
There's a tree and every one of its nodes has a cost associated with it. Some of these nodes are labelled special nodes. You are supposed to answer a few queries on this tree. In each query, a source and destination node (SNODE$SNODE$ and DNODE$DNODE$) is given along with a value W$W$. For a walk between SNODE$SNODE$ and DNODE$DNODE$ to be valid you have to choose a special node and call it the pivot P$P$. Now the path will be SNODE$SNODE$ ->P$ P$ -> DNODE$DNODE$. For any valid path, there is a path value (PV$PV$) attached to it. It is defined as follows: Select a subset of nodes(can be empty) in the path from SNODE$SNODE$ to P$P$ (both inclusive) such that sum of their costs (CTOT1$CTOT_{1}$) doesn't exceed W$W$. Select a subset of nodes(can be empty) in the path from P$P$ to DNODE$DNODE$ (both inclusive) such that sum of their costs (CTOT2$CTOT_{2}$) doesn't exceed W$W$. Now define PV=CTOT1+CTOT2$PV = CTOT_{1} + CTOT_{2}$ such that the absolute difference x=|CTOT1−CTOT2|$x = |CTOT_{1} - CTOT_{2}|$ is as low as possible. If there are multiple pairs of subsets that give the same minimum absolute difference, the pair of subsets which maximize PV$PV$ should be chosen. For each query, output the path value PV$PV$ minimizing x$x$ as defined above. Note that the sum of costs of an empty subset is zero. -----Input----- - First line contains three integers N$N$ - number of vertices in the tree, NSP$NSP$ - number of special nodes in the tree and Q$Q$ - number of queries to answer. - Second line contains N−1$N-1$ integers. If the i$i$th integer is Vi$V_i$ then there is an undirected edge between i+1$i + 1$ and Vi$V_i$ (i$i$ starts from 1$1$ and goes till N−1$N-1$). - Third line contains N$N$ integers, the i$i$th integer represents cost of the i$i$th vertex. - Fourth line contains NSP$NSP$ integers - these represent which nodes are the special nodes. - Following Q$Q$ lines contains three integers each - SNODE$SNODE$, DNODE$DNODE$ and W$W$ for each query. -----Output----- For each query output a single line containing a single integer - the path value PV$PV$ between SNODE$SNODE$ and DNODE$DNODE$. -----Constraints:----- - 1≤$1 \leq $ Number of nodes ≤1000$ \leq 1000 $ - 0≤W≤1000$ 0 \leq W \leq 1000 $ - 1≤$ 1 \leq $ Number of special nodes ≤10$ \leq 10 $ - 0≤$ 0 \leq $ Cost of each node ≤1000$ \leq 1000 $ - 1≤$ 1 \leq $ Number of queries ≤1000$ \leq 1000 $ -----Sample Input----- 7 1 5 1 1 2 2 3 3 3 5 4 2 7 9 1 1 2 3 100 1 1 100 2 1 100 4 5 100 4 7 100 -----Sample Output:----- 6 6 6 20 16 -----Explanation:----- Consider query 4$4$. The only path is 4−>2−>1−>2−>5$4->2->1->2->5$. The two sets defined for this path are {3,2,5${3,2,5}$} and {3,5,7${3,5,7}$}. Pick subsets {3,2,5${3,2,5}$} and {3,7${3,7}$} from each set which minimizes PV$PV$. Note that node 2$2$ can repeat as it is in different paths (both to and from the pivot).
["# cook your dish here\n# cook your dish here\nimport numpy as np\nn, s, q = [int(j) for j in input().split()]\nedges = [int(j)-1 for j in input().split()]\ncosts = [int(j) for j in input().split()]\nspecial = [int(j)-1 for j in input().split()]\nqueries = [[0] * 3 for _ in range(q)]\nfor i in range(q):\n queries[i] = [int(j)-1 for j in input().split()]\n\nedge_set = [[] for _ in range(n)]\nfor i in range(n-1):\n edge_set[i+1].append(edges[i])\n edge_set[edges[i]].append(i+1)\n\nstored = np.zeros((s,n,1001),dtype=bool)\nvisited = [[] for _ in range(s)]\nfor i in range(s):\n s_vertex = special[i]\n s_cost = costs[s_vertex]\n s_visited = visited[i]\n s_visited.append(s_vertex)\n s_stored = stored[i]\n s_stored[s_vertex][0] = True\n s_stored[s_vertex][s_cost] = True\n for edge in edge_set[s_vertex]:\n s_visited.append(edge)\n s_stored[edge] = np.array(s_stored[s_vertex])\n for j in range(1,n):\n vertex = s_visited[j]\n cost = costs[vertex]\n s_stored[vertex][cost:1001] = np.logical_or(s_stored[vertex][0:1001-cost],s_stored[vertex][cost:1001])\n for edge in edge_set[vertex]:\n if edge not in s_visited:\n s_visited.append(edge)\n s_stored[edge] = np.array(s_stored[vertex])\n\nfor i in range(q):\n first, second, max_cost = queries[i]\n bool_array = np.zeros(max_cost+2,dtype=bool)\n for j in range(s):\n bool_array = np.logical_or(bool_array,np.logical_and(stored[j][first][:max_cost+2],stored[j][second][:max_cost+2]))\n for j in range(max_cost+1,-1,-1):\n if bool_array[j]:\n print(2 * j)\n break", "# cook your dish here\nimport numpy as np\nn, s, q = [int(j) for j in input().split()]\nedges = [int(j)-1 for j in input().split()]\ncosts = [int(j) for j in input().split()]\nspecial = [int(j)-1 for j in input().split()]\nqueries = [[0] * 3 for _ in range(q)]\nfor i in range(q):\n queries[i] = [int(j)-1 for j in input().split()]\n\nedge_set = [[] for _ in range(n)]\nfor i in range(n-1):\n edge_set[i+1].append(edges[i])\n edge_set[edges[i]].append(i+1)\n\nstored = np.zeros((s,n,1001),dtype=bool)\nvisited = [[] for _ in range(s)]\nfor i in range(s):\n s_vertex = special[i]\n s_cost = costs[s_vertex]\n s_visited = visited[i]\n s_visited.append(s_vertex)\n s_stored = stored[i]\n s_stored[s_vertex][0] = True\n s_stored[s_vertex][s_cost] = True\n for edge in edge_set[s_vertex]:\n s_visited.append(edge)\n s_stored[edge] = np.array(s_stored[s_vertex])\n for j in range(1,n):\n vertex = s_visited[j]\n cost = costs[vertex]\n s_stored[vertex][cost:1001] = np.logical_or(s_stored[vertex][0:1001-cost],s_stored[vertex][cost:1001])\n for edge in edge_set[vertex]:\n if edge not in s_visited:\n s_visited.append(edge)\n s_stored[edge] = np.array(s_stored[vertex])\n\nfor i in range(q):\n first, second, max_cost = queries[i]\n bool_array = np.zeros(max_cost+2,dtype=bool)\n for j in range(s):\n bool_array = np.logical_or(bool_array,np.logical_and(stored[j][first][:max_cost+2],stored[j][second][:max_cost+2]))\n for j in range(max_cost+1,-1,-1):\n if bool_array[j]:\n print(2 * j)\n break"]
{"inputs": [["7 1 5", "1 1 2 2 3 3", "3 5 4 2 7 9 1", "1", "2 3 100", "1 1 100", "2 1 100", "4 5 100", "4 7 100"]], "outputs": [["6", "6", "6", "20", "16"]]}
INTERVIEW
PYTHON3
CODECHEF
3,243
1d265b01812017e02804e5663d929256
UNKNOWN
Indraneel's student has given him data from two sets of experiments that the student has performed. Indraneel wants to establish a correlation between the two sets of data. Each data set is a sequence of $N$ numbers. The two data sets do not match number for number, but Indraneel believes that this is because data has been shifted due to inexact tuning of the equipment. For example, consider the following two sequences: $ $ 3 8 4 23 9 11 28 2 3 22 26 8 16 12 $ $ Indraneel observes that if we consider the subsequences $3,4,23,9$ and $2,3,22,8$ and examine their successive differences we get $1,19,-14$. He considers these two subsequences to be "identical". He would like to find the longest such pair of subsequences so that the successive differences are identical. Your task is to help him do this. -----Input:----- The first line of the input will contain a single integer $N$ indicating the number of data points in each of Indraneel's student's data sets. This is followed by two lines, each containing $N$ integers. -----Output:----- The output consists of three lines. The first line of output contains a single integer indicating the length of the longest pair of subsequences (one from each sequence) that has identical successive differences. This is followed by two lines each containing the corresponding subsequences. If there is more than one answer, it suffices to print one. -----Constraints:----- - $1 \leq N \leq 150$. - $0 \leq$ Each data point $\leq 1000$ -----Sample Input----- 7 3 8 4 23 9 11 28 2 3 22 26 8 16 12 -----Sample Output----- 4 3 4 23 9 2 3 22 8
["import copy\r\nn=int(input())\r\na=[int(x) for x in input().split()]\r\nb=[int(x) for x in input().split()]\r\nc=[]\r\nd=[]\r\nlcs=[]\r\ndef lcsfn(a,c,corda,cordb):\r\n\tfor i in range(n+1):\r\n\t\td.append([0]*(n+1))\r\n\t\tlcs.append([0]*(n+1))\r\n\tfor i in range(1,n+1):\r\n\t\tfor j in range(1,n+1):\r\n\t\t\tif a[i-1]==c[j-1]:\r\n\t\t\t\tlcs[i][j]=lcs[i-1][j-1]+1\r\n\t\t\t\td[i][j]='d'\r\n\t\t\telif lcs[i-1][j]>lcs[i][j-1]:\r\n\t\t\t\tlcs[i][j]=lcs[i-1][j]\r\n\t\t\t\td[i][j]='u'\r\n\t\t\telse:\r\n\t\t\t\tlcs[i][j]=lcs[i][j-1]\r\n\t\t\t\td[i][j]='l'\r\n\ti=n\r\n\tj=n\r\n\tcost=0\r\n\twhile i>=1 and j>=1:\r\n\t\tif d[i][j]=='d':\r\n\t\t\tcorda.append(a[i-1])\r\n\t\t\tcordb.append(b[j-1])\r\n\t\t\ti-=1\r\n\t\t\tj-=1\r\n\t\t\tcost+=1\r\n\t\telif d[i][j]=='l':\r\n\t\t\tj-=1\r\n\t\telif d[i][j]=='u':\r\n\t\t\ti-=1\r\n\treturn cost\r\n\r\n\r\nma=-10**9\r\np1=[]\r\np2=[]\r\nfor i in range(-1000,1001):\r\n\tc=[]\r\n\tcorda=[]\r\n\tcordb=[]\r\n\tfor j in range(n):\r\n\t\tc.append(b[j]+i)\r\n\tp=lcsfn(a,c,corda,cordb)\r\n\tif ma<p:\r\n\t\tma=p\r\n\t\tp1=copy.deepcopy(corda)\r\n\t\tp1=p1[::-1]\r\n\t\tp2=copy.deepcopy(cordb)\r\n\t\tp2=p2[::-1]\r\nprint(ma)\r\nprint(*p1)\r\nprint(*p2)\r\n\r\n", "n = int(input())\r\na = list(map(int, input().split()))\r\nb = list(map(int, input().split()))\r\nbb = b[:]\r\nfor i in range(n):\r\n b[i] -= 1000\r\nans = []\r\ndp = []\r\nfor i in range(n+1):\r\n dp.append([0] * (n+1))\r\nfor offset in range(2001):\r\n for i in range(n+1):\r\n dp[i][0] = 0\r\n dp[i][0] = 0\r\n for i in range(1, n+1):\r\n for j in range(1, n+1):\r\n if(a[i-1] == b[j-1]):\r\n dp[i][j] = 1 + dp[i-1][j-1]\r\n else:\r\n dp[i][j] = max(dp[i][j-1], dp[i-1][j])\r\n if(dp[n][n] > len(ans)):\r\n ans = []\r\n j = n\r\n i = n\r\n while(i > 0 and j > 0):\r\n if(dp[i][j] > dp[i][j-1] and dp[i][j] > dp[i-1][j]):\r\n ans.append([i-1, j-1])\r\n i -= 1\r\n j -= 1\r\n elif(dp[i][j] > dp[i][j-1]):\r\n i -= 1\r\n else:\r\n j -= 1\r\n for i in range(n):\r\n b[i] += 1\r\nprint(len(ans))\r\nprintable = [[], []]\r\nfor i, j in ans[::-1]:\r\n printable[0].append(a[i])\r\n printable[1].append(bb[j])\r\nprint(*printable[0])\r\nprint(*printable[1])", "# cook your dish here\nans = []\nmxlen = 0\n\ndef lcs(a,b,d,mxlen):\n L = [0]*(n+1)\n for i in range(n+1):\n L[i] = [0]*(n+1)\n #print(\"#\",L,a,b)\n for i in range(1,n+1):\n for j in range(1,n+1):\n if a[i-1] == b[j-1]:\n L[i][j] = L[i-1][j-1] + 1\n else:\n L[i][j] = max(L[i-1][j],L[i][j-1])\n #print(L)\n if L[n][n] > mxlen:\n mxlen = L[n][n]\n i,j = n,n\n t = []\n while i>0 and j>0:\n if a[i-1] == b[j-1]:\n t.insert(0,a[i-1])\n i -= 1\n j -= 1\n elif L[i-1][j] > L[i][j-1]:\n i -= 1\n else:\n j -= 1\n #print(t)\n ans.append({d:t})\n\ntry:\n n = int(input())\n a = [int(w) for w in input().split()]\n b = [int(w) for w in input().split()]\n minb = min(b)\n b = [(i-minb-1) for i in b]\n for i in range(-1000,1001):\n bb = b.copy()\n bb = [(j+i) for j in bb]\n lcs(a,bb,i,mxlen)\n ans = sorted(ans, key=lambda i: len(list(i.values())[0]),reverse = True)\n t = ans[0]\n d = list(t.keys())[0]\n t = t[d]\n print(len(t))\n print(*t)\n t = [(i-d+minb+1) for i in t]\n print(*t)\nexcept:\n pass"]
{"inputs": [["7", "3 8 4 23 9 11 28", "2 3 22 26 8 16 12"]], "outputs": [["4", "3 4 23 9", "2 3 22 8"]]}
INTERVIEW
PYTHON3
CODECHEF
3,699
d6eb7146f58bf96ba4fff67bd31c0f39
UNKNOWN
A prime number is number x which has only divisors as 1 and x itself. Harsh is playing a game with his friends, where his friends give him a few numbers claiming that they are divisors of some number x but divisor 1 and the number x itself are not being given as divisors. You need to help harsh find which number's divisors are given here. His friends can also give him wrong set of divisors as a trick question for which no number exists. Simply, We are given the divisors of a number x ( divisors except 1 and x itself ) , you have to print the number if only it is possible. You have to answer t queries. (USE LONG LONG TO PREVENT OVERFLOW) -----Input:----- - First line is T queires. - Next are T queries. - First line is N ( No of divisors except 1 and the number itself ) - Next line has N integers or basically the divisors. -----Output:----- Print the minimum possible x which has such divisors and print -1 if not possible. -----Constraints----- - 1<= T <= 30 - 1<= N <= 350 - 2<= Di <=10^6 -----Sample Input:----- 3 2 2 3 2 4 2 3 12 3 2 -----Sample Output:----- 6 8 -1 -----EXPLANATION:----- Query 1 : Divisors of 6 are ( 1,2,3,6) Therefore, Divisors except 1 and the number 6 itself are ( 2 , 3). Thus, ans = 6. Query 2 : Divisors of 8 are ( 1,2,4,8) Therefore, Divisors except 1 and the number 8 itself are ( 2 , 4). Thus, ans = 8. Query 3 : There is no such number x with only ( 1,2,3,12,x ) as the divisors.
["import math\ndef findnumber(l,n):\n l.sort()\n x = l[0] * l[-1]\n vec = []\n i = 2\n while (i*i)<=x:\n if x%i==0:\n vec.append(i)\n if x//i !=i:\n vec.append(x//i)\n i = i + 1\n vec.sort() \n if len(vec)!=n:\n return -1\n else:\n j = 0\n for it in range(n):\n if(l[j] != vec[it]):\n return -1\n else:\n j += 1\n return x\ndef __starting_point():\n t = int(input())\n while t:\n n = int(input())\n arr = list(map(int,input().split()))\n n = len(arr)\n print(findnumber(arr,n))\n print()\n t=t-1\n__starting_point()", "from math import ceil,sqrt,floor\r\nfor _ in range(int(input())):\r\n n=int(input())\r\n a=list(map(int,input().split()))\r\n a.sort()\r\n x=a[0]*a[n-1]\r\n div=[]\r\n for i in range(2,floor(sqrt(x))+1):\r\n if x%i==0:\r\n div.append(i)\r\n y=x//i\r\n if i!=y:\r\n div.append(y)\r\n div.sort()\r\n ## print(div)\r\n if a==div:\r\n print(x)\r\n else:\r\n print(-1)", "ddd = 0\r\n\r\n\r\ndef prime(n):\r\n if n < 2:\r\n return []\r\n else:\r\n n += 1\r\n save = [True] * (n // 2)\r\n for i in range(3, int(n ** 0.5) + 1, 2):\r\n\r\n if save[i // 2]:\r\n k = i * i\r\n save[k // 2::i] = [False] * ((n - k - 1) // (2 * i) + 1)\r\n return [2] + [2 * i + 1 for i in range(1, n // 2) if save[i]]\r\n\r\n\r\npp = prime(1000000)\r\n\r\n\r\ndef decom(x):\r\n nns = 0\r\n arrr = 0\r\n answer = 1\r\n while (x != 1):\r\n nonlocal ddd\r\n ddd += 1\r\n while (x % pp[nns] == 0):\r\n x = x / pp[nns]\r\n arrr += 1\r\n if arrr != 0:\r\n answer = answer * (arrr + 1)\r\n arrr = 0\r\n nns += 1\r\n return answer - 2\r\n\r\n\r\nrr = int(input())\r\np = 0\r\nfor i in range(rr):\r\n n = int(input())\r\n arr = list(map(int, input().split()))\r\n\r\n arr.sort()\r\n\r\n ss = arr[0] * arr[n - 1]\r\n f = 0\r\n for i in range(n):\r\n ddd += 1\r\n if arr[i] * arr[n - 1 - i] != ss:\r\n f = 1\r\n break\r\n if decom(ss) != n:\r\n f = 1\r\n if f == 1:\r\n print(-1)\r\n else:\r\n print(ss)", "# your code goes here\nimport math\ndef isp(n):\n\ti=2\n\tj=math.sqrt(n)\n\twhile i<=j:\n\t\tif n%i==0:\n\t\t\tbreak\n\t\ti+=1\n\treturn i>j\nt=int(input())\nwhile t>0:\n\tn=int(input())\n\tarr=list(map(int,input().split()))\n\tarr.sort()\n\tlst=[0]*1000001\n\tans=1\n\tk=0\n\tfor x in arr:\n\t\tif(isp(x)):\n\t\t\tif lst[x]==0:\n\t\t\t\tlst[x]=1\n\t\t\t\tans*=x\n\t\t\telse:\n\t\t\t k+=1\n\t\t\t break\n\t\telse:\n\t\t\ti=2\n\t\t\tj=math.sqrt(x)\n\t\t\twhile i<=j:\n\t\t\t\tif x%i==0:\n\t\t\t\t\tif lst[i]==0 or lst[x//i]==0:\n\t\t\t\t\t\tbreak\n\t\t\t\ti+=1\n\t\t\tif i<=j:\n\t\t\t\tk+=1\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t if lst[x]==0:\n\t\t\t lst[x]=1\n\t\t\t ans*=x//math.gcd(ans,x)\n\t\t\t else:\n\t\t\t\t k+=1\n\t\t\t\t break\n\t\t\t\t\n\tif k:\n\t\tprint(-1)\n\telse:\n\t\tif ans==arr[n-1]:\n\t\t\tp=ans\n\t\t\twhile p%arr[0]==0:\n\t\t\t\tp//=arr[0]\n\t\t\tif p==1:\n\t\t\t\tans*=arr[0]\n\t\t\t\tprint(ans)\n\t\t\telse:\n\t\t\t\tprint(-1)\n\t\telse:\n\t\t\tprint(ans)\n\tt-=1", "import math\n\ndef factorialToNum(N, n):\n\tN.sort()\n\tt = N[0]*N[n-1]\n\tL = []\n\ti = 2\n\n\twhile(i * i <= t):\n\t\tif(t % i == 0):\n\t\t\tL.append(i)\n\t\t\tif ((t//i) != i):\n\t\t\t\tL.append(t//i)\n\t\ti += 1\n\n\tL.sort()\n\n\tif(len(L) != n):\n\t\treturn -1\n\telse:\n\t\tj = 0\n\t\tfor it in range(n):\n\t\t\tif(N[j] != L[it]):\n\t\t\t\treturn -1\n\t\t\telse:\n\t\t\t\tj += 1\n\treturn t\n\n\nt = int(input())\n\nfor _ in range(t):\n n = int(input()) \n N = list(map(int, input().strip().split(' ')))\n print(factorialToNum(N, n))\n", "# cook your dish here\nimport math\nfor _ in range(int(input())):\n input()\n l=list(map(int,input().split(\" \")))\n l.sort()\n i=0\n j=len(l)-1\n n= -1\n f=True\n while i<=j:\n if i==0:\n n=l[i]*l[j]\n elif l[i]*l[j]!=n:\n f=False\n break\n i+=1\n j-=1\n \n j=len(l)\n for i in range(2,int(math.sqrt(n)+1)):\n if n%i==0:\n if n//i==i:\n j-=1\n else:\n j-=2\n \n if f and j==0:\n print(n)\n else:\n print(-1)\n \n \n", "# cook your dish here\nimport math\n \n \ndef findX(list, int):\n # Sort the given array\n list.sort()\n \n # Get the possible X\n x = list[0]*list[int-1]\n \n # Container to store divisors\n vec = []\n \n # Find the divisors of x\n i = 2\n while(i * i <= x):\n # Check if divisor\n if(x % i == 0):\n vec.append(i)\n if ((x//i) != i):\n vec.append(x//i)\n i += 1\n \n # sort the vec because a is sorted\n # and we have to compare all the elements\n vec.sort()\n # if size of both vectors is not same\n # then we are sure that both vectors\n # can't be equal\n if(len(vec) != int):\n return -1\n else:\n # Check if a and vec have\n # same elements in them\n j = 0\n for it in range(int):\n if(list[j] != vec[it]):\n return -1\n else:\n j += 1\n return x\n \n \n# Driver code\ntry:\n \n m = int(input())\n for i in range (m):\n n = int(input())\n x = list(map(int, input().split()))\n \n\n print(findX(x, n))\nexcept EOFError as e: \n print(e)\n", "# cook your dish here\ndef fun(l, z):\n l.sort()\n x = l[0]*l[z-1]\n temp = []\n i = 2\n while(i * i <= x):\n if(x % i == 0):\n temp.append(i)\n if ((x//i) != i):\n temp.append(x//i)\n i += 1\n temp.sort()\n if(len(temp) != z):\n return -1\n else:\n j = 0\n for k in range(z):\n if(temp[j] != temp[k]):\n return -1\n else:\n j += 1\n return x\nfor i in range(int(input())):\n n = int(input())\n arr = list(map(int,input().split(' ')))\n print(fun(arr,n))", "def solve():\r\n x = a[0] * a[-1]\r\n\r\n vec = []\r\n i = 2\r\n while i * i <= x:\r\n if x % i == 0:\r\n vec.append(i)\r\n if (x // i) != i:\r\n vec.append(x // i)\r\n i += 1\r\n\r\n vec.sort()\r\n\r\n if len(vec) != n:\r\n return -1\r\n else:\r\n j = 0\r\n for it in range(n):\r\n if a[j] != vec[it]:\r\n return -1\r\n else:\r\n j += 1\r\n\r\n return x\r\n\r\nfor _ in range(int(input())):\r\n n = int(input())\r\n a = sorted(map(int, input().split()))\r\n\r\n print(solve())\r\n", "import math\n\n\ndef findX(list, int):\n\n\tlist.sort()\n\n\tx = list[0]*list[int-1]\n\n\tvec = []\n\n\ti = 2\n\twhile(i * i <= x):\n\n\t\tif(x % i == 0):\n\t\t\tvec.append(i)\n\t\t\tif ((x//i) != i):\n\t\t\t\tvec.append(x//i)\n\t\ti += 1\n\n\tvec.sort()\n\n\tif(len(vec) != int):\n\t\treturn -1\n\telse:\n\t\tj = 0\n\t\tfor it in range(int):\n\t\t\tif(a[j] != vec[it]):\n\t\t\t\treturn -1\n\t\t\telse:\n\t\t\t\tj += 1\n\treturn x\n\n\nt = int(input())\nfor i in range(t):\n n = int(input())\n a = [int(x) for x in input().split()]\n print(findX(a, n))\n\n\n \n"]
{"inputs": [["3", "2", "2 3", "2", "4 2", "3", "12 3 2"]], "outputs": [["6", "8", "-1"]]}
INTERVIEW
PYTHON3
CODECHEF
7,637
a709869f7dbc8a58e55b4508ca0df6f4
UNKNOWN
Mandarin chinese , Russian and Vietnamese as well. Let's denote $S(x)$ by the sum of prime numbers that divides $x$. You are given an array $a_1, a_2, \ldots, a_n$ of $n$ numbers, find the number of pairs $i, j$ such that $i \neq j$, $a_i$ divides $a_j$ and $S(a_i)$ divides $S(a_j)$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - First line of each testcase contains one integer $n$ — number of elements of the array. - Second line of each testcase contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$. -----Output:----- For each testcase, output in a single line number of pairs that each of it satisfies given conditions. -----Constraints----- - $1 \leq T \leq 100$ - $2 \leq n, a_i \leq 10^6$ - the sum of $n$ for all test cases does not exceed $10^6$ -----Subtasks----- Subtask #2 (20 points): $2 \leq n \leq 100$, $2 \leq a_i \leq 10^4$ Subtask #2 (80 points): original contsraints -----Sample Input:----- 1 5 2 30 2 4 3 -----Sample Output:----- 6 -----EXPLANATION:----- $S(2) = 2, S(30) = 2 + 3 + 5 = 10, S(4) = 2, S(3) = 3$. So using this information, the pairs of indicies are $(1,2)$, $(1, 3)$, $(1, 4)$, $(3, 1)$, $(3, 2)$, $(3, 4)$.
["# cook your dish here\ndef prime_factors(n):\n i = 2\n factors =set()\n while i * i <= n:\n if n % i:\n i += 1\n else:\n n //= i\n factors.add(i)\n if n > 1:\n factors.add(n)\n return factors\nfor _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n ans=0\n s=[]\n for i in range(n):\n s.append(sum(prime_factors(a[i])))\n for i in range(n):\n for j in range(n):\n if i!=j and a[j]%a[i]==0 and s[j]%s[i]==0:\n ans=ans+1\n print(ans)", "# cook your dish here\nfrom math import sqrt\ndef prime(n):\n for i in range(2,int(sqrt(n))+1):\n if(n%i==0):\n return False;\n return True;\ndef psum(n):\n a=[]\n for i in range(2,int(n/2)+1):\n if n%i==0:\n if prime(i):\n a.append(i)\n if prime(n):\n a.append(n)\n return sum(a);\nt=int(input())\nfor _ in range(t):\n n=int(input())\n a=list(map(int,input().split()))\n l=0\n k=0\n while(l<len(a)):\n for i in range(len(a)):\n if i!=l:\n if a[i]%a[l]==0 :\n # print(a[i],\" \",a[l])\n if psum(a[i])%psum(a[l])==0 :\n k+=1\n l+=1\n print(k)", "# cook your dish here\ndef prime(n):\n for i in range(2,int(n/2)+1):\n if(n%i==0):\n return False;\n return True;\ndef psum(n):\n a=[]\n for i in range(2,n+1):\n if n%i==0:\n if prime(i):\n a.append(i)\n return sum(a);\nt=int(input())\nfor _ in range(t):\n n=int(input())\n a=list(map(int,input().split()))\n l=0\n k=0\n while(l<len(a)):\n for i in range(len(a)):\n if i!=l:\n if a[i]%a[l]==0 :\n # print(a[i],\" \",a[l])\n if psum(a[i])%psum(a[l])==0 :\n k+=1\n l+=1\n print(k)", "import bisect\nimport sys\nimport math\n\nt = int(input())\n\nM = 2000001\n\ns = [0 for x in range(M)]\n\nfor i in range(2,M):\n if (s[i] == 0):\n s[i] = i;\n for j in range(i*2,M,i):\n s[j] += i\n\nwhile (t):\n\n n = int(input())\n a = [int(x) for x in input().split()]\n\n dict = {}\n m = 0\n for x in a:\n m = max(x,m)\n if (x in dict):\n dict[x] += 1\n else:\n dict[x] = 1\n\n count = 0\n \n for k in dict.keys():\n d = dict[k] * (dict[k] - 1)\n count += d\n for i in range(2,math.ceil(m/k)+1):\n if (i*k in dict and s[i*k] % s[k] == 0):\n d = dict[k] * dict[i*k] \n count += d\n\n sys.stdout.write(str(count)+\"\\n\")\n \n \n t -= 1", "\np=10**6+5\ndef Sieve():\n l=[True]*p \n s=[0]*p \n for i in range(2,p):\n if l[i]:\n for j in range(i,p,i):\n s[j]+=i \n l[j]=False \n i+=1 \n l[0]=l[1]=False\n return l,s \nisprime,s=Sieve()\nfrom collections import Counter \nfor _ in range(int(input())):\n n=int(input())\n l=[int(i) for i in input().split()]\n c=Counter(l)\n ans=0\n maxi=max(l)\n for i in range(2,maxi+1):\n if c[i]>0:\n for j in range(i,maxi+1,i):\n if s[j]%s[i]==0:\n ans+=c[i]*c[j]\n ans-=n\n print(ans)", "p=10**5+5\ndef Sieve():\n l=[True]*p \n s=[0]*p \n for i in range(2,p):\n if l[i]:\n for j in range(i,p,i):\n s[j]+=i \n l[j]=False \n i+=1 \n l[0]=l[1]=False\n return l,s \nisprime,s=Sieve()\nfrom collections import defaultdict\ngood=defaultdict(list)\nfor i in range(2,p):\n for j in range(i,p,i):\n if s[j]%s[i]==0:\n good[i].append(j)\nfrom collections import Counter \nfor _ in range(int(input())):\n n=int(input())\n l=[int(i) for i in input().split()]\n c=Counter(l)\n ans=0\n for i in range(2,p):\n if c[i]:\n for j in good[i]:\n ans+=c[i]*c[j]\n ans-=n \n print(ans)", "p=10**4+5\ndef Sieve():\n l=[True]*p \n s=[0]*p \n for i in range(2,p):\n if l[i]:\n for j in range(i,p,i):\n s[j]+=i \n l[j]=False \n i+=1 \n l[0]=l[1]=False\n return l,s \nisprime,s=Sieve()\nfrom collections import defaultdict\ngood=defaultdict(list)\nfor i in range(2,p):\n for j in range(i,p,i):\n if s[j]%s[i]==0:\n good[i].append(j)\nfrom collections import Counter \nfor _ in range(int(input())):\n n=int(input())\n l=[int(i) for i in input().split()]\n c=Counter(l)\n ans=0\n for i in range(2,p):\n if c[i]:\n for j in good[i]:\n ans+=c[i]*c[j]\n ans-=n \n print(ans)", "# cook your dish here\np=10**6+5\ndef Sieve():\n l=[True]*p \n s=[0]*p \n for i in range(2,p):\n if l[i]:\n for j in range(i,p,i):\n s[j]+=i \n l[j]=False \n i+=1 \n l[0]=l[1]=False\n return l,s \nisprime,s=Sieve()\nfor _ in range(int(input())):\n n=int(input())\n l=[int(i) for i in input().split()]\n #l.sort(reverse=True)\n c=0\n # prnt(s[2])\n for i in range(n):\n for j in range(n):\n if (s[l[j]]%s[l[i]]==0) and l[j]%l[i]==0 and i!=j:\n #print(i,j)\n c+=1 \n print(c)", "def prime_factors(n):\n i = 2\n factors =set()\n while i * i <= n:\n if n % i:\n i += 1\n else:\n n //= i\n factors.add(i)\n if n > 1:\n factors.add(n)\n return factors\nfor _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n ans=0\n s=[]\n for i in range(n):\n s.append(sum(prime_factors(a[i])))\n for i in range(n):\n for j in range(n):\n if i!=j and a[j]%a[i]==0 and s[j]%s[i]==0:\n ans=ans+1\n print(ans)"]
{"inputs": [["1", "5", "2 30 2 4 3"]], "outputs": [["6"]]}
INTERVIEW
PYTHON3
CODECHEF
4,880
39dbfda1043e087f6890bb78c42a70c3
UNKNOWN
Chef has arrived in Dagobah to meet with Yoda to study cooking. Yoda is a very busy cook and he doesn't want to spend time with losers. So he challenges the Chef to a series of games, and agrees to teach the Chef if Chef can win at least P of the games. The total number of games is K. The games will be played on a chessboard of size N*M. That is, it has N rows, each of which has M squares. At the beginning of the game, a coin is on square (1, 1), which corresponds to the top-left corner, and every other square is empty. At every step, Yoda and Chef have to move the coin on the chessboard. The player who cannot make a move loses. Chef makes the first move. They can't move the coin to a square where it had been placed sometime before in the game, and they can't move outside chessboard. In this game, there are two different sets of rules according to which the game can be played: -from (x, y) player can move coin to (x+1, y), (x-1, y), (x, y+1), (x, y-1) in his turn, if they are valid squares. -from (x, y) player can move coin to (x+1, y+1), (x-1, y-1), (x-1, y+1), (x+1, y-1) in his turn, if they are valid squares. Before every game, the Power of the kitchen chooses one among the two sets of rules with equal probability of 0.5, and the game will be played according to those rules. Chef and Yoda are very wise, therefore they play optimally. You have to calculate the probability that Yoda will teach Chef. -----Input----- Input begins with an integer T, the number of test cases Each test case begins with 4 integers N, M, P, K. -----Output----- For each test case, output a line containing the answer for task. The output must have an absolute error at most 0.000001 (10-6). -----Constraints and Subtasks----- - 1 ≤ T ≤ 50 - 1 ≤ K Subtask 1 : 10 points - 2 ≤ N, M ≤ 5 - 0 ≤ P ≤ K ≤ 5 Subtusk 2 : 10 points - 2 ≤ N, M ≤ 10 - 0 ≤ P ≤ K ≤ 10^3 Subtusk 3 : 20 points - 2 ≤ N, M ≤ 100 - 0 ≤ P ≤ K ≤ 10^3 Subtusk 4 : 60 points - 2 ≤ N, M ≤ 100 - 0 ≤ P ≤ K ≤ 10^6 -----Example----- Input: 2 2 3 2 3 2 2 5 5 Output: 0.500000 1.000000
["import math\n\ndp = []\ndp.append(0)\nfor i in range(1,1000005):\n\tdp.append(math.log(i) + dp[i-1])\n\nt = int(input())\nfor i in range(t):\n\tn,m,p,k = input().split()\n\tn = int(n)\n\tm = int(m)\n\tp = int(p)\n\tk = int(k)\n\n\tif p==0 or (n%2==0 and m%2==0):\n\t\tans = 1.0\n\t\tprint(ans)\n\telif n%2==1 and m%2==1:\n\t\tans=0.0\n\t\tprint(ans*100)\n\telse:\n\t\tP = 0\n\t\tkln2 = k*math.log(2)\n\t\tfor i in range(p, k+1):\n\t\t\tlnPi = dp[k] - dp[i] - dp[k-i] - kln2\n\t\t\tPi = pow(math.e, lnPi)\n\t\t\tP += Pi\n\t\tprint(P)\n\n", "import math\n\ndp = []\ndp.append(0)\nfor i in range(1,1000005):\n\tdp.append(math.log(i) + dp[i-1])\n\nt = int(input())\nfor i in range(t):\n\tn,m,p,k = input().split()\n\tn = int(n)\n\tm = int(m)\n\tp = int(p)\n\tk = int(k)\n\n\tif p==10 or (n%2==0 and m%2==0):\n\t\tans = 1.0\n\t\tprint(ans)\n\telif n%2==1 and m%2==1:\n\t\tans=0.0\n\t\tprint(ans)\n\telse:\n\t\tP = 0\n\t\tkln2 = k*math.log(2)\n\t\tfor i in range(p, k+1):\n\t\t\tlnPi = dp[k] - dp[i] - dp[k-i] - kln2\n\t\t\tPi = pow(math.e, lnPi)\n\t\t\tP += Pi\n\t\tprint(P)\n\n", "# cook your dish here\nimport math\n\ndp = []\ndp.append(0)\nfor i in range(1,1000005):\n\tdp.append(math.log(i) + dp[i-1])\n\nt = int(input())\nfor i in range(t):\n\tn,m,p,k = input().split()\n\tn = int(n)\n\tm = int(m)\n\tp = int(p)\n\tk = int(k)\n\n\tif p==0 or (n%2==0 and m%2==0):\n\t\tans = 1.0\n\t\tprint(ans)\n\telif n%2==1 and m%2==1:\n\t\tans=0.0\n\t\tprint(ans)\n\telse:\n\t\tP = 0\n\t\tkln2 = k*math.log(2)\n\t\tfor i in range(p, k+1):\n\t\t\tlnPi = dp[k] - dp[i] - dp[k-i] - kln2\n\t\t\tPi = pow(math.e, lnPi)\n\t\t\tP += Pi\n\t\tprint(P)\n\n\n", "# cook your dish here\nimport math\n\ndp = []\ndp.append(0)\nfor i in range(1,1000005):\n\tdp.append(math.log(i) + dp[i-1])\n\nt = int(input())\nfor i in range(t):\n\tn,m,p,k = input().split()\n\tn = int(n)\n\tm = int(m)\n\tp = int(p)\n\tk = int(k)\n\n\tif p==0 or (n%2==0 and m%2==0):\n\t\tans = 1.0\n\t\tprint(ans)\n\telif n%2==1 and m%2==1:\n\t\tans=0.0\n\t\tprint(ans)\n\telse:\n\t\tP = 0\n\t\tkln2 = k*math.log(2)\n\t\tfor i in range(p, k+1):\n\t\t\tlnPi = dp[k] - dp[i] - dp[k-i] - kln2\n\t\t\tPi = pow(math.e, lnPi)\n\t\t\tP += Pi\n\t\tprint(P)\n\n\n", "import math\r\n\r\ndp = []\r\ndp.append(0)\r\nfor i in range(1,1000005):\r\n\tdp.append(math.log(i) + dp[i-1])\r\n\r\nt = int(input())\r\nfor i in range(t):\r\n\tn,m,p,k = input().split()\r\n\tn = int(n)\r\n\tm = int(m)\r\n\tp = int(p)\r\n\tk = int(k)\r\n\r\n\tif p==0 or (n%2==0 and m%2==0):\r\n\t\tans = 1.0\r\n\t\tprint(ans)\r\n\telif n%2==1 and m%2==1:\r\n\t\tans=0.0\r\n\t\tprint(ans)\r\n\telse:\r\n\t\tP = 0\r\n\t\tkln2 = k*math.log(2)\r\n\t\tfor i in range(p, k+1):\r\n\t\t\tlnPi = dp[k] - dp[i] - dp[k-i] - kln2\r\n\t\t\tPi = pow(math.e, lnPi)\r\n\t\t\tP += Pi\r\n\t\tprint(P)\r\n\r\n\r\n", "import math\r\n\r\ndp = []\r\ndp.append(0)\r\nfor i in range(1,1000005):\r\n\tdp.append(math.log(i) + dp[i-1])\r\n\r\nt = int(input())\r\nfor i in range(t):\r\n\tn,m,p,k = input().split()\r\n\tn = int(n)\r\n\tm = int(m)\r\n\tp = int(p)\r\n\tk = int(k)\r\n\r\n\tif n%2==0 and m%2==0:\r\n\t\tans = 1.0\r\n\t\tprint(ans)\r\n\telif n%2==1 and m%2==1:\r\n\t\tans=0.0\r\n\t\tprint(ans)\r\n\telse:\r\n\t\tP = 0\r\n\t\tkln2 = k*math.log(2)\r\n\t\tfor i in range(p, k+1):\r\n\t\t\tlnPi = dp[k] - dp[i] - dp[k-i] - kln2\r\n\t\t\tPi = pow(math.e, lnPi)\r\n\t\t\tP += Pi\r\n\t\tprint(P)\r\n\r\n", "import math\r\n\r\ndp = []\r\ndp.append(0)\r\nfor i in range(1,1000005):\r\n\tdp.append(math.log(i) + dp[i-1])\r\n\r\nt = int(input())\r\nfor i in range(t):\r\n\tn,m,p,k = input().split()\r\n\tn = int(n)\r\n\tm = int(m)\r\n\tp = int(p)\r\n\tk = int(k)\r\n\r\n\tif n%2==0 and m%2==0:\r\n\t\tans = 1.0\r\n\t\tprint(ans)\r\n\telif n%2==1 and m%2==1:\r\n\t\tans=0.0\r\n\t\tprint(ans)\r\n\telse:\r\n\t\tans = 0\r\n\t\tfor j in range(p,k+1):\r\n\t\t\ttotal = math.exp(dp[k] - dp[j] - dp[k-j] - k*math.log(2))\r\n\t\t\tans = ans + total\r\n\t\tprint(ans)\r\n\r\n", "import math\r\n\r\ndp = []\r\ndp.append(0.0)\r\nfor i in range(1,1000005):\r\n\tdp.append(math.log(i) + dp[i-1])\r\n\r\nt = int(input())\r\nfor i in range(t):\r\n\tn,m,p,k = input().split()\r\n\tn = int(n)\r\n\tm = int(m)\r\n\tp = int(p)\r\n\tk = int(k)\r\n\r\n\tif n%2==0 and m%2==0:\r\n\t\tans = 1.0\r\n\t\tprint(ans)\r\n\telif n%2==1 and m%2==1:\r\n\t\tans=0.0\r\n\t\tprint(ans)\r\n\telse:\r\n\t\tans = 0.0\r\n\t\tfor j in range(p,k+1):\r\n\t\t\ttotal = math.exp(dp[k] - dp[j] - dp[k-j] - k*math.log(2))\r\n\t\t\tans+=total\r\n\t\tprint(ans)\r\n\r\n"]
{"inputs": [["2", "2 3 2 3", "2 2 5 5"]], "outputs": [["0.500000", "1.000000"]]}
INTERVIEW
PYTHON3
CODECHEF
4,500
0a67ffb6d3fa27a08b7e69f319cf792d
UNKNOWN
You are given an array $A$ of $N$ positive and pairwise distinct integers. You can permute the elements in any way you want. The cost of an ordering $(A_1, A_2, \ldots, A_N)$ is defined as $ (((A_1 \bmod A_2) \bmod A_3)......) \bmod A_N$ where $X \bmod Y$ means the remainder when $X$ is divided by $Y$. You need to find the maximum cost which can be attained through any possible ordering of the elements. -----Input:----- - The first line contains $T$ denoting the number of test cases. - The first line of each testcase contains a single integer $N$. - The second line of each testcase contains $N$ space-separated integers, the elements of $A$. -----Output:----- - For each testcase, output the maximum possible cost in a new line. -----Constraints----- - $1 \leq T \leq 5*10^5$ - $2 \leq N \leq 5*10^5$ - $1 \leq A_i \leq 10^9$ - Sum of $N$ over all testcases is less than or equal to $10^6$ - All elements in a single testcase are distinct. -----Subtasks----- - 100 points : Original constraints. -----Sample Input:----- 1 2 7 12 -----Sample Output:----- 7 -----Explanation:----- The two possible ways to order the elements are [7, 12] and [12, 7]. In the first case, the cost is $7 \bmod 12 = 7$ and in the second case the cost is $12 \bmod 7 = 5$. Clearly the answer is 7.
["# cook your dish here\nt=int(input())\nfor i in range(t):\n n=int(input())\n a=list(map(int,input().split()))\n print(min(a))", "t=int(input())\nfor i in range(t):\n n=int(input())\n a=list(map(int,input().split()))\n print(min(a))", "# cook your dish here\nfor i in range(int(input())):\n n=int(input())\n x=list(map(int,input().split()))\n print(min(x))", "for i in range(int(input())):\n n=int(input())\n x=list(map(int,input().split()))\n print(min(x))", "for i in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n a.sort()\n print(max(a[0]%a[n-1],a[n-1]%a[0]))", "for _ in range(int(input())):\n n = int(input())\n nums = [int(j) for j in input().split()]\n print(min(nums))", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n print(min(a))\n", "# cook your dish here\nt = int(input())\nfor i in range(t):\n n = int(input())\n a = list(map(int,input().split()))\n j = 1\n r = []\n b = a[0]\n while j<len(a):\n b = b%a[j]\n r.append(b)\n j += 1\n print(min(r))\n\n", "T = int(input())\nfor T1 in range(T):\n n = int(input())\n a = []\n a = list(map(int,input().split()))\n #a.sort(reverse=True)\n print(min(a))", "t=int(input())\nfor T in range(t):\n n=int(input())\n l=list(map(int,input().split()))\n l.sort()\n res=l[0]\n for i in range(1,n):\n res=res%l[i]\n print(res)\n", "for _ in range(int(input())):\n n = int(input())\n a = list(map(int,input().split()))\n mod = a[0]%a[1]\n for i in range(1,n):\n if mod>a[i]:\n mod = a[i]%mod\n else:\n mod = mod%a[i]\n print(mod)", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n d=a[0]%a[1]\n for i in range(1,n):\n if d>a[i]:\n d=a[i]%d\n else:\n d=d%a[i]\n print(d)", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n d=a[0]%a[1]\n for i in range(2,n):\n d=d%a[i]\n print(d)", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n d=a[0]%a[1]\n for i in range(2,n):\n if d>a[i]:\n d=a[i]%d\n else:\n d=d%a[i]\n print(d)", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n s=a[0]%a[1]\n m=a[1]%a[0]\n d=max(s,m)\n for i in range(2,n):\n if d>a[i]:\n d=a[i]%d\n else:\n d=d%a[i]\n print(d)", "for kt in range(int(input())):\n n = input()\n arr = list(map(int,input().split()))\n print(min(arr))", "for i in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n print(min(l))", "for i in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n print(sorted(l)[0])", "for _ in range(int(input())):\n # n,k = map(int,input().split())\n n = input()\n arr = list(map(int,input().split()))\n print(min(arr))\n", "# cook your dish here\nt=int(input())\nfor i in range(t):\n n=int(input())\n a=sorted(list(map(int,input().split())))\n b=a[0]%a[1]\n for n in a[2:]:\n b=b%n\n print(b)", "for _ in range(int(input())):\n n = int(input())\n nums = sorted(list(map(int, input().split())))\n \n res = nums[0] % nums[1]\n \n for n in nums[2:]:\n res = res % n\n \n print(res)", "for _ in range(int(input())):\n N = int(input())\n L = sorted(map(int, input().split()))\n mod = L[0]\n for i in range(1,N):\n mod = mod % L[i]\n print(mod)", "# cook your dish here\nt=int(input())\nfor r in range(t):\n n=int(input())\n s=input()\n a=list(map(int,s.split()))\n a.sort()\n p=a[0]\n for j in range(1,n):\n p=p%a[j]\n print(p)", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n a.sort()\n s=a[0]\n for i in range(1,n):\n s=s%a[i]\n print(s) ", "for _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n s=a[0]\n a.sort()\n for i in range(1,n):\n s=s%a[i]\n print(s) "]
{"inputs": [["1", "2", "7 12"]], "outputs": [["7"]]}
INTERVIEW
PYTHON3
CODECHEF
3,743
296f764ec48da17f3cf1a3d9df92e37c
UNKNOWN
Chef is again playing a game with his best friend Garry. As usual, the rules of this game are extremely strange and uncommon. First, they are given a stack of $N$ discs. Each disc has a distinct, non-negative integer written on it. The players exchange turns to make a move. Before the start of the game, they both agree upon a set of positive integers $S$ of size $K$. It is guaranteed that S contains the integer $1$. In a move, a player can select any value $x$ from $S$ and pop exactly $x$ elements from the top of the stack. The game ends when there are no discs remaining. Chef goes first. Scoring: For every disc a player pops, his score increases by $2^p$ where $p$ is the integer written on the disc. For example, if a player pops the discs, with integers $p_1, p_2, p_3, \dots, p_m$ written on it, during the entire course of the game, then his total score will be $2^{p_1} + 2^{p_2} + 2^{p_3} + \dots + 2^{p_m}$. The player with higher score wins the game. Determine the winner if both the players play optimally, or if the game ends in a draw. -----Input:----- - First line contains $T$, the number of testcases. Then the testcases follow. - The first line of each test case contains two space separated integers $N$ and $K$, denoting the size of the stack and the set S respectively. - Next line contains $N$ space separated integers $A_i$ where $A_1$ is the topmost element, denoting the initial arrangement of the stack. - The last line of each test case contains $K$ space separated integers each denoting $x_i$. -----Output:----- For each testcase, output "Chef" (without quotes) if Chef wins, "Garry" (without quotes) if Garry wins, otherwise "Draw" (without quotes) in a separate line. -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 10^5$ - $1 \leq K \leq \min(100, N)$ - $0 \leq A_i \leq 10^9$ - $1 \leq x_i \leq N$ - $x_i \neq x_j$ for all $i \neq j$ - $A_i \neq A_j$ for all $i \neq j$ - Set $S$ contains integer $1$. - Sum of $N$ over all test cases does not exceed $10^5$. -----Sample Input:----- 1 3 2 5 7 1 1 2 -----Sample Output:----- Chef -----Explanation:----- Chef can select 2 from the set and draw the top two discs (with integers 5 and 7 written on it) from the stack. Garry cannot select 2 from the set as there is only 1 disc left in the stack. However, he can select 1 from the set and pop the last disc. So, Chef's score = $2^5$ + $2^7$ = $160$ Garry's score = $2^1$ = $2$ Chef wins.
["for _ in range(int(input())):\n n, k = map(int, input().split())\n a = list(map(int, input().split()))\n b = list(map(int, input().split()))\n\n a = [-1] + a[::-1]\n mx = a.index(max(a))\n dp = [0] * (n + 1)\n for i in range(1, n + 1):\n for x in b:\n if i - x < 0: continue\n if i - x < mx <= i:\n dp[i] = 1\n else:\n dp[i] |= not dp[i - x]\n\n print('Chef' if dp[-1] else 'Garry')", "for t in range(int(input())):\n n,k=list(map(int,input().split()))\n arr=list(map(int,input().split()))\n brr=list(map(int,input().split()))\n arr.append(-1)\n arr=arr[::-1]\n x=arr.index(max(arr))\n # print(arr)\n dp=[0]*(n+1)\n for i in range(1,n+1,1):\n for l in brr:\n # print(i-l)\n if i-l<0:\n continue\n elif i-l<x and x<=i:\n dp[i]=1\n else:\n dp[i]|=not dp[i-l]\n # print(dp)\n if dp[n]:\n print(\"Chef\")\n else:\n print(\"Garry\")\n\n\n\n", "for _ in range(int(input())):\n n, k = map(int, input().split())\n a = list(map(int, input().split()))\n b = list(map(int, input().split()))\n\n a = [-1] + a[::-1]\n mx = a.index(max(a))\n dp = [0] * (n + 1)\n for i in range(1, n + 1):\n for x in b:\n if i - x < 0: continue\n if i - x < mx <= i:\n dp[i] = 1\n else:\n dp[i] |= not dp[i - x]\n\n print('Chef' if dp[-1] else 'Garry') ", "# cook your dish here\nfor t in range(int(input())):\n n, k = map(int, input().split())\n a = list(map(int, input().split()))\n x = list(map(int, input().split()))\n \n wv = max(a)\n wvi = a.index(wv)\n \n dp = [False]*n\n dp[wvi] = True\n # print(wvi)\n for i in range(wvi-1, -1, -1):\n for j in x:\n if (i+j)>n:\n continue\n if (i+j)>wvi:\n dp[i] = True\n elif dp[i+j]==False:\n # print(i, j)\n dp[i] = True\n break\n # print(dp)\n \n if dp[0]:\n print(\"Chef\")\n else:\n print(\"Garry\")", "for _ in range(int(input())):\n n,k=list(map(int,input().split()))\n a=list(map(int,input().split()))\n s=list(map(int,input().split()))\n m=-1;p=-1\n for i in range(n):\n if a[i]>m:\n m=a[i]\n p=i+1\n dp=[False]*(p+1)\n s.sort()\n #print(s)\n for i in range(1,p+1):\n flag=0;\n for j in range(k):\n if s[j]<=i:\n if dp[i-s[j]]==False:\n dp[i]=True\n break\n else:\n if s[j]-i<=n-p:\n dp[i]=True\n break\n if dp[-1]==False:\n print('Garry')\n else:\n print('Chef')\n", "for _ in range(int(input())):\n n, k = map(int, input().split())\n a = list(map(int, input().split()))\n b = list(map(int, input().split()))\n\n a = [-1] + a[::-1]\n mx = a.index(max(a))\n dp = [0] * (n + 1)\n for i in range(1, n + 1):\n for x in b:\n if i - x < 0: continue\n if i - x < mx <= i:\n dp[i] = 1\n else:\n dp[i] |= not dp[i - x]\n\n print('Chef' if dp[-1] else 'Garry') ", "for _ in range(int(input())):\n n, k = map(int, input().split())\n a = list(map(int, input().split()))\n s = list(map(int, input().split()))\n \n m = max(a)\n mi = a.index(m)\n dp = [0] * (n + 1)\n dp[mi] = 1\n for i in range(mi, -1, -1):\n for j in s:\n if i + j > n:\n continue\n if not dp[i + j]:\n dp[i] = 1\n break \n\n print('Chef' if dp[0] else 'Garry')"]
{"inputs": [["1", "3 2", "5 7 1", "1 2"]], "outputs": [["Chef"]]}
INTERVIEW
PYTHON3
CODECHEF
3,077
d80acf580e15fc55301d0a506594c146
UNKNOWN
Chef is baking a cake. While baking, in each minute the size of cake doubles as compared to its previous size. In this cake, baking of cake is directly proportional to its size. You are given $a$, the total time taken(in minutes) to bake the whole cake. Let cake be half baked at $k^{th}$ minute. Your task is to find the value of $k+2$. -----Input:----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of T test cases follows. - The first and only line of each test case contains a single integer $a$. -----Output:----- For each testcase , print one line, the value of $k+2$. -----Constraints----- - $1 \leq T \leq 8 $ - $2 \leq a \leq 10^{128}$ -----Sample Input:----- 1 2 -----Sample Output:----- 3 -----Explaination----- Time was 1 min when cake was half baked by chef so answer is 1+2=3
["# cook your dish here\nfor _ in range(int(input())):\n a=int(input())\n print(a/2+2)", "# cook your dish here\nfor _ in range(int(input())):\n a=int(input())\n print(a//2+2)"]
{"inputs": [["1", "2"]], "outputs": [["3"]]}
INTERVIEW
PYTHON3
CODECHEF
189
4d753d0fc0d40fe7d111e98ce00dc795
UNKNOWN
Once N boys and M girls attended a party. You are given a matrix A of N rows and M columns where Aij is 1 if the i-th boy likes the j-th girl, otherwise it will be 0. Note that it is not necessary that if a boy x likes girl y, then girl y should like boy x. You know that if there are two different boys x and y, who both like girl z, then there will be a collision. Can you calculate the number of different collisions at this party? Note that order of boys in the collision doesn't matter. -----Input----- The first line contains a single integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains two space separated integers N, M denoting the number of boys and girls, respectively. Each of the following N lines contain M characters, each of them is either '0' or '1'. -----Output----- For each test case output a single line containing an integer corresponding to the number of collisions at the party. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N, M ≤ 10 -----Example----- Input: 2 4 3 111 100 110 000 2 2 10 01 Output: 4 0 -----Explanation----- Example Case 1. All three boys like the first girl, so there are (1, 2, 1), (1, 3, 1), (2, 3, 1) collisions with her. Boys 1 and 3 both like the second girl so this is one more collision. Only one boy likes the third girl, so there are no collisions with her and thus we have 4 collisions total. Example Case 2. For each girl there is only one boy who likes her, so there are no collisions at all.
["for i in range(int(input())):\n n,k=map(int,input().split())\n m=[]\n for j in range(n):\n l=list(input())\n m.append(l)\n a=0\n for k in range(k):\n b=0\n for p in range(n):\n if m[p][k]=='1':\n b+=1\n if b>1:\n a+=((b*(b-1))//2)\n print(a)", "a=int(input())\nfor i in range(a):\n n,m=map(int,input().split())\n b=[]\n for j in range(n):\n j=list(input())\n b.append(j)\n c=0\n for k in range(m):\n d=0\n for p in range(n):\n if b[p][k]=='1':\n d+=1\n if d>1:\n c+=((d*(d-1))//2)\n print(c)", "# cook your dish here\nw=int(input())\nfor i in range(w):\n B,f=list(map(int,input().split()))\n v =[]\n for j in range(B):\n j=list(input())\n v.append(j)\n q=0\n for k in range(f):\n s=0\n for n in range(B):\n if v[n][k]=='1':\n s+=1\n if s>1:\n q+=((s*(s-1))//2)\n print(q)\n \n", "# cook your dish here\nfor i in range(int(input())):\n n,m=map(int,input().split())\n matrix = []\n for i in range(n):\n row = []\n s = input()\n for i in s:\n row.append(i)\n matrix.append(row)\n ans = 0\n for i in range(m):\n c= 0\n for j in range(n):\n if(matrix[j][i]=='1'):\n c+=1\n ans +=(c*(c-1)//2)\n print(ans)", "T=int(input())\nfor i in range(T):\n B,G=list(map(int,input().split()))\n l=[]\n for j in range(B):\n m=list(input())\n l.append(m)\n d=0\n for k in range(G):\n c=0\n for n in range(B):\n if l[n][k]=='1':\n c+=1\n if c>1:\n d+=((c*(c-1))//2)\n print(d)\n", "T=int(input())\nfor i in range(T):\n B,G=list(map(int,input().split()))\n l=[]\n for j in range(B):\n m=list(input())\n l.append(m)\n d=0\n for k in range(G):\n c=0\n for n in range(B):\n if l[n][k]=='1':\n c+=1\n if c>1:\n d+=((c*(c-1))//2)\n print(d)\n \n\n", "for t in range(int(input())):\n n, m = list(map(int, input().split()))\n matrix = []\n for i in range(n):\n matrix.append(list(input()))\n colli = 0\n for i in range(m):\n count = 0\n for j in range(n):\n if matrix[j][i] == '1':\n count += 1\n if count > 1:\n colli += ((count * (count-1)) // 2)\n print(colli)\n", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n n, m = map(int, input().split())\n matrix = []\n for i in range(n):\n matrix.append(list(input()))\n collision = 0\n for i in range(m):\n count = 0\n for j in range(n):\n if matrix[j][i] == '1':\n count += 1\n # print('Count:', count)\n if count > 1:\n collision += ((count * (count-1)) // 2)\n print(collision)", "t = int(input())\nfor _ in range(t):\n n, m = map(int, input().split())\n matrix = []\n for i in range(n):\n matrix.append(list(input()))\n collision = 0\n for i in range(m):\n count = 0\n for j in range(n):\n if matrix[j][i] == '1':\n count += 1\n # print('Count:', count)\n if count > 1:\n collision += ((count * (count-1)) // 2)\n print(collision)", "def factorial(n):\n fact = 1\n for i in range(1, n+1):\n fact = fact * i\n return fact\nt = int(input())\nfor _ in range(t):\n n, m = map(int, input().split())\n matrix = []\n for i in range(n):\n matrix.append(list(input()))\n collision = 0\n for i in range(m):\n count = 0\n for j in range(n):\n if matrix[j][i] == '1':\n count += 1\n # print('Count:', count)\n if count > 1:\n collision += ((count * (count-1)) // 2)\n print(collision)", "for _ in range(int(input())):\n N,M = (list(map(int,input().split(' '))))\n grid = []\n \n \n for x in range(N):\n row = [i for i in input()]\n grid.append(row)\n \n collisions = 0\n \n for x in range(M):\n z = 0\n for y in range(N):\n if grid[y][x] == '1':\n z += 1\n if z == 2:\n collisions += 1\n elif z > 2:\n collisions += (z * (z - 1)) // 2\n print(collisions)\n \n \n", "# cook your dish here\n\ndef collisions(ar, n, m):\n res = 0\n for i in range(m):\n tem1, tem2 = 0, 0\n for j in range(n):\n if ar[j][i] == '1':\n tem1 += tem2\n tem2 += 1\n res += tem1\n return res\n\ndef __starting_point():\n t = int(input())\n for i in range(t):\n n, m = map(int, input().split())\n ar = []\n for i in range(n):\n ar.append(list(input()))\n print(collisions(ar, n, m))\n__starting_point()", "for _ in range(int(input())):\n m,n=map(int,input().split())\n l=[]\n for i in range(m):\n s=input()\n l.append(s)\n final=0\n for i in range(n):\n count=0\n for j in range(m):\n if l[j][i]=='1':\n count+=1\n if count>1:\n for k in range(count):\n final+=k\n print(final)", "tc = int(input())\nwhile tc !=0: \n r,c = map(int, input().split())\n arr= []\n for i in range(r):\n temp = list(map(int, input()))\n arr.append(temp)\n count = 0\n for j in range(c):\n temp = 0\n for i in range(r):\n if arr[i][j]==1:\n temp += 1\n if temp!=1 and temp!=0:\n count += (temp*(temp-1))//2\n print(count)\n\n tc -= 1", "# cook your dish here\nt=int(input())\nwhile t>0:\n n,m=map(int,input().split())\n l=[]\n co=0\n for i in range(n):\n l.append(input())\n for i in range(m):\n c=0\n for j in range(n):\n if l[j][i]==\"1\":\n c+=1\n co+=c*(c-1)/2\n print(int(co))\n t-=1", "def comb(n, r):\n an = fact(n)\n an = an/fact(r)\n an = an/fact(n-r)\n \n return int(an)\n \ndef fact(n):\n ans = 1\n while(n>1):\n ans = ans*n\n n -= 1\n return ans\n \nt = int(input())\n\nfor _ in range(t):\n n, m = [int(x) for x in input().split()]\n \n a = []\n ans = 0\n \n for i in range(n):\n temp = [int(x) for x in input()]\n a.append(temp)\n \n for i in range(m):\n c = 0\n for j in range(n):\n if(a[j][i] == 1):\n c += 1\n if(c>=2):\n ans += comb(c, 2)\n \n print(ans)", "# cook your dish here\ntry:\n for _ in range(int(input())):\n n, m = map(int, input().split())\n l = []\n for j in range(n):\n l.append(list(input()))\n ans = []\n c = 0\n for u in range(m):\n for v in range(n):\n if l[v][u] == '1':\n c += 1\n ans.append(c)\n c = 0\n count = 0\n for j in range(len(ans)):\n count += ((ans[j] - 1) * (ans[j])) //2\n print(count)\nexcept:\n pass", "t=int(input())\nfor i in range(t):\n n,m=map(int,input().split())\n l=[]\n for j in range(n):\n l.append(list(input()))\n ans=[]\n c=0\n for u in range(m):\n for v in range(n):\n if l[v][u]=='1':\n c=c+1\n ans.append(c)\n c=0\n count=0\n for j in range(len(ans)):\n count=count+(((ans[j]-1)*(ans[j]))//2)\n print(count)", "def fact(i):\n a = i\n while a > 1:\n i += (a-1)\n a = a - 1\n return i\n\nfor _ in range(int(input())):\n n, m = list(map(int, input().split()))\n matrix = [0] * m\n for _ in range(n):\n s = input()\n for i in range(len(s)):\n if s[i] == '1':\n matrix[i] += 1\n coll = [fact(matrix[i]-1) for i in range(m) if matrix[i] > 1]\n print((sum(coll) if coll else 0))# cook your dish here\n", "def fact(i):\n a = i\n while a > 1:\n i += (a-1)\n a = a - 1\n return i\n\nfor _ in range(int(input())):\n n, m = list(map(int, input().split()))\n matrix = [0] * m\n for _ in range(n):\n s = input()\n for i in range(len(s)):\n if s[i] == '1':\n matrix[i] += 1\n coll = [fact(matrix[i]-1) for i in range(m) if matrix[i] > 1]\n print((sum(coll) if coll else 0))# cook your dish here\n", "t=int(input())\nfor i in range(t):\n n,m=list(map(int,input().split()))\n l=[]\n for j in range(n):\n l.append(list(input()))\n ans=[]\n c=0\n for u in range(m):\n for v in range(n):\n if l[v][u]=='1':\n c=c+1\n ans.append(c)\n #print(ans)\n c=0\n count=0\n for j in range(len(ans)):\n count=count+(((ans[j]-1)*(ans[j]))//2)\n print(count)\n", "t=int(input())\nfor i in range(t):\n n,m=list(map(int,input().split()))\n l=[]\n for j in range(n):\n l.append(list(input()))\n ans=[]\n c=0\n for u in range(m):\n for v in range(n):\n if l[v][u]=='1':\n c=c+1\n ans.append(c)\n c=0\n count=0\n for j in range(len(ans)):\n count=count+(((ans[j]-1)*(ans[j]))//2)\n print(count)\n", "for _ in range(int(input())):\n n,m=map(int,input().split())\n matrix = []\n for i in range(n):\n row = []\n s = input()\n for ele in s:\n row.append(ele)\n matrix.append(row)\n ans = 0\n for i in range(m):\n cnt = 0\n for j in range(n):\n if(matrix[j][i]=='1'):\n cnt+=1\n ans +=(cnt*(cnt-1)//2)\n print(ans)"]
{"inputs": [["2", "4 3", "111", "100", "110", "000", "2 2", "10", "01"]], "outputs": [["4", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
8,013
30e947780207cc28f32878374aaf55d7
UNKNOWN
Chef is a big fan of soccer! He loves soccer so much that he even invented soccer for dogs! Here are the rules of soccer for dogs: - N$N$ dogs (numbered 1$1$ through N$N$) stand in a line in such a way that for each valid i$i$, dogs i$i$ and i+1$i + 1$ are adjacent. - Each dog has a skill level, which is either 1$1$ or 2$2$. - At the beginning of the game, Chef passes a ball to dog 1$1$ (dog 1$1$ receives the ball). - For each valid i$i$, if dog i$i$ has skill level s$s$, this dog can pass the ball to any dog with number j$j$ such that 1≤|i−j|≤s$1 \le |i-j| \le s$. - Each dog (including dog 1$1$) may receive the ball at most once. - Whenever a dog receives the ball, it must either pass it to another dog or finish the game by scoring a goal. While the dogs were playing, Chef also created a game for developers. He defined the result of a game of soccer for dogs as the sequence of dogs which received the ball in the order in which they received it. The last dog in the sequence is the dog that decided to score a goal; if a dog never received the ball, it does not appear in the sequence. In the game for developers, you should find the number of possible results of soccer for dogs. Find this number of possible results modulo 109+7$10^9 + 7$. Two results of soccer for dogs (sequences of dogs' numbers) are considered different if these sequences have different lengths or if there is a valid index i$i$ such that the i$i$-th dog in one sequence is different from the i$i$-th dog in the other sequence. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$ denoting the skill levels of the dogs. -----Output----- For each test case, print a single line containing one integer - the number of different results of soccer for dogs, modulo 109+7$10^9 + 7$. -----Constraints----- - 1≤T≤10$1 \le T \le 10$ - 1≤N≤105$1 \le N \le 10^5$ - 1≤Ai≤2$1 \le A_i \le 2$ for each valid i$i$ -----Subtasks----- Subtask #1 (10 points): N≤10$N \le 10$ Subtask #2 (30 points): N≤103$N \le 10^3$ Subtask #3 (60 points): original constraints -----Example Input----- 3 4 1 1 1 1 3 2 2 2 4 1 2 1 1 -----Example Output----- 4 5 6 -----Explanation----- Example case 1: The following results are possible: 1$1$, [1,2]$[1, 2]$, [1,2,3]$[1, 2, 3]$, [1,2,3,4]$[1, 2, 3, 4]$. Example case 2: The following results are possible: [1]$[1]$, [1,2]$[1, 2]$, [1,2,3]$[1, 2, 3]$, [1,3,2]$[1, 3, 2]$, [1,3]$[1, 3]$.
["for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdp[0]=1\r\n\tans=1\r\n\tj=0\r\n\tfor i in range(1,num):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]%1000000007\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]%1000000007\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]%1000000007\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\tif i>=j or j==0:\r\n\t\t\t\tj=i+1\r\n\t\t\twhile j<num and arr[j]==2:\r\n\t\t\t \tj+=1\r\n\t\t\tcount=j-i\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==num:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)%1000000007\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)%1000000007\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\tprint(ans%1000000007)\r\n\r\n\r\n", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdpr=[0]*num\r\n\tdpt=[0]*num\r\n\tdp[0]=1\r\n\tdpr[0]=0\r\n\tans=1\r\n\tj=0\r\n\tfor i in range(1,num):\r\n\t\tdp[i]=dp[i-1]%1000000007\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]%1000000007\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]%1000000007\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\tif i>=j or j==0:\r\n\t\t\t\tj=i+1 \r\n\t\t\t\twhile j<num and arr[j]==2:\r\n\t\t\t\t\tj+=1\r\n\t\t\tcount=j-i\r\n\t\t\tif j==num:\r\n\t\t\t\tdpr[i]=dp[i-1]*(count-1)%1000000007\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tdpr[i]=dp[i-1]*(count+1)%1000000007\r\n\t\t\t\telse:\r\n\t\t\t\t\tdpr[i]=dp[i-1]*(count)%1000000007\r\n\t\t\telif count%2==0:\r\n\t\t\t\tdpr[i]=dp[i-1]*(count-1)%1000000007\r\n\t\tans+=(dpr[i]+dp[i])%1000000007\r\n\tprint(ans%1000000007)\r\n\r\n\r\n\r\n\r\n\t\t\t", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdp[0]=1\r\n\tans=1\r\n\tj=0\r\n\tfor i in range(1,num):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]%1000000007\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\tif i>=j or j==0:\r\n\t\t\t\tj=i+1\r\n\t\t\twhile j<num and arr[j]==2:\r\n\t\t\t \tj+=1\r\n\t\t\tcount=j-i\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==num:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)%1000000007\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)%1000000007\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\tprint(ans%1000000007)\r\n\r\n\r\n", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdp[0]=1\r\n\tans=1\r\n\tfor i in range(1,num):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]%1000000007\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==num:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)%1000000007\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)%1000000007\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\tprint(ans%1000000007)\r\n\r\n\r\n\r\n\r\n\t\t\t", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdp[0]=1\r\n\tans=1\r\n\tfor i in range(1,num):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]%1000000007\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==num:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)%1000000007\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)%1000000007\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\tprint(ans)\r\n\r\n\r\n\r\n\r\n\t\t\t", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdp[0]=1\r\n\tans=1\r\n\tfor i in range(1,num):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]%1000000007\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==num:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)%1000000007\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)%1000000007\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)%1000000007\r\n\tprint(ans%1000000007)\r\n\r\n\r\n\r\n\r\n\t\t\t", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*num\r\n\tdp[0]=1\r\n\tans=1\r\n\tfor i in range(1,num):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]\r\n\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==num:\r\n\t\t\t\tans+=dp[i-1]*(count-1)\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)\r\n\tprint(ans)\r\n\r\n\r\n\r\n\r\n\t\t\t", "for _ in range(int(input())):\r\n\tnum=int(input())\r\n\tarr=list(map(int,input().split()))\r\n\tdp=[0]*len(arr)\r\n\tdpr=[0]*len(arr)\r\n\tdpt=[0]*len(arr)\r\n\tdp[0]=1\r\n\tdpr[0]=0\r\n\tans=1\r\n\tfor i in range(1,len(arr)):\r\n\t\tj=i+1\r\n\t\tcount=1\r\n\t\tdp[i]=dp[i-1]\r\n\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\tdp[i]+=dp[i-2]\r\n\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\tdp[i]+=dp[i-3]\r\n\t\tans+=dp[i]\r\n\t\tif arr[i-1]==2 and i<len(arr)-1:\r\n\t\t\twhile j<len(arr) and arr[j]==2:\r\n\t\t\t\tj+=1\r\n\t\t\t\tcount+=1\r\n\t\t\tif j==len(arr):\r\n\t\t\t\tans+=dp[i-1]*(count-1)\r\n\t\t\telif count%2!=0:\r\n\t\t\t\tif j<len(arr)-1 and arr[j+1]==2:\r\n\t\t\t\t\tans+=dp[i-1]*(count+1)\r\n\t\t\t\telse:\r\n\t\t\t\t\tans+=dp[i-1]*(count)\r\n\t\t\telif count%2==0:\r\n\t\t\t\tans+=dp[i-1]*(count-1)\r\n\tprint(ans)\r\n\r\n\r\n\r\n\r\n\t\t\t", "try:\r\n\tfor _ in range(int(input())):\r\n\t\tnum=int(input())\r\n\t\tarr=list(map(int,input().split()))\r\n\t\tdp=[0]*num\r\n\t\tdpr=[0]*num\r\n\t\tdpt=[0]*num\r\n\t\tdp[0]=1\r\n\t\tdpr[0]=0\r\n\t\tans=1\r\n\t\tfor i in range(1,num):\r\n\t\t\tj=i+1\r\n\t\t\tcount=1\r\n\t\t\tdp[i]=dp[i-1]\r\n\t\t\tif i-2>=0 and arr[i-2]==2:\r\n\t\t\t\tdp[i]+=dp[i-2]\r\n\t\t\t\tif i-3>=0 and arr[i-3]==2:\r\n\t\t\t\t\tdp[i]+=dp[i-3]\r\n\t\t\tif arr[i-1]==2 and i<num-1:\r\n\t\t\t\twhile j<num and arr[j]==2:\r\n\t\t\t\t\tj+=1\r\n\t\t\t\t\tcount+=1\r\n\t\t\t\tif j==num:\r\n\t\t\t\t\tdpr[i]=dp[i-1]*(count-1)\r\n\t\t\t\telif count%2!=0:\r\n\t\t\t\t\tif j<num-1 and arr[j+1]==2:\r\n\t\t\t\t\t\tdpr[i]=dp[i-1]*(count+1)\r\n\t\t\t\t\telse:\r\n\t\t\t\t\t\tdpr[i]=dp[i-1]*(count)\r\n\t\t\t\telif count%2==0:\r\n\t\t\t\t\tdpr[i]=dp[i-1]*(count-1)\r\n\t\t\tans+=dpr[i]+dp[i]\r\n\t\tprint(ans)\r\nexcept:\r\n\tpass\r\n\r\n\r\n\r\n\t\t\t", "# cook your dish here\r\nimport sys\r\nsys.setrecursionlimit(10**8)\r\ntry:\r\n for _ in range(int(input())):\r\n class mat(object):\r\n limit=[]\r\n arr=[]\r\n num=0\r\n ans=0\r\n def passes(n,m):\r\n if m>mat.num:\r\n return\r\n mat.ans+=1\r\n mat.limit.append(m)\r\n for i in range(1,mat.arr[m]+1):\r\n if m-i>0 and n<mat.num-1 and not(m-i in mat.limit) :\r\n passes(n+1,m-i)\r\n if m+i<mat.num and n<mat.num-1 and not(m+i in mat.limit):\r\n passes(n+1,m+i)\r\n mat.limit.pop()\r\n mat.num=int(input())\r\n mat.arr=[]*mat.num\r\n mat.arr=list(map(int,input().split()))\r\n passes(0,0)\r\n print(mat.ans)\r\n \r\nexcept:\r\n pass", "# cook your dish here\r\ntry:\r\n for _ in range(int(input())):\r\n class mat(object):\r\n game=[]\r\n limit=[]\r\n arr=[]\r\n num=0\r\n ans=0\r\n def passes(n,m):\r\n if m>mat.num:\r\n return\r\n mat.ans+=1\r\n mat.limit.append(m)\r\n for i in range(1,mat.arr[m]+1):\r\n if m-i>0 and n<mat.num-1 and not(m-i in mat.limit) :\r\n passes(n+1,m-i)\r\n if m+i<mat.num and n<mat.num-1 and not(m+i in mat.limit):\r\n passes(n+1,m+i)\r\n mat.limit.pop()\r\n mat.num=int(input())\r\n mat.arr=[]*mat.num\r\n mat.arr=list(map(int,input().split()))\r\n passes(0,0)\r\n print(mat.ans)\r\n \r\nexcept:\r\n pass", "# cook your dish here\ntry:\n for _ in range(int(input())):\n class mat(object):\n \tgame=[]\n \tlimit=[]\n def see(key):\n \tfor i in range(len(mat.limit)-1,-1,-1):\n \t\tif mat.limit[i]==key:\n \t\t\treturn False\n \treturn True\n def passes(n,m,arr):\n \tif m>len(arr) or n>len(arr):\n \t\treturn\n \tmat.game[n][m]+=1\n \tmat.limit.append(m)\n \tfor i in range(1,arr[m]+1):\n \t\tif m-i>0 and n<len(arr)-1 and not(m-i in mat.limit) :\n \t\t\tpasses(n+1,m-i,arr)\n \t\tif m+i<len(arr) and n<len(arr)-1 and not(m+i in mat.limit):\n \t\t\tpasses(n+1,m+i,arr)\n \tmat.limit.pop()\n n=int(input())\n arr=list(map(int,input().split()))\n mat.game=[[0]*(len(arr)) for _ in range(len(arr))]\n passes(0,0,arr)\n ans=0\n for i in mat.game:\n \tans+=sum(i)\n print(ans)\n \nexcept:\n pass", "for _ in range(int(input())):\r\n n=int(input())\r\n p=[int(o) for o in input().split()]\r\n s=[0]*n\r\n pr=[0]*n\r\n s[n-1]=1\r\n pr[n-1]=0\r\n if n-2>=0:\r\n s[n-2]=2\r\n pr[n-2]=1\r\n if n-3>=0:\r\n s[n-3]=3\r\n pr[n-3]=1\r\n if p[n-3]==2:\r\n s[n-3]+=2\r\n pr[n-3]+=1\r\n i=n-4\r\n while i>=0:\r\n s[i]=1\r\n s[i]+=s[i+1]\r\n pr[i]=1\r\n if p[i]==2:\r\n s[i]+=s[i+2]\r\n s[i]+=1\r\n pr[i]+=1\r\n if p[i+3]==2:\r\n s[i]+=pr[i+2]\r\n pr[i]+=pr[i+2]\r\n if p[i+1]==2:\r\n s[i]+=s[i+3]\r\n s[i]=s[i]%1000000007\r\n pr[i]=pr[i]%1000000007\r\n i-=1\r\n print(s[0])", "import math;\r\nfrom math import log2,sqrt;\r\nimport sys;\r\nsys.setrecursionlimit(pow(10,6))\r\nimport collections\r\nfrom collections import defaultdict\r\nfrom statistics import median\r\ninf = float(\"inf\")\r\nmod=pow(10,9)+7\r\ndef give(l,n,index,visited):\r\n if index<0 or index>=n:\r\n return 0;\r\n if visited[index]==1:\r\n return 0;\r\n visited[index]=1;\r\n theta=1\r\n # print(\"index is\",index)\r\n for i in graph[index]:\r\n theta+=give(l,n,i,visited.copy())\r\n\r\n return theta;\r\n\r\nfor i in range(int(input())):\r\n n=int(input())\r\n l=list(map(int,input().split()))\r\n visited=defaultdict(int)\r\n graph=defaultdict(set)\r\n for i in range(len(l)):\r\n if l[i]==1:\r\n graph[i].add(i-1)\r\n graph[i].add(i+1)\r\n else:\r\n graph[i].add(i-2)\r\n graph[i].add(i-1)\r\n graph[i].add(i+1)\r\n graph[i].add(i+2)\r\n theta=give(l,n,0,visited.copy())\r\n\r\n print(theta)\r\n\r\n\r\n\r\n", "# cook your dish here\n# cook your dish here\ndef array1(size):\n return [0 for _ in range(size)]\n\n\ndef array2(rows, cols):\n return [[0 for _ in range(cols)] for _ in range(rows)]\n\n\nt = int(input())\nfor _ in range(t):\n n = int(input())\n a = list(map(int, input().split()))\n no_of_twos = array1(n + 1)\n for i in range(n - 1, -1, -1):\n if a[i] == 1:\n no_of_twos[i] = 0\n else:\n no_of_twos[i] += 1\n no_of_twos[i] += no_of_twos[i + 1]\n dp = array1(n + 1)\n dp[0] = 1\n ans = 1\n for i in range(1, n):\n if i - 1 >= 0:\n dp[i] += dp[i - 1]\n if i - 2 >= 0 and a[i - 2] == 2:\n dp[i] += dp[i - 2]\n if i - 3 >= 0 and a[i - 3] == 2 and a[i - 2] == 2:\n dp[i] += dp[i - 3]\n dp[i] = dp[i] % 1000000007\n ans += dp[i]\n x = no_of_twos[i + 1]\n if i + 1 < n and a[i - 1] == 2:\n ans += (x * dp[i - 1])\n if x % 2 == 0:\n if i + x + 1 < n:\n ans += dp[i - 1]\n if i + x + 2 < n and a[i + x + 2] == 2:\n ans += dp[i - 1]\n ans = ans % 1000000007\n print(ans % 1000000007)\n", "# cook your dish here\ndef array1(size):\n return [0 for _ in range(size)]\n\n\ndef array2(rows, cols):\n return [[0 for _ in range(cols)] for _ in range(rows)]\n\n\nt = int(input())\nfor _ in range(t):\n n = int(input())\n a = list(map(int, input().split()))\n no_of_twos = array1(n + 1)\n for i in range(n - 1, -1, -1):\n if a[i] == 1:\n no_of_twos[i] = 0\n else:\n no_of_twos[i] += 1\n no_of_twos[i] += no_of_twos[i + 1]\n dp = array1(n + 1)\n dp[0] = 1\n ans = 1\n for i in range(1, n):\n if i - 1 >= 0:\n dp[i] += dp[i - 1]\n if i - 2 >= 0 and a[i - 2] == 2:\n dp[i] += dp[i - 2]\n if i - 3 >= 0 and a[i - 3] == 2 and a[i - 2] == 2:\n dp[i] += dp[i - 3]\n dp[i] = dp[i] % 1000000007\n ans += dp[i]\n x = no_of_twos[i + 1]\n if i + 1 < n and a[i - 1] == 2:\n ans += (x * dp[i - 1])\n if x % 2 == 0:\n if i + x + 1 < n:\n ans += dp[i - 1]\n if i + x + 2 < n and a[i + x + 2] == 2:\n ans += dp[i - 1]\n ans = ans % 1000000007\n print(ans % 1000000007)\n", "def array1(size):\r\n return [0 for _ in range(size)]\r\n\r\n\r\ndef array2(rows, cols):\r\n return [[0 for _ in range(cols)] for _ in range(rows)]\r\n\r\n\r\nt = int(input())\r\nfor _ in range(t):\r\n n = int(input())\r\n a = list(map(int, input().split()))\r\n no_of_twos = array1(n + 1)\r\n for i in range(n - 1, -1, -1):\r\n if a[i] == 1:\r\n no_of_twos[i] = 0\r\n else:\r\n no_of_twos[i] += 1\r\n no_of_twos[i] += no_of_twos[i + 1]\r\n dp = array1(n + 1)\r\n dp[0] = 1\r\n ans = 1\r\n for i in range(1, n):\r\n if i - 1 >= 0:\r\n dp[i] += dp[i - 1]\r\n if i - 2 >= 0 and a[i - 2] == 2:\r\n dp[i] += dp[i - 2]\r\n if i - 3 >= 0 and a[i - 3] == 2 and a[i - 2] == 2:\r\n dp[i] += dp[i - 3]\r\n dp[i] = dp[i] % 1000000007\r\n ans += dp[i]\r\n x = no_of_twos[i + 1]\r\n if i + 1 < n and a[i - 1] == 2:\r\n ans += (x * dp[i - 1])\r\n if x % 2 == 0:\r\n if i + x + 1 < n:\r\n ans += dp[i - 1]\r\n if i + x + 2 < n and a[i + x + 2] == 2:\r\n ans += dp[i - 1]\r\n ans = ans % 1000000007\r\n print(ans % 1000000007)\r\n", "for i in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n dp=[0]*(n+3)\n dp[0]=1\n for i in range(1,n):\n dp[i]=dp[i-1]\n if i-2>=0 and a[i-2]==2:dp[i]+=dp[i-2]\n if i-3>=0 and a[i-3]==2 and a[i-2]==2:dp[i]+=dp[i-3]\n dpr=[0]*(n+3)\n i1=[0]*n\n i2=-1\n for i in range(n-1,-1,-1):\n i1[i]=i2\n if a[i]==1:i2=i\n for i in range(1,n-1):\n if a[i-1]==1:continue\n if i1[i]==-1:\n i2=n-i-1\n if i2%2:z=2*(i2//2)+1\n else:z=i2\n else:\n i2=i1[i]-i-1\n if not i2%2:\n z=i2+1\n if i1[i]+1 <n and a[i1[i]+1]==2:z+=1\n else:z=2*(i2//2)+1\n dpr[i]=z*dp[i-1]\n c=0\n for i in range(n):c+=dp[i]+dpr[i]\n x=7+pow(10,9)\n print(c%x)", "# cook your dish here\ndef f(x,ud,li):\n if not ud:return 1\n m=1\n if x+1 in ud:\n ud1=ud[:]\n ud.remove(x+1)\n m+=f(x+1,ud,li)\n ud=ud1[:]\n if x-1 in ud:\n ud1=ud[:]\n ud.remove(x-1)\n m+=f(x-1,ud,li)\n ud=ud1[:]\n if li[x]==2:\n if x+2 in ud:\n ud1=ud[:]\n ud.remove(x+2)\n m+=f(x+2,ud,li)\n ud=ud1[:]\n if x-2 in ud:\n ud1=ud[:]\n ud.remove(x-2)\n m+=f(x-2,ud,li)\n ud=ud1[:]\n return m\nfor i in range(int(input())):\n n=int(input())\n a=list(map(int,input().split()))\n ud=[]\n for i in range(1,n):ud.append(i)\n c=f(0,ud,a)\n x=7+pow(10,9)\n print(c%x)", "for _ in range(int(input())):\r\n n=int(input())\r\n p=[int(o) for o in input().split()]\r\n s=[0]*n\r\n pr=[0]*n\r\n s[n-1]=1\r\n pr[n-1]=0\r\n if n-2>=0:\r\n s[n-2]=2\r\n pr[n-2]=1\r\n if n-3>=0:\r\n s[n-3]=3\r\n pr[n-3]=1\r\n if p[n-3]==2:\r\n s[n-3]+=2\r\n pr[n-3]+=1\r\n i=n-4\r\n while i>=0:\r\n s[i]=1\r\n s[i]+=s[i+1]\r\n pr[i]=1\r\n if p[i]==2:\r\n s[i]+=s[i+2]\r\n s[i]+=1\r\n pr[i]+=1\r\n if p[i+3]==2:\r\n s[i]+=pr[i+2]\r\n pr[i]+=pr[i+2]\r\n if p[i+1]==2:\r\n s[i]+=s[i+3]\r\n s[i]=s[i]%1000000007\r\n pr[i]=pr[i]%1000000007\r\n i-=1\r\n print(s[0])", "for _ in range(int(input())):\r\n n=int(input())\r\n p=[int(o) for o in input().split()]\r\n s=[0]*n\r\n pr=[0]*n\r\n s[n-1]=1\r\n pr[n-1]=0\r\n if n-2>=0:\r\n s[n-2]=2\r\n pr[n-2]=1\r\n if n-3>=0:\r\n s[n-3]=3\r\n pr[n-3]=1\r\n if p[n-3]==2:\r\n s[n-3]+=2\r\n pr[n-3]+=1\r\n i=n-4\r\n while i>=0:\r\n s[i]=1\r\n s[i]+=s[i+1]\r\n pr[i]=1\r\n if p[i]==2:\r\n s[i]+=s[i+2]\r\n s[i]+=1\r\n pr[i]+=1\r\n if p[i+3]==2:\r\n s[i]+=pr[i+2]\r\n pr[i]+=pr[i+2]\r\n if p[i+1]==2:\r\n s[i]+=s[i+3]\r\n s[i]=s[i]%1000000007\r\n pr[i]=pr[i]%1000000007\r\n i-=1\r\n print(s[0])", "for _ in range(int(input())):\r\n n=int(input())\r\n p=[int(o) for o in input().split()]\r\n s=[0]*n\r\n pr=[0]*n\r\n s[n-1]=1\r\n pr[n-1]=0\r\n if n-2>=0:\r\n s[n-2]=2\r\n pr[n-2]=1\r\n if n-3>=0:\r\n s[n-3]=3\r\n pr[n-3]=1\r\n if p[n-3]==2:\r\n s[n-3]+=2\r\n pr[n-3]+=1\r\n i=n-4\r\n while i>=0:\r\n s[i]=1\r\n s[i]+=s[i+1]\r\n pr[i]=1\r\n if p[i]==2:\r\n s[i]+=s[i+2]\r\n s[i]+=1\r\n pr[i]+=1\r\n if p[i+3]==2:\r\n s[i]+=pr[i+2]\r\n pr[i]+=pr[i+2]\r\n if p[i+1]==2:\r\n s[i]+=s[i+3]\r\n s[i]=s[i]%1000000007\r\n pr[i]=pr[i]%1000000007\r\n i-=1\r\n print(s[0])"]
{"inputs": [["3", "4", "1 1 1 1", "3", "2 2 2", "4", "1 2 1 1"]], "outputs": [["4", "5", "6"]]}
INTERVIEW
PYTHON3
CODECHEF
20,256
575aadb3a23f45487a7445fe10e31084
UNKNOWN
There are N islands in the sea, enumerated from 1 to N. Each of them is so small that we can consider them as points on a plane. You are given the Cartesian coordinates of all islands. X-axis is directed towards East and Y-axis is directed towards North. You need to illuminate all the islands. To do this, you can place lighthouses on some of the islands. You can't place more than one lighthouse on any single island. Each lighthouse can light only one of the 4 quadrants: North-Western, North-Eastern, South-Western or South-Eastern. If some island is located on the border of an illuminated quadrant, it is considered to be illuminated as well. Note that this means that a lighthouse always illuminates it's own island as well. Find the smallest possible number of lighthouses required to illuminate all the islands (say L). Describe their configurations — positions and quadrants illuminated — as well. -----Input----- The first line of 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 integer N denoting the number of islands. The ith of the following N lines contains two integers Xi and Yi denoting the coordinates of the ith island. -----Output----- For each test case, first line of output must contain minimum number of lighthouses required to illuminate all islands, L. Following L lines must describe configuration of the lighthouses, one configuration per line. Description of a lighthouse configuration consists of the number of the island where the lighthouse is placed, and the direction of the quadrant (NW for North-Western, NE for North-Eastern, SW for South-Western, SE for South-Eastern) illuminated by it, separated by a single space. If there are many possible placements, output any one of them. -----Constraints----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 105 - The sum of N over all test cases doesn't exceed 5*105 - Absolute value of each coordinate doesn't exceed 109 - No two given islands coincide. -----Subtasks----- Subtask 1: (15 points) - 1 ≤ N ≤ 8 - Absolute value of each coordinate doesn't exceed 50 Subtask 2: (85 points) - Original constraints -----Example----- Input: 2 5 0 0 1 0 2 0 0 -1 0 -2 4 5 0 -5 0 0 5 0 -5 Output: 1 3 SW 2 4 NE 2 NE -----Explanation----- Example case 1. Also we can place lighthouse on 1st or 5th island. Example case 2. Notice that some islands can be illuminated by more than 1 lighthouse.
["t= int(input())\nfor _ in range(t):\n n = int(input())\n ar = []\n y = []\n for i in range(n):\n ar.append( list(map(int,input().split())) )\n y.append(ar[-1][1])\n ar[-1].append(i)\n \n y.sort()\n mny = y[0]\n mxy = y[-1]\n ar.sort()\n ssx,ssy,ssi = ar[0]\n bbx,bby,bbi = ar[-1]\n \n sbx,sby,sbi = ar[0]\n bsx,bsy,bsi = ar[-1]\n \n for i in range(len(ar)):\n if ar[i][0]>ssx:\n sbx,sby,sbi = ar[i-1]\n break\n \n for i in range(len(ar)-1,-1,-1):\n if ar[i][0]<bsx:\n bsx,bsy,bsi = ar[i+1]\n break \n \n if (ssy <=mny):\n print(1)\n print(ssi+1,'NE')\n continue\n if (sby>=mxy):\n print(1)\n print(sbi+1,'SE')\n continue\n if (bsy <=mny):\n print(1)\n print(bsi+1,'NW')\n continue\n if (bby>=mxy):\n print(1)\n print(bbi+1,'SW')\n continue \n \n print(2)\n if(ssy<bby):\n print(ssi+1,'NE')\n print(bbi+1,'SW')\n else:\n print(ssi+1,'SE')\n print(bbi+1,'NW')\n \n", "for cas in range(eval(input())):\n n = eval(input())\n pts = []\n for i in range(n):\n x, y = list(map(int, input().strip().split()))\n pts.append((x, y))\n\n minx = min(x for x, y in pts)\n maxx = max(x for x, y in pts)\n miny = min(y for x, y in pts)\n maxy = max(y for x, y in pts)\n\n for i in range(n):\n if pts[i] == (minx, miny):\n print(\"1\\n%d NE\" % (i+1))\n break\n if pts[i] == (minx, maxy):\n print(\"1\\n%d SE\" % (i+1))\n break\n if pts[i] == (maxx, miny):\n print(\"1\\n%d NW\" % (i+1))\n break\n if pts[i] == (maxx, maxy):\n print(\"1\\n%d SW\" % (i+1))\n break\n else:\n mini = min(list(range(n)), key=lambda i: pts[i]) #vvi: finding \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0minium in set of 0 to n-1 based comparison of key which takes pts[i] \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0through lambda\n maxi = max(list(range(n)), key=lambda i: pts[i])\n \n if pts[mini][1] < pts[maxi][1]:\n print(\"2\\n%d NE\\n%d SW\" % (mini+1, maxi+1))\n else:\n print(\"2\\n%d SE\\n%d NW\" % (mini+1, maxi+1))\n", "for cas in range(eval(input())):\n n = eval(input())\n pts = []\n for i in range(n):\n x, y = list(map(int, input().strip().split()))\n pts.append((x, y))\n\n minx = min(x for x, y in pts)\n maxx = max(x for x, y in pts)\n miny = min(y for x, y in pts)\n maxy = max(y for x, y in pts)\n\n for i in range(n):\n if pts[i] == (minx, miny):\n print(\"1\\n%d NE\" % (i+1))\n break\n if pts[i] == (minx, maxy):\n print(\"1\\n%d SE\" % (i+1))\n break\n if pts[i] == (maxx, miny):\n print(\"1\\n%d NW\" % (i+1))\n break\n if pts[i] == (maxx, maxy):\n print(\"1\\n%d SW\" % (i+1))\n break\n else:\n mini = min(range(n), key=lambda i: pts[i])\n maxi = max(range(n), key=lambda i: pts[i])\n\n if pts[mini][1] < pts[maxi][1]:\n print(\"2\\n%d NE\\n%d SW\" % (mini+1, maxi+1))\n else:\n print(\"2\\n%d SE\\n%d NW\" % (mini+1, maxi+1))", "class island:\n 'number, x, y' \n def __init__(self, x,y, number):\n self.x = x\n self.y = y\n self.number = number\n\ndef onelighthouseenough(islands, leftbottom):\n bottomleft = min(islands, key = lambda a : (a.y, a.x))\n\n if(leftbottom == bottomleft):\n print(1)\n print(leftbottom.number, end=' ')\n print(' NE')\n return True\n\n rightbottom = min(islands, key = lambda a : (-a.x, a.y))\n bottomright = min(islands, key = lambda a : (a.y, -a.x))\n\n if(rightbottom == bottomright):\n print(1)\n print(rightbottom.number, end=' ')\n print(' NW')\n return True\n\n lefttop = min(islands, key = lambda a : (a.x, -a.y))\n topleft = min(islands, key = lambda a : (-a.y, a.x))\n\n if(lefttop == topleft):\n print(1)\n print(lefttop.number, end=' ')\n print(' SE')\n return True\n\n righttop = min(islands, key = lambda a : (-a.x, -a.y))\n topright = min(islands, key = lambda a : (-a.y, -a.x))\n\n if(righttop == topright):\n print(1)\n print(righttop.number, end=' ')\n print(' SW')\n return True\n return False\n\ndef lighthouses(islands):\n leftbottom = min(islands, key = lambda a : (a.x, a.y))\n if(onelighthouseenough(islands, leftbottom)):\n return\n\n islands.remove(leftbottom)\n nextleft = min(islands, key = lambda a : a.x)\n\n if(leftbottom.y < nextleft.y):\n print(2)\n print(leftbottom.number, end=' ')\n print(' NE')\n print(nextleft.number, end=' ')\n print(' SE')\n else:\n print(2)\n print(leftbottom.number, end=' ')\n print(' SE')\n print(nextleft.number, end=' ')\n print(' NE')\n\n\nT = int(input())\n\nfor i in range(T):\n N = int(input())\n islands = []\n for j in range(N):\n x, y = list(map(int, input().split()))\n islands.append(island(x,y, j+1))\n lighthouses(islands)\n \n", "class island:\n 'number, x, y' \n def __init__(self,x,y,number):\n self.x = x\n self.y = y\n self.number = number\n\ndef lighthouses(islands):\n low = high = left = right = islands[0]\n\n left = min(islands, key = lambda a : a.x)\n right= max(islands, key = lambda a : a.x)\n low = min(islands, key = lambda a : a.y)\n high = max(islands, key = lambda a : a.y)\n\n if left == low:\n print(1)\n print(str(left.number) + ' NE')\n elif left == high:\n print(1)\n print(str(left.number) + ' SE')\n elif right == low:\n print(1)\n print(str(right.number) + ' NW')\n elif right == high:\n print(1)\n print(str(right.number) + ' SW')\n else:\n islands.remove(left)\n nextleft = min(islands, key = lambda a : a.x)\n \n if left.y < nextleft.y:\n print(2)\n print(str(left.number) + ' NE')\n print(str(nextleft.number) + ' SE')\n else:\n print(2)\n print(str(left.number) + ' SE')\n print(str(nextleft.number) + ' NE')\n\n\nT = int(input())\nfor i in range(T):\n N = int(input())\n islands = []\n for j in range(N):\n x, y = list(map(int, input().split()))\n islands.append(island(x,y,j+1))\n lighthouses(islands)\n", "class island:\n 'number, x, y' \n def __init__(self,x,y,number):\n self.x = x\n self.y = y\n self.number = number\n\ndef lighthouses(islands):\n low = high = left = right = islands[0]\n\n left = min(islands, key = lambda a : a.x)\n right= max(islands, key = lambda a : a.x)\n low = min(islands, key = lambda a : a.y)\n high = max(islands, key = lambda a : a.y)\n\n if left == low:\n print(1)\n print(str(left.number) + ' NE')\n elif left == high:\n print(1)\n print(str(left.number) + ' SE')\n elif right == low:\n print(1)\n print(str(right.number) + ' NW')\n elif right == high:\n print(1)\n print(str(right.number) + ' SW')\n else:\n islands.remove(left)\n nextleft = min(islands, key = lambda a : a.x)\n \n if left.y < nextleft.y:\n print(2)\n print(str(left.number) + ' NE')\n print(str(nextleft.number) + ' SE')\n else:\n print(2)\n print(str(left.number) + ' SE')\n print(str(nextleft.number) + ' NE')\n\n\nT = int(input())\nfor i in range(T):\n N = int(input())\n islands = []\n for j in range(N):\n x, y = list(map(int, input().split()))\n islands.append(island(x,y,j+1))\n lighthouses(islands)\n", "t=int(input())\n\nans=[]\nwhile(t):\n X=[];\n Y=[];\n XY=[]\n \n t-=1;\n n=int(input());\n st='';\n for i in range(1,n+1):\n xi,yi=list(map(int,input().split()));\n X.append(xi);\n Y.append(yi);\n XY.append((xi,yi));\n lborder=X.index(min(X));\n rborder=X.index(max(X));\n tborder=Y.index(max(Y));\n bborder=Y.index(min(Y));\n if((X[lborder],Y[tborder])in XY):\n st='1\\n'+str(XY.index((X[lborder],Y[tborder]))+1)+' SE';\n elif((X[lborder],Y[bborder])in XY):\n st='1\\n'+str(XY.index((X[lborder],Y[bborder]))+1)+' NE';\n elif((X[rborder],Y[tborder])in XY):\n st='1\\n'+str(XY.index((X[rborder],Y[tborder]))+1)+' SW';\n elif((X[rborder],Y[bborder])in XY):\n st='1\\n'+str(XY.index((X[rborder],Y[bborder]))+1)+' NW';\n elif((rborder==lborder)):\n st='1\\n'+str(tborder+1)+' SE';\n elif(tborder==bborder):\n st='1\\n'+str(lborder+1)+' NE';\n else:\n if(Y[lborder]>=Y[rborder]):\n st='2\\n'+str(lborder+1)+' SE\\n'+str(rborder+1)+' NW';\n else:\n st='2\\n'+str(lborder+1)+' NE\\n'+str(rborder+1)+' SW';\n\n\n ans.append(st);\nfor x in ans:\n print(x)\n"]
{"inputs": [["2", "5", "0 0", "1 0", "2 0", "0 -1", "0 -2", "4", "5 0", "-5 0", "0 5", "0 -5"]], "outputs": [["1", "3 SW", "2", "4 NE", "2 NE"]]}
INTERVIEW
PYTHON3
CODECHEF
7,957
d2ebcb3a11a9e051e7bb720bbdaa6d71
UNKNOWN
Given an integer N, Chef wants to find the smallest positive integer M such that the bitwise XOR of M and M+1 is N. If no such M exists output -1. -----Input----- The first line of input contain an integer T denoting the number of test cases. Each of the following T lines contains an integer N for that test case. -----Output----- For each test case, output a single line containing the number M or -1 as described above. -----Constraints----- - 1 ≤ T ≤ 5000 - 1 ≤ N ≤ 230 -----Example----- Input: 1 3 Output: 1 -----Explanation-----First Example : M desired in the problem would be 1. As bitwise XOR of 1 and 2 is equal to 3.
["# from math import log2\n# N = 10000\n# for i in range(1,N):\n# # print(i)\n# for m in range(i):\n# if( (m^(m+1))==i ):\n# print(i)\n# print(m,m+1,bin(m)[2:])\n# print()\n# break\n# # else:\n# # print(-1)\n# # print()\nT = int(input())\nans = []\n\nfor _ in range(T):\n N = int(input())\n\n # x = log2(N+1)\n if(N==1):\n ans.append(2)\n elif('0' not in bin(N)[2:]):\n ans.append(N//2)\n else:\n ans.append(-1)\n\nfor i in ans:\n print(i)", "# cook your dish here\nt=int(input())\ns=[]\nfor i in range(31):\n s.append(2**i-1)\nfor _ in range(t):\n \n n=int(input())\n \n if n==1:\n print(2)\n else:\n if n in s:\n print(n//2)\n else:\n print(-1)", "s = set()\nfor i in range(31):\n s.add(2**i - 1)\n\nfor _ in range(int(input())):\n n = int(input())\n if n==1:\n print(2)\n else:\n if n in s:\n print(n//2)\n else:print(-1)", "for _ in range(int(input())):\n n = int(input())\n if n==1:\n print(2)\n else:\n if (n+1)&n==0:\n print(n//2)\n else:print(-1)", "# cook your dish here\nimport math\nfor _ in range(int(input())):\n n=int(input())\n if n==1:\n print(2)\n continue\n temp=math.floor(math.log(n+1,2))\n if 2**temp==n+1:\n print((n+1)//2-1)\n else:\n print(-1)\n", "import math\nt=int(input())\nfor _ in range(t):\n n=int(input())\n \n if n==1:\n print(2)\n continue\n \n temp=math.floor(math.log(n+1,2))\n if 2**temp==n+1:\n print((n+1)//2-1)\n else:\n print(-1)\n", "# cook your dish here\ndef xor():\n n = int(input())\n a = bin(n)[2:]\n if a.count('0') == 0:\n if n == 1:\n print(2)\n else:\n print(n >> 1)\n else:\n print(-1)\n\n\ndef __starting_point():\n t = int(input())\n while t != 0:\n xor()\n t -= 1\n\n__starting_point()", "def solve():\n n = int(input())\n a = bin(n)[2:]\n if a.count('0') == 0:\n if n == 1:\n print(2)\n else:\n print(n >> 1)\n else:\n print(-1)\n\n\ndef __starting_point():\n t = int(input())\n while t != 0:\n solve()\n t -= 1\n\n__starting_point()", "for _ in range(int(input())):\n l=int(input())\n if l==1:\n print(2)\n continue\n t_1=list(bin(l)[2:])\n flag=0\n for i in range(len(t_1)-1,0,-1):\n if flag==1 and t_1[i]=='1':\n break\n if t_1[i]==t_1[i-1] and t_1[i]=='1':\n continue\n else:\n flag=1\n if flag==1:\n print(-1)\n continue\n t_2=t_1.count('1')\n t_3=((2**(t_2-1))-1)\n print(t_3)", "t=int(input())\nwhile(t>0):\n n=int(input())\n if(n==1):\n print(2)\n else:\n q=0\n for i in range(1,31):\n p=1<<i\n if(p^(p-1)==n):\n print(p-1)\n q=1\n break\n if(q==0):\n print(-1)\n t-=1", "# cook your dish here\nT=int(input())\nfor i in range(T):\n N=int(input())\n f=bin(N)[2:]\n if f.count('0')==0:\n if N==1:\n print(2)\n else:\n print(N>>1)\n else:\n print(-1)", "test=int(input())\nfor _ in range(test):\n a=int(input())\n flag=0\n r=0\n if a==1:\n print('2')\n elif a&(a+1)==0:\n print(a//2)\n else:\n print('-1')", "for _ in range(int(input())):\n a = int(input())\n flag =0\n r=0\n if a == 1:\n print('2')\n elif a&(a+1) == 0:\n print(a//2)\n else:\n print('-1')", "for _ in range(int(input())):\n a = int(input())\n flag =0\n r=0\n if a == 1:\n print('2')\n elif a&(a+1) == 0:\n print(a//2)\n else:\n print('-1')", "import math \ndef Log2(x): \n if x == 0: \n return false; \n \n return (math.log10(x)/math.log10(2))\ndef isPowerOfTwo(n): \n return (math.ceil(Log2(n))==math.floor(Log2(n))); \n \nfor _ in range(int(input())):\n n=int(input())\n m=n+1\n if n==1:\n print(\"2\")\n else:\n if isPowerOfTwo(m):\n print(n//2)\n else:\n print(\"-1\")\n", "d = {1:0}\nst = 1\nprev = 1\nwhile True:\n new = st+st+1\n d[new] = prev\n prev = new\n st = new\n if st>2*(2**30):\n break\nfor _ in range(int(input())):\n n = int(input())\n if n == 1:\n print(2)\n else:\n try:\n print(d[n])\n except:\n print(-1)", "T = int(input())\nfor i in range(T):\n N = int(input())\n b = bin(N)[2:]\n \n if N == 1:\n print(2)\n else:\n if '0' in b:\n print(-1)\n else:\n print((N - 1) // 2)\n", "t = int(input())\nfor T in range(t):\n n = int(input())\n b = bin(n)[2:]\n \n if n==1:\n print(2)\n else:\n if '0' in b:\n print(-1)\n else:\n print((n-1)//2)\n", "# cook your dish here\nt = int(input())\nwhile t!=0:\n n = int(input())\n b = bin(n)[2:]\n \n if n==1:\n print(2)\n else:\n if '0' in b:\n print(-1)\n else:\n print((n-1)//2)\n t-=1", "for i in range(int(input())):\n N = int(input())\n binary = (bin(N)[2:])\n if N == 1 :\n print(2)\n else :\n if '0' in binary:\n print(-1)\n else:\n print((N-1)//2)", "for _ in range(int(input())):\n n=int(input())\n if(n==1):\n print(2)\n else:\n arr=[]\n\n i=0\n while(True):\n if(2**i<=n):\n arr.append(2**i)\n i+=1\n else:\n break\n\n flg=0\n for i in arr:\n if(i^(i-1)==n):\n flg=1\n print(i-1)\n break\n\n if(not flg):\n print(-1)\n", "for i in range(int(input())):\n n=int(input())\n t=-1\n for j in range(30):\n if n==2**j-1:\n t=j\n break\n if t==1:\n print(2)\n elif t==-1:\n print(-1)\n else:\n print(2**(t-1)-1)\n", "# cook your dish here\nfor _ in range(int(input())):\n \n n = int(input())\n if n==1:\n print('2')\n elif n%2==0:\n print(-1)\n else:\n x=bin(n)[2:].count('0')\n if x==0:\n ans=n//2\n print(ans)\n else:\n print(-1)\n"]
{"inputs": [["1", "3"]], "outputs": [["1"]]}
INTERVIEW
PYTHON3
CODECHEF
5,299
8d08598ea6226edd8f5cd4529cd7b9bd
UNKNOWN
In Chefland, types of ingredients are represented by integers and recipes are represented by sequences of ingredients that are used when cooking. One day, Chef found a recipe represented by a sequence $A_1, A_2, \ldots, A_N$ at his front door and he is wondering if this recipe was prepared by him. Chef is a very picky person. He uses one ingredient jar for each type of ingredient and when he stops using a jar, he does not want to use it again later while preparing the same recipe, so ingredients of each type (which is used in his recipe) always appear as a contiguous subsequence. Chef is innovative, too, so he makes sure that in each of his recipes, the quantity of each ingredient (i.e. the number of occurrences of this type of ingredient) is unique ― distinct from the quantities of all other ingredients. Determine whether Chef could have prepared the given recipe. -----Input----- - The first line of the input contains a single 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 integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing the string "YES" if the recipe could have been prepared by Chef or "NO" otherwise (without quotes). -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 10^3$ - $1 \le A_i \le 10^3$ for each valid $i$ -----Example Input----- 3 6 1 1 4 2 2 2 8 1 1 4 3 4 7 7 7 8 1 7 7 3 3 4 4 4 -----Example Output----- YES NO NO -----Explanation----- Example case 1: For each ingredient type, its ingredient jar is used only once and the quantities of all ingredients are pairwise distinct. Hence, this recipe could have been prepared by Chef. Example case 2: The jar of ingredient $4$ is used twice in the recipe, so it was not prepared by Chef.
["# cook your dish here\n \nfor __ in range(int(input())):\n n=int(input())\n arr=list(map(int,input().split()))\n d={}\n s=set()\n flag=0\n for i in range(n):\n if arr[i] in list(d.keys()):\n d[arr[i]]+=1\n else:\n d[arr[i]]=1\n curr_ele=arr[i]\n if (curr_ele in s) and arr[i-1]!=arr[i]:\n flag=1\n break\n else:\n s.add(arr[i])\n c=list(d.values())\n if len(c)!=len(set(c)):\n flag=1\n if flag==1:\n print(\"NO\")\n else:\n print(\"YES\")\n \n \n \n \n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n a=list(map(int,input().strip().split()))\n m={}\n for i in a:\n if(i not in m):\n m[i]=1\n else: m[i]+=1\n s= set()\n for i in m:\n s.add(m[i])\n \n if len(s)!=len(m):\n print(\"NO\")\n continue\n tt=1\n for i in range(n):\n flag=1\n for j in range(i+1,n):\n if(a[i]!=a[j]):\n flag=0;\n if flag==0 and a[i]==a[j]:\n print(\"NO\")\n tt=0\n break;\n if tt==0:\n break\n if tt==1:\n print(\"YES\")\n \n \n \n", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n arr = list(map(int,input().split()))\n\n d = {}\n\n i = 0\n\n flag = True\n while i<n:\n if arr[i] not in d:\n \n count = 1\n\n while i<n-1 and arr[i]==arr[i+1]:\n count+=1\n i+=1\n \n d[arr[i]] = count\n\n else:\n flag = False\n break\n\n i+=1\n \n # print(d,flag)\n\n if flag==False:\n print(\"NO\")\n else:\n t = list(d.values())\n\n if len(t)==len(set(t)):\n print(\"YES\")\n else:\n print(\"NO\")\n\n", "for _ in range(int(input())):\n n = int(input())\n arr = list(map(int,input().split()))\n\n d = {}\n\n i = 0\n\n flag = True\n while i<n:\n if arr[i] not in d:\n \n count = 1\n\n while i<n-1 and arr[i]==arr[i+1]:\n count+=1\n i+=1\n \n d[arr[i]] = count\n\n else:\n flag = False\n break\n\n i+=1\n \n # print(d,flag)\n\n if flag==False:\n print(\"NO\")\n else:\n t = list(d.values())\n\n if len(t)==len(set(t)):\n print(\"YES\")\n else:\n print(\"NO\")\n\n \n\n \n \n\n\n \n\n\n\n \n", "for t in range(int(input())):\n n = int(input())\n arr=list(map(int,input().split()))\n con=[]\n count=1\n c=[]\n i=0\n while i<n:\n while (i<n-1) and (arr[i]==arr[i+1]):\n i+=1\n count+=1\n if arr[i] in con or count in c:\n break\n con.append(arr[i])\n c.append(count)\n i+=1\n count=1\n \n if i==n :\n print(\"YES\")\n else:\n print(\"NO\")\n", "# cook your dish here\nfrom collections import Counter\ndef solve(arr):\n n = len(arr)\n d = Counter(arr)\n if len(d.values()) != len(set(d.values())):\n return False\n i = 0\n j = 0 \n while i<n and j<n:\n j = i + 1 \n while j<n and arr[j] == arr[i]:\n j = j + 1 \n if j - i != d[arr[i]]:\n return False\n i = j \n return True\n \nfor _ in range(int(input())):\n n = int(input())\n arr = [int(num) for num in input().split(' ')]\n ans = solve(arr)\n if ans == True:\n print('YES')\n else:\n print('NO')", "# cook your dish here\nt=int(input())\nwhile t:\n n=int(input())\n arr=list(map(int,input().split()))\n con=[]\n count=1\n c=[]\n i=0\n while i<n:\n while (i<n-1) and (arr[i]==arr[i+1]):\n i+=1\n count+=1\n if arr[i] in con or count in c:\n break\n con.append(arr[i])\n c.append(count)\n i+=1\n count=1\n \n if i==n :\n print(\"YES\")\n else:\n print(\"NO\")\n #print(*con)\n #print(*c)\n t-=1", "def check(l):\n c=[]\n s=list(set(l))\n for i in s:\n c.append(l.count(i))\n if len(c)==len(set(c)):\n return 1\n return 0\n \nfor _ in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n s=0\n if check(l)==0:\n print(\"NO\")\n continue\n for i in range(1,n):\n if l[i]!=l[i-1]:\n s+=1\n if s==len(set(l))-1:\n print(\"YES\")\n else:\n print(\"NO\")", "# your code goes here\ndef fun(l):\n t=[]\n s=list(set(l))\n for i in s:\n t.append(l.count(i))\n #print(t)\n #print(len(t),len(set(t)))\n if len(t)==len(set(t)):\n #print(1)\n return 1\n #print(0)\n return 0\n \nfor _ in range(int(input())):\n n=int(input())\n l=[int(i) for i in input().split()]\n s=0\n if fun(l)==0:\n print(\"NO\")\n else:\n for i in range(n-1):\n if l[i]!=l[i+1]:\n s+=1\n if s==len(set(l))-1:\n print(\"YES\")\n else:\n print(\"NO\")", "a = int(input())\nfor i in range(0, a):\n b = int(input())\n c = list(map(int, input().split()))\n n = [0 for k in range(0, 1000)]\n f1 = [0 for k in range(0, 1000)]\n f2 = [0 for k in range(0, 1000)]\n for l in range(0, b):\n f1[c[l] - 1] += 1\n bo = True\n for l in range(0, 1000):\n if(f1[l] > 0):\n f2[f1[l]] += 1\n if(f2[f1[l]] > 1):\n bo = False\n break\n if(bo):\n j = 1\n n[c[0] - 1] = 1\n while(j < b):\n if(c[j] == c[j-1]):\n j += 1\n elif(c[j] != c[j - 1] and n[c[j] - 1] != 1):\n n[c[j] - 1] = 1\n j+= 1\n else:\n print(\"NO\")\n break\n if(j == b):\n print(\"YES\")\n else:\n print(\"NO\")\n", "for _ in range(int(input())):\n n = int(input())\n inp = list(map(int, input().split()))\n ing = [inp[0]]\n count = []\n last = inp[0]\n ans = \"YES\"\n cnt = 1\n for i in range(1,n):\n if last == inp[i]:\n cnt += 1\n else:\n if cnt not in count and (inp[i] not in ing):\n count.append(cnt)\n cnt = 1\n last = inp[i]\n ing.append(last)\n else:\n ans = \"NO\"\n break\n\n if cnt in count:\n ans = \"NO\"\n print(ans)\n", "for _ in range(int(input())):\n n = int(input())\n val = [int(x) for x in input().split()]\n ing = [val[0]]\n count = []\n c = 1\n prev = val[0]\n ans = \"YES\"\n for i in val[1:]:\n if prev == i:\n c += 1\n else:\n if c not in count and (i not in ing):\n count.append(c)\n c = 1\n prev = i\n ing.append(prev)\n else:\n ans = \"NO\"\n break\n\n if c in count:\n ans = \"NO\"\n print(ans)", "for _ in range(int(input())):\n n = int(input())\n val = [int(x) for x in input().split()]\n ing = [val[0]]\n count = []\n c = 1\n prev = val[0]\n ans = \"YES\"\n for i in val[1:]:\n if prev == i:\n c += 1\n else:\n if c not in count and (i not in ing):\n count.append(c)\n c = 1\n prev = i\n ing.append(prev)\n else:\n ans = \"NO\"\n break\n\n if c in count:\n ans = \"NO\"\n print(ans)", "from collections import Counter\nfor _ in range(int(input())):\n n = int(input())\n arr = [int(x) for x in input().split()]\n lis = [arr[0]]\n lis += [arr[i] for i in range(1,n) if(arr[i-1]!=arr[i])]\n if(len(set(lis)) != len(lis)):\n print(\"NO\")\n else:\n c = dict(Counter(arr))\n if(len(set(c.values())) == len(c)):\n print(\"YES\")\n else:\n print(\"NO\")\n", "for _ in range(int(input())):\n n = int(input())\n l = list(map(int,input().split()))\n ing = [l[0]]\n count = []\n c = 1\n d = l[0]\n ans = \"YES\"\n for i in l[1:]:\n if d == i:\n c += 1\n else:\n if c not in count and (i not in ing):\n count.append(c)\n c = 1\n d = i\n ing.append(d)\n else:\n ans = \"NO\"\n break\n #print(ing,count)\n if c in count:\n ans = \"NO\"\n print(ans)", "def sol(n, l):\n no, oc, m, k = {}, {}, l[0], 1\n for i in range(1, n):\n if (l[i] != m):\n if m in no or k in oc: return \"NO\"\n else: no[m], oc[k] = True, True\n m, k = l[i], 1\n else: k += 1\n if m in no or k in oc: return \"NO\"\n return \"YES\"\n\nfor _ in range(int(input())):\n n = int(input())\n l = list(map(int, input().split()))\n print(sol(n, l))", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n ingredients=list(map(int,input().split(\" \")))\n ls=[ingredients[0]]\n count=[]\n index=[]\n c=1\n flag=1\n for i in range(1,n):\n if ingredients[i]==ls[-1]:\n c=c+1\n else:\n if ingredients[i] in ls:\n flag=0\n break\n index.append(i)\n ls.append(ingredients[i])\n if c in count:\n flag=0\n break\n count.append(c)\n c=1\n if c in count or flag==0:\n print(\"NO\")\n continue\n print(\"YES\")\n \n # for i in range(n):\n", "# cook your dish here\nt = int(input())\n\nfor _ in range(t):\n n = int(input())\n a = list(map(int , input().split(\" \")))\n \n d={}\n flag=0\n for i in range(n):\n if a[i] not in d:\n d[a[i]]=[i,1]\n elif a[i] in d and d[a[i]][0]+1==i:\n temp = d[a[i]][1]\n d[a[i]] = [i , temp+1]\n else:\n print(\"NO\")\n flag=1\n break\n \n # print(d)\n vals = list(d.values())\n dist=[]\n for i in vals:\n #print(i)\n dist.append(i[1])\n\n if flag==0:\n if len(dist)== len(set(dist)):\n print(\"YES\")\n else:\n print(\"NO\")", "# MOD = 1000000007\nfor _ in range(int(input())):\n # n,b,m = map(int,input().split())\n n = int(input())\n ings = list(map(int,input().split()))\n di_ings = {}\n last = 0\n possible = True\n for ing in ings :\n if ing in di_ings :\n if last == ing :\n di_ings[ing] += 1\n else :\n possible = False\n break\n else :\n di_ings[ing] = 1\n last = ing\n # print(di_ings)\n if possible and len(set(di_ings)) == len(set(di_ings.values())) :\n print('YES')\n else :\n print('NO')\n", "for _ in range(int(input())):\n n = int(input())\n arr = list(map(int, input().split()))\n \n count = []\n jars = [arr[0]]\n prev = arr[0]\n curr_count = 1\n flag = True\n for a in arr[1:]:\n if a != prev:\n if a in jars or curr_count in count:\n flag = False\n break\n jars.append(a)\n count.append(curr_count)\n curr_count = 1\n \n else:\n curr_count += 1\n prev = a\n \n if curr_count in count:\n flag = False\n \n print(\"YES\" if flag else \"NO\")", "import sys\ncases = int(sys.stdin.readline().rstrip())\nfor case in range(cases):\n N = int(sys.stdin.readline().rstrip())\n p = list(map(int,sys.stdin.readline().rstrip().split(\" \")))\n freq = [0]*(1001)\n freq[p[0]]+=1\n error = False\n for i in range(1,len(p)):\n if( freq[p[i]] > 0 and p [i-1] != p[i]):\n error = True\n break\n else :\n freq[p[i]] +=1\n # print(freq)\n if(error):\n print(\"NO\")\n else :\n ms = []\n error = False\n for q in freq:\n \n \n if q in ms and q !=0:\n error = True \n break\n ms.append(q) \n if(error):\n print(\"NO\")\n else :\n print(\"YES\")\n \n", "t = int(input())\n\nfor _ in range(t):\n n = int(input())\n \n arr = [int(x) for x in input().split()]\n freq = {}\n freq[arr[0]] = 1\n \n flag = 0\n \n for i in range(1, n):\n if(arr[i-1] != arr[i] and arr[i] in freq):\n flag = 1\n break\n if arr[i] in freq:\n freq[arr[i]] += 1\n else:\n freq[arr[i]] = 1\n \n sam = []\n \n for i in freq:\n sam.append(freq[i])\n \n sam2 = list(set(sam))\n sam.sort()\n sam2.sort()\n \n # print(\"flag\", flag, sam, sam2)\n \n if(sam != sam2):\n flag = 1\n \n if(flag == 1):\n print(\"NO\")\n else:\n print(\"YES\")", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n u=[]\n t=[]\n ut=[]\n qw=[]\n c=0\n for i in l:\n if i not in u:\n u.append(i)\n t.append(l.count(i))\n for i in t:\n if i not in ut:\n ut.append(i)\n for i in l:\n if i not in qw or qw[-1]==i:\n qw.append(i)\n else:\n print(\"NO\")\n c=1\n break\n if c==0 and len(ut)==len(t):\n print(\"YES\")\n elif c==0:\n print(\"NO\")\n \n \n \n", "for _ in range(int(input())):\n N = int(input())\n L = list(map(int, input().split()))\n new = {}\n for i in L:\n if i in new.keys():\n if i != list(new.keys())[-1]:\n print('NO')\n break\n new[i] += 1\n else:\n new[i] = 0\n else:\n print('YES' if len(new.values()) == len(set(new.values())) else 'NO')"]
{"inputs": [["3", "6", "1 1 4 2 2 2", "8", "1 1 4 3 4 7 7 7", "8", "1 7 7 3 3 4 4 4"]], "outputs": [["YES", "NO", "NO"]]}
INTERVIEW
PYTHON3
CODECHEF
11,297
49067de904719163493e439153a495d5
UNKNOWN
Kshitij has recently started solving problems on codechef. As he is real problem solving enthusiast, he wants continuous growth in number of problems solved per day. He started with $a$ problems on first day. He solves $d$ problems more than previous day. But after every $k$ days , he increases $d$ by $inc$ . Can you guess how many questions he will solve on $nth $ day ? -----Input:----- - First line contains $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input,five integers $a, d, k, n, inc$. -----Output:----- For each testcase, output in a single line number of questions solved on $nth$ day. -----Constraints----- - $1 \leq T \leq 15$ - $1 \leq a \leq 99$ - $1 \leq d \leq 100$ - $1 \leq n \leq 10000$ - $1 \leq k \leq n$ - $0 \leq inc \leq 99$ -----Sample Input:----- 1 1 4 3 8 2 -----Sample Output:----- 43 -----EXPLANATION:----- The number of questions solved in first 8 days is : $1$ $5$ $9$ $15$ $21$ $27$ $35$ $43$ . On first day he solved 1 problem . Here $d$ is 4 for first 3 days. Then after 3 days $d$ increases by 2 (that is 6).
["# cook your dish here\n\nt = int(input())\n\nfor _ in range(t):\n\ta,d,k,n,inc = map(int, input().strip().split())\n\n\tres = a\n\tfor i in range(1, n):\n\t\tif i%k == 0:\n\t\t\td += inc\n\t\tres += d\n\n\tprint(res)", "# cook your dish here\ntry:\n for i in range(int(input())):\n inp=list(map(int,input().split()))\n \n for i in range(2,inp[3]+1):\n if inp[3]==1:\n break\n inp[0]+=inp[1] \n if int(i%inp[2])==0:\n inp[1]+=inp[4]\n \n \n print(inp[0])\nexcept:\n pass", "# cook your dish here\nt=int(input())\nfor i in range(t):\n a,d,k,n,inc=map(int,input().split())\n for i in range(1,n):\n if i%k==0:\n d=d+inc\n a=a+d\n else:\n a=a+d\n print(a) ", "# cook your dish here\nt=int(input())\nfor i in range(t):\n a,d,k,n,inc=map(int,input().split())\n for i in range(1,n):\n if i%k==0:\n d=d+inc\n a=a+d\n else:\n a=a+d\n print(a) ", "try:\n for i in range(int(input())):\n a1=list(map(int,input().split()))\n a=a1[0]\n d=a1[1]\n k=a1[2]\n n=a1[3]\n inc=a1[4]\n for i in range(1,n):\n if((i)%k==0):\n d=d+inc\n a=a+d\n print(a)\nexcept:\n pass", "# cook your dish here\nt=int(input())\nfor i in range(t):\n a,d,k,n,inc=map(int,input().split())\n l=[a]\n for i in range(2,n+1):\n a+=d\n l.append(a)\n if i%k==0:\n d+=inc\n print(l[n-1])", "import sys,math,collections\r\nfrom collections import defaultdict\r\n\r\n#from itertools import permutations,combinations\r\n \r\ndef file():\r\n sys.stdin = open('input.py', 'r')\r\n sys.stdout = open('output.py', 'w') \r\ndef get_array():\r\n l=list(map(int, input().split()))\r\n return l\r\ndef get_ints(): \r\n return map(int, input().split())\r\n #return a,b\r\ndef get_3_ints(): \r\n a,b,c=map(int, input().split())\r\n return a,b,c \r\ndef sod(n):\r\n n,c=str(n),0\r\n for i in n: \r\n c+=int(i)\r\n return c \r\ndef isPrime(n):\r\n if (n <= 1):\r\n return False\r\n if (n <= 3):\r\n return True\r\n if (n % 2 == 0 or n % 3 == 0):\r\n return False\r\n i = 5\r\n while(i * i <= n):\r\n if (n % i == 0 or n % (i + 2) == 0):\r\n return False\r\n i = i + 6\r\n \r\n return True\r\ndef getFloor(A, x):\r\n\r\n (left, right) = (0, len(A) - 1)\r\n\r\n floor = -1\r\n while left <= right:\r\n mid = (left + right) // 2\r\n if A[mid] == x:\r\n return A[mid]\r\n elif x < A[mid]:\r\n right = mid - 1\r\n else:\r\n floor = A[mid]\r\n left = mid + 1\r\n \r\n return floor\r\ndef chk(aa,bb):\r\n\tf=0\r\n\tfor i in aa:\r\n\t\tfor j in bb:\r\n\t\t\tif(j[0]>=i[0] and j[1]<=i[1]):\r\n\t\t\t\tf+=1\r\n\t\t\telif(j[0]<=i[0] and j[1]>=i[1]):\t\r\n\t\t\t\tf+=1\r\n\t\t\telif(i[0]==j[1] or i[1]==j[0]):\r\n\t\t\t\tf+=1\r\n\t\t\telse:\r\n\t\t\t\tcontinue\r\n\treturn f\t\t\t\r\n\r\n#file()\r\ndef main():\r\n\tfor tt in range(int(input())):\r\n\t\ta,d,k,n,inc=get_ints()\r\n\t\tj=1\r\n\t\tfor i in range(n-1):\r\n\t\t\tif(j==k):\r\n\t\t\t\td+=inc\r\n\t\t\t\tj=1\r\n\t\t\telse:\r\n\t\t\t\tj+=1\r\n\t\t\ta=a+d\t\r\n\t\tprint(a)\t\t\r\n\r\n\r\n\r\n\r\n\t\t\t\t\r\n\r\n\r\n\r\n\r\n\r\n\t\t\r\n\r\n\r\n\t\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n \r\n \r\n\r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n\r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\ndef __starting_point():\r\n main()\n__starting_point()", "try:\n t=int(input())\n ans=[]\n while(t>0):\n t-=1\n li=list(map(int,input().split()))\n a=li[0]\n d=li[1]\n k=li[2]\n n=li[3]\n inc=li[4]\n for i in range(1,n):\n if((i)%k==0):\n d=d+inc\n a=a+d\n ans.append(a)\n for i in ans:\n print(i)\nexcept:\n pass\n", "p=int(input())\r\nfor j in range(p):\r\n a,d,k,n,inc=list(map(int,input().split()))\r\n l=[a]\r\n for i in range(2,n+1):\r\n a=a+d\r\n l.append(a)\r\n if(i%k==0):\r\n d=d+inc\r\n print(l[n-1])\r\n \r\n", "\nT=int(input())\nfor i in range(T):\n a,d,k,n,inc=list(map(int,input().split()))\n for i in range(1,n+1):\n if i==1:\n a=a\n else:\n if i%k!=0:\n a+=d\n else:\n a+=d\n d+=inc\n print(a)\n\n", "# cook your dish here\nfor _ in range(int(input())):\n a, d, k, n, inc = map(int, input().split())\n sum = a\n for i in range(1, n):\n if i % k == 0:\n d = d + inc\n sum = sum + d\n print(sum)", "t = int(input())\nwhile t>0:\n t-=1\n a,d,k,n,inc = map(int,input().split())\n s = a\n count = 1\n for i in range(1,n):\n if count == k:\n d += inc\n count = 0\n s = s+d\n count+=1\n # print(s)\n \n print(s)", "t = int(input())\nfor _ in range(t):\n a, d, k, n, inc = map(int, input().split())\n a -= d\n day = 0\n while day < n:\n for j in range(k):\n a += d\n day += 1\n if day == n:\n break\n d += inc\n print(a)", "for _ in range(int(input())):\r\n a,d,k,n,inc = map(int,input().split())\r\n ans = a\r\n for i in range(1,n):\r\n if i % k == 0:\r\n d += inc\r\n ans += d\r\n else:\r\n ans += d\r\n print(ans)", "t = int(input())\nfor _ in range(t):\n a, d, k, n, inc = map(int, input().split())\n a -= d\n day = 0\n while day < n:\n for j in range(k):\n a += d\n day += 1\n if day == n:\n break\n d += inc\n print(a)", "try:\n for _ in range(int(input())):\n a,d,k,n,inc=map(int,input().split())\n p=0\n s=a\n for i in range(n//k):\n if i==0:\n s+=(k-1)*d\n d+=inc\n \n else:\n s+=k*d\n d+=inc\n \n s+=(n%k)*(d)\n print(s)\n \n \nexcept:\n pass", "# cook your dish here\n#Author : Ashutosh Wagh, Codechef : ashutosh0903\nfor _ in range(int(input())) :\n a,d,k,n,inc = list(map(int,input().split()))\n ans = a \n for i in range(2,n+1) :\n if i%k!=1 :\n ans = ans + d\n else :\n d = d + inc\n ans = ans + d\n print(ans)\n", "# cook your dish here\n#Author : Ashutosh Wagh, Codechef : ashutosh0903\nfor _ in range(int(input())) :\n a,d,k,n,inc = list(map(int,input().split()))\n ans = a \n for i in range(2,n+1) :\n if i%k!=1 :\n ans = ans + d\n else :\n d = d + inc\n ans = ans + d\n print(ans)\n", "# cook your dish here\nfor i in range(int(input())):\n a1,d,k,n,inc=list(map(int,input().split()))\n a=[a1]\n #k=3\n for i in range(1,n):\n if((i)%k==0):\n d+=inc\n a.append(a[i-1]+d)\n print(a[n-1]) \n \n \n", "for _ in range(int(input())):\r\n a,d,k,n,inc = list(map(int, input().split()))\r\n ans=a\r\n for i in range(2,n+1):\r\n ans+=d\r\n if(i%k==0 and i>=k):\r\n d+=inc\r\n print(ans)\r\n\r\n\r\n\r\n\r\n", "# cook your dish here\nfor _ in range(int(input())):\n (a,d,k,n,inc) = map(int,input().split())\n \n ans = a-d \n for i in range(1,n+1):\n ans += d \n if i%k == 0:\n d += inc \n \n print(ans)", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n a,d,k,n,inc=list(map(int,input().split()))\n if(n%k==0):\n c=n//k\n else:\n c=n//k + 1\n while(c>0):\n s=a+(k-1)*d\n d=d+inc\n a=s+d\n c=c-1\n if(n%k==0):\n print(s)\n else:\n r=n%k\n r1=k-r\n print(s-r1*(d-inc))\n", "for _ in range(int(input())):\n l=list(map(int,input().split()))\n for i in range(l[-2]-1):\n if((i+1)%l[2]==0):\n l[1]+=l[-1]\n l[0]+=l[1]\n print(l[0])\n\n\n", "# cook your dish here\nfor _ in range(int(input())):\n a,d,k,n,inc=list(map(int,input().split()))\n sum1=a\n for i in range(0,n):\n if i==0:\n pass\n else:\n if i%k==0:\n d+=inc\n sum1+=d\n \n \n # if i==1:\n # pass\n # elif i==(k+1):\n # print(i)\n # d+=inc\n # sum1+=d\n # print(sum1)\n # elif i==k:\n # sum1+=d\n # else:\n # if i%(k)==0: \n # d+=inc\n # sum1+=d\n # print(sum1)\n print(sum1)\n \n", "# cook your dish here\nt=int(input())\nwhile t:\n a,d,k,n,inc=map(int,input().split())\n n-=1\n if inc and n>=k:\n a+=(d*(k-1))\n n-=(k-1)\n d+=inc\n while k<=n:\n a+=(d*k)\n d+=inc\n n-=k\n if n:\n a+=(d*n)\n print(a)\n t-=1"]
{"inputs": [["1", "1 4 3 8 2"]], "outputs": [["43"]]}
INTERVIEW
PYTHON3
CODECHEF
9,985
495766d613ab61e9480a44032b99f4d0
UNKNOWN
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 ith 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.
["x=eval(input())\nfor x in range(0,x):\n\tans=0\n\td=input()\n\ta=0\n\tcont=0\n\tfor i in range(0,len(d)):\n\t\ta+=len(d)-i\n\t\tif d[i]=='7':\n\t\t\tans+=1+cont\n\t\t\tcont+=1\n\t\telse:\n\t\t\tcont=0\n\tans=a-ans\n\tprint(ans)\n", "for _ in range(int(input())):\n\ts = input()\n\ta = s.split('4')\n\tsum1 = 0\n\tfor k in a:\n\t\tsum1 = sum1 + (len(k)*(len(k)+1))/2\n\tl = len(s) *(len(s)+1) / 2\n\tprint(l - sum1)", "#import psyco\n#psyco.full()\n\nfor _ in range(int(input())):\n s=input().strip()\n ways=0\n l=len(s)\n \n index=[]\n for i in range(l):\n if s[i]=='4':\n index.append(i)\n if len(index)==0:\n print(0)\n else:\n ways=0\n ways+=(index[0]+1-0)*(l-index[0])\n for i in range(1,len(index)):\n ways+=(index[i]-index[i-1])*(l-index[i])\n \n print(ways)", "tcase=int(eval(input()))\ni=0\nwhile(tcase):\n tcase-=1\n a=str(input())\n j=0\n i=0\n rem=0\n length=len(a)\n ans=(length*(length+1))/2\n #print ans,\n while(i<length):\n if(a[i]=='7'):\n rem+=(i-j+1)\n #print i,j,\n else:\n j=i+1\n i+=1\n ans-= rem;\n print(ans);\n\n", "#import psyco\n#psyco.full()\n\nfor _ in range(int(input())):\n s=input().strip()\n ways=0\n l=len(s)\n \n index=[]\n for i in range(l):\n if s[i]=='4':\n index.append(i)\n ways=0 \n for i in range(len(index)):\n if i==0:\n ways+=(index[i]+1-0)*(l-index[i])\n else:\n ways+=(index[i]-index[i-1])*(l-index[i])\n \n print(ways)", "t=int(input().strip())\nfor i in range(t):\n word=input().strip()\n n=len(word)\n s=n*(n+1)/2\n t7=0\n for j in range(n):\n if word[j]=='7': t7+=1\n else:\n s-=t7*(t7+1)/2\n t7=0\n s-=t7*(t7+1)/2\n print(s)\n \n", "t=int(input().strip())\nfor i in range(t):\n word=input().strip()\n n=len(word)\n s=n*(n+1)/2\n t7=0\n for j in range(n):\n if word[j]=='7': t7+=1\n else:\n s-=t7*(t7+1)/2\n t7=0\n s-=t7*(t7+1)/2\n print(s)\n \n"]
{"inputs": [["4", "47", "74", "477", "4747477", "", ""]], "outputs": [["2", "2", "3", "23"]]}
INTERVIEW
PYTHON3
CODECHEF
2,209
5abf06b57dfee6ef29df4ea632a6240b
UNKNOWN
Cheffina challanges chef to rearrange the given array as arr[i] > arr[i+1] < arr[i+2] > arr[i+3].. and so on…, i.e. also arr[i] < arr[i+2] and arr[i+1] < arr[i+3] and arr[i] < arr[i+3] so on.. Chef accepts the challenge, chef starts coding but his code is not compiling help him to write new code. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains two lines of input, First $N$ as the size of the array. - N space-separated distinct integers. -----Output:----- For each test case, output in a single line answer given to the Chefffina. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^5$ - $1 \leq arr[i] \leq 10^5$ -----Sample Input:----- 2 4 4 1 6 3 5 4 5 1 6 3 -----Sample Output:----- 3 1 6 4 3 1 5 4 6
["# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n a = list(map(int,input().split()))\n a.sort()\n i=1\n while(i<n):\n a[i-1],a[i] = a[i],a[i-1]\n i+=2\n print(*a)\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n arr=[int(i) for i in input().split()]\n arr=sorted(arr)\n for i in range(n):\n \n if i%2==0:\n if i==n-1:\n print(arr[i],'',end='')\n else:\n print(arr[i+1],'',end='')\n else:\n print(arr[i-1],'',end='')\n print()\n \n \n ", "from sys import stdin, stdout\r\ninput = stdin.readline\r\nfrom collections import defaultdict as dd\r\nimport math\r\ndef geti(): return list(map(int, input().strip().split()))\r\ndef getl(): return list(map(int, input().strip().split()))\r\ndef gets(): return input()\r\ndef geta(): return int(input())\r\ndef print_s(s): stdout.write(s+'\\n')\r\n\r\ndef solve():\r\n for _ in range(geta()):\r\n n=geta()\r\n a=getl()\r\n a.sort()\r\n for i in range(1,n,2):\r\n a[i],a[i-1]=a[i-1],a[i]\r\n print(*a)\r\n\r\n\r\n\r\ndef __starting_point():\r\n solve()\r\n\n__starting_point()", "# cook your dish here\ndef gcd(x, y):\n while y:\n x, y = y, x % y\n return x\n\ndef lcm(x, y):\n return (x*y)//(gcd(x,y))\n\ndef isPrime(n) : \n if (n <= 1) : \n return False\n if (n <= 3) : \n return True\n if (n % 2 == 0 or n % 3 == 0) : \n return False\n i = 5\n while(i * i <= n) : \n if (n % i == 0 or n % (i + 2) == 0) : \n return False\n i = i + 6\n return True\n\nabc=\"abcdefghijklmnopqrstuvwxyz\"\n\npi=3.141592653589793238\n\nt = int(input())\nfor _ in range(t):\n n = int(input())\n arr = list(map(int,input().split()))\n arr.sort()\n ans=[]\n for i in range(1,n+1,2):\n try:\n ans.append(arr[i])\n except:\n pass\n try:\n ans.append(arr[i-1])\n except:\n pass\n print(*ans)\n \n \n \n"]
{"inputs": [["2", "4", "4 1 6 3", "5", "4 5 1 6 3"]], "outputs": [["3 1 6 4", "3 1 5 4 6"]]}
INTERVIEW
PYTHON3
CODECHEF
2,158
183c220057d6f72c51e84c665c740687
UNKNOWN
There is only little time left until the New Year! For that reason, Chef decided to make a present for his best friend. He made a connected and undirected simple graph with N$N$ vertices and M$M$ edges. Since Chef does not like odd numbers and his friend does not like undirected graphs, Chef decided to direct each edge in one of two possible directions in such a way that the indegrees of all vertices of the resulting graph are even. The indegree of a vertex is the number of edges directed to that vertex from another vertex. As usual, Chef is busy in the kitchen, so you need to help him with directing the edges. Find one possible way to direct them or determine that it is impossible under the given conditions, so that Chef could bake a cake as a present instead. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains two space-separated integers N$N$ and M$M$. - M$M$ lines follow. For each i$i$ (1≤i≤M$1 \le i \le M$), the i$i$-th of these lines contains two space-separated integers ui$u_i$ and vi$v_i$ denoting an edge between vertices ui$u_i$ and vi$v_i$. -----Output----- For each test case: - If a valid way to direct the edges does not exist, print a single line containing one integer −1$-1$. - Otherwise, print a single line containing M$M$ space-separated integers. For each valid i$i$, the i$i$-th of these integers should be 0$0$ if edge i$i$ is directed from ui$u_i$ to vi$v_i$ or 1$1$ if it is directed from vi$v_i$ to ui$u_i$. -----Constraints----- - 1≤T≤5$1 \le T \le 5$ - 1≤N,M≤105$1 \le N, M \le 10^5$ - 1≤ui,vi≤N$1 \le u_i, v_i \le N$ for each valid i$i$ - the graph on the input is connected, does not contain multiple edges or self-loops -----Subtasks----- Subtask #1 (30 points): 1≤N,M≤1,000$1 \le N, M \le 1,000$ Subtask #2 (70 points): original constraints -----Example Input----- 2 4 4 1 2 1 3 2 4 3 4 3 3 1 2 2 3 1 3 -----Example Output----- 0 0 1 1 -1
["# cook your dish here\nfor _ in range(int(input())):\n N, M = [int(x) for x in input().split()]\n edges = [0]*M\n dir = {}\n nodes = [[] for j in range(N+1)]\n ind = [0]*(N+1)\n graph = {}\n final_edges = []\n for i in range(M):\n u, v = [int(x) for x in input().split()]\n nodes[u].append(v)\n nodes[v].append(u)\n dir[(u,v)]=1\n dir[(v,u)] = 0\n ind[v] += 1\n graph[(u,v)] = graph[(v,u)] = i\n final_edges.append([u,v])\n if M%2!=0:\n print(-1)\n continue\n for i in range(M):\n u, v = final_edges[i]\n if ind[u]%2!=0 and ind[v]%2!=0:\n d = dir[(u,v)]\n if d:\n ind[u] += 1\n ind[v] -= 1\n dir[(u,v)] = 0\n dir[(v,u)] = 1\n edges[i] = abs(edges[i]-1)\n else:\n ind[u] -= 1\n ind[v] += 1\n dir[(u, v)] = 1\n dir[(v, u)] = 0\n edges[i] = abs(edges[i]-1)\n s = []\n for i in range(1, N+1):\n if ind[i]%2:\n s.append(i)\n while s:\n set1 = set()\n for u in s:\n if ind[u]%2:\n v = nodes[u][0]\n d = dir[(u,v)]\n index = graph[(u, v)]\n set1.add(v)\n if d:\n ind[u] += 1\n ind[v] -= 1\n dir[(u, v)] = 1\n dir[(v, u)] = 1\n edges[index] = abs(edges[index]-1)\n else:\n ind[u] -= 1\n ind[v] += 1\n dir[(u, v)] = 1\n dir[(v, u)] = 0\n edges[index] = abs(edges[index]-1)\n\n s = set1\n print(*edges)", "for __ in range(int(input())):\r\n n,m=map(int,input().split())\r\n g=[[]for _ in range(n+1)]\r\n long=[]\r\n di={}\r\n stable=[0 for _ in range(n+1)]\r\n edges=[0 for _ in range(m)]\r\n for i in range(m):\r\n x,y=map(int,input().split())\r\n g[x].append(y)\r\n g[y].append(x)\r\n stable[y]+=1\r\n long.append([x,y])\r\n di[(x,y)]=i\r\n di[(y,x)]=i\r\n f=1\r\n if m%2:\r\n f=0\r\n for i in range(m):\r\n c,d=long[i][0],long[i][1]\r\n if stable[c]%2==1 and stable[d]%2==1:\r\n stable[c]+=1\r\n stable[d]-=1\r\n edges[i]=1\r\n s=[]\r\n for i in range(1,n+1):\r\n if stable[i]%2==1:\r\n s.append(i)\r\n while s and f:\r\n set1=set()\r\n for i in s:\r\n if stable[i]%2:\r\n y=g[i][0]\r\n w=di[(i,y)]\r\n stable[y]+=1\r\n stable[i]-=1\r\n\r\n set1.add(y)\r\n edges[w]=abs(edges[w]-1)\r\n s=set1\r\n if(f):\r\n print(*edges)\r\n else:\r\n print(-1)\r\n", "import sys\r\nfrom collections import deque\r\n\r\ncroot = dict()\r\nsetnumber = dict()\r\n\r\ndef finalroot(v):\r\n if croot[v] != v:\r\n v = finalroot(croot[v])\r\n return v\r\n\r\ndef union(v1,v2):\r\n r1 = finalroot(v1)\r\n r2 = finalroot(v2)\r\n if r1 != r2:\r\n if setnumber[r1] > setnumber[r2]:\r\n croot[r2] = r1\r\n else:\r\n croot[r1] = r2\r\n if setnumber[r1] == setnumber[r2]:\r\n setnumber[r2] += 1\r\n\r\ndef minspan(n,edges):\r\n\r\n finale = []\r\n edgecount = {}\r\n\r\n for i in range(n):\r\n edgecount[i+1] = 0\r\n setnumber[i+1] = 0\r\n croot[i+1] = i+1\r\n\r\n edges.sort()\r\n\r\n for e in edges:\r\n u,v = e\r\n if finalroot(u) != finalroot(v):\r\n edgecount[u] += 1\r\n edgecount[v] += 1\r\n finale.append((u,v))\r\n union(u,v)\r\n\r\n root = max(edgecount.keys(), key=(lambda k: edgecount[k]))\r\n return set(finale),root\r\n\r\ndef main():\r\n\r\n t = int(input())\r\n\r\n for i in range(t):\r\n\r\n n,m = [int(item) for item in input().split()]\r\n e = set()\r\n elist = []\r\n\r\n for i in range(m):\r\n u,v = [int(item) for item in input().split()]\r\n elist.append((u,v))\r\n e.add((u,v))\r\n\r\n finale,root = minspan(n,list(e))\r\n sroot = root\r\n tofix = e - finale\r\n\r\n indegree = {}\r\n for i in range(n):\r\n indegree[i+1] = 0\r\n\r\n decided = set()\r\n\r\n # randomly assign directions to edges not in min span\r\n for t in tofix:\r\n indegree[t[1]] += 1\r\n decided.add((t[0],t[1]))\r\n\r\n cf = {}\r\n pf = {}\r\n\r\n for i in range(n):\r\n cf[i+1] = set()\r\n pf[i+1] = set()\r\n\r\n # minspan finale- create tree\r\n # 2 dics cf - contains each node with it's child\r\n # pf - each node with it's parent\r\n\r\n for f in finale:\r\n u,v = f\r\n cf[u].add(v)\r\n cf[v].add(u)\r\n\r\n r = deque()\r\n r.append(root)\r\n\r\n while len(r) != 0:\r\n root = r.pop()\r\n for c in cf[root]:\r\n r.append(c)\r\n cf[c].remove(root)\r\n pf[c] = root\r\n\r\n\r\n q = deque([sroot])\r\n st = deque()\r\n # tree traversal\r\n while len(q) != 0:\r\n node = q.pop()\r\n st.append(node)\r\n for e in cf[node]:\r\n q.append(e)\r\n\r\n while len(st) != 0:\r\n c = st.pop()\r\n if c != sroot:\r\n p = pf[c]\r\n if indegree[c]%2 == 0:\r\n #outgoing\r\n decided.add((c,p))\r\n indegree[p] += 1\r\n else:\r\n decided.add((p,c))\r\n indegree[c] += 1\r\n\r\n possible = (indegree[sroot]%2 == 0)\r\n if possible:\r\n ans = []\r\n for i in elist:\r\n if i in decided:\r\n ans.append(0)\r\n else:\r\n ans.append(1)\r\n print(*ans)\r\n else:\r\n print(-1)\r\n\r\n return 0\r\n\r\ndef __starting_point():\r\n main()\r\n\n__starting_point()", "import sys\r\nfrom collections import deque\r\n\r\ndef minspan(n,edges):\r\n\r\n nodelist = []\r\n finale = []\r\n edgecount = {}\r\n\r\n for i in range(n):\r\n edgecount[i+1] = 0\r\n\r\n for i in range(n):\r\n nodelist.append(set([i+1]))\r\n\r\n for e in edges:\r\n\r\n u = e[0]\r\n v = e[1]\r\n\r\n for each_set in nodelist:\r\n if u in each_set:\r\n save1 = each_set\r\n if v in each_set:\r\n save2 = each_set\r\n\r\n if save1 != save2:\r\n finale.append((u,v))\r\n edgecount[u] += 1\r\n edgecount[v] += 1\r\n merged = save1 | save2\r\n nodelist.remove(save1)\r\n nodelist.remove(save2)\r\n nodelist.append(merged)\r\n\r\n if len(nodelist) == 1:\r\n break\r\n\r\n root = max(edgecount.keys(), key=(lambda k: edgecount[k]))\r\n return set(finale),root\r\n\r\ndef main():\r\n\r\n t = int(input())\r\n\r\n for i in range(t):\r\n\r\n n,m = [int(item) for item in input().split()]\r\n e = set()\r\n elist = []\r\n\r\n for i in range(m):\r\n u,v = [int(item) for item in input().split()]\r\n elist.append((u,v))\r\n e.add((u,v))\r\n\r\n finale,root = minspan(n,e)\r\n sroot = root\r\n tofix = e - finale\r\n\r\n indegree = {}\r\n for i in range(n):\r\n indegree[i+1] = 0\r\n\r\n decided = set()\r\n\r\n # randomly assign directions to edges not in min span\r\n for t in tofix:\r\n indegree[t[1]] += 1\r\n decided.add((t[0],t[1]))\r\n\r\n cf = {}\r\n pf = {}\r\n\r\n for i in range(n):\r\n cf[i+1] = set()\r\n pf[i+1] = set()\r\n\r\n # minspan finale- create tree\r\n # 2 dics cf - contains each node with it's child\r\n # pf - each node with it's parent\r\n\r\n for f in finale:\r\n u,v = f\r\n cf[u].add(v)\r\n cf[v].add(u)\r\n\r\n r = deque()\r\n r.append(root)\r\n\r\n while len(r) != 0:\r\n root = r.pop()\r\n for c in cf[root]:\r\n r.append(c)\r\n cf[c].remove(root)\r\n pf[c] = root\r\n\r\n\r\n q = deque([sroot])\r\n st = deque()\r\n # tree traversal\r\n while len(q) != 0:\r\n node = q.pop()\r\n st.append(node)\r\n for e in cf[node]:\r\n q.append(e)\r\n\r\n while len(st) != 0:\r\n c = st.pop()\r\n if c != sroot:\r\n p = pf[c]\r\n if indegree[c]%2 == 0:\r\n #outgoing\r\n decided.add((c,p))\r\n indegree[p] += 1\r\n else:\r\n decided.add((p,c))\r\n indegree[c] += 1\r\n\r\n\r\n possible = (indegree[sroot]%2 == 0)\r\n if possible:\r\n ans = []\r\n for i in elist:\r\n if i in decided:\r\n ans.append(0)\r\n else:\r\n ans.append(1)\r\n print(*ans)\r\n else:\r\n print(-1)\r\n\r\n return 0\r\n\r\ndef __starting_point():\r\n main()\r\n\n__starting_point()", "import sys\r\nfrom collections import deque\r\n\r\ndef minspan(n,edges):\r\n\r\n nodelist = []\r\n finale = []\r\n\r\n for i in range(n):\r\n nodelist.append(set([i+1]))\r\n\r\n for e in edges:\r\n u = e[0]\r\n v = e[1]\r\n\r\n for each_set in nodelist:\r\n if u in each_set:\r\n save1 = each_set\r\n if v in each_set:\r\n save2 = each_set\r\n\r\n if save1 != save2:\r\n finale.append((u,v))\r\n merged = save1 | save2\r\n nodelist.remove(save1)\r\n nodelist.remove(save2)\r\n nodelist.append(merged)\r\n\r\n if len(nodelist) == 1:\r\n break\r\n\r\n return finale\r\n\r\ndef main():\r\n\r\n t = int(input())\r\n\r\n for i in range(t):\r\n\r\n n,m = [int(item) for item in input().split()]\r\n e = set()\r\n elist = []\r\n\r\n for i in range(m):\r\n u,v = [int(item) for item in input().split()]\r\n elist.append((u,v))\r\n e.add((u,v))\r\n\r\n finale = set(minspan(n,e))\r\n tofix = e - finale\r\n\r\n indegree = {}\r\n for i in range(n):\r\n indegree[i+1] = 0\r\n\r\n decided = set()\r\n\r\n # randomly assign directions to edges not in min span\r\n for t in tofix:\r\n indegree[t[1]] += 1\r\n decided.add((t[0],t[1]))\r\n\r\n ## find leaf in finale\r\n ## fix random root i = 1\r\n cf = {}\r\n pf = {}\r\n\r\n for i in range(n):\r\n cf[i+1] = set()\r\n pf[i+1] = set()\r\n\r\n for f in finale:\r\n cf[f[0]].add(f[1])\r\n cf[f[1]].add(f[0])\r\n\r\n root = 1\r\n visit = set([root])\r\n visited = set()\r\n parent = {}\r\n\r\n #create the min span tree , every child with one parent\r\n # root is 1\r\n while len(visited) < n and len(visit) > 0:\r\n\r\n v = visit.pop()\r\n d = cf[v]\r\n visited.add(v)\r\n\r\n for i in d:\r\n if i not in visited:\r\n parent[i] = v\r\n pf[v].add(i)\r\n visit.add(i)\r\n\r\n\r\n leaf = set()\r\n for k,v in cf.items():\r\n if len(v) == 1:\r\n leaf.add(k)\r\n\r\n\r\n st = [1]\r\n sc = [1]\r\n\r\n # get traversal order from kids to parents\r\n while len(st) < n+1:\r\n if len(sc) == 0:\r\n break\r\n j = sc.pop()\r\n for i in pf[j]:\r\n st.append(i)\r\n sc.append(i)\r\n\r\n while len(st) != 1:\r\n\r\n g = st.pop()\r\n h = parent[g]\r\n pair1 = (g,h)\r\n pair2 = (h,g)\r\n\r\n if indegree[g]%2 == 0:\r\n #outgoing\r\n decided.add(pair1)\r\n indegree[h] += 1\r\n else:\r\n #incoming\r\n decided.add(pair2)\r\n indegree[g] += 1\r\n\r\n\r\n possible = (indegree[root]%2 == 0)\r\n if possible:\r\n ans = []\r\n for i in elist:\r\n if i in decided:\r\n ans.append(0)\r\n else:\r\n ans.append(1)\r\n print(*ans)\r\n else:\r\n print(-1)\r\n\r\n\r\n return 0\r\n\r\n\r\ndef __starting_point():\r\n main()\r\n\n__starting_point()", "for t in range(int(input())):\r\n n,m=map(int,input().split())\r\n g=[[]for _ in range(n+1)]\r\n long=[]\r\n di={}\r\n stable=[0]*(n+1)\r\n edges=[0]*(m)\r\n for i in range(m):\r\n x,y=map(int,input().split())\r\n g[x].append(y)\r\n g[y].append(x)\r\n stable[y]+=1\r\n long.append([x,y])\r\n di[(x,y)]=i\r\n di[(y,x)]=i\r\n f=1\r\n if m%2:\r\n f=0\r\n for i in range(m):\r\n c,d=long[i][0],long[i][1]\r\n if stable[c]%2==1 and stable[d]%2==1:\r\n stable[c]+=1\r\n stable[d]-=1\r\n edges[i]=1\r\n s=[]\r\n for i in range(1,n+1):\r\n if stable[i]%2==1:\r\n s.append(i)\r\n while s and f:\r\n set1=set()\r\n for i in s:\r\n if stable[i]%2:\r\n y=g[i][0]\r\n w=di[(i,y)]\r\n if edges[x]:\r\n stable[y]+=1\r\n stable[i]-=1\r\n else:\r\n stable[i]+=1\r\n stable[y]-=1\r\n set1.add(y)\r\n edges[w]=abs(edges[w]-1)\r\n s=set1\r\n if(f):\r\n print(*edges)\r\n else:\r\n print(-1)", "for _ in range(int(input())):\r\n N, M = [int(x) for x in input().split()]\r\n edges = [0]*M\r\n dir = {}\r\n nodes = [[] for j in range(N+1)]\r\n ind = [0]*(N+1)\r\n graph = {}\r\n final_edges = []\r\n for i in range(M):\r\n u, v = [int(x) for x in input().split()]\r\n nodes[u].append(v)\r\n nodes[v].append(u)\r\n dir[(u,v)]=1\r\n dir[(v,u)] = 0\r\n ind[v] += 1\r\n graph[(u,v)] = graph[(v,u)] = i\r\n final_edges.append([u,v])\r\n if M%2!=0:\r\n print(-1)\r\n continue\r\n for i in range(M):\r\n u, v = final_edges[i]\r\n if ind[u]%2!=0 and ind[v]%2!=0:\r\n d = dir[(u,v)]\r\n if d:\r\n ind[u] += 1\r\n ind[v] -= 1\r\n dir[(u,v)] = 0\r\n dir[(v,u)] = 1\r\n edges[i] = abs(edges[i]-1)\r\n else:\r\n ind[u] -= 1\r\n ind[v] += 1\r\n dir[(u, v)] = 1\r\n dir[(v, u)] = 0\r\n edges[i] = abs(edges[i]-1)\r\n s = []\r\n for i in range(1, N+1):\r\n if ind[i]%2:\r\n s.append(i)\r\n while s:\r\n set1 = set()\r\n for u in s:\r\n if ind[u]%2:\r\n v = nodes[u][0]\r\n d = dir[(u,v)]\r\n index = graph[(u, v)]\r\n set1.add(v)\r\n if d:\r\n ind[u] += 1\r\n ind[v] -= 1\r\n dir[(u, v)] = 0\r\n dir[(v, u)] = 1\r\n edges[index] = abs(edges[index]-1)\r\n else:\r\n ind[u] -= 1\r\n ind[v] += 1\r\n dir[(u, v)] = 1\r\n dir[(v, u)] = 0\r\n edges[index] = abs(edges[index]-1)\r\n\r\n s = set1\r\n print(*edges)", "for _ in range(int(input())):\r\n l=[]\r\n mp={}\r\n cr={}\r\n ans=[]\r\n n,m=map(int,input().split())\r\n for i in range(1,n+1):\r\n mp[i]=0\r\n cr[i]=False\r\n for _ in range(m):\r\n f,t=map(int,input().split())\r\n l.append((f,t))\r\n mp[f]+=1\r\n mp[t]+=1\r\n if m%2>0:\r\n print(-1)\r\n continue\r\n else:\r\n for _ in range(m):\r\n cr[l[_][1]]=not cr[l[_][1]]\r\n ans.append(0)\r\n for _ in range(m-1,-1,-1):\r\n if cr[l[_][1]]==True:\r\n cr[l[_][1]]=not True\r\n ans[_]=1\r\n if cr[l[_][0]]:\r\n cr[l[_][0]]=False\r\n else:\r\n cr[l[_][0]]=True\r\n for i in range(len(ans)):\r\n print(ans[i],end=' ')\r\n print()\r\n "]
{"inputs": [["2", "4 4", "1 2", "1 3", "2 4", "3 4", "3 3", "1 2", "2 3", "1 3"]], "outputs": [["0 0 1 1", "-1"]]}
INTERVIEW
PYTHON3
CODECHEF
17,524
0470f9a947e8a235bba0e83006585ed5
UNKNOWN
Chef has bought N robots to transport cakes for a large community wedding. He has assigned unique indices, from 1 to N, to each of them. How it will happen? Chef arranges the N robots in a row, in the (increasing) order of their indices. Then, he chooses the first M robots and moves them to the end of the queue. Now, Chef goes to the robot at the first position in the row and hands it one cake. He then notes this robot's index (say k) in his notebook, and goes to the kth position in the row. If the robot at this position does not have a cake, he give him one cake, notes his index in his notebook, and continues the same process. If a robot visited by Chef already has a cake with it, then he stops moving and the cake assignment process is stopped. Chef will be satisfied if all robots have a cake in the end. In order to prepare the kitchen staff for Chef's wrath (or happiness :) ), you must find out if he will be satisfied or not? If not, you have to find out how much robots have a cake, so that the kitchen staff can prepare themselves accordingly. -----Input----- - The first line of input contains a single integer T denoting the number of test cases. - The single line of each test cases contains two space separated integers N and M. -----Output----- For each of the T test cases, output a single line: - If all N robots have a cake, output "Yes" (without quotes). - Otherwise, output "No" (without quotes) followed by a space and the number of robots which have a cake. -----Constraints and Subtasks----- - 1 ≤ T ≤ 10 - 0 ≤ M < NSubtask 1: 25 points - 1 ≤ N ≤ 10^5Subtask 3: 75 points - 1 ≤ N ≤ 10^9 -----Example----- Input: 3 2 0 2 1 4 2 Output: No 1 Yes No 2 -----Explanation----- In test case 1, we have two robots indexed 1 and 2. They are arranged as (1 2). Chef goes to the first robot, gives him a cake, and moves to position 1. In the next step, he sees that robot at this position already has a has cake. So Chef stops moving, and our answer is "No 1". In test case 2, we again have two robots indexed 1 and 2. Initially, they are arranged as (1 2). Then, Chef moves robot#1 to the end of the row, and thus the arrangement becomes (2 1). Chef goes to the robot at the first position, which is robot#2. Chef hands him a cake, and moves to position 2. Then, he hands a cake to robot#1 at position 2, and moves back to the first position. Since, robot#2 at the first position already ahs a cake, Chef stops moving. All N robots have cakes, so Chef is satisfied, and our answer is "Yes". In the 3rd test case, we have the following arrangement of robots: (3 4 1 2). Only robots with indices 3 and 1 will get cakes. So our answer is "No 2".
["#read input\ncases = int(input())\ncaselist = []\nfor i in range(0, cases):\n caselist.append(input())\n\n#iterate each case\nfor j in range(0, cases):\n\n #current case's parameters:\n current_input = caselist[j].split(' ')\n bots = int(current_input[0])\n switch = int(current_input[1])\n\n #generate botlist and cakelist\n botlist = list(range(switch, bots)) + list(range(0, switch))\n cakelist = [False] * bots\n\n\n counter = 0\n index = 0\n for i in range(0,bots):\n if cakelist[index] == False:\n cakelist[index] = True\n counter += 1\n index = botlist[index]\n else:\n break\n\n if counter == bots:\n print(\"Yes\")\n else:\n print(\"No\", counter)\n \n\n \n", "def ggt(a, b):\n while b != 0:\n c = a % b\n a = b\n b = c\n return a\n\ndef kgv(a, b):\n return (a * b) / ggt(a, b)\n\n\ncase = int(input())\n\nfor l in range(case):\n n, k = list(map(int, input().split()))\n if n == 1:\n print(\"Yes\")\n elif k == 0:\n print(\"No 1\") \n else: \n remaining = kgv(n, k)/k\n if remaining == n:\n print(\"Yes\") \n \n else:\n print(\"No %d\" % remaining)\n \n\n\n \n", "import array\nT = int(input())\nfor _ in range(T):\n N,M = list(map(int,input().split()))\n robots = array.array('I')\n robots.extend((0,)*((N>>5)+1))\n c = 0\n Id = M\n while robots[Id>>5] & (1 << (Id&31))==0:\n robots[Id>>5]+=1<<(Id&31)\n c+=1\n Id = (Id+M)%N\n if c==N:\n print(\"Yes\")\n else:\n print(\"No\",c)", "import fractions\n\ndef eval(n, m):\n g = fractions.gcd(n, m)\n\n if g == n:\n if n == 1:\n print(\"Yes\")\n else:\n print(\"No 1\")\n elif g == 1:\n print(\"Yes\")\n else:\n print(\"No %d\" % (n / g))\n\nt = int(input())\n\nfor _ in range(t):\n (n, m) = [int(x) for x in input().split()]\n eval(n, m)\n", "T = int(input())\nfor _ in range(T):\n N,M = list(map(int,input().split()))\n robots = [0]*N\n c = 0\n Id = M\n while robots[Id]!=1:\n robots[Id]=1\n c+=1\n Id = (Id+M)%N\n if c==N:\n print(\"Yes\")\n else:\n print(\"No\",c)", "def idat(pos,M,N):\n return (pos+M)%N\n\nT = eval(input())\nfor _ in range(T):\n N,M = list(map(int,input().split(\" \")))\n robots = [0]*N\n# print range(1,N+1)[M:]+range(1,N+1)[:M]\n Id = idat(0,M,N)\n while robots[Id]!=1:\n robots[Id]=1\n newpos = Id\n Id = idat(newpos,M,N)\n# print robots[M:]+robots[:M]\n if robots.count(1)==N:\n print(\"Yes\")\n else:\n print(\"No\",robots.count(1))", "def gcd(n,m):\n if m==0:\n return n\n else:\n return gcd(m,n%m)\nt=int(input())\nwhile t:\n par=[int(i) for i in input().split(' ')]\n n,m=par[0],par[1]\n g_div=gcd(n,m)\n if g_div==1:\n print(\"Yes\")\n else:\n print(\"No \"+str(int(n/g_div)))\n t=t-1\n", "t=int(input())\nwhile t>0:\n t-=1\n l=input().split(' ')\n m=int(l[0])\n n=int(l[1])\n k=n+1\n c=1\n while k!=1:\n c+=1\n k=n+k\n if k>m:\n k=k-m\n if c==m:\n print(\"Yes\")\n else:\n print(\"No\",c)\n", "def ggt(a, b):\n while b != 0:\n c = a % b\n a, b = b, c\n return a\n\ndef kgv(a, b):\n return (a * b) / ggt(a, b)\n\n\ncase = int(input())\n\nfor l in range(case):\n n, k = list(map(int, input().split()))\n if n == 1:\n print(\"Yes\")\n elif k == 0:\n print(\"No 1\") \n else: \n cak = kgv(n, k)/k\n if cak == n:\n print(\"Yes\") \n \n else:\n print(\"No %d\" % cak)\n \n\n\n \n", "# your code goes here\nt = eval(input())\n\nfor i in range(t):\n n,m = list(map(int, input().split()))\n hsck = 0\n z = list(range(1,n+1))\n \n \n z = z[m:]+z[:m]\n \n arr = {j:0 for j in z}\n \n y = z[0]\n while arr[y]!=1:\n arr[y] = 1\n y = z[y-1]\n c = 0\n for j in list(arr.keys()):\n c+=arr[j]\n \n if c==n:\n print('Yes')\n else:\n print('No',c)\n \n", "import numpy as np\n\nt=int(input())\nwhile(t):\n t-=1\n n,m=list(map(int,input().split()))\n '''\n a=range(1,n+1)\n if m!=0:\n a=a[m:]+a[:m]\n ''' \n b=np.zeros(n,dtype='bool')\n \n \n #print b\n pos=0\n while(True):\n if b[(pos+m)%n]==True:\n break\n else:\n b[(pos+m)%n]=True\n pos=(pos+m)%n\n \n #print b,pos \n \n s=sum(b)\n if s==n:\n print('Yes')\n else:\n print('No'+' '+str(s))", "for i in range(int(input())):\n l = [int(j) for j in input().split()]\n n, m = l[0], l[1]\n count = 0\n j = 1\n while j <= n:\n j = j + m\n if j > n:\n j = j - n\n count += 1\n if j == 1:\n break\n if count == n:\n print('Yes')\n else:\n print('No ' + str(count))", "for i in range(int(input())):\n l = [int(j) for j in input().split()]\n n, m = l[0], l[1]\n idx = 0\n count = 0\n arr = []\n for j in range(m, n):\n arr.append(j + 1)\n for j in range(m):\n arr.append(j + 1)\n for j in range(n):\n idx = (arr[idx] - 1)\n count += 1\n if idx == 0:\n break\n if count == n:\n print('Yes')\n else:\n print('No ' + str(count))", "import sys\nT=int(input())\nwhile T>0:\n t=sys.stdin.readline().split()\n N=int(t[0])\n M=int(t[1])\n A=[i for i in range(1,N+1)]\n A=A[M:]+A[0:M]\n count=1\n flag=True\n i=A[0]-1\n A[0]=(-1)*A[0]\n while (flag and (count<N)):\n if A[i]<0:\n flag=False\n else:\n j=A[i]-1\n A[i]=(-1)*A[i]\n i=j\n count+=1\n if not flag:\n print('No',count)\n else:\n print('Yes')\n T=T-1\n", "T=int(input())\nwhile T>0:\n N,M=list(map(int,input().strip().split()))\n A=[i for i in range(1,N+1)]\n A=A[M:]+A[0:M]\n count=1\n flag=True\n i=A[0]-1\n A[0]=(-1)*A[0]\n while (flag and (count<N)):\n if A[i]<0:\n flag=False\n else:\n j=A[i]-1\n A[i]=(-1)*A[i]\n i=j\n count+=1\n if not flag:\n print('No',count)\n else:\n print('Yes')\n T=T-1\n", "n=list(map(int,input().split()))\ng=[]\nl=[]\nfor i in range(n[0]):\n m=[]\n l=list(map(int,input().split()))\n for j in range(l[1]+1,l[0]+1):\n m.append(j)\n for j in range(1,l[1]+1):\n m.append(j) \n c=0\n f=0\n d={}\n for j in range(0,len(m)):\n if f not in d:d[f]=\"No\"\n if d[f]==\"Yes\":\n break\n else:\n d[f]=\"Yes\"\n f=m[f]-1\n c=c+1\n if c==len(m):\n g.append(\"Yes\")\n else:\n g.append(\"No\"+\" \"+str(c))\nfor i in range(len(g)):\n print(g[i])"]
{"inputs": [["3", "2 0", "2 1", "4 2"]], "outputs": [["No 1", "Yes", "No 2"]]}
INTERVIEW
PYTHON3
CODECHEF
5,915
46c341e41e7e9dccc61e871e00d2d2f6
UNKNOWN
Chef has a sequence $A_1, A_2, \ldots, A_N$; each element of this sequence is either $0$ or $1$. Appy gave him a string $S$ with length $Q$ describing a sequence of queries. There are two types of queries: - '!': right-shift the sequence $A$, i.e. replace $A$ by another sequence $B_1, B_2, \ldots, B_N$ satisfying $B_{i+1} = A_i$ for each valid $i$ and $B_1 = A_N$ - '?': find the length of the longest contiguous subsequence of $A$ with length $\le K$ such that each element of this subsequence is equal to $1$ Answer all queries of the second type. -----Input----- - The first line of the input contains three space-separated integers $N$, $Q$ and $K$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. - The third line contains a string with length $Q$ describing queries. Each character of this string is either '?', denoting a query of the second type, or '!', denoting a query of the first type. -----Output----- For each query of the second type, print a single line containing one integer — the length of the longest required subsequence. -----Constraints----- - $1 \le K \le N \le 10^5$ - $1 \le Q \le 3 \cdot 10^5$ - $0 \le A_i \le 1$ for each valid $i$ - $S$ contains only characters '?' and '!' -----Subtasks----- Subtask #1 (30 points): - $1 \le N \le 10^3$ - $1 \le Q \le 3 \cdot 10^3$ Subtask #2 (70 points): original constraints -----Example Input----- 5 5 3 1 1 0 1 1 ?!?!? -----Example Output----- 2 3 3 -----Explanation----- - In the first query, there are two longest contiguous subsequences containing only $1$-s: $A_1, A_2$ and $A_4, A_5$. Each has length $2$. - After the second query, the sequence $A$ is $[1, 1, 1, 0, 1]$. - In the third query, the longest contiguous subsequence containing only $1$-s is $A_1, A_2, A_3$. - After the fourth query, $A = [1, 1, 1, 1, 0]$. - In the fifth query, the longest contiguous subsequence containing only $1$-s is $A_1, A_2, A_3, A_4$ with length $4$. However, we only want subsequences with lengths $\le K$. One of the longest such subsequences is $A_2, A_3, A_4$, with length $3$.
["n, q, k = map(int, input().split())\narr = list(map(int, input().split()))\nquery = list(input())\nq_ = len(query)\nc1 = query.count('?')\nc = arr.count(0)\nif c == n:\n for i in range(c1):\n print(0)\nelse:\n for i in range(q_):\n if (i!=0) and (query[i] == '?' and query[i-1] == '?'):\n print(max_c)\n elif query[i] == '?':\n max_c = cnt = 0\n for j in range(n):\n if (j != n - 1) and (arr[j] == 1 and arr[j + 1] == 1):\n cnt += 1\n else:\n max_c = max(max_c, cnt + 1)\n cnt = 0\n if k < max_c:\n max_c = k\n break\n print(max_c)\n elif query[i] == '!':\n temp = arr[n - 1]\n del arr[n - 1]\n arr.insert(0, temp)", "n,q,k = map(int, input().split())\na = [int(x) for x in input().split()]\ns = input()\nm = 0\nl = 0\nfor i in a:\n if i==1:\n m += 1\n else:\n m = 0\n if m>=k:\n l = k\n break\n elif m>l:\n l = m\nfor i in s:\n if i == '?':\n print(l)\n else:\n a.insert(0,a[n-1])\n a.pop()\n if a[0]!=0:\n m = 0\n l = 0\n for j in a:\n if j==1:\n m += 1\n else:\n m = 0\n if m>=k:\n l = k\n break\n elif m>l:\n l = m", "# cook your dish here\nimport collections\nn , q , k = map(int,input().split())\na = list(map(int,input().split()))\nst = str(input())\nde = collections.deque(a)\nfor f in range(q):\n if st[f]=='!':\n de.rotate(1)\n else:\n mi = 0\n prev = 0\n cnt = 0\n while 1:\n i = prev\n if de[i]==1:\n while de[i]==1:\n cnt+=1\n i+=1\n if i>n-1:\n break\n else:\n i+=1\n prev = i\n if cnt>mi:\n mi=cnt\n if prev>=n-1:\n break\n cnt = 0\n if mi<k:\n print(mi)\n else:\n print(k)", "n,q,k = map(int,input().split())\nl = list(map(str,input().split()))\ns = input()\nfor x in s:\n if x==\"!\":\n temp = l[-1]\n l[1:] = l[:-1]\n l[0] = temp\n continue\n elif x==\"?\":\n s_new = \"\".join(l[:])\n l_new = s_new.split(\"0\")\n l_new.sort()\n if len(l_new[-1])>=k:\n print(k)\n else:\n print(len(l_new[-1]))"]
{"inputs": [["5 5 3", "1 1 0 1 1", "?!?!?"]], "outputs": [["2", "3", "3"]]}
INTERVIEW
PYTHON3
CODECHEF
1,977
5d8962662c446038d581398413da64ee
UNKNOWN
It's Christmas time and Santa is in town. There are N children each having a bag with a mission to fill in as many toffees as possible. They are accompanied by a teacher whose ulterior motive is to test their counting skills. The toffees are of different brands (denoted by lowercase letters a-z). Santa picks up a child numbered m and gives n toffees of brand p to the child. The teacher wishes to ask the children to calculate the number of toffees there are of a particular brand amongst a given range of children. Formally, there are 2 queries: Input: First line consists of Q queries. Each line follows and consists of four space separated values: Type 1: It is of the form 1 m x p 1 is the type no,"m" is the child to which "x" toffees of a brand "p" are given Type 2: It is of the form 2 m n p where m - n is the range of children (inclusive) being queried for brand p Output: Report the sum of toffees of brand p within the given range m - n for each query of type 2 Constraints: 1 <= Q <= 10^5 1 <= N <= 10^6 1 <= m <= n <= 10^6 1 <= x <= 10^6 all brand of toffees are lowercase letters Subtask 1: (30 points) 1 <= Q <= 10^3 1 <= m <= n <= 10^3 1 <= x <= 10^3 Subtask 2: (70 points) Original Constraints Sample Input: 5 1 3 4 a 1 5 8 a 1 1 7 x 2 1 6 a 2 3 6 y Sample Output: 12 0 In first case, there are two children between 3 and 5 between 0 - 6 having sum (4 + 8) There's no toffee for y in given range
["import numpy as np\n\nN=10**6+1\nt=eval(input())\ninp = ()\n\nt1=ord('z')\n#bag=[[0 for _ in xrange(t1)] for _ in xrange(N+1)]\nbag=np.zeros((N+1,t1),dtype=np.int)\n#print bag\nwhile t:\n t-=1\n inp=input().split()\n t2=ord(inp[3]) - ord('a')\n t3=int(inp[1])\n t4=int(inp[2]) + 1\n if inp[0]==\"1\":\n #print \"enter\"\n bag[t3][t2]+=int(inp[2])\n\n\n if inp[0]==\"2\":\n sum=0\n for i in range(t3,t4):\n sum+=bag[i][t2]\n print(sum)\n\n#\n# for j in range(ord('z')-ord('a')):\n# for i in range(N+1):\n# if bag[i][j]!=0:\n# print bag[i][j] ,i,j\n\n\n\n", "N=10**3+1\nt=eval(input())\ninp = ()\nbag=[[0 for j in range(ord('z'))] for i in range(N+1)]\n#print bag\nwhile t:\n t-=1\n inp=input().split()\n if inp[0]==\"1\":\n #print \"enter\"\n bag[int(inp[1])][ord(inp[3])-ord('a')]+=int(inp[2])\n\n\n if inp[0]==\"2\":\n sum=0\n for i in range(int(inp[1]),int(inp[2])+1):\n sum+=bag[i][ord(inp[3])-ord('a')]\n print(sum)\n\n#\n# for j in range(ord('z')-ord('a')):\n# for i in range(N+1):\n# if bag[i][j]!=0:\n# print bag[i][j] ,i,j\n\n\n\n"]
{"inputs": [["5", "1 3 4 a", "1 5 8 a", "1 1 7 x", "2 1 6 a", "2 3 6 y"]], "outputs": [["12", "0", "In first case, there are two children between 3 and 5 between 0 - 6 having sum (4 + 8)", "There's no toffee for y in given range"]]}
INTERVIEW
PYTHON3
CODECHEF
1,105
7633f07d89509a1d71a8c0f76d22546c
UNKNOWN
Leha is a usual student at 'The Usual University for Usual Students'. Sometimes he studies hard; at other times he plays truant and gets busy with other things besides academics. He has already studied at the university for N months. For the ith month (1 ≤ i ≤ N), he has received some non-negative integer grade A[i]. Now he wants to analyse his progress for some periods of his university education. An arbitrary period, defined by two positive integers L and R, begins at Leha's Lth month at the university and ends at the Rth. The analysis is performed via the following steps. 1. Write down all the grades for each month from L to R and sort them. Let's call the sorted list S. 2. Calculate the sum of squared differences of consecutive elements in S, that is, (S[2] - S[1])2 + (S[3] - S[2])2 + ... + (S[R-L+1] - S[R-L])2. -----Input----- The first line contains one integer N — the number of months Leha has already studied at the university. The second line contains N integers — list A of Leha's grades. The third line contains one integer M — the number of periods Leha is interested in analyzing. Each of the following M lines contain two integers L and R describing each period. -----Output----- For each query, output one integer — the result of the progress analysis for the corresponding period. -----Constraints----- - 1 ≤ N, M ≤ 5*104 - 0 ≤ A[i] ≤ 106 -----Subtasks----- - Subtask 1 (19 points) 1 ≤ N, M ≤ 200, time limit = 2 sec - Subtask 2 (31 points) 1 ≤ N, M ≤ 10 000, time limit = 2 sec - Subtask 3 (26 points) 0 ≤ A[i] ≤ 100, time limit = 5 sec - Subtask 4 (24 points) no additional constraints, , time limit = 5 sec -----Example----- Input:5 1 3 2 4 5 5 1 5 1 4 2 4 3 3 3 5 Output:4 3 2 0 5 Explanation The first query: sorted array looks like (1, 2, 3, 4, 5) and the answer is calculated as (2-1)2 + (3-2)2 + (4-3)2 + (5-4)2 = 4 The second query: sorted array looks like (1, 2, 3, 4) and the answer is calculated as (2-1)2 + (3-2)2 + (4-3)2 = 3 The last query: sorted array looks like (2, 4, 5) and the answer is calculated as (4-2)2 + (5-4)2 = 5
["n=eval(input())\ngrades=list(map(int,input().split()))\nm=eval(input())\nfor df in range(m):\n x,y=list(map(int,input().split()))\n arr=[]\n arr=grades[x-1:y]\n arr.sort()\n sum=0\n #arr.append(1000000)\n for nh in range(0,len(arr)-1,1):\n sum=sum+(arr[nh+1]-arr[nh])**2\n #print sum,len(arr),nh+1,nh\n print(sum)", "n = eval(input())\ngr = [int(i) for i in input().split()]\nq = eval(input())\nfor _ in range(q):\n l,r = list(map(int,input().split()))\n temp = gr[l-1:r]\n temp.sort()\n ans = 0\n l = len(temp)\n for i in range(l-1):\n x = temp[i]\n y = temp[i+1]\n ans += (x-y)*(x-y)\n print(ans)\n", "n=eval(input())\ngrades=list(map(int,input().split()))\n\nm=eval(input())\nmain=[]\nfor df in range(m):\n x,y=list(map(int,input().split()))\n arr=[]\n arr=grades[x-1:y]\n arr.sort()\n sum=0\n #arr.append(1000000)\n for nh in range(0,len(arr)-1,1):\n sum=sum+(arr[nh+1]-arr[nh])*(arr[nh+1]-arr[nh])\n #print sum,len(arr),nh+1,nh\n print(sum)", "n,a,m=eval(input()),list(map(int,input().split())),eval(input())\nfor i__i in range(m):\n l,r= list(map(int,input().split()))\n l-=1\n Rev,_=a[l:r],0\n Rev.sort()\n d=len(Rev)\n for i_i in range(0,d-1):\n _+=(Rev[i_i+1]-Rev[i_i])*(Rev[i_i+1]-Rev[i_i])\n print(_)\n", "MOD = 10**9+7\nfaltu = eval(input())\ntheList = list(map(int,input().split()))\ntcase = eval(input())\nfor times in range(tcase):\n left, right = list(map(int,input().split()))\n sub = sorted(theList[left-1:right])\n sol = 0\n for index in range(1,right-left+1):\n sol += (sub[index] - sub[index-1])**2\n print(sol)", "N = eval(input())\nimport math\narr=list(map(int,input().split()))\nM=eval(input())\nfor iI in range(M):\n L,R=list(map(int,input().split()))\n su=0\n ok = arr[L-1:R]\n #print ok\n aks=len(ok)\n ok.sort()\n for ii in range(0,aks-1):\n su=su+(math.pow((ok[ii+1]-ok[ii]),2))\n print(int(su)) \n \n \n", "n=int(input())\nm=list(map(int,input().split()))\nq=int(input())\nfor case in range(q):\n l,r=list(map(int,input().split()))\n l=l-1\n r=r-1\n s=m[l:r+1]\n s.sort()\n ans=0\n for i in range(r-l):\n ans=ans+(s[i+1]-s[i])**2\n print(ans)\n", "n=int(input())\na=list(map(int,input().split()))\nm=int(input())\nfor i in range(0,m):\n l,r=list(map(int,input().split()))\n b=a[l-1:r]\n b.sort()\n le=len(b)\n sum=0\n for j in range(1,le):\n sum+=((b[j]-b[j-1])**2)\n \n print(sum)", "def qsort(inlist):\n if inlist == []:\n return []\n else:\n pivot = inlist[0]\n lesser = qsort([x for x in inlist[1:] if x < pivot])\n greater = qsort([x for x in inlist[1:] if x >= pivot])\n return lesser + [pivot] + greater\n\nn=eval(input())\na=list(map(int,input().split()))\nfor i in range(eval(input())):\n l,r=(int(x) for x in input().split())\n b=a[l-1:r]\n c=[]\n c=qsort(b)\n sum=0\n for i in range(1,len(c)):\n j=c[i]-c[i-1]\n sum+=pow(j,2)\n print(sum)\n", "import sys\n\nn = eval(input())\nlists = list(map(int,sys.stdin.readline().split()))\nfor __ in range(eval(input())) :\n l , r = [x-1 for x in list(map(int,sys.stdin.readline().split()))]\n k = [i for i in sorted(lists[l:r+1])]\n ans = 0\n for i in range(1,len(k)) :\n ans += (k[i-1]-k[i])**2\n print(ans)\n", "n=int(input())\nli=list(map(int, input().split()))\nm=int(input())\nfor i in range(m):\n l, r = list(map(int, input().split()))\n temp = li[l-1:r]\n temp.sort()\n ans=0\n for j in range(1, len(temp)):\n ans+=(temp[j]-temp[j-1])**2\n print(ans)", "n=eval(input())\na=list(map(int,input().split()))\nfor i in range(eval(input())):\n l,r=(int(x) for x in input().split())\n b=a[l-1:r]\n b.sort()\n sum=0\n for i in range(1,len(b)):\n j=b[i]-b[i-1]\n sum+=pow(j,2)\n print(sum)\n", "n=int(input())\ng=[int(x) for x in input().split()]\nm=int(input())\nwhile(m>0):\n q=[int(x) for x in input().split()]\n k=g[q[0]-1:q[1]]\n k.sort()\n s=0\n for i in range(0,len(k)-1):\n s+=(k[i+1]-k[i])**2\n print(s)\n m-=1", "import sys\n\nn = int(sys.stdin.readline())\nl = list(map(int,sys.stdin.readline().split()))\n\nfor cases in range(int(sys.stdin.readline())):\n L, R = list(map(int,sys.stdin.readline().split()))\n List = sorted(l[L-1:R])\n ans = 0\n for i in range(1,len(List)):\n ans += (List[i]-List[i-1])**2\n print(ans) \n", "x = eval(input())\na = list(map(int, input().split()))\nq = eval(input())\nfor __ in range(q):\n l, r = list(map(int, input().split()))\n l-=1\n R = sorted(a[l:r])\n x = 0\n for ii in range(1,len(R)):\n x+=(R[ii]-R[ii-1])**2\n print(x)\n", "n=int(input())\na=list(map(int,input().split()))\nfor q in range(int(input())):\n l,r=list(map(int,input().split()))\n s=sorted(a[l-1:r])\n ans=0\n for i in range(r-l):\n ans+=(s[i+1]-s[i])**2\n print(ans)", "n=eval(input())\na=input().split()\nfor i in range(n):\n a[i]=int(a[i])\nm=eval(input())\nfor i in range(m):\n l,r=input().split()\n l=int(l)-1\n r=int(r)\n s=a[l:r]\n s.sort()\n sum=0\n for i in range(1,r-l):\n sum+=(s[i]-s[i-1])**2\n \n print(sum)\n"]
{"inputs": [["5", "1 3 2 4 5", "5", "1 5", "1 4", "2 4", "3 3", "3 5"]], "outputs": [["4", "3", "2", "0", "5"]]}
INTERVIEW
PYTHON3
CODECHEF
4,852
79bcbbe605a6eeef42ab66219caaf099
UNKNOWN
The XOR pair representation (XPR) of a positive integer $N$ is defined as a pair of integers $(A, B)$ such that: - $1 \le A \le B \le N$ - $A \oplus B = N$ - if there is no way to choose $A$ and $B$ satisfying the above conditions, $A = B = -1$ - otherwise, the value of $A$ should be the smallest possible These conditions uniquely define the XPR. Next, we define a function $F(N)$ = the value of $B$ in $XPR(N)$, and a function $G(L, R) = \sum\limits_{i=L}^R F(i)$. You are given $L$ and $R$. Compute $G(L, R)$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the value of the function $G(L, R)$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^9$ -----Example Input----- 5 1 10 3 6 4 10 10 17 100 159 -----Example Output----- 28 9 28 79 7485
["import math\ndef GLR(x):\n summation_N = (x*(x+1))//2\n initial = x\n power = 0\n sum_A = 0\n while x>=1:\n count = (x+1)//2\n sum_A += count * 2**power\n x = x - count\n power += 1\n sum_B = summation_N - sum_A\n ans = sum_B - (int(math.log(initial,2))+1)\n return ans\n \nfor _ in range(int(input())):\n l,r = list(map(int,input().split()))\n if l==1:\n print(GLR(r))\n else:\n print((GLR(r) - GLR(l-1)))# cook your dish here\n", "import math \nfrom collections import defaultdict\ndef IsPowerOfTwo(x):\n return (x != 0) and ((x & (x - 1)) == 0)\n\ndef getFirstSetBitPos(n): \n return int(math.log2(n&-n)+1)-1\n\ndef xpr(n):\n if IsPowerOfTwo(n): \n return (-1, -1)\n # Has the first set bit from lsb\n index = getFirstSetBitPos(n)\n # print(\"Index : \", index)\n # Convert the number to binary string\n binary_n_string = str(bin(n))[2:]\n # print(binary_n_string)\n a = \"\"\n b = \"\"\n i = 0\n for bit in reversed(binary_n_string):\n if i < index:\n a = '0' + a\n b = '0' + b\n elif i == index:\n a = '1' + a\n b = '0' + b\n else:\n if bit == '0':\n a = '0' + a\n b = '0' + b\n else:\n a = '0' + a\n b = '1' + b\n i = i + 1\n a, b = int(a, 2), int(b, 2)\n return (a, b)\n \n \ndef glr(x):\n tot = (x*(x+1))//2\n lb = 0\n while x >= 1:\n tot -= int(((x+1)//2)*2**lb)\n lb += 1\n tot -= 1\n x = x / 2\n return tot \n \nt = int(input())\nwhile t > 0:\n l, r = list(map(int,input().split()))\n print(glr(r) - glr(l-1))\n t = t - 1", "import math \nfrom collections import defaultdict\ndef IsPowerOfTwo(x):\n return (x != 0) and ((x & (x - 1)) == 0)\n\ndef getFirstSetBitPos(n): \n return int(math.log2(n&-n)+1)-1\n\ndef xpr(n):\n if IsPowerOfTwo(n): \n return (-1, -1)\n # Has the first set bit from lsb\n index = getFirstSetBitPos(n)\n # print(\"Index : \", index)\n # Convert the number to binary string\n binary_n_string = str(bin(n))[2:]\n # print(binary_n_string)\n a = \"\"\n b = \"\"\n i = 0\n for bit in reversed(binary_n_string):\n if i < index:\n a = '0' + a\n b = '0' + b\n elif i == index:\n a = '1' + a\n b = '0' + b\n else:\n if bit == '0':\n a = '0' + a\n b = '0' + b\n else:\n a = '0' + a\n b = '1' + b\n i = i + 1\n a, b = int(a, 2), int(b, 2)\n return (a, b)\n \n \ndef glr(x):\n tot = (x*(x+1))//2\n lb = 0\n while x >= 1:\n tot -= int(((x+1)//2)*2**lb)\n lb += 1\n tot -= 1\n x = x / 2\n return tot \n \nt = int(input())\nwhile t > 0:\n l, r = list(map(int,input().split()))\n sumt = 0\n # dict1 = defaultdict(int)\n # for i in range(l, r+1):\n # val = xpr(i)\n # if val[1] != -1 and i%2 == 0:\n # dict1[val[1]] += 1\n # # print(i, val)\n # sumt += val[1]\n \n print(glr(r) - glr(l-1))\n t = t - 1", "def fun(m):\n sumi=(m*(m+1))//2\n f=1\n while(f<=m):\n x=(m//f)+1\n sumi-=1+f*((x//2))\n f*=2\n return sumi \n \nt=int(input())\nfor _ in range(t):\n l,r=map(int,input().split())\n print(fun(r)-fun(l-1))", "# cook your dish here\n\nfrom math import log2\n\n\ndef get_a(n):\n if n < 1:\n return 0\n if n == 1:\n return 1\n return (n + 1) // 2 + get_a(n // 2) * 2\n\n\ndef get(n):\n if n < 1:\n return 0\n total = n * (n + 1) // 2\n a = get_a(n)\n return total - a - int(log2(n)) - 1\n\n\ndef read():\n t = int(input())\n for i in range(t):\n l, r = list(map(int, input().strip().split()))\n print(get(r) - get(l - 1))\n\n\nread()\n", "#!/usr/bin/python3\nimport math\n\ndef xor_solution(l, r):\n sum_xors = 0\n for i in range(l, r + 1):\n if((i & (i - 1)) == 0):\n sum_xors = sum_xors - 1\n else:\n sum_xors = sum_xors + (i & (i - 1))\n return sum_xors\n \n\ndef sum_xor(x):\n if(x == 0):\n return 0\n sum_n = (x * (x + 1)) // 2\n temp = x\n power2 = 1\n sum_a = 0\n while(x >= 1):\n count = (x + 1) // 2\n sum_a += count * power2\n x = x - count\n power2 = power2 * 2\n\n sum_b = sum_n - sum_a\n return (sum_b - (int(math.log2(temp)) + 1))\n\ndef main():\n test_case = int(input())\n ans = []\n while(test_case > 0):\n l, r = list(map(int, input().split()))\n ans.append(sum_xor(r) - sum_xor(l - 1))\n\n test_case -= 1\n \n for i in ans:\n print(i)\n\ndef __starting_point():\n main()\n\n__starting_point()", "# cook your dish here\ndef ans(p):\n pika=(p*(p+1))//2\n tmep=1\n while(p>=1):\n pika-=(p+1)//2*tmep\n pika-=1\n p//=2\n tmep*=2\n return pika\nfor _ in range(int(input())):\n l,r=map(int,input().split())\n print(ans(r)-ans(l-1))", "import math\ndef GLR(x):\n summation_N = (x*(x+1))//2\n initial = x\n power = 0\n sum_A = 0\n while x>=1:\n count = (x+1)//2\n sum_A += count * 2**power\n x = x - count\n power += 1\n sum_B = summation_N - sum_A\n ans = sum_B - (int(math.log(initial,2))+1)\n return ans\n \nfor _ in range(int(input())):\n l,r = map(int,input().split())\n if l==1:\n print(GLR(r))\n else:\n print(GLR(r) - GLR(l-1))", "import math\ndef GLR(x):\n summation_N = (x*(x+1))//2\n initial = x\n power = 0\n sum_A = 0\n while x>=1:\n count = (x+1)//2\n sum_A += count * 2**power\n x = x - count\n power += 1\n sum_B = summation_N - sum_A\n ans = sum_B - (int(math.log(initial,2))+1)\n return ans\n \nfor _ in range(int(input())):\n l,r = list(map(int,input().split()))\n if l==1:\n print(GLR(r))\n else:\n print(GLR(r) - GLR(l-1))\n", "import math\nt=int(input())\n\ndef glr(x):\n sigma_a=0\n a=x\n sigma_n=(x*(x+1))//2\n p=0\n while(x>0):\n \n if(x%2==0):\n count=x//2\n else:\n count=(x+1)//2\n\n sigma_a+=count*(2**p)\n x-=count\n p+=1\n sigma_b=sigma_n-sigma_a\n\n sigma_b=sigma_b+(-1*(int(math.log(a,2))))\n\n return sigma_b\n \n\nfor i in range(t):\n \n\n l,r=list(map(int,input().split()))\n\n if(l>1):\n ans=glr(r)-glr(l-1)\n else:\n \n ans=glr(r)-1\n \n \n \n\n \n\n\n print(ans)\n \n\n \n\n \n", "# cook your dish here\nimport math\ndef jd(x):\n n=x\n y=(x*(x+1))//2\n ans=0\n p=0\n while n>=1:\n c=(n+1)//2\n ans+=int(c*math.pow(2,p))\n n-=c\n p+=1\n y-=ans\n y-=int(math.log2(x)+1)\n return int(y)\nfor _ in range(int(input())):\n l,r=map(int,input().split())\n if l>1:\n print(jd(r)-jd(l-1))\n else:\n print(jd(r))", "from math import log2\ndef grx(x):\n if(x==0):\n return 0\n i = x\n summation_n = (x*(x+1))//2\n curr = (x+1)//2\n ans = 0\n p = 0\n while(x>=1):\n ans+=((2**p)*(curr))\n x-=curr\n curr = (x+1)//2\n p+=1\n return (summation_n-ans-int(log2(i))-1)\nfor _ in range(int(input())):\n l,r = map(int,input().split())\n print(grx(r)-grx(l-1))", "# cook your dish here\nimport math\ndef howmany(n):\n total=n*(n+1)//2 \n a=1 \n while(a<=n):\n total=total-a*((n-a)//(2*a)+1)\n total=total-1 \n a=a*2 \n return total \nt=int(input())\nfor you in range(t):\n l=input().split()\n x=int(l[0])\n y=int(l[1])\n print(howmany(y)-howmany(x-1))\n \n", "import math\ndef count(N):\n if(N<1):\n return 0\n total=N*(N+1)//2\n a=1\n d=2\n while(N>=a):\n n=math.floor((N-a)/d)+1\n total-=(n*a+1)\n a*=2\n d*=2\n return total\n \n \n\nT=int(input())\nfor _ in range(T):\n L,R=list(map(int,input().split()))\n print(count(R)-count(L-1))\n", "# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Sun Dec 22 22:04:23 2019\n\n@author: user\n\"\"\"\nimport math\ndef ceil(n):\n if(int(n)==n):\n return int(n)\n else:\n return int(n)+1\ndef compute(n):\n if(n==0):\n return 0\n temp=int(math.log(n,2))+1\n ans=((n)*(n+1))//2\n ans-=temp \n rr=1 \n while(n!=0):\n ans-=ceil(n/2)*rr\n n=n//2\n rr=rr*2\n return ans\n \nt=int(input())\nfor i in range(t):\n l,r=list(map(int,input().split()))\n ans=compute(r)-compute(l-1)\n print(ans)\n", "import math\ndef Fun(a):\n if(a==0):\n return 0\n sum_n = (a*(a+1))//2\n ini = a\n p=0\n anss = 0\n while(a>=1):\n c = (a+1)//2\n anss += c*pow(2,p)\n a= a-c\n p+=1\n dd = int(math.log2(ini))\n return (sum_n-anss-dd-1)\n \nt = int(input())\nwhile(t>0):\n arr = list(map(int,input().split()))\n l = arr[0]\n r = arr[1]\n ans = Fun(r) - Fun(l-1)\n print(ans)\n t=t-1", "# cook your dish here\nimport math\ndef GLR(x):\n summation_N = (x*(x+1))//2\n initial = x\n power = 0\n sum_A = 0\n while x>=1:\n count = (x+1)//2\n sum_A += count * 2**power\n x = x - count\n power += 1\n sum_B = summation_N - sum_A\n ans = sum_B - (int(math.log(initial,2))+1)\n return ans\n\nfor _ in range(int(input())):\n l,r = map(int,input().split())\n if l==1:\n print(GLR(r))\n else:\n print(GLR(r) - GLR(l-1))", "from math import *\ndef find(n):\n if n==0:\n return 0\n if n==1:\n return -1\n if n==2:\n return -2\n a=1\n ans=0\n while a<=n:\n ans+=(floor(n/a)-floor(n/(2*a)))*a\n # print((floor(n/a)-floor(n/(2*a)))*a)\n a*=2\n k=int(log(n,2))\n return n*(n+1)//2-int(ans)-k-1\n#print(find(17))\n\nt=int(input())\nfor _ in range(t):\n l,r=list(map(int,input().split()))\n print(find(r)-find(l-1))\n", "import math\ndef GLR(x):\n summation_N = (x * (x + 1)) // 2\n initial = x\n power = 0\n sum_A = 0\n while x >= 1:\n count = (x + 1) // 2\n sum_A += count * 2 ** power\n x -= count\n power += 1\n sum_B = summation_N - sum_A\n ans = sum_B - ( int(math.log(initial, 2)) + 1)\n return ans\n\nfor _ in range(int(input())):\n l, r = list(map(int, input().split()))\n if l == 1:\n print(GLR(r))\n else: \n print(GLR(r) - GLR(l - 1))\n", "import math\nt=int(input())\nfor _ in range(t):\n a,b=list(map(int,input().split()))\n if a==1:\n ans=-1\n else:\n ans=0\n k=((b-a+1)*(b+a))//2\n ans+=k\n if a%2:\n ans-=math.ceil((b-a+1)/2)\n else:\n if b%2:\n ans-=math.ceil((b-a)/2)\n else:\n ans-=(b-a)//2\n i=2**30\n c=0\n while i>=2:\n x=b//i-(a-1)//i\n d=x-c\n c=x\n ans-=d*i\n if i>=a and i<=b:\n ans-=1\n i=i//2\n print(ans)\n \n \n \n \n \n \n \n", "import math\ndef xpn(n):\n sum_n = (n*(n+1))//2\n temp = n \n sum_a = 0\n p = 0\n while n>=1:\n c = (n+1)//2\n sum_a += c * 2**p\n n-=c\n p+=1 \n sum_b = sum_n - sum_a\n b = sum_b - (int(math.log(temp,2))+1)\n return b \nt = int(input())\nfor _ in range(t):\n l,r = map(int,input().split())\n if l==1:\n m = xpn(r)\n else:\n m = xpn(r) - xpn(l-1)\n print(m) ", "import numpy as np\nfrom numba import njit \n\n@njit\ndef solve(a):\n b = 1\n for i in range(31):\n if a < b:\n length = i\n b >>= 1\n break\n b <<= 1\n res = a * (a + 1) // 2\n cumusum = 0\n for _ in range(length):\n n = a // b - cumusum\n res -= n * b + 1\n cumusum += n\n b >>= 1\n return res\n\ndef main():\n T = int(input())\n for _ in range(T):\n l, r = list(map(int, input().split()))\n print(solve(r) - solve(l - 1))\n\ndef __starting_point():\n main()\n\n__starting_point()", "def solve(a):\n b = 1\n for i in range(31):\n if a < b:\n length = i\n b >>= 1\n break\n b <<= 1\n res = a * (a + 1) // 2\n cumusum = 0\n for _ in range(length):\n n = a // b - cumusum\n res -= n * b + 1\n cumusum += n\n b >>= 1\n return res\n\ndef main():\n T = int(input())\n for _ in range(T):\n l, r = list(map(int, input().split()))\n print(solve(r) - solve(l - 1))\n\ndef __starting_point():\n main()\n\n__starting_point()", "# cook your dish here\nimport math\ndef GLR(x):\n if x == 0:\n return 0\n y = x\n p = 0\n sum_a = 0\n \n while x>=1:\n c = (x+1)//2\n sum_a += c*2**p\n p += 1\n x = x - c\n \n sum_b = (y*(y+1))//2 - sum_a\n ans = sum_b - (int(math.log2(y))+1)\n return ans\n\nfor t in range(int(input())):\n l,r = map(int,input().split(' '))\n ans = GLR(r)-GLR(l-1)\n print(ans)"]
{"inputs": [["5", "1 10", "3 6", "4 10", "10 17", "100 159"]], "outputs": [["28", "9", "28", "79", "7485"]]}
INTERVIEW
PYTHON3
CODECHEF
11,201
301f07583e33e5e9b73a46782986cba2
UNKNOWN
The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime. -----Input----- The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each. -----Output----- Write a single integer to output, denoting how many integers ti are divisible by k. -----Example----- Input: 7 3 1 51 966369 7 9 999996 11 Output: 4
["#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "(n,k) = map(int, input().split())\r\nans=0\r\nfor i in range(n):\r\n\tt=int(input())\r\n\tif t%k==0:\r\n\t\tans+=1\r\n\r\nprint(ans)\t", "(n,k) = map(int, input().split())\r\nans=0\r\nfor i in range(n):\r\n\tt=int(input())\r\n\tif t%k==0:\r\n\t\tans+=1\r\n\r\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "n,k=map(int,input().split())\nans=0\nfor i in range(n):\n t=int(input())\n if t%k==0:\n ans+=1\nprint(ans)", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "n,k=input().split()\r\ntotal=0\r\nfor i in range(0, int(n)):\r\n t=int(input())\r\n if t%int(k)==0:\r\n total+=1\r\nprint(total)", "n,k = input().split()\r\ncount=0\r\nfor i in range (0,int(n)):\r\n x=int(input())\r\n if(x%int(k) == 0):\r\n count+=1\r\n else:\r\n continue\r\nprint(count)\r\n", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the term\nn,k=list(map(int,input().split()))\nans = 0\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "(n, k) = map(int, input().split())\r\nans = 0\r\nfor i in range(n):\r\n\tx = int(input())\r\n\tif x % k == 0:\r\n\t\tans += 1\r\nprint(ans)\t", "(n, k) = map(int, input().split())\nans = 0\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)", "(n, k) = map(int, input().split())\r\nans = 0\r\nfor i in range(n):\r\n\tx = int(input())\r\n\tif x % k == 0:\r\n\t\tans += 1\r\n\r\nprint(ans)\t", "(n, k) = map(int, input().split())\r\nans = 0\r\nfor i in range(n):\r\n\tx = int(input())\r\n\tif x % k == 0:\r\n\t\tans += 1\r\n\r\nprint(ans)\t", "n, k = map(int, input().split())\r\nans = 0\r\nfor i in range(n):\r\n\tx = int(input())\r\n\tif x % k == 0:\r\n\t\tans += 1\r\n\r\nprint(ans)\t", "n,k = map(int,input().split())\r\ncount = 0\r\nfor i in range(n):\r\n t = int(input())\r\n if t % k == 0:\r\n count += 1\r\nprint(count)", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the termi\nn,k= list(map(int,input().split()))\nsum=0\nfor i in range(n): \n if int(input())%k==0:\n sum+=1\nprint(sum) ", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\nn,k=list(map(int, input().split()))\ni=1\nsum=0\nwhile i<=n:\n \n z=int(input())\n if z%k==0:\n sum=sum+1\n i=i+1\n else:\n i=i+1 \n \nprint(sum)\n", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\nn, k = list(map(int, input().split(' ')))\nans = 0\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\nprint(ans)\t", "#Note that it's python3 Code. Here, we are using input() instead of raw_input().\n#You can check on your local machine the version of python by typing \"python --version\" in the terminal.\n\n(n, k) = list(map(int, input().split(' ')))\n\nans = 0\n\nfor i in range(n):\n\tx = int(input())\n\tif x % k == 0:\n\t\tans += 1\n\nprint(ans)\t"]
{"inputs": [["7 3", "1", "51", "966369", "7", "9", "999996", "11"]], "outputs": [["4"]]}
INTERVIEW
PYTHON3
CODECHEF
6,344
78a53389812fe6124f208a0a65bb37a4
UNKNOWN
Chef has recently been playing a lot of chess in preparation for the ICCT (International Chef Chess Tournament). Since putting in long hours is not an easy task, Chef's mind wanders elsewhere. He starts counting the number of squares with odd side length on his chessboard.. However, Chef is not satisfied. He wants to know the number of squares of odd side length on a generic $N*N$ chessboard. -----Input:----- - The first line will contain a single integer $T$, the number of test cases. - The next $T$ lines will have a single integer $N$, the size of the chess board. -----Output:----- For each test case, print a integer denoting the number of squares with odd length. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N \leq 1000$ -----Sample Input:----- 2 3 8 -----Sample Output:----- 10 120
["# cook your dish here\nt=int(input())\nfor i in range(t):\n a=0\n n=int(input())\n while(n>0):\n a += n*n\n n=n-2\n print(a)\n", "t=int(input())\nfor i in range (t):\n a=0\n n=int(input())\n while(n>0):\n a += n*n\n n=n-2\n print(a)\n", "t=int(input())\nfor i in range (t):\n a=0\n n=int(input())\n while(n>0):\n a += n*n\n n=n-2\n print(a)\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n s=0\n if n%2==0:\n for i in range(2,n+1,2):\n s=s+(i*i)\n else:\n for i in range(1,n+1,2):\n s=s+(i*i)\n print(s)", "# cook your dish here\nfor i in range(int(input())):\n a=0\n n=int(input())\n while(n>0):\n a += n*n\n n=n-2\n print(a)\n \n", "for i in range(int(input())):\n a=0\n n=int(input())\n while(n>0):\n a += n*n\n n=n-2\n print(a)", "# cook your dish here\nt=int(input())\nfor i in range(t):\n a=0\n n=int(input())\n while(n>0):\n a=a+(n*n)\n n=n-2\n print(a)", "# cook your dish here\nt=int(input())\nfor i in range(t):\n c=0\n n=int(input())\n while(n>0):\n c=c+(n*n)\n n=n-2\n print(c)", "# cook your dish here\nfor i in range(int(input())):\n x = int(input())\n s = 0\n for i in range(1,x+1,2):\n k = x-i+1 \n s+= k*k \n print(s)", "# cook your dish here\nx=int(input())\nfor i in range(x):\n c=0\n y=int(input())\n while(y>0):\n c=c+(y*y)\n y=y-2\n print(c)", "# cook your dish here\nfor i in range(int(input())):\n x=int(input())\n c=0\n for i in range(1,x+1,2):\n tot=x-i+1\n c+=(tot*tot)\n print(c)", "for _ in range(int(input())):\n n=int(input())\n c=0\n for i in range(1,n+1,2):\n k=n-i+1\n c+=k**2\n print(c)", "t=int(input())\nfor _ in range(t):\n a=0\n n=int(input())\n while(n>0):\n a+=(n*n)\n n-=2\n print(a)\n", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n s=0\n n=int(input())\n while(n>0):\n s+=(n*n)\n n-=2\n print(s)", "for _ in range(int(input())):\n n=int(input())\n count=0\n for i in range(1,n+1,2):\n k=n-i+1\n count+=(k*k)\n print(count)", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n count=0\n for i in range(1,n+1,2):\n k=n-i+1\n count+=(k*k)\n print(count)\n", "t = int(input())\nwhile t :\n n = int(input())\n count = 0\n for i in range(1 , n + 1 , 2 ) :\n count = count + ((n + 1 - i) * (n + 1 -i))\n print(count)\n t = t-1\n", "# cook your dish here\n\ndef countSquares(n):\n return sum([(n - i + 1) ** 2 for i in range(1, n + 1, 2)])\n\ndef __starting_point():\n t = int(input())\n for i in range(t):\n n = int(input())\n print(countSquares(n))\n__starting_point()", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n count1=0\n for i in range(1,n+1,2):\n k=n-i+1\n count1+=k*k\n print(count1)", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n cnt=0\n for i in range(1,n+1,2):\n s=n+1-i\n cnt=cnt+s*s\n print(cnt)\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n count1=0\n for i in range(1,n+1,2):\n k=n-i+1\n count1+=k*k\n print(count1)", "for _ in range(int(input())):\n n=int(input())\n t=n\n s=0\n while t>0:\n s=s+(t*t)\n t=t-2\n print(s)", "# cook your dish here\nfor t in range(int(input())):\n N = int(input())\n sum = 0\n for i in range(1, N + 1, 2):\n sum += (N - i + 1) ** 2\n print(sum)\n", "import sys\nimport math\nimport bisect\nfrom math import factorial as fact\ndef input(): return sys.stdin.readline().strip()\ndef iinput(): return int(input())\ndef rinput(): return map(int, sys.stdin.readline().strip().split()) \ndef get_list(): return list(map(int, sys.stdin.readline().strip().split())) \nmod = int(1e9)+7\n\ndef ncr(n, i):\n num = fact(n)\n den = fact(i)*fact(n-i)\n\n return num//den\n\nfor _ in range(iinput()):\n n = iinput()\n ans = 0\n\n for i in range(1, n+1, 2):\n ans += (n-i+1)*(n-i+1)\n\n print(ans) ", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n count_odd_square=0\n for i in range(1,n+1,2):\n k=n-i+1\n count_odd_square+=(k*k)\n print(count_odd_square)"]
{"inputs": [["2", "3", "8"]], "outputs": [["10", "120"]]}
INTERVIEW
PYTHON3
CODECHEF
3,963
27a49498c1994cefe06eafd169696101
UNKNOWN
Today, Chef has a fencing job at hand and has to fence up a surface covering N$N$ points. To minimize his work, he started looking for an algorithm that had him fence the least amount of length. He came up with the Convex Hull algorithm, but soon realized it gave him some random shape to fence. However, Chef likes rectangles and has a favourite number M$M$. Help him find the minimum perimeter he has to fence if he wants to fence a rectangle, with slope of one of the sides as M$M$, to cover all the points. -----Input:----- - The first line contains two integers N$N$ and M$M$, the number of points and the Chef's favourite Number. - The next n lines contain two space separated integers X$X$ and Y$Y$, the coordinates of the point. -----Output:----- Print a single decimal number, denoting the perimeter of the rectangle. Answer will considered correct if it has absolute error less than 10−6$10^{-6}$. -----Constraints----- - 2≤N≤1000000$2 \leq N \leq 1000000$ - −1000≤M≤1000$-1000 \leq M \leq 1000$ - −1000000≤X≤1000000$-1000000 \leq X \leq 1000000$ - −1000000≤Y≤1000000$-1000000 \leq Y \leq 1000000$ -----Sample Input:----- 4 1 0 1 0 -1 1 0 -1 0 -----Sample Output:----- 5.656854249492380 -----Note:----- - As the input size is large, it is recommended to use Fast IO.
["import math\n\n\nn,m = map(int, input().split())\nhyp = math.sqrt(1+m*m)\ncosx = 1/hyp\nsinx = m/hyp\n\npts = [[], []]\nfor i in range(n):\n p = input().split()\n px = int(p[0])\n py = int(p[1])\n pts[0].append(cosx*px+sinx*py)\n pts[1].append(cosx*py-sinx*px)\n\nw = max(pts[0])-min(pts[0])\nl = max(pts[1])-min(pts[1])\n\nprint(2*l+2*w)", "# cook your dish here\nimport math\n\n\nn,m = map(int, input().split())\nhyp = math.sqrt(1+m*m)\ncosx = 1/hyp\nsinx = m/hyp\n\npts = [[], []]\nfor i in range(n):\n p = input().split()\n px = int(p[0])\n py = int(p[1])\n pts[0].append(cosx*px+sinx*py)\n pts[1].append(cosx*py-sinx*px)\n\nw = max(pts[0])-min(pts[0])\nl = max(pts[1])-min(pts[1])\n\nprint(2*l+2*w)", "# cook your dish here\n# cook your dish here\nimport math\n\n\nn,m = map(int, input().split())\nhyp = math.sqrt(1+m*m)\ncosx = 1/hyp\nsinx = m/hyp\n\npts = [[], []]\nfor i in range(n):\n p = input().split()\n px = int(p[0])\n py = int(p[1])\n pts[0].append(cosx*px+sinx*py)\n pts[1].append(cosx*py-sinx*px)\n\nw = max(pts[0])-min(pts[0])\nl = max(pts[1])-min(pts[1])\n\nprint(2*l+2*w)", "# cook your dish here\nimport math\n\n\nn,m = map(int, input().split())\nhyp = math.sqrt(1+m*m)\ncosx = 1/hyp\nsinx = m/hyp\n\npts = [[], []]\nfor i in range(n):\n p = input().split()\n px = int(p[0])\n py = int(p[1])\n pts[0].append(cosx*px+sinx*py)\n pts[1].append(cosx*py-sinx*px)\n\nw = max(pts[0])-min(pts[0])\nl = max(pts[1])-min(pts[1])\n\nprint(2*l+2*w)", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[], []]\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n pts[0].append(cosx*px+sinx*py)\r\n pts[1].append(cosx*py-sinx*px)\r\n\r\nw = max(pts[0])-min(pts[0])\r\nl = max(pts[1])-min(pts[1])\r\n\r\nprint(2*l+2*w)", "from math import sqrt\r\nN,M = input().split()\r\nN,M = int(N),float(M)\r\nif(M==0):\r\n n = 0\r\n while(n<N):\r\n \r\n xi,yi = input().split()\r\n xi,yi = float(xi),float(yi)\r\n if n==0:\r\n c1_min = c1_max = xi\r\n c2_min = c2_max = yi\r\n else:\r\n if c1_min>xi:\r\n c1_min = xi\r\n elif c1_max<xi:\r\n c1_max = xi\r\n \r\n if c2_min>yi:\r\n c2_min = yi\r\n elif c2_max<yi:\r\n c2_max = yi\r\n \r\n n+=1\r\n perimeter = 2*(c2_max-c2_min+c1_max-c1_min)\r\nelse:\r\n n = 0\r\n while(n<N):\r\n xi,yi = input().split()\r\n xi,yi = float(xi),float(yi)\r\n temp1 = yi-M*xi\r\n temp2 = yi+xi/M\r\n if n==0:\r\n c1_min = c1_max = temp1\r\n c2_min = c2_max = temp2\r\n else:\r\n if c1_min>temp1:\r\n c1_min = temp1\r\n elif c1_max<temp1:\r\n c1_max = temp1\r\n \r\n if c2_min>temp2:\r\n c2_min = temp2\r\n elif c2_max<temp2:\r\n c2_max = temp2\r\n n+=1\r\n perimeter = 2*(((c1_max-c1_min)/sqrt(1.0+M*M))+\r\n ((c2_max-c2_min)/sqrt(1.0+1.0/(M*M))));\r\nprint(perimeter)", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[], []]\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n pts[0].append(cosx*px+sinx*py)\r\n pts[1].append(cosx*py-sinx*px)\r\n\r\nw = max(pts[0])-min(pts[0])\r\nl = max(pts[1])-min(pts[1])\r\n\r\nprint(2*l+2*w)", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[], []]\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n pts[0].append(cosx*px+sinx*py)\r\n pts[1].append(cosx*py-sinx*px)\r\n\r\nw = max(pts[0])-min(pts[0])\r\nl = max(pts[1])-min(pts[1])\r\n\r\nprint(2*l+2*w)", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[], []]\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n pts[0].append(cosx*px+sinx*py)\r\n pts[1].append(cosx*py-sinx*px)\r\n\r\nw = max(pts[0])-min(pts[0])\r\nl = max(pts[1])-min(pts[1])\r\n\r\nprint(2*l+2*w)", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[], []]\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n pts[0].append(cosx*px+sinx*py)\r\n pts[1].append(cosx*py-sinx*px)\r\n\r\nw = max(pts[0])-min(pts[0])\r\nl = max(pts[1])-min(pts[1])\r\n\r\nprint(2*l+2*w)", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[], []]\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n pts[0].append(cosx*px+sinx*py)\r\n pts[1].append(cosx*py-sinx*px)\r\n\r\nw = max(pts[0])-min(pts[0])\r\nl = max(pts[1])-min(pts[1])\r\n\r\nprint(2*l+2*w)", "import math\r\nimport numpy as np\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npx = np.empty(n)\r\npy = np.empty(n)\r\nfor i in range(n):\r\n p = input().split()\r\n px[i] = int(p[0])\r\n py[i] = int(p[1])\r\n\r\nw = max(cosx*px+sinx*py)-min(cosx*px+sinx*py)\r\nl = max(cosx*py-sinx*px)-min(cosx*py-sinx*px)\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\nimport numpy as np\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = np.array([list(map(int, input().split())) for i in range(n)]).T\r\n \r\nptsx = cosx*pts[0]+sinx*pts[1]\r\nptsy = cosx*pts[1]-sinx*pts[0]\r\n\r\nw = max(ptsx)-min(ptsx)\r\nl = max(ptsy)-min(ptsy)\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\nptsx = []\r\nptsy = []\r\nfor i in range(n):\r\n p = input().split()\r\n px = int(p[0])\r\n py = int(p[1])\r\n ptsx.append(cosx*px+sinx*py)\r\n ptsy.append(cosx*py-sinx*px)\r\n\r\nw = max(ptsx)-min(ptsx)\r\nl = max(ptsy)-min(ptsy)\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\nfor i in range(n):\r\n p = list(map(int, input().strip().split()))\r\n px = cosx*p[0]+sinx*p[1]\r\n py = cosx*p[1]-sinx*p[0]\r\n\r\n if i == 0:\r\n left = px\r\n rght = px\r\n lowr = py\r\n uppr = py\r\n else:\r\n left = min(left, px)\r\n rght = max(rght, px)\r\n lowr = min(lowr, py)\r\n uppr = max(uppr, py)\r\n\r\nw = rght-left\r\nl = uppr-lowr\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\nptsx = []\r\nptsy = []\r\nfor i in range(n):\r\n px, py = list(map(int, input().strip().split()))\r\n ptsx.append(cosx*px+sinx*py)\r\n ptsy.append(cosx*py-sinx*px)\r\n\r\nw = max(ptsx)-min(ptsx)\r\nl = max(ptsy)-min(ptsy)\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\nfor i in range(n):\r\n px, py = list(map(int, input().strip().split()))\r\n px_temp = cosx*px+sinx*py\r\n py_temp = cosx*py-sinx*px\r\n\r\n if i == 0:\r\n px_max = px_temp\r\n px_min = px_temp\r\n py_max = py_temp\r\n py_min = py_temp\r\n\r\n if px_temp > px_max:\r\n px_max = px_temp\r\n elif px_temp < px_min:\r\n px_min = px_temp\r\n\r\n if py_temp > py_max:\r\n py_max = py_temp\r\n elif py_temp < py_min:\r\n py_min = py_temp\r\n\r\nw = px_max-px_min\r\nl = py_max-py_min\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\npts = [list(map(int, input().strip().split())) for i in range(n)]\r\n\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = dict([[cosx*p[0]+sinx*p[1], -sinx*p[0]+cosx*p[1]] for p in pts])\r\n\r\nw = max(pts.keys())-min(pts.keys())\r\nl = max(pts.values())-min(pts.values())\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\npts = [list(map(int, input().strip().split())) for i in range(n)]\r\n\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\npts = [[cosx*p[0]+sinx*p[1], -sinx*p[0]+cosx*p[1]] for p in pts]\r\n\r\nw = max(pts, key=lambda x: x[0])[0]-min(pts, key=lambda x: x[0])[0]\r\nl = max(pts, key=lambda x: x[1])[1]-min(pts, key=lambda x: x[1])[1]\r\n\r\nprint(2*l+2*w)\r\n", "import math\r\n\r\n\r\nn,m = map(int, input().split())\r\npts = [list(map(int, input().strip().split())) for i in range(n)]\r\n\r\nhyp = math.sqrt(1+m*m)\r\ncosx = 1/hyp\r\nsinx = m/hyp\r\n\r\nfor apt in pts:\r\n apt[0],apt[1] = (cosx*apt[0] + sinx*apt[1], -sinx*apt[0] + cosx*apt[1])\r\n\r\nl = max(a[0] for a in pts) - min(a[0] for a in pts)\r\nw = max(a[1] for a in pts) - min(a[1] for a in pts)\r\n\r\nprint(2*l+2*w)\r\n", "import math\nimport sys\n\nn,m = map(int, input().split())\npts = [list(map(int, line.strip().split())) for line in sys.stdin]\n\nhyp = math.sqrt(1+m*m)\ncosx = 1/hyp\nsinx = m/hyp\n\nfor apt in pts:\n apt[0],apt[1] = (cosx*apt[0] + sinx*apt[1], -sinx*apt[0] + cosx*apt[1])\n\nl = max(a[0] for a in pts) - min(a[0] for a in pts)\nw = max(a[1] for a in pts) - min(a[1] for a in pts)\n\nprint(2*l+2*w)", "n, m = [int(x) for x in input().split()]\ncoordinates = []\nfor i in range(n):\n coordinates.append([int(y) for y in input().split()])\nhyp = (1+m*m)**(1/2)\ncosx = 1/hyp\nsinx = m/hyp\nfor point in coordinates:\n point[0], point[1] = (cosx*point[0] + sinx*point[1], -sinx*point[0] + cosx*point[1])\n\nl = max(a[0] for a in coordinates) - min(a[0] for a in coordinates)\nb = max(a[1] for a in coordinates) - min(a[1] for a in coordinates)\n\nprint(2*(l+b))"]
{"inputs": [["4 1", " 0 1", " 0 -1", " 1 0", " -1 0", ""]], "outputs": [["5.656854249492380"]]}
INTERVIEW
PYTHON3
CODECHEF
10,199
2fd9b4d645c4ea370cf50cfdde86e1be
UNKNOWN
Mia is working as a waitress at a breakfast diner. She can take up only one shift from 6 shifts a day i.e. from 10 am to 4 pm. She needs to save 300$ after completion of the month. She works only for $D$ days in the month. She estimates that she gets her highest tip in the first shift and the tip starts decreasing by 2% every hour as the day prolongs. She gets a minimum wage of $X$ $ for every shift. And her highest tip in the first shift is $Y$ $. Determine whether Mia will be able to save 300$ from her wages and tips after working $D$ days of the month. If she can, print YES, else print NO. -----Constraints----- - 8 <= D <=30 - 7 <= X <=30 - 4 <= Y <= 20 -----Input:----- - First line has three parameters $D$, $X$ and $Y$ i.e. number of days worked, minimum wage and highest tip. - Second line contains D integers indicating her shifts every $i$-th day she has worked. -----Output:----- - Print YES, if Mia has saved 300$, NO otherwise. -----Sample Input:----- 9 17 5 1 3 2 4 5 6 1 2 2 -----Sample Output:----- NO -----Explanation:----- No. of days Mia worked (D) is 9, so minimum wage she earns (X) is 17 dollars. Highest tip at first hour (Y) = 5 dollars, 1st day she took 1st shift and 2nd day she took 3rd shift and so on. Upon calculation we will find that Mia was not able to save 300 dollars.
["# cook your dish here\n\nl=[int(k) for k in input().split()]\ns=[int(k) for k in input().split()]\nx=l[1]*l[0]\nfor i in range(l[0]):\n if(s[i]==1):\n x+=l[2]\n elif(s[i]==2):\n x+=(l[2]*98/100)\n elif(s[i]==3):\n x+=(l[2]*96/100)\n elif(s[i]==4):\n x+=(l[2]*94/100)\n elif(s[i]==5):\n x+=(l[2]*92/100)\n elif(s[i]==6):\n x+=(l[2]*90/100)\nif(x>=300):\n print(\"YES\")\nelse:\n print(\"NO\")", "# cook your dish here\nn,sal,tip=list(map(int,input().split()))\na=list(map(int,input().split()))\nres=n*sal\nfor i in range(len(a)):\n num=a[i]-1\n res+=(tip-(tip*2*num/100))\n#print(res) \nif res<300:\n print(\"NO\")\nelse:\n print(\"YES\")\n", "# cook your dish here\nn,sal,tip=list(map(int,input().split()))\na=list(map(int,input().split()))\nres=n*sal\nfor i in range(len(a)):\n num=a[i]-1\n res+=(tip-(tip*2*num/100))\n#print(res) \nif res<300:\n print(\"NO\")\nelse:\n print(\"YES\")\n", "n,x,y=map(int,input().split())\nl=list(map(int,input().split()))\nc=0\nfor i in l:\n c+=x\n if i==1:\n c+=y\n else:\n t=(i-1)*(y*2/100)\n if y-t>0:\n c+=y-t\n if c>=300:\n print(\"YES\")\n break\nelse:\n print(\"NO\")", "ns,x,y = list(map(int,input().split()))\nshifts = list(map(int,input().split()))\ndis = 0.02 * y \nshift_amt = [x+y]+ [ (x+(y-(i*dis))) for i in range(1,6) ]\n\namt = 0\nfor sn in shifts:\n amt += shift_amt[sn-1]\n\nif(amt<300):\n print(\"NO\",end=\"\")\nelse:\n print(\"YES\",end=\"\")", "# cook your dish here\nd,x,y=map(int,input().split())\nl=list(map(int,input().split()))\ns=0\ns+=d*x\ntip=y\ntipm=0\nn=len(l)\nfor i in range(n):\n\ty=tip\n\tfor j in range(6*(l[i]-1)):\n\t\ty-=0.02*y\n\ttipm+=y\ns+=tipm\n#print(s)\nif s>=300:\n\tprint('YES')\nelse:\n\tprint('NO')", "# cook your dish here\nd, wage, tip = map(int, input().split())\n\narr = list(map(int, input().split()))\n\ns = 0\nwage_arr = [wage + tip*(1 - (1/50)*i) for i in range(6)]\n\nfor i in range(d):\n s += wage_arr[arr[i]-1]\n\nif s >= 300:\n print(\"YES\")\nelse:\n print(\"NO\")", "try:\r\n def total(i,t):\r\n dec = 0\r\n t1 = t\r\n for i in range(i-1):\r\n dec = (t1*2)/100\r\n t1 = dec\r\n earn = t - (i*dec)\r\n return earn \r\n d,w,t=list(map(int,input().split()))\r\n s=list(map(int,input().split()))\r\n m=0\r\n for i in s:\r\n m=m+w+total(i,t)\r\n if m>=300:\r\n print(\"YES\")\r\n else:\r\n print(\"NO\") \r\nexcept:\r\n pass", "d,x,y=map(int,input().split())\r\narr=[int(i) for i in input().split()];s=x*d;arr1=[]\r\nfor i in range(6):\r\n arr1.append(i*2)\r\nfor i in arr:\r\n s=s+(y-(y*arr1[i-1]*2/100))\r\nif s>=300:\r\n print(\"YES\")\r\nelse:\r\n print(\"NO\")", "n,x,y=map(int,input().split())\nl=list(map(int,input().split()))\nc=0\nfor i in l:\n c+=x\n if i==1:\n c+=y\n else:\n t=(i-1)*(y*2/100)\n if y-t>0:\n c+=y-t\n if c>=300:\n print(\"YES\")\n break\nelse:\n print(\"NO\")", "# cook your dish here\nd,x,y=map(int,input().split())\narr=[int(i) for i in input().split()];s=x*d;arr1=[]\nfor i in range(6):\n arr1.append(i*2)\nfor i in arr:\n s=s+(y-(y*arr1[i-1]*2/100))\nif s>=300:\n print(\"YES\")\nelse:\n print(\"NO\")", "d, x, y = list(map(int,input().split()))\r\nl = list(map(int,input().split()))\r\n\r\nk = [y]\r\n\r\nfor i in range(6):\r\n k.append(k[-1]*0.98)\r\n\r\n\r\ns = x*d\r\n\r\nfor i in l:\r\n s += k[i-1]\r\n\r\nif s >= 300:\r\n print(\"YES\")\r\nelse:\r\n print(\"NO\")\r\n", "try:\n n,x,y = map(int,input().split())\n shifts = list(map(int,input().split()))\n totel = x*n\n for i in shifts:\n tip = int(y *(1-(2*i)//100))\n totel += tip\n if totel <300:\n print('NO')\n else:\n print('YES')\nexcept:\n pass", "n,x,y=list(map(int,input().split()))\na=list(map(int,input().split()))\nans=x*n\ntemp1=(y-(y*2)/100)\ntemp2=(temp1-(y*2)/100)\ntemp3=(temp2-(y*2)/100)\ntemp4=(temp3-(y*2)/100)\ntemp5=(temp4-(y*2)/100)\nfor i in range(n):\n if a[i]==1:\n ans+=y\n elif a[i]==2:\n ans+=temp1\n elif a[i]==3:\n ans+=temp2\n elif a[i]==4:\n ans+=temp3\n elif a[i]==5:\n ans+=temp4\n elif a[i]==6:\n ans+=temp5\nif int(ans)>=300:\n print(\"YES\")\nelse:\n print(\"NO\")\n \n\n", "(D,X,Y)=list(map(int,input().split()))\r\ns=list(map(int,input().split()))\r\nT=[]\r\nfor _ in range(6):\r\n T.append(Y)\r\n Y=Y-(Y/50)\r\nt=0\r\nfor i in s:\r\n t=t+T[i-1]\r\np=D*X+t\r\nif(p>=300):\r\n print(\"YES\")\r\nelse:\r\n print(\"NO\")\r\n \r\n \r\n\r\n\r\n \r\n \r\n", "# cook your dish here\nd,x,y=list(map(int,input().split()))\nl=list(map(int,input().split()))\nwage=0\nwage+=(d*x)\nfor i in l:\n if i==1:\n wage+=y\n elif i==2:\n wage+=(0.98*y)\n elif i==3:\n wage+=(0.96*y)\n elif i==4:\n wage+=(0.94*y)\n elif i==5:\n wage+=(0.92*y)\n elif i==6:\n wage+=(0.90*y)\nif wage>=300:\n print(\"YES\")\nelse:\n print(\"NO\")\n", "# cook your dish here\nd,x,y=list(map(int,input().split()))\nl=list(map(int,input().split()))\nwage=0\nwage+=(d*x)\nfor i in l:\n if i==1:\n wage+=y\n elif i==2:\n wage+=(0.98*y)\n elif i==3:\n wage+=(0.96*y)\n elif i==4:\n wage+=(0.94*y)\n elif i==5:\n wage+=(0.92*y)\n elif i==6:\n wage+=(0.90*y)\nif wage>=300:\n print(\"YES\")\nelse:\n print(\"NO\")\n", "#Enter your code here...\ntry:\n num=list(map(int,input().split()))\n n=num[0]\n x=num[1]\n y=num[2]\n inp=list(map(int,input().split()))\n x=x*n\n dic={}\n dic[1]=y\n for i in range(2,n+1):\n dic[i]=(y-0.02*y)\n y=dic[i]\n for i in inp:\n x+=dic[i]\n if x<300:\n print(\"NO\")\n else:\n print(\"YES\")\nexcept EOFError:\n pass", "# cook your dish here\nd,x,y=list(map(float,input().split()))\nl=list(map(int,input().split()))\nsum1=d*x\nft=y\nst=y-(y*2)/100\ntt=y-(y*4)/100\nftt=y-(y*6)/100\nfifth=y-(y*8)/100\nsixth=y-(y*10)/100\nsum2=0\nfor i in range(len(l)):\n if(l[i]==6):\n sum2=sum2+sixth\n elif(l[i]==5):\n sum2=sum2+fifth\n elif(l[i]==4):\n sum2=sum2+ftt\n elif(l[i]==3):\n sum2=sum2+tt\n elif(l[i]==2):\n sum2=sum2+st\n elif(l[i]==1):\n sum2=sum2+ft\nif((sum1+sum2)>=300):\n print('YES')\nelse:\n print('NO')\n", "D,X,Y = map(int,input().split())\r\nworkingDays = list(map(int,input().split()))\r\nlistOfTips = [Y]\r\nfor i in range(1,6):\r\n temp = listOfTips[0] - (0.02*i*(listOfTips[0]))\r\n listOfTips.append(temp)\r\n \r\n#print(listOfTips) \r\ntotal = D*X\r\n#print(total)\r\nfor i in workingDays:\r\n #print(i,\" \",listOfTips[i-1])\r\n total += listOfTips[i-1]\r\n \r\n#print(total)\r\n\r\nprint(\"YES\") if total>=300 else print(\"NO\")", "# cook your dish here\nd,x,y=[int(i) for i in list(input().split())]\nl=[int(i) for i in list(input().split())]\nsav=d*x\ndec=(y*2)/100\nfor i in range(0,d):\n temp=y\n for j in range(1,l[i]):\n temp-=dec\n dec=(temp*2)/100\n sav+=temp\nif sav>=300:\n print('YES')\nelse:\n print(\"NO\")\n \n", "D,X,Y=map(int,input().split())\r\nT=list(map(int,input().split()))\r\nT.sort()\r\nrokda=D*X\r\nans=\"YES\"\r\nfor i in T:\r\n y=Y\r\n for j in range(i-1):\r\n y=0.98*y\r\n rokda+=y\r\nif rokda<300:\r\n ans=\"NO\"\r\nprint(ans)", "d,x,y=list(map(int,input().split()))\r\nans=0\r\nfor i in list(map(int,input().split())):\r\n ans+=x\r\n v=y\r\n for j in range(i-1):\r\n v=v-(2*v)/100\r\n ans+=v\r\n \r\nif ans>=300:\r\n print('YES')\r\nelse:\r\n print('NO')\r\n", "# cook your dish here\nd,x,y=list(map(int,input().split()))\nlis=list(map(int,input().split()))\nans=d*x\nfor i in range(d):\n while(lis[i]>1):\n y=y-(2/100)*y\n lis[i]=lis[i]-1\n ans=ans+y \nif (ans<300):\n print(\"NO\")\nelse:\n print(\"YES\")\n", "D,X,Y = list(map(int,input().split()))\nshifts = list(map(int,input().split()))\nmoney = 0\nfor i in range(D):\n money = money + X + ((0.8)**(shifts[i]-1))*Y\nif money >= 300:\n print(\"YES\")\nelse:\n print(\"NO\")\n\n"]
{"inputs": [["9 17 5", "1 3 2 4 5 6 1 2 2"]], "outputs": [["NO"]]}
INTERVIEW
PYTHON3
CODECHEF
8,422
3b176ac7472640ba1a276747fef3b45d
UNKNOWN
Consider the fraction, $a/b$, where $a$ and $b$ are positive integers. If $a < b$ and $GCD(a,b) = 1$, it is called a reduced proper fraction. If we list the set of a reduced proper fraction for $d \leq 8$, (where $d$ is the denominator) in ascending order of size, we get: $1/8$, $1/7$, $1/6$, $1/5$, $1/4$, $2/7$, $1/3$, $3/8$, $2/5$ , $3/7$, $1/2$, $4/7$, $3/5$, $5/8$, $2/3$, $5/7$, $3/4$, $4/5$, $5/6$, $6/7$, $7/8$ It can be seen that $2/5$ is the fraction immediately to the left of $3/7$. By listing the set of reduced proper fractions for $d \leq N$ in ascending order of value, find the numerator and denominator of the fraction immediately to the left of $a/b$ when $a$ and $b$ are given. -----Input:----- - First line of input contains an integer $T$, number of test cases - Next $T$ lines contain $a$ $b$ $N$ separated by space -----Output:----- Print the numerator and denominator separated by a space corresponding to each test case on a new line -----Constraints----- - $1 \leq T \leq 50$ - $1 \leq a < b \leq 10^9$ - $GCD(a,b) = 1$ - $b < N \leq 10^{15}$ -----Subtasks----- - 10 points: $1 \leq N \leq 100$ - 30 points : $1 \leq N \leq 10^6$ - 60 points : $1 \leq N \leq 10^{15}$ -----Sample Input:----- 5 3 7 8 3 5 8 4 5 8 6 7 8 1 5 8 -----Sample Output:----- 2 5 4 7 3 4 5 6 1 6
["from sys import stdin\nfrom fractions import Fraction\n\ninput = stdin.readline\n\nfor _ in range(int(input())):\n a, b, n = list(map(int, input().split()))\n ab = Fraction(a, b)\n\n p = set()\n\n for i in range(1, n+1):\n for j in range(n, 0, -1):\n x = Fraction(i, j)\n\n if x > ab:\n break\n\n p.add(x)\n\n x = sorted(p)[-2]\n\n print(x.numerator, x.denominator)\n", "from fractions import Fraction as F\r\n\r\n\r\ndef farey_seq(x, limit):\r\n l, r = F(0), F(1)\r\n while l.denominator + r.denominator <= limit:\r\n m = F(l.numerator + r.numerator, l.denominator + r.denominator)\r\n if m < x:\r\n l = m\r\n else:\r\n r = m\r\n if r == x:\r\n break\r\n\r\n if l.denominator + r.denominator <= limit:\r\n k = 1 + (limit - (l.denominator + r.denominator)) // r.denominator\r\n l = F(l.numerator + k * r.numerator, l.denominator + k * r.denominator)\r\n\r\n return l\r\n\r\n\r\n# ProjectEuler problem:\r\n# print(farey_seq(F(3, 7), 1000000).numerator)\r\n\r\nfor _ in range(int(input())):\r\n a, b, n = map(int, input().split())\r\n r = farey_seq(F(a, b), n)\r\n print(r.numerator, r.denominator)", "# Python code to demonstrate naive \n# method to compute gcd ( recursion ) \n\ndef gcd(a,b): \n\tif(b==0): \n\t\treturn a \n\telse: \n\t\treturn gcd(b,a%b) \n\nT=int(input())\nwhile T:\n a,b,N=map(int,input().split())\n c=set()\n ls=[]\n for i in range(1,N+1):\n for j in range(1,i):\n k=gcd(i,j)\n if(j/i not in c):\n c.add(j/i)\n ls.append([j/i,j//k,i//k])\n ls.sort()\n for i in range(len(ls)):\n if(ls[i][1]==a and ls[i][2]==b):\n break\n print(ls[i-1][1],ls[i-1][2])\n T-=1", "R1_HIGH, R1_LOW, R2_HIGH, R2_LOW = 0, 0, 0, 0\n\ndef cross_multiply(a, b, i):\n \n nonlocal R1_HIGH\n nonlocal R1_LOW\n nonlocal R2_HIGH\n nonlocal R2_LOW\n \n if i == 1:\n R1_HIGH = 6*a*b\n R1_LOW = R1_HIGH // 2\n elif i == 2:\n R2_HIGH = 6*a*b\n R2_LOW = R2_HIGH // 2\n\n\n\ndef isLess(a,b,c,d):\n cross_multiply(a, d, 1)\n cross_multiply(c, b, 2)\n \n if (R1_HIGH < R2_HIGH):\n return True\n \n if (R1_HIGH > R2_HIGH):\n return False\n \n return (R1_LOW < R2_LOW)\n \n\n\ndef negb_frac(a,b,N):\n leftN , leftD = 0, 1\n rightN, rightD = 1, 1\n \n while((leftD + rightD) <= N):\n mediantN = leftN + rightN\n mediantD = leftD + rightD\n \n if isLess(mediantN, mediantD, a, b):\n leftN = mediantN\n leftD = mediantD\n else:\n rightN = mediantN\n rightD = mediantD\n \n if (rightN == a and rightD == b):\n break\n \n if ((leftD + rightD) <= N):\n diff = N - (leftD + rightD)\n repeat = 1 + diff // rightD\n leftN += repeat * rightN\n leftD += repeat * rightD\n \n print(leftN, leftD)\n \ndef __starting_point():\n T = int(input())\n for _ in range(T):\n a,b,N = list(map(int, input().split()))\n negb_frac(a,b,N)\n__starting_point()"]
{"inputs": [["5", "3 7 8", "3 5 8", "4 5 8", "6 7 8", "1 5 8"]], "outputs": [["2 5", "4 7", "3 4", "5 6", "1 6"]]}
INTERVIEW
PYTHON3
CODECHEF
3,278
3b220d696de019f9e05aa76e35f2fcf5
UNKNOWN
The name of our college is "Government College of Engineering and Textile Technology Berhampore". There is another college named "Government College of Engineering and Textile Technology Serampore". As the names are quite similar, those who are unaware of existence of both the colleges, often get confused. And mistake one with other. Given a string, if it contains the word berhampore (case insensitive), print GCETTB or if it contains serampore(case-insensitive), print GCETTS . If the string contains neither print Others. If it contains both Berhampore and Serampore print Both Input - First line contains single integer T, No. of test case - Next line for every test contain case a string S Output Print GCETTB or GCETTS or Others or Both on a new line Constraints - 1 <= T <= 10 - 0 <= len(S) <= 100 - S contain a-z and A-Z and space only Sample Input 3 Government clg Berhampore SeRaMporE textile college Girls college Kolkata Sample Output GCETTB GCETTS Others Explanation Self-Explanatory
["# cook your dish here\ntry:\n t=int(input()) \n for i in range(t):\n n=input() \n n=n.lower()\n a=\"berhampore\"\n b=\"serampore\"\n if a in n:\n if b in n:\n print(\"Both\")\n else:\n print(\"GCETTB\")\n elif b in n:\n if a in n:\n print(\"Both\")\n else:\n print(\"GCETTS\")\n else:\n print(\"Others\")\nexcept Exception as e:\n pass", "tc = int(input())\nfor i in range(tc):\n cllg = input()\n cllg = cllg.lower()\n cllglist = cllg.split()\n if(\"berhampore\" in cllglist) and (\"serampore\" in cllglist):\n print(\"Both\")\n elif(\"berhampore\" in cllglist):\n print(\"GCETTB\")\n elif(\"serampore\" in cllglist):\n print(\"GCETTS\")\n else:\n print(\"Others\")", "for _ in range(int(input())):\r\n s = input()\r\n check1 = 'berhampore'\r\n check2 = 'serampore'\r\n if check1 in s.casefold() and check2 in s.casefold():\r\n print(\"Both\")\r\n else:\r\n if check1 in s.casefold():\r\n print(\"GCETTB\")\r\n elif check2 in s.casefold():\r\n print(\"GCETTS\")\r\n else:\r\n print(\"Others\")", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n s=input()\n ss=s.lower()\n a='berhampore'\n b='serampore'\n if a in ss and b in ss:\n print(\"Both\")\n elif a in ss:\n print(\"GCETTB\")\n elif b in ss:\n print(\"GCETTS\")\n else:\n print(\"Others\")", "\"\"\"\nProblem Statement: https://www.codechef.com/COMT2020/problems/BHPORSRP\nAuthor: striker\n\"\"\"\n\ndef main():\n for _ in range(int(input().strip())):\n college_name = input().strip().lower()\n if \"berhampore\" in college_name and \"serampore\" in college_name:\n print(\"Both\")\n elif \"berhampore\" in college_name:\n print(\"GCETTB\")\n elif \"serampore\" in college_name:\n print(\"GCETTS\")\n else:\n print(\"Others\")\n\ndef __starting_point():\n main()\n__starting_point()", "# cook your dish here\nt=int(input())\nfor i in range(t):\n try:\n s=str((input()));\n s1=s.lower();\n if('berhampore' in s1 and 'serampore' not in s1):\n print(\"GCETTB\")\n elif('serampore' in s1 and 'berhampore' not in s1):\n print(\"GCETTS\")\n elif('berhampore' in s1 and 'serampore' in s1):\n print(\"Both\")\n else:\n print(\"Others\")\n except EOFError:\n print(\"Others\")", "try:\n for t in range(int(input())):\n inp = input().lower()\n b = False\n s = False\n for i in inp.split():\n if i == 'berhampore':\n b = True\n elif i == 'serampore':\n s = True\n if b and s:\n print('Both')\n elif b:\n print('GCETTB')\n elif s:\n print('GCETTS')\n else:\n print('Others')\nexcept:\n pass", "# cook your dish here\nt=int(input())\nfor i in range(t):\n a=input()\n a=a.upper()\n b=a.find(\"BERHAMPORE\",0,len(a))\n c=a.find(\"SERAMPORE\",0,len(a))\n if b!= -1 and c == -1:\n print(\"GCETTB\")\n elif b == -1 and c!= -1:\n print(\"GCETTS\")\n elif b != -1 and c!= -1:\n print(\"Both\")\n elif b == -1 and c == -1:\n print(\"Others\")", "try:\r\n\tt=int(input())\r\n\tfor _ in range(t):\r\n\t\tx=input().lower()\r\n\t\tif(\"berhampore\" in x and \"serampore\" in x):\r\n\t\t\tprint(\"Both\")\r\n\t\telif(\"berhampore\" in x):\r\n\t\t\tprint(\"GCETTB\")\r\n\t\telif(\"serampore\" in x):\r\n\t\t\tprint(\"GCETTS\")\r\n\t\telse:\r\n\t\t\tprint(\"Others\")\r\nexcept:\r\n\tpass", "# cook your dish here\nn=int(input())\nfor l in range(n):\n s=list(map(str,input().strip().split()))\n a=[]\n for x in s:\n a.append(x.lower())\n # print(a)\n if \"berhampore\" in a and \"serampore\" in a:\n print(\"Both\")\n elif \"serampore\" in a:\n print(\"GCETTS\")\n elif \"berhampore\" in a:\n print(\"GCETTB\")\n else:\n print(\"Others\")", "# cook your dish here\nfor _ in range(int(input())):\n \n s=(input()).lower()\n if \"berhampore\" in s and \"serampore\" in s:\n print(\"Both\")\n elif \"berhampore\" in s:\n print(\"GCETTB\")\n elif \"serampore\" in s: \n print(\"GCETTS\")\n else:\n print(\"Others\")\n", "# cook your dish here\nt=int(input())\nc=0\nd=0\nfor i in range(t):\n c=0\n d=0\n s=input()\n l=s.split(\" \")\n #l=list(map(str,input().split(\" \")))\n for j in range(len(l)):\n if(l[j].lower()==\"berhampore\"):\n c+=1\n if(l[j].lower()==\"serampore\"):\n d+=1\n if(c>0 and d>0):\n print(\"Both\")\n else:\n if(c>0):\n print(\"GCETTB\")\n else:\n if(d>0):\n print(\"GCETTS\")\n else:\n print(\"Others\")", "# cook your dish here\nfor _ in range(int(input())):\n s=input()\n s1=s.lower()\n if \"berhampore\" in s1:\n if \"serampore\" in s1:\n print(\"Both\")\n else:\n print(\"GCETTB\")\n elif \"serampore\" in s1:\n print(\"GCETTS\")\n else:\n print(\"Others\")\n\n", "# cook your dish here\nfor i in range(0,int(input())):\n s=input()\n l=s.split(\" \")\n p=\"berhampore\"\n q=\"serampore\"\n for i in range(0,len(l)):\n l[i]=l[i].lower()\n b=0\n s=0\n for i in l:\n if i == p:\n b +=1\n elif i == q:\n s+=1\n if b > 0 and s > 0 :\n print(\"Both\")\n elif s > 0:\n print(\"GCETTS\")\n elif b > 0:\n print(\"GCETTB\")\n else:\n print(\"Others\")\n \n", "for _ in range(int(input())):\n l=list(map(str,input().split()))\n k=[]\n for i in l:\n k.append(i.lower())\n if 'berhampore' in k and \"serampore\" in k:\n print('Both')\n elif \"serampore\" in k and 'berhampore' not in k:\n print('GCETTS')\n elif \"serampore\" not in k and 'berhampore' in k:\n print('GCETTB')\n else:\n print('Others')\n\n\n", "for i in range(int(input())):\r\n val = list(map(str,input().split(\" \")))\r\n for j in range(len(val)):\r\n val[j] = val[j].lower()\r\n if(\"berhampore\" in val and \"serampore\" in val):\r\n print(\"Both\")\r\n elif(\"serampore\" in val):\r\n print(\"GCETTS\")\r\n elif(\"berhampore\" in val):\r\n print(\"GCETTB\")\r\n else:\r\n print(\"Others\")", "# cook your dish here\nT = int(input())\nfor i in range(T):\n s = input()\n s = s.lower()\n l = s.split()\n f1=0\n f2=0\n for j in l:\n if (j==\"serampore\"):\n f1=1 \n elif (j==\"berhampore\"):\n f2=1 \n if (f1==1 and f2==1):\n print(\"Both\")\n elif (f1==1 and f2==0):\n print(\"GCETTS\")\n elif (f1==0 and f2==1):\n print(\"GCETTB\")\n else:\n print(\"Others\")\n", "try:\r\n for _ in range(int(input())):\r\n s=input().lower().split()\r\n a=0\r\n b=0\r\n if 'berhampore' in s:\r\n a=1\r\n if 'serampore' in s:\r\n b=1\r\n if a==1 and b==1:\r\n print(\"Both\")\r\n elif a==1:\r\n print(\"GCETTB\")\r\n elif b==1:\r\n print(\"GCETTS\")\r\n else:\r\n print(\"Others\")\r\nexcept:\r\n pass\r\n", "#import logging\r\n#import sys\r\n#sys.stdin = open(\"input.txt\", \"r\")\r\n#sys.stdout = open(\"output.txt\", \"w\")\r\n#logging.basicConfig(filename=\"test.log\", level=logging.DEBUG, format=\"%(asctime)s: %(levelname)s: %(message)s\")\r\nt = int(input())\r\nwhile t:\r\n t -= 1\r\n s = input()\r\n s = s.upper()\r\n if s.find('BERHAMPORE') != -1 and s.find('SERAMPORE') != -1:\r\n print(\"Both\")\r\n continue\r\n if s.find('BERHAMPORE') != -1:\r\n print(\"GCETTB\")\r\n continue\r\n elif s.find('SERAMPORE') != -1:\r\n print(\"GCETTS\")\r\n continue\r\n else:\r\n print(\"Others\")\r\n", "import math\nt=int(input())\nfor _ in range(t):\n f=0\n s=input()\n r=s.lower()\n if \"berhampore\" in r and \"serampore\" in r:\n f=1\n print(\"Both\")\n if \"berhampore\" in r:\n if f==0:\n f=1\n print(\"GCETTB\")\n if \"serampore\" in r:\n if f==0:\n f=1\n print(\"GCETTS\")\n if f==0:\n print(\"Others\")\n", "t=int(input())\r\nfor j in range(t):\r\n p=input()\r\n p=p.split(\" \")\r\n flag=\"Others\"\r\n for i in range(int(len(p))):\r\n if(p[i].lower()==\"serampore\"):\r\n if(flag==\"Others\"):\r\n flag=\"GCETTS\"\r\n if(flag==\"GCETTB\"):\r\n flag=\"Both\"\r\n if(p[i].lower()==\"berhampore\"):\r\n if(flag==\"Others\"):\r\n flag=\"GCETTB\"\r\n if(flag==\"GCETTS\"):\r\n flag=\"Both\"\r\n print(flag)\r\n", "# cook your dish here\nt = int(input())\nfor i in range(t):\n s = input()\n s=s.lower()\n s=s.split()\n if \"berhampore\" in s and \"serampore\" in s:\n print(\"Both\")\n elif \"berhampore\" in s:\n print(\"GCETTB\")\n elif \"serampore\" in s:\n print(\"GCETTS\")\n else:\n print(\"Others\")\n", "# cook your dish here\nT = int(input())\nfor t in range(T):\n s = input()\n s = s.lower()\n b_check = 'berhampore' in s\n s_check = 'serampore' in s\n if (b_check) and (s_check): print('Both')\n elif (b_check) and (not s_check): print('GCETTB')\n elif (not b_check) and (s_check): print('GCETTS')\n else: print('Others')"]
{"inputs": [["3", "Government clg Berhampore", "SeRaMporE textile college", "Girls college Kolkata"]], "outputs": [["GCETTB", "GCETTS", "Others"]]}
INTERVIEW
PYTHON3
CODECHEF
9,996
e9237fd84585226f65e3c1ce062c55b8
UNKNOWN
Chef has a recipe book. He wishes to read it completely as soon as possible so that he could try to cook the dishes mentioned in the book. The pages of the book are numbered $1$ through $N$. Over a series of days, Chef wants to read each page. On each day, Chef can choose to read any set of pages such that there is no prime that divides the numbers of two or more of these pages, i.e. the numbers of pages he reads on the same day must be pairwise coprime. For example, Chef can read pages $1$, $3$ and $10$ on one day, since $(1, 3)$, $(3, 10)$ and $(1, 10)$ are pairs of coprime integers; however, he cannot read pages $1$, $3$ and $6$ on one day, as $3$ and $6$ are both divisible by $3$. Since chef might get bored by reading the same recipe again and again, Chef will read every page exactly once. Given $N$, determine the minimum number of days Chef needs to read the entire book and the pages Chef should read on each of these days. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains a single integer $N$. -----Output----- For each test case: - First, print a line containing a single integer $D$ ― the minimum number of days required to read the book. Let's number these days $1$ through $D$. - Then, print $D$ lines describing the pages Chef should read. For each valid $i$, the $i$-th of these lines should contain an integer $C_i$ followed by a space and $C_i$ space-separated integers ― the numbers of pages Chef should read on the $i$-th day. If there are multiple solutions with the minimum number of days, you may print any one. -----Constraints----- - $1 \le T \le 10$ - $1 \le N \le 10^6$ -----Subtasks----- Subtask #1 (20 points): $N \le 100$ Subtask #2 (80 points): original constraints -----Example Input----- 1 5 -----Example Output----- 2 3 1 2 5 2 3 4 -----Explanation----- Example case 1: - On the first day, Chef should read three pages: $1$, $2$ and $5$. - On the second day, Chef should read the remaining two pages: $3$ and $4$. There are other valid solutions as well.
["def ugcd(n):\r\n ans = [[1]]\r\n if(n==1):\r\n return ans\r\n elif(n%2==1):\r\n ans = [[1, 2, n]]\r\n else:\r\n ans = [[1, 2]]\r\n for k in range(1, int(n//2)):\r\n ans.append([k*2+1, k*2+2])\r\n return ans\r\nt = int(input())\r\nfor i in range(t):\r\n n = int(input())\r\n s = (ugcd(n))\r\n print(len(s))\r\n for j in range(len(s)):\r\n print(len(s[j]), end=\" \")\r\n print(*s[j])", "def ugcd(n):\r\n ans = [[1]]\r\n if(n==1):\r\n return ans\r\n elif(n%2==1):\r\n ans = [[1, 2, n]]\r\n else:\r\n ans = [[1, 2]]\r\n for k in range(1, int(n//2)):\r\n ans.append([k*2+1, k*2+2])\r\n \r\n return ans\r\n\r\ntest = int(input())\r\nfor i in range(test):\r\n n = int(input())\r\n p = (ugcd(n))\r\n print(len(p))\r\n for j in range(len(p)):\r\n print(len(p[j]), end=\" \")\r\n print(*p[j])", "try:\r\n for _ in range(int(input())):\r\n n = int(input())\r\n if n == 1:\r\n print(1)\r\n print(1, 1)\r\n elif n == 2:\r\n print(1)\r\n print(2, 1, 2)\r\n elif n == 3:\r\n print(1)\r\n print(3, 1, 2, 3)\r\n else:\r\n days = int(n / 2)\r\n if n % 2 == 0:\r\n print(days)\r\n for j in range(1, n + 1, 2):\r\n print(2, j, j + 1)\r\n else:\r\n print(days)\r\n print(3, 1, 2, 3)\r\n for j in range(4, n + 1, 2):\r\n print(2, j, j + 1)\r\nexcept EOFError as e:\r\n print(end=\" \")\r\n", "from itertools import combinations\r\n\r\ntry:\r\n for _ in range(int(input())):\r\n def gcd(a, b):\r\n if a == 0 or b == 0: \r\n return 0\r\n if a == b: \r\n return a\r\n if a > b:\r\n return gcd(a - b, b)\r\n return gcd(a, b - a)\r\n\r\n\r\n def areNocoprime(arr):\r\n if gcd(arr[0], arr[1]) == 1:\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n def iscoprime(arr):\r\n arr2 = list(combinations(arr, 2))\r\n arr3 = [areNocoprime(i) for i in arr2]\r\n if False in arr3:\r\n return False\r\n return True\r\n\r\n\r\n def isarraycoprime(arr, n):\r\n arr2 = []\r\n arr2.extend(arr)\r\n arr2.append(n)\r\n if iscoprime(arr2) is True:\r\n return arr2\r\n else:\r\n return arr\r\n \r\n sum = 0\r\n n = int(input())\r\n pages = [i for i in range(1, n + 1)]\r\n big_arr = []\r\n small_arr = []\r\n summ = 0\r\n while pages.count(0) != n:\r\n for i in range(0, n):\r\n if pages[i] != 0:\r\n x = len(small_arr)\r\n small_arr = isarraycoprime(small_arr, pages[i])\r\n y = len(small_arr)\r\n if x != y:\r\n pages[i] = 0\r\n big_arr.append(small_arr)\r\n small_arr = []\r\n print(len(big_arr))\r\n for i in big_arr:\r\n print(len(i), *i)\r\n\r\nexcept EOFError as e:\r\n print(end=\" \")", "from itertools import combinations\r\n\r\ntry:\r\n for _ in range(int(input())):\r\n def __gcd(a, b):\r\n if a == 0 or b == 0: return 0\r\n if a == b: return a\r\n if a > b:\r\n return __gcd(a - b, b)\r\n return __gcd(a, b - a)\r\n\r\n\r\n def areNocoprime(arr):\r\n if __gcd(arr[0], arr[1]) == 1:\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n def iscoprime(arr):\r\n arr2 = list(combinations(arr, 2))\r\n arr3 = [areNocoprime(i) for i in arr2]\r\n if False in arr3:\r\n return False\r\n return True\r\n\r\n\r\n def isarraycoprime(arr, n):\r\n arr2 = []\r\n arr2.extend(arr)\r\n arr2.append(n)\r\n if iscoprime(arr2) is True:\r\n return arr2\r\n else:\r\n return arr\r\n \r\n sum = 0\r\n n = int(input())\r\n pages = [i for i in range(1, n + 1)]\r\n big_arr = []\r\n small_arr = []\r\n summ = 0\r\n while pages.count(0) != n:\r\n for i in range(0, n):\r\n if pages[i] != 0:\r\n x = len(small_arr)\r\n small_arr = isarraycoprime(small_arr, pages[i])\r\n y = len(small_arr)\r\n if x != y:\r\n pages[i] = 0\r\n big_arr.append(small_arr)\r\n small_arr = []\r\n print(len(big_arr))\r\n for i in big_arr:\r\n print(len(i), *i)\r\n\r\nexcept EOFError as e:\r\n print(end=\" \")", "# cook your dish here\ntest=int(input())\nwhile test>0:\n n=int(input())\n if n==1:\n print(1)\n print(1,1)\n else:\n print(n//2)\n if n%2==0:\n i=1\n while i<=n:\n print(2,end=' ')\n print(i,end=' ')\n i+=1\n print(i,end=' ')\n i+=1\n print()\n else:\n i=1\n print(3,end=' ')\n print(i,end=' ')\n i+=1\n print(i,end=' ')\n i+=1\n print(i,end=' ')\n i+=1\n print()\n while i<=n:\n print(2,end=' ')\n print(i,end=' ')\n i+=1\n print(i,end=' ')\n i+=1\n print()\n test-=1\n", "# cook your dish here\nfor _ in range(int(input())):\n N=int(input())\n seq=N//2\n if(N==1):\n print(1)\n print(1,1)\n elif(N==2):\n print(seq)\n print(2,1,2)\n elif(N%2!=0):\n print(seq)\n for i in range(1,N+1,2):\n if(i==1):\n print(3,1,i+1,i+2)\n elif(i>=4):\n print(2,i-1,i)\n else:\n print(seq)\n for i in range(1,N+1,2):\n if(i==1):\n print(2,1,i+1)\n elif(i>=3):\n print(2,i,i+1)", "# cook your dish here\nimport math\nt = int(input())\nfor _ in range(t):\n n = int(input())\n l = []\n if n<=3:\n print(1)\n print(1,n)\n else:\n l.append([3,1,2,3])\n for i in range(4,n,2):\n l.append([2,i,i+1])\n if n%2==0:\n l.append([1,n])\n print(len(l))\n for i in l:\n print(*i)\n \n", "def gcd(a, b): \n if (b == 0): \n return a \n return gcd(b, a%b)\n\ntestcase = int(input())\nfor tes in range(testcase) :\n n = int(input())\n arr = []\n for i in range(2, n+1, 2) :\n arr.append([i])\n leng = len(arr)\n for i in range(1, n+1, 2) :\n flag1 = False\n for j in range(leng) :\n flag2 = True\n for k in arr[j] :\n if gcd(i, k) != 1 and i!=1 and k!=1 :\n flag2 = False\n break\n if flag2 :\n arr[j].append(i)\n flag1 = True\n break\n if not flag1 :\n arr.append([i])\n leng += 1\n print(len(arr))\n for ele in arr :\n print(len(ele), end=' ')\n for i in ele :\n print(i, end=' ')\n print()", "# cook your dish here\ndef gcd(a, b):\n while True :\n if (b == 0): \n return a\n c = b\n b = a%b\n a = c\ntestcase = int(input())\nfor tes in range(testcase) :\n n = int(input())\n arr = []\n left = [i for i in range(3, n+1, 2)]\n for i in range(2, n+1, 2) :\n arr.append([i])\n leng = len(arr)\n if n > 1 :\n arr[0].append(1)\n if n == 1 :\n arr.append([1])\n num = 0\n np = 3\n while len(left) > 0 :\n if num == 0 or num > n :\n num = left[0]\n pnum = num\n i = 0\n p = np\n np += 2\n if num in left :\n left.remove(num)\n flag1 = False\n while i<len(arr) :\n flag2 = True\n for j in arr[i] :\n if num%j==0 or ( j!=1 and gcd(num, j) != 1) :\n flag2 = False\n break\n if flag2 :\n arr[i].append(num)\n num = (p*pnum)\n p += 2\n flag1 = True\n break\n i += 1\n if not flag1 :\n arr.append([i])\n num = (p*pnum)\n p += 2\n else :\n num = (p*pnum)\n p += 2\n print(len(arr))\n for ele in arr :\n print(len(ele), end=' ')\n for i in ele :\n print(i, end=' ')\n print()", "# cook your dish here\nimport math\n\nt = int(input())\nfor _ in range(t):\n n = int(input())\n a = []\n if n <= 3:\n print(1)\n print(n, end=\" \")\n for i in range(1, n + 1):\n print(str(i), end=\" \")\n print()\n else:\n print(math.ceil((n - 3) / 2) + 1)\n a = [\"3 1 2 3\"]\n for i in range(4, n, 2):\n a.append(\"2 \" + str(i) + \" \" + str(i + 1))\n if (n & 1) == 0:\n a.append(\"1 \"+str(n))\n print(*a, sep=\"\\n\")\n", "# cook your dish here\nfor _ in range(int(input())):\n \n n=int(input())\n if n<=3:\n print(1)\n ans=[]\n for i in range(n):\n ans.append(i+1)\n print(n,*ans)\n \n else:\n print(n//2)\n if n%2==0:\n for i in range(1,n+1,2):\n print(2,i,i+1)\n else:\n print(3,1,2,3)\n for i in range(4,n+1,2):\n print(2,i,i+1)\n\n", "# cook your dish here\nfor _ in range(int(input())):\n \n n=int(input())\n if n<=3:\n print(1)\n ans=[]\n for i in range(n):\n ans.append(i+1)\n print(n,*ans)\n \n else:\n print(n//2)\n if n%2==0:\n for i in range(1,n+1,2):\n print(2,i,i+1)\n else:\n print(3,1,2,3)\n for i in range(4,n+1,2):\n print(2,i,i+1)\n", "t=int(input())\r\nwhile(t>=1):\r\n t-=1\r\n a=int(input())\r\n if(a==1):\r\n print(1)\r\n print(*[1,1])\r\n else:\r\n print(a//2)\r\n for i in range(a//2-1):\r\n print(*[2,i*2+1,i*2+2])\r\n if(a%2==0):\r\n print(*[2,a-1,a])\r\n else:\r\n print(*[3,a-2,a-1,a])\r\n ", "import math\nfor _ in range(int(input())):\n x=int(input())\n if x==1:\n print(\"1\")\n print(\"1\"+' 1')\n elif x==2:\n print('1')\n print('2'+' 1'+' 2')\n elif x==3:\n print('1')\n print('3'+' 1'+' 2'+' 3')\n else:\n c=x//2\n if x%2==0:\n print(c)\n for i in range(1,x,2):\n print(\"2\",end=' ')\n print(i,end=' ')\n print(i+1)\n \n else:\n print(c)\n print('3'+' 1'+' 2'+' 3')\n for i in range(4,x,2):\n print('2',end=' ')\n print(i,end=' ')\n print(i+1)\n \n \n ", "for _ in range(int(input())):\n n=int(input())\n if n%2==0:\n print(n//2)\n for i in range(1,n,2):\n print(2,i,i+1)\n else:\n if n==1:\n print(1)\n print(1,1)\n else:\n print(n//2)\n print(3,1,2,3)\n for i in range(4,n+1,2):\n print(2,i,i+1)\n \n", "for _ in range(int(input())):\n n=int(input())\n if n%2==0:\n print(n//2)\n for i in range(1,n+1,2):\n print(2,i,i+1)\n else:\n if n==1:\n print(1)\n print(1,1)\n else:\n print(n//2)\n print(3,1,2,3)\n for i in range(4,n+1,2):\n print(2,i,i+1)\n \n", "# cook your dish here\nt=int(input())\nwhile (t>0):\n t=t-1\n n=int(input())\n if n==1:\n print(1)\n print(1,1)\n else:\n print(n//2)\n if (n%2==0):\n for j in range(1,n,2):\n print(2,end=' ')\n print(j,j+1)\n else:\n print(3,1,2,3)\n j=4\n while(j<n):\n print(2,end=' ')\n print(j,j+1)\n j=j+2", "# cook your dish here\n\nfor j in range(int(input())):\n n=int(input())\n if n==1:\n print(1)\n print(1,1)\n else:\n print(n//2)\n if (n%2==0):\n for j in range(1,n,2):\n print(2,end=' ')\n print(j,j+1)\n else:\n print(3,1,2,3)\n j=4\n while(j<n):\n print(2,end=' ')\n print(j,j+1)\n j=j+2", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n if n%2==0:\n print(n//2)\n for i in range(1,n+1,2):\n print(2,i,i+1)\n else:\n if n==1:\n print(1)\n print(1,1)\n elif n>=3:\n print(n//2)\n print(3,1,2,3)\n for i in range(4,n+1,2):\n print(2,i,i+1)\n \n ", "try:\n t = int(input())\n for _ in range(t):\n n = int(input())\n l = []\n for y in range(1,n+1):\n l.append(y)\n if(n<4):\n print(1)\n print(n,end=\" \")\n for i in range(1,n+1):\n print(i,end = \" \")\n print()\n else:\n if(n%2==0):\n print(n//2)\n for i in range(1,n+1,2):\n print(2,i,i+1)\n else:\n print(n//2)\n print(3,1,2,3)\n for i in range(4,n,2):\n print(2,i,i+1)\nexcept:\n pass", "for _ in range(int(input())):\n n=int(input())\n if n==1:\n print(1)\n print(1,1)\n continue\n j=0\n if n%2==0:\n print(n//2)\n s=[[2,i,i+1] for i in range(1,n+1,2)]\n for i in s:\n print(*i)\n else:\n print(n//2)\n s=[[2,i,i+1] for i in range(4,n+1,2)]\n s.insert(0,[3,1,2,3])\n for i in s:\n print(*i)", "t=int(input())\nwhile(t>0):\n n=int(input())\n if(n==1):\n print(1)\n print(1,1)\n else:\n print(n//2)\n l=[]\n for i in range(1,n,2):\n l.append([i,i+1])\n if(n%2!=0):\n l[-1].append(n)\n for i in range(len(l)-1):\n print(2,*l[i])\n print(3,*(l[-1]))\n else:\n for i in l:\n print(2,*i)\n t-=1", "t = int(input())\r\nwhile(t):\r\n t-=1\r\n n = int(input())\r\n if n>1:\r\n print(n//2)\r\n else:\r\n print(\"1\")\r\n if n<3:\r\n print(n,end = \" \")\r\n for i in range(1,n+1):\r\n print(i,end = \" \")\r\n if n==3:\r\n print(\"3\",end = \" \")\r\n print(\"1\",end = \" \")\r\n print(\"2\",end = \" \")\r\n print(\"3\")\r\n if n>3:\r\n print(\"3\",end = \" \")\r\n print(\"1\",end = \" \")\r\n print(\"2\",end = \" \")\r\n print(\"3\")\r\n for i in range(4,n,2):\r\n print(\"2\",end = \" \")\r\n print(i,end = ' ')\r\n print(i+1)\r\n if(n%2 == 0):\r\n print(\"1\",end = \" \")\r\n print(n)"]
{"inputs": [["1", "5", ""]], "outputs": [["2", "3 1 2 5", "2 3 4"]]}
INTERVIEW
PYTHON3
CODECHEF
15,849
01bc9ef313f7acc0ab693b923d5adff4
UNKNOWN
You visit a doctor on a date given in the format $yyyy:mm:dd$. Your doctor suggests you to take pills every alternate day starting from that day. You being a forgetful person are pretty sure won’t be able to remember the last day you took the medicine and would end up in taking the medicines on wrong days. So you come up with the idea of taking medicine on the dates whose day is odd or even depending on whether $dd$ is odd or even. Calculate the number of pills you took on right time before messing up for the first time. -----Note:----- Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, in the format $yyyy:mm:dd$ -----Output:----- For each testcase, output in a single line the required answer. -----Constraints----- - $ 1 \leq T \leq 1000 $ - $ 1900 \leq yyyy \leq 2038 $ - $yyyy:mm:dd$ is a valid date -----Sample Input:----- 1 2019:03:31 -----Sample Output:----- 1 -----EXPLANATION:----- You can take pill on the right day only on 31st March. Next you will take it on 1st April which is not on the alternate day.
["t=int(input())\nli1=[31,29,31,30,31,30,31,31,30,31,30,31]\nli2=[31,28,31,30,31,30,31,31,30,31,30,31]\nfor z in range(t):\n y,m,d=list(map(int,input().split(':')))\n if y%4 == 0:\n if y%100 == 0:\n if y%400 == 0:\n li=li1\n else:\n li=li2\n else:\n li=li1\n else:\n li=li2\n c=0\n if d%2 == 0:\n while d%2 == 0:\n c+=1\n d+=2\n if d>li[m-1]:\n d=d%li[m-1]\n m+=1\n else: \n while d%2 != 0:\n c+=1\n d+=2\n if d>li[m-1]:\n d=d%li[m-1]\n m+=1\n print(c)\n \n", "for _ in range(int(input())):\n a,b,c=map(int,input().split(':'))\n\n if b in[1,3,5,7,8,10,12]:\n print(int((31-c)/2+1))\n continue\n if b in [4,6,9,11]:\n print(int((61-c)/2+1))\n continue\n if a%400==0 or a%100!=0 and a%4==0:\n print(int((29-c)/2+1))\n continue\n else:\n print(int((59-c)/2+1))\n continue", "for _ in range(int(input())):\n EVEN=[4,6,9,11]\n ODD=[1,3,5,7,8,10,12]\n Y,M,D=list(map(int,input().strip().split(\":\")))\n Ans=0\n if Y%400==0 or (Y%4==0 and Y%100!=0):\n ODD.append(2)\n if M in EVEN:\n if D%2==0:\n Ans=(((30-D)//2)+1)+15\n else:\n Ans=(((30-D)//2)+1)+16\n elif M in ODD:\n if D%2==0:\n if M==2:\n Ans=(((29-D)//2)+1)\n else:\n Ans=(((31-D)//2)+1)\n else:\n if M==2:\n Ans=(((29-D)//2)+1)\n else:\n Ans=(((31-D)//2)+1)\n else:\n EVEN.append(2)\n if M in ODD:\n if D%2==0:\n Ans=(((31-D)//2)+1)\n else:\n Ans=(((31-D)//2)+1)\n elif M in EVEN:\n if D%2==0:\n if M==2:\n Ans=(((28-D)//2)+1)+15\n else:\n Ans=(((30-D)//2)+1)+15\n else:\n if M==2:\n Ans=(((28-D)//2)+1)+16\n else:\n Ans=(((30-D)//2)+1)+16\n print(Ans)\n \n \n", "for _ in range(int(input())):\n y, m, d = list(map(int, input().split(':')))\n if m in [1,3,5,7,8,10,12]:\n print((31 - d) // 2 + 1)\n elif m in [4,6,9,11]:\n print((61 - d) // 2 + 1)\n else:\n if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):\n print((29 - d) // 2 + 1)\n else:\n print((59 - d) // 2 + 1)\n", "# cook your dish here\nfor _ in range(int(input())):\n yy,mm,dd = list(map(int, input().split(':')))\n if mm in [1,3,5,7,8,10,12]:\n sum1 = ((31-dd)//2)+1\n elif mm in [4,6,9,11]:\n sum1 = ((61 - dd) // 2) + 1\n else:\n if (yy % 4 == 0 and yy % 100 != 0) or yy % 400 == 0:\n sum1 = ((29 - dd) // 2) + 1\n else:\n sum1 = ((28+31 - dd) // 2)+1\n print(sum1)\n", "# cook your dish here\nfor _ in range(int(input())):\n yy,mm,dd = map(int, input().split(':'))\n if mm in [1,3,5,7,8,10,12]:\n sum1 = ((31-dd)//2)+1\n elif mm in [4,6,9,11]:\n sum1 = ((61 - dd) // 2) + 1\n else:\n if (yy % 4 == 0 and yy % 100 != 0) or yy % 400 == 0:\n sum1 = ((29 - dd) // 2) + 1\n else:\n sum1 = ((28+31 - dd) // 2)+1\n print(sum1)", "for _ in range(int(input())):\n y, m, d = map(int, input().split(':'))\n if m in [1,3,5,7,8,10,12]:\n print((31 - d) // 2 + 1)\n elif m in [4,6,9,11]:\n print((61 - d) // 2 + 1)\n else:\n if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):\n print((29 - d) // 2 + 1)\n else:\n print((59 - d) // 2 + 1)", "for i in range(int(input())):\n y,m,d = list(map(int,input().split(\":\")))\n if m in [1,3,5,7,8,10,12]:\n sol = ((31-d)//2)+1\n elif m in [4,6,9,11]:\n sol = ((61-d)//2)+1\n else:\n if (y%4==0 and y%100!=0) or y%400==0:\n sol = ((29-d)//2)+1\n else:\n sol = ((59-d)//2)+1\n print(sol) ", "# cook your dish here\nfor i in range(int(input())):\n y,m,d = list(map(int,input().split(\":\")))\n if m in [1,3,5,7,8,10,12]:\n sol = ((31-d)//2)+1\n elif m in [4,6,9,11]:\n sol = ((61-d)//2)+1\n else:\n if (y%4==0 and y%100!=0) or y%400==0:\n sol = ((29-d)//2)+1\n else:\n sol = ((59-d)//2)+1\n print(sol) ", "for i in range(int(input())):\n y,m,d = list(map(int,input().split(\":\")))\n if m in [1,3,5,7,8,10,12]:\n sol = ((31-d)//2)+1\n elif m in [4,6,9,11]:\n sol = ((61-d)//2)+1\n else:\n if (y%4==0 and y%100!=0) or y%400==0:\n sol = ((29-d)//2)+1\n else:\n sol = ((59-d)//2)+1\n print(sol) ", "# cook your dish here\nfor _ in range(int(input())):\n yy,mm,dd = map(int, input().split(':'))\n if mm in [1,3,5,7,8,10,12]:\n sum1 = ((31-dd)//2)+1\n elif mm in [4,6,9,11]:\n sum1 = ((61 - dd) // 2) + 1\n else:\n if (yy % 4 == 0 and yy % 100 != 0) or yy % 400 == 0:\n sum1 = ((29 - dd) // 2) + 1\n else:\n sum1 = ((28+31 - dd) // 2)+1\n print(sum1)", "# cook your dish here\nfor _ in range(int(input())):\n yy,mm,dd = map(int, input().split(':'))\n if mm in [1,3,5,7,8,10,12]:\n ans = ((31-dd)//2)+1\n elif mm in [4,6,9,11]:\n ans = ((61 - dd) // 2) + 1\n else:\n if (yy % 4 == 0 and yy % 100 != 0) or yy % 400 == 0:\n ans = ((29 - dd) // 2) + 1\n else:\n ans = ((28+31 - dd) // 2)+1\n print(ans)", "for _ in range(int(input())):\n yy,mm,dd = map(int, input().split(':'))\n if mm in [1,3,5,7,8,10,12]:\n ans = ((31-dd)//2)+1\n elif mm in [4,6,9,11]:\n ans = ((61 - dd) // 2) + 1\n else:\n if (yy % 4 == 0 and yy % 100 != 0) or yy % 400 == 0:\n ans = ((29 - dd) // 2) + 1\n else:\n ans = ((28+31 - dd) // 2)+1\n print(ans)", "from sys import stdin, stdout\nans = []\n\ndef leap_check(yy):\n if yy % 400 == 0:\n return True\n elif yy % 100 == 0:\n return False\n elif yy % 4 == 0:\n return True\n else:\n return 0\n\nfor _ in range(int(stdin.readline())):\n yy, mm, dd = list(map(int, stdin.readline().split(':')))\n if mm in [1, 3, 5, 7, 8, 10, 12]:\n ans.append(str(int(((31 - dd) / 2) + 1)))\n elif mm in [4, 6, 9, 11]:\n ans.append(str(int(((30 - dd) / 2) + 16 + (dd % 2))))\n else:\n ans.append(str(int((29 - dd) / 2 + 1)\n if leap_check(yy) else int((28 - dd)/2 + 16 + (dd % 2))))\nstdout.write('\\n'.join(ans))\n", "for i in range(int(input())):\n y,m,d=map(int, input().split(':'))\n if (m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12):\n s=(31-d)//2 + 1\n elif m==4 or m==6 or m==9 or m==11:\n s=(61-d)//2 + 1\n else:\n if (y%4==0 and y%100 !=0)or y%400==0:\n s=(29-d)//2 + 1\n else:\n s=(28+31-d)//2 + 1\n print(s)", "# cook your dish here\nfor _ in range(int(input())):\n y,m,d=list(map(int,input().split(':')))\n if m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12:\n if d%2==0:\n print(((30-d)//2)+1)\n else:\n print(((31-d)//2)+1)\n elif m==4 or m==6 or m==9 or m==11:\n if d%2==0:\n print(((30-d)//2)+16)\n else:\n print(((31-d)//2)+16)\n elif (y%4==0 and y%100!=0) or y%400==0:\n print((29 - d)//2 + 1)\n else:\n if d%2==0:\n print(((28-d)//2)+16)\n else:\n print(((27-d)//2)+17)\n", "# cook your dish here\nfor _ in range(int(input())):\n y,m,d=list(map(int, input().split(':')))\n if (m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12):\n c=(31-d)//2 + 1\n elif m==4 or m==6 or m==9 or m==11:\n c=(61-d)//2 + 1\n else:\n if (y%4==0 and y%100 !=0)or y%400==0:\n c=(29-d)//2 + 1\n else:\n c=(28+31-d)//2 + 1\n print(c)\n \n", "# cook your dish here\nfor _ in range(int(input())):\n s = input().split(':')\n y = int(s[0])\n m = int(s[1])\n d = int(s[2])\n if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:\n count = (31 - d)//2 + 1\n elif m == 4 or m == 6 or m == 9 or m == 11:\n count = (30 + 31 - d)//2 + 1\n else:\n if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:\n count = (29 - d)//2 + 1\n else:\n count = (28 + 31 - d)//2 + 1\n print(count)", "for i in range(int(input())):\n li = input().split(':')\n y = int(li[0])\n m = int(li[1])\n d = int(li[2])\n if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:\n count = (31 - d)//2 + 1\n elif m == 4 or m == 6 or m == 9 or m == 11:\n count = (30 + 31 - d)//2 + 1\n else:\n if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:\n count = (29 - d)//2 + 1\n else:\n count = (28 + 31 - d)//2 + 1\n print(count)", "# cook your dish here\nt = int(input())\n\nfor _ in range(t):\n s = input()\n a = s.split(\":\")\n year = int(a[0])\n m = int(a[1])\n d = int(a[2])\n\n if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:\n count = (31 - d) // 2 + 1\n elif m == 4 or m == 6 or m == 9 or m == 11:\n count = (30 + 31 - d) // 2 + 1\n else:\n if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:\n count = (29 - d) // 2 + 1\n else:\n count = (28 + 31 - d) // 2 + 1\n\n print(count)\n \n", "# cook your dish here\nt = int(input())\n\nfor _ in range(t):\n s = input()\n a = s.split(\":\")\n year = int(a[0])\n m = int(a[1])\n d = int(a[2])\n\n if m == 2:\n if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:\n count = (29 - d) // 2 + 1\n else:\n count = (28 + 31 - d) // 2 + 1\n\n if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:\n count = (31 - d) // 2 + 1\n elif m == 4 or m == 6 or m == 9 or m == 11:\n count = (30 + 31 - d) // 2 + 1\n print(count)\n \n", "# cook your dish here\nt = int(input())\n\n\ndef leapYear(Year):\n if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:\n return True\n else:\n return False\n\n\nfor _ in range(t):\n s = input()\n a = s.split(\":\")\n year = int(a[0])\n m = int(a[1])\n d = int(a[2])\n\n if m == 2:\n if leapYear(year):\n print((29 - d) // 2 + 1)\n else:\n print((28 + 31 - d) // 2 + 1)\n\n if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:\n print((31 - d) // 2 + 1)\n elif m == 4 or m == 6 or m == 9 or m == 11:\n print((30 + 31 - d) // 2 + 1)\n \n", "# cook your dish here\nt = int(input())\n\n\ndef leapYear(Year):\n if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:\n return True\n else:\n return False\n\n\nfor _ in range(t):\n s = input()\n a = s.split(\":\")\n year = int(a[0])\n m = int(a[1])\n d = int(a[2])\n\n if m == 2:\n if leapYear(year):\n print((29 - d) // 2 + 1)\n else:\n print((28 + 31 - d) // 2 + 1)\n\n if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:\n print((31 - d) // 2 + 1)\n elif m == 4 or m == 6 or m == 9 or m == 11:\n print((30 + 31 - d) // 2 + 1)\n"]
{"inputs": [["1", "2019:03:31"]], "outputs": [["1"]]}
INTERVIEW
PYTHON3
CODECHEF
9,814
9f8013696dc77a447cfef1f3f7fb9ad1
UNKNOWN
You are given a tree consisting of n nodes numbered from 1 to n. The weights of edges of the tree can be any binary integer satisfying following Q conditions. - Each condition is of form u, v, x where u, v are nodes of the tree and x is a binary number. For satisfying this condition, sum of the weight of all the edges present in the path from node u to v of the tree, should have even if x = 0, odd otherwise. Now, you have to find out number of ways of assigning 0/1 (binary) weights to the edges of the tree satisfying the above conditions. As the answer could be quite large, print your answer modulo 109 + 7. -----Input----- - The first line of input contains a single integer T denoting number of test cases. - For each test case: - First line contains two space separated integers n, Q. - Each of the next n - 1 lines will contain two space separated integer u, v denoting that there is an edge between vertex u and v in the tree. - Each of the next Q lines will contain three space separated integer u, v, x denoting a condition as stated in the probelm. -----Output----- - For each test case, output a single integer corresponding to the answer of the problem. -----Constraints----- - 1 ≤ u, v ≤ n - 0 ≤ x ≤ 1 -----Subtasks----- Subtask #1 : (10 points) - Sum of each of variables n and Q over all the test cases ≤ 20 Subtask #2 : (20 points) - Sum of each of variables n and Q over all the test cases ≤ 100 Subtask #3 : (30 points) - Sum of each of variables n and Q over all the test cases ≤ 5000 Subtask #4 : (40 points) - Sum of each of variables n and Q over all the test cases ≤ 100000 -----Example----- Input: 3 3 2 1 2 1 3 1 2 0 1 3 0 3 0 1 2 2 3 3 1 1 2 2 3 1 2 1 Output: 1 4 2 -----Explanation----- In the first example, You can only set the weight of each edge equal to 0 for satisfying the given condition. So, there is exactly one way of doing this. Hence answer is 1. In the second example, There are two edges and there is no condition on the edges. So, you can assign them in 4 ways. In the third example, You have to assign the weight of edge between node 1 and 2 to 1. You can assign the remaining edge from 2 to 3 either 0 or 1. So, the answer is 2.
["import sys\n\ndef powc(x,n,m):\n res = 1\n xx=x\n while n:\n if n&1:\n res = (res*xx)%m\n xx=xx*xx%m\n n >>= 1\n return res\n\ndef circles(u):\n r = 0\n S = [(u,-1,0)]\n Visited[u] = 0\n for s in S:\n for e in V[s[0]]:\n if e[0] != s[1]:\n if Visited[e[0]]==-1: \n Visited[e[0]] = s[2]^e[1]\n S.append((e[0], s[0], s[2]^e[1])) \n elif Visited[e[0]] != s[2]^e[1]:\n return -1\n else:\n r += s[0]<e[0]\n return r\n\nT = int(sys.stdin.readline())\nfor _ in range(T):\n is_bad = False\n empty = 0\n n,Q = list(map(int, sys.stdin.readline().split()))\n for _ in range(n-1):\n sys.stdin.readline() \n paths = []\n V=list(map(list,[[]]*n))\n for q in range(Q):\n u,v,x = list(map(int, sys.stdin.readline().split()))\n u-=1\n v-=1\n if (v,x^1) in V[u]:\n is_bad = True\n elif (v,x) in V[u]:\n empty += 1\n elif u!=v:\n V[u].append((v,x))\n V[v].append((u,x))\n elif x==1:\n is_bad = True\n else:\n empty += 1\n paths.append((u,v,x))\n if is_bad:\n print(0)\n elif n<=1:\n print(1)\n else:\n Visited = [-1]*n\n components = 0\n for i in range(n):\n if Visited[i]==-1:\n components += 1\n c = circles(i)\n if c==-1:\n is_bad = True\n break\n empty += c\n if is_bad:\n print(0)\n else:\n print(powc(2,n-1-(Q-empty),10**9+7)) \n", "import sys\n\ndef powc(x,n,m):\n res = 1\n xx=x\n while n:\n if n&1:\n res = (res*xx)%m\n xx=xx*xx%m\n n >>= 1\n return res\n\ndef circles(u):\n r = 0\n S = [(u,-1,0)]\n Been = [-1]*n\n Been[u] = 0\n for s in S:\n Visited[s[0]] = 1\n for e in V[s[0]]:\n if e[0] != s[1]:\n if Been[e[0]]==-1: \n Been[e[0]] = s[2]^e[1]\n S.append((e[0], s[0], s[2]^e[1])) \n elif Been[e[0]] != s[2]^e[1]:\n return -1\n else:\n r += s[0]<e[0]\n return r\n\nT = int(sys.stdin.readline())\nfor _ in range(T):\n is_bad = False\n empty = 0\n n,Q = list(map(int, sys.stdin.readline().split()))\n for _ in range(n-1):\n sys.stdin.readline() \n paths = []\n V=list(map(list,[[]]*n))\n E = []\n for q in range(Q):\n u,v,x = list(map(int, sys.stdin.readline().split()))\n u-=1\n v-=1\n if (v,x^1) in V[u]:\n is_bad = True\n elif (v,x) in V[u]:\n empty += 1\n elif u!=v:\n E.append((u,v,x))\n V[u].append((v,x))\n V[v].append((u,x))\n elif x==1:\n is_bad = True\n else:\n empty += 1\n paths.append((u,v,x))\n if is_bad:\n print(0)\n elif n<=1:\n print(1)\n else:\n Visited = [0]*n\n components = 0\n for i in range(n):\n if Visited[i]==0:\n components += 1\n c = circles(i)\n if c==-1:\n is_bad = True\n break\n empty += c\n if is_bad:\n print(0)\n else:\n print(powc(2,n-1-(Q-empty),10**9+7)) \n", "import sys\n\ndef powc(x,n,m):\n res = 1\n xx=x\n while n:\n if n&1:\n res = (res*xx)%m\n xx=xx*xx%m\n n >>= 1\n return res\n\ndef circles(u):\n r = 0\n S = [(u,-1,0)]\n Been = [-1]*n\n for s in S:\n if Been[s[0]]!=-1:\n if Been[s[0]][1] != s[2]:\n return -1\n r += 1\n continue\n Been[s[0]] = (0,s[2])\n Visited[s[0]] = 1\n for e in V[s[0]]:\n if e[0] != s[1]:\n if Been[e[0]]==-1: \n S.append((e[0], s[0], s[2]^e[1])) \n return r\n\nT = int(sys.stdin.readline())\nfor _ in range(T):\n is_bad = False\n empty = 0\n n,Q = list(map(int, sys.stdin.readline().split()))\n for _ in range(n-1):\n sys.stdin.readline() \n paths = []\n V=list(map(list,[[]]*n))\n E = []\n for q in range(Q):\n u,v,x = list(map(int, sys.stdin.readline().split()))\n u-=1\n v-=1\n if (v,x^1) in V[u]:\n is_bad = True\n elif (v,x) in V[u]:\n empty += 1\n elif u!=v:\n E.append((u,v,x))\n V[u].append((v,x))\n V[v].append((u,x))\n elif x==1:\n is_bad = True\n else:\n empty += 1\n paths.append((u,v,x))\n if is_bad:\n print(0)\n elif n<=1:\n print(1)\n else:\n Visited = [0]*n\n components = 0\n for i in range(n):\n if Visited[i]==0:\n components += 1\n c = circles(i)\n if c==-1:\n is_bad = True\n break\n empty += c\n if is_bad:\n print(0)\n else:\n print(powc(2,n-1-(Q-empty),10**9+7)) \n", "import sys\n\ndef powc(x,n,m):\n res = 1\n xx=x\n while n:\n if n&1:\n res = (res*xx)%m\n xx=xx*xx%m\n n >>= 1\n return res\n\ndef findRoot():\n S = [(0,-1)]\n for u in S:\n for w in V[u[0]]:\n if w[0]!=u[1]:\n S.append((w[0],u[0]))\n S = [(S[-1][0],-1,0)]\n D = [0]*n\n for u in S:\n for w in V[u[0]]:\n if w[0]!=u[1]:\n D[w[0]]=u[2]+1\n S.append((w[0],u[0],u[2]+1))\n d = S[-1][2]\n size = d\n u = S[-1][0]\n while size/2<d:\n for w in V[u]:\n if D[w[0]]+1==D[u]:\n u = w[0]\n d -= 1\n break \n return u\n \nclass Node:\n def __init__(self, value, edge, parent = None):\n self.value = value\n self.edge = edge\n if parent:\n parent.addChild(self)\n else:\n self.parent = None\n self.children = []\n def addChild(self, node):\n node.parent = self\n self.children.append(node)\n def __repr__(self):\n r = repr(self.value)\n for v in self.children:\n r += ' ' + repr(v)\n return r\n\n\ndef hangTree(root):\n global NodesArray\n NodesArray = [None]*n\n S=[(root, Node(root,-1),-1)]\n NodesArray[root] = S[0][1]\n for u in S:\n for v in V[u[0]]:\n if v[0] != u[2]:\n node = Node(v[0],v[1],u[1])\n NodesArray[v[0]] = node\n S.append((v[0],node,u[0]))\n\ndef findPath2(u,v):\n n0 = NodesArray[u]\n n1 = NodesArray[v]\n q = [0]*n\n while n0.parent:\n q[n0.edge] ^= 1\n n0 = n0.parent\n while n1.parent:\n q[n1.edge] ^= 1\n n1 = n1.parent\n return q\n \nT = int(sys.stdin.readline())\nfor _ in range(T):\n n,Q = list(map(int,sys.stdin.readline().split()))\n V = list(map(list,[[]]*n))\n W = [0]*n\n for i in range(n-1):\n u,v = list(map(int,sys.stdin.readline().split()))\n u-=1\n v-=1\n V[u].append((v,i))\n V[v].append((u,i))\n W[u] += 1\n W[v] += 1\n easy = n==1\n root = findRoot()\n hangTree(root)\n M = []\n for _ in range(Q):\n u,v,x = list(map(int,sys.stdin.readline().split()))\n if not easy:\n q = findPath2(u-1,v-1)\n q[-1] = x\n M.append(q)\n if easy:\n print(1)\n continue\n empty = [0]*n\n bad = [0]*n\n bad[-1] = 1\n is_there_bad = False\n empty_cnt = 0\n i = 0\n for q in M:\n i += 1\n if q == empty:\n empty_cnt += 1\n continue\n if q == bad:\n is_there_bad = True\n break\n o = q.index(1)\n for next in range(i,Q):\n if M[next][o]==1:\n for k in range(n):\n M[next][k] ^= q[k]\n if is_there_bad:\n print(0)\n else:\n print(powc(2,n-1-Q+empty_cnt,10**9+7))\n", "import sys\n\ndef powc(x,n,m):\n res = 1\n xx=x\n while n:\n if n&1:\n res = (res*xx)%m\n xx=xx*xx%m\n n >>= 1\n return res\n\n\ndef findPath(u,v,x):\n S = [(u,v,x)]\n for s in S:\n if s[0]==v:\n return s[2]\n for e in V[s[0]]: \n if e[0] != s[1]:\n S.append((e[0],s[0],s[2]^e[1]))\n return None\n\nT = int(sys.stdin.readline())\nfor _ in range(T):\n is_bad = False\n empty = 0\n n,Q = list(map(int, sys.stdin.readline().split(' ')))\n for _ in range(n-1):\n sys.stdin.readline() \n paths = []\n V=list(map(list,[[]]*n))\n E = []\n for q in range(Q):\n u,v,x = list(map(int, sys.stdin.readline().split(' ')))\n u-=1\n v-=1\n if (v,x^1) in V[u]:\n is_bad = True\n elif (v,x) in V[u]:\n empty += 1\n else:\n E.append((u,v,x))\n V[u].append((v,x))\n V[v].append((u,x))\n paths.append((u,v,x))\n if is_bad:\n print(0)\n else:\n while E:\n e = E.pop()\n x = findPath(e[0],e[1],e[2]) \n V[e[0]].remove((e[1],e[2]))\n V[e[1]].remove((e[0],e[2]))\n if x==1:\n is_bad = True\n break\n if x==0:\n empty += 1\n if is_bad:\n print(0)\n else:\n print(powc(2,n-1-(Q-empty),10**9+7))\n", "def modpow(a,x):\n\tif(x==0):\n\t\treturn 1;\n\telif(x%2==0):\n\t\tt=modpow(a,x/2);\n\t\treturn (t*t)%(1000000007);\n\telse:\n\t\tt=modpow(a,x/2);\n\t\treturn (t*t*a)%(1000000007);\n\t\t\n\t\t\t\t\t\nT=eval(input());\nans=[0]*T;\nfor j in range(T):\n\t[N,Q]=[int(x) for x in (input()).split()];\n\tfor i in range(N-1):\n\t\tinput();\n\tcomp=list(range(N+1));\n\trevcomp=[];\n\tfor i in range(N+1):\n\t\trevcomp.append([i]);\t\n\tsumcomp=[0]*(N+1);\n\tflag=True;\n\trank=0;\n\tfor i in range(Q):\n\t\tif(not(flag)):\n\t\t\tinput();\n\t\telse:\t\n\t\t\t[u,v,x]=[int(x) for x in (input()).split()];\n\t\t\tif(comp[u]==comp[v]):\n\t\t\t\tif(not((sumcomp[u]+sumcomp[v])%2==(x%2))):\n\t\t\t\t\tflag=False;\n\t\t\telse:\n\t\t\t\trank=rank+1;\n\t\t\t\tn1=len(revcomp[comp[u]]);\n\t\t\t\tn2=len(revcomp[comp[v]]);\n\t\t\t\tif(n1<n2):\n\t\t\t\t\toldsu=sumcomp[u];\n\t\t\t\t\tl=revcomp[comp[v]];\n\t\t\t\t\tfor w in revcomp[comp[u]]:\n\t\t\t\t\t\tl.append(w);\n\t\t\t\t\t\tcomp[w]=comp[v];\n\t\t\t\t\t\tsumcomp[w]=(sumcomp[w]+sumcomp[v]+x+oldsu)%2;\n\t\t\t\t\t#revcomp[comp[u]]=[];\t\n\t\t\t\telse:\n\t\t\t\t\toldsv=sumcomp[v];\n\t\t\t\t\tl=revcomp[comp[u]];\n\t\t\t\t\tfor w in revcomp[comp[v]]:\n\t\t\t\t\t\tl.append(w);\n\t\t\t\t\t\tcomp[w]=comp[u];\n\t\t\t\t\t\tsumcomp[w]=(sumcomp[w]+sumcomp[u]+x+oldsv)%2;\n\t\t\t\t\t#revcomp[comp[v]]=[];\t\n\tif(not(flag)):\n\t\tans[j]=0;\n\telse:\n\t\tans[j]=modpow(2,(N-rank-1));\n\nfor j in range(T):\n\tprint((ans[j]));\t\t\t\n", "def modpow(a,x):\n\tif(x==0):\n\t\treturn 1;\n\telif(x%2==0):\n\t\tt=modpow(a,x/2);\n\t\treturn (t*t)%(1000000007);\n\telse:\n\t\tt=modpow(a,x/2);\n\t\treturn (t*t*a)%(1000000007);\n\t\t\n\t\t\t\n\t\t\n\t\t\t\nT=eval(input());\nans=[0]*T;\nfor j in range(T):\n\t[N,Q]=[int(x) for x in (input()).split()];\n\tfor i in range(N-1):\n\t\tinput();\n\tcomp=list(range(N+1));\n\trevcomp=[];\n\tfor i in range(N+1):\n\t\trevcomp.append([i]);\t\n\tsumcomp=[0]*(N+1);\n\tflag=True;\n\trank=0;\n\tfor i in range(Q):\n\t\tif(not(flag)):\n\t\t\tinput();\n\t\telse:\t\n\t\t\t[u,v,x]=[int(x) for x in (input()).split()];\n\t\t\tif(comp[u]==comp[v]):\n\t\t\t\tif(not((sumcomp[u]+sumcomp[v])%2==(x%2))):\n\t\t\t\t\tflag=False;\n\t\t\telse:\n\t\t\t\trank=rank+1;\n\t\t\t\tn1=len(revcomp[comp[u]]);\n\t\t\t\tn2=len(revcomp[comp[v]]);\n\t\t\t\tif(n1<n2):\n\t\t\t\t\toldsu=sumcomp[u];\n\t\t\t\t\tl=revcomp[comp[v]];\n\t\t\t\t\tfor w in revcomp[u]:\n\t\t\t\t\t\tl.append(w);\n\t\t\t\t\t\tcomp[w]=comp[v];\n\t\t\t\t\t\tsumcomp[w]=(sumcomp[w]+sumcomp[v]+x+oldsu)%2;\n\t\t\t\telse:\n\t\t\t\t\toldsv=sumcomp[v];\n\t\t\t\t\tl=revcomp[comp[u]];\n\t\t\t\t\tfor w in revcomp[v]:\n\t\t\t\t\t\tl.append(w);\n\t\t\t\t\t\tcomp[w]=comp[u];\n\t\t\t\t\t\tsumcomp[w]=(sumcomp[w]+sumcomp[u]+x+oldsv)%2;\n\tif(not(flag)):\n\t\tans[j]=0;\n\telse:\n\t\tans[j]=modpow(2,(N-rank-1));\n\nfor j in range(T):\n\tprint((ans[j]));\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\n\t\t\t\n\t\t\n\n", "T=eval(input());\nans=[0]*T;\nfor j in range(T):\n\t[N,Q]=[int(x) for x in (input()).split()];\n\tfor i in range(N-1):\n\t\tinput();\n\tcomp=list(range(N+1));\n\trevcomp=[];\n\tfor i in range(N+1):\n\t\trevcomp.append([i]);\t\n\tsumcomp=[0]*(N+1);\n\tflag=True;\n\trank=0;\n\tfor i in range(Q):\n\t\tif(not(flag)):\n\t\t\tinput();\n\t\telse:\t\n\t\t\t[u,v,x]=[int(x) for x in (input()).split()];\n\t\t\tif(comp[u]==comp[v]):\n\t\t\t\tif(not((sumcomp[u]+sumcomp[v])%2==(x%2))):\n\t\t\t\t\tflag=False;\n\t\t\telse:\n\t\t\t\trank=rank+1;\n\t\t\t\tn1=len(revcomp[comp[u]]);\n\t\t\t\tn2=len(revcomp[comp[v]]);\n\t\t\t\tif(n1<n2):\n\t\t\t\t\toldsu=sumcomp[u];\n\t\t\t\t\tl=revcomp[comp[v]];\n\t\t\t\t\tfor w in revcomp[u]:\n\t\t\t\t\t\tl.append(w);\n\t\t\t\t\t\tcomp[w]=comp[v];\n\t\t\t\t\t\tsumcomp[w]=(sumcomp[w]+sumcomp[v]+x+oldsu)%2;\n\t\t\t\telse:\n\t\t\t\t\toldsv=sumcomp[v];\n\t\t\t\t\tl=revcomp[comp[u]];\n\t\t\t\t\tfor w in revcomp[v]:\n\t\t\t\t\t\tl.append(w);\n\t\t\t\t\t\tcomp[w]=comp[u];\n\t\t\t\t\t\tsumcomp[w]=(sumcomp[w]+sumcomp[u]+x+oldsv)%2;\n\tif(not(flag)):\n\t\tans[j]=0;\n\telse:\n\t\tans[j]=2**(N-rank-1);\n\nfor j in range(T):\n\tprint((ans[j]));\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\n\t\t\t\n\t\t\n\n"]
{"inputs": [["3", "3 2", "1 2", "1 3", "1 2 0", "1 3 0", "3 0", "1 2", "2 3", "3 1", "1 2", "2 3", "1 2 1"]], "outputs": [["1", "4", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
12,473
e4ef5106b7ebb22273336de489334006
UNKNOWN
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
["# cook your dish here\nn,q=map(int,input().split())\nls=[int(i) for i in input().split()]\ncur=0\ns=[0]\nfor i in ls:\n cur=cur^i\n s.append(cur)\nfor i in range(q):\n k=int(input())\n print(s[k%(n+1)])", "# cook your dish here\nn,k=list(map(int,input().split()))\nd=[0]+list(map(int,input().split()))\nl=[0]\nfor i in range(1,n+1):\n l.append(l[i-1]^d[i])\nfor q in range(k):\n x=int(input())\n print(l[x%(n+1)])\n", "#!/usr/bin/env python3\n\n(n, q) = input().split()\nn = int(n)\nq = int(q)\nf = [int(fi) for fi in input().split()]\ncur = 0\ns = [0]\nfor fi in f:\n cur = cur ^ fi\n s.append(cur)\nfor i in range(q):\n k = int(input())\n print(s[k % (n + 1)])\n", "# cook your dish here\nn,q=list(map(int,input().split(' ')))\nar,xor=list(map(int,input().split(' '))),0\n\nfor x in ar: xor^=x\nar.append(xor)\n\nprefix=[0]*(n+1)\nprefix[0]=ar[0]\n\nfor i in range(1,len(prefix)): prefix[i]=ar[i]^prefix[i-1]\nfor i in range(q): print(prefix[(int(input())-1)%(n+1)])\n", "n,q=list([int(x) for x in input().split()])\na=list([int(x) for x in input().split()])\nxor_a=0\nfor i in a:\n xor_a=xor_a^i\n\na.append(xor_a)\nprefix=[a[0]]\nfor i in range(1,len(a)):\n prefix.append(a[i]^prefix[i-1])\n# print(prefix)\nfor i in range(0,q):\n x=int(input())\n print(prefix[(x-1)%(n+1)])\n", "from math import *\nfrom collections import *\nfrom itertools import *\nfrom functools import *\nfrom bisect import *\nfrom heapq import *\nfrom operator import *\nfrom sys import *\nsetrecursionlimit(100000000)\n\nn,k=map(int,input().split())\nl=list(map(int,input().split()))\nl.append(reduce(xor,l))\nl=list(accumulate(l,xor))\nfor _ in range(k):\n v=int(input())\n print(l[(v-1)%len(l)]) ", "from math import *\nfrom collections import *\nfrom itertools import *\nfrom functools import *\nfrom bisect import *\nfrom heapq import *\nfrom operator import *\nfrom sys import *\nsetrecursionlimit(100000000)\n\nn,k=map(int,input().split())\nl=list(map(int,input().split()))\nl.append(reduce(xor,l))\nl=list(accumulate(l,xor))\nfor _ in range(k):\n v=int(input())\n print(l[(v-1)%len(l)]) ", "# cook your dish here\na = [int(s) for s in input().split()]\nb = [int(s) for s in input().split()]\nc = [0]\nd = 0\nfor e in range(a[0]):\n d = d ^ b[e]\n c.append(d)\nfor j in range(a[1]):\n e = int(input())\n if(e <= a[0]): print(c[e])\n else: print(c[(e - (a[0] + 1)) % (a[0] + 1)])", "n,q=list(map(int,input().split()))\nl=list(map(int,input().split()))\nv=[0 for x in range(n)]\nv[0]=l[0]\nfor j in range(1,n):\n v[j]=v[j-1]^l[j]\nfor i in range(q):\n k=int(input())\n if k<=n:\n print(v[k-1])\n else:\n ans=(k-n)%(n+1)-1\n if ans==0:\n print(0)\n elif ans<0:\n print(v[n-1])\n else:\n print(v[ans-1])\n", "n,q = map(int,input().split())\nf = list(map(int,input().split()))\nl = [0,f[0]]\nfor i in range(1,n,1):\n l.append(l[-1]^f[i])\nl.append(0)\nfor _ in range(q):\n a = int(input())\n print(l[a%(n+1)])", "# cook your dish here\nl=input().split()\nn=int(l[0])\nq=int(l[1])\nl=input().split()\nli=[int(i) for i in l]\nxori=0\nsi=[0 for i in range(n)]\nfor i in range(n):\n si[i]=xori\n xori=xori^li[i]\nsi.append(xori)\nfi=list(li)\nfi.append(xori)\nfor you in range(q):\n q1=int(input())\n print(si[q1%(n+1)])", "# cook your dish here\nn,q = list(map(int,input().split()))\n\na = list(map(int,input().split()))\ns = [0]*(n+1)\nfor i in range(n):\n if i==0:\n s[i] = a[i]\n else:\n s[i] = s[i-1]^a[i]\ns[n] = 0\nfor _ in range(q):\n k = int(input())\n k-=1\n k = k%(n+1)\n print(s[k])", "# cook your dish here\n\nn, q = [int(a) for a in input().strip().split()]\n\nF = [int(a) for a in input().strip().split()]\n\nS = [0]\n\nfor i in range(n):\n S.append(S[i] ^ F[i])\n\nfor _ in range(q):\n k = int(input())\n print(S[k%(n+1)])", "# cook your dish here\nnums = list(map(int, input().split()))\n\narr = list(map(int, input().split()))\n\ncount = 0\ns = []\n\nfor item in arr:\n \n count = count ^ item\n s.append(count)\n \nfor i in range(nums[1]):\n query = int(input())\n \n if query % (nums[0] + 1) == 0:\n print(0)\n else:\n print(s[query % (nums[0] + 1) - 1])\n", "# cook your dish here\nn, q = map(int, input().split(' '))\n\nn_bonacci = list(map(int, input().split(' ')))\narr = []\nxor = 0\nfor val in n_bonacci:\n xor = xor ^ val\n arr.append(xor)\n\nfor query in range(q):\n s = int(input())\n \n if s % (n + 1) == 0:\n print(0)\n else:\n print(arr[s % (n + 1) - 1])", "n,q = list(map(int, input().split()))\nF = []\nS = []\nline1 = input().split()\nlast = 0\nfor i in range(n):\n F.append(int(line1[i]))\n last = last^int(line1[i])\n S.append(last)\nS.append(0)\n\nfor i in range(q):\n query = int(input())\n pos = (query-1) % len(S)\n final = S[pos]\n print(final)\n", "n,q=list(map(int,input().split()))\narr=[int(n) for n in input().split()]\nx=arr[0]\narr1=[]\narr1.append(x)\nfor i in range(1,n):\n x=x^arr[i]\n arr1.append(x)\narr1.append(0)\nwhile(q):\n q=q-1\n x=int(input())\n r=x%(n+1)\n print(arr1[r-1])\n \n", "n,q=map(int,input().split())\na=list(map(int,input().split()))\nxor=0\nsl=[]\nfor i in a:\n\n xor=xor^i\n sl.append(xor)\nfor i in range(q):\n qval=int(input())\n if(qval%(n+1)==0):\n print(0)\n else:\n print(sl[qval%(n+1)-1])", "n,q = map(int,input().split())\nf = [int(w) for w in input().split()]\n\nft = []\n\nft.append(f[0])\n\nfor i in range(1,n):\n temp = ft[i-1]\n temp = temp ^ f[i]\n ft.append(temp)\nft.append(0)\n\nwhile (q):\n k = int(input())\n index = k%(n+1) - 1\n print(ft[index])\n q = q - 1", "#Afrikavi\n\nnq = [x for x in list(map(int, input().split(' ')))]\ntab = [x for x in list(map(int, input().split(' ')))]\nxs = tab[0]\nsol = []\nsol.append(tab[0])\nfor i in range(1, nq[0]):\n xs ^= tab[i]\n sol.append(xs)\nsol.append(sol[-1] ^ xs)\n#print(sol)\nfor i in range(nq[1]):\n q = int(input())\n print(sol[(q - 1) % (nq[0] + 1)])\n", "n,q=map(int,input().split())\nf=list(map(int,input().split()))\nfor j in range(1,n):\n f[j]^=f[j-1]\n#print(f)\nfor g in range(q):\n k=int(input())\n tyagibagi=k%(n+1)\n if tyagibagi==0:\n print(0)\n else:\n print(f[tyagibagi-1])", "n,q=[int(i) for i in input().split()]\na=[int(i) for i in input().split()]\nxor=a[0]\nfor i in range(1,len(a)):\n xor = xor^a[i]\na.append(xor)\n#q=int(input())\ns = [a[0]]\nfor i in range(1, len(a)):\n s.append(s[-1] ^ a[i])\nfor I in range(q):\n k=int(input())\n k-=1\n print(s[(k)%(n + 1)])\n\n"]
{"inputs": [["3 4", "0 1 2", "7", "2", "5", "1000000000"]], "outputs": [["3", "1", "0", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
6,334
daec23f77254d7c82f72417eed79ade0
UNKNOWN
Alice and Johnny are playing a simple guessing game. Johnny picks an arbitrary positive integer n (1<=n<=109) and gives Alice exactly k hints about the value of n. It is Alice's task to guess n, based on the received hints. Alice often has a serious problem guessing the value of n, and she's beginning to suspect that Johnny occasionally cheats, that is, gives her incorrect hints. After the last game, they had the following little conversation: - [Alice] Johnny, you keep cheating! - [Johnny] Indeed? You cannot prove it. - [Alice] Oh yes I can. In fact, I can tell you with the utmost certainty that in the last game you lied to me at least *** times. So, how many times at least did Johnny lie to Alice? Try to determine this, knowing only the hints Johnny gave to Alice. -----Input----- The first line of input contains t, the number of test cases (about 20). Exactly t test cases follow. Each test case starts with a line containing a single integer k, denoting the number of hints given by Johnny (1<=k<=100000). Each of the next k lines contains exactly one hint. The i-th hint is of the form: operator li logical_value where operator denotes one of the symbols < , > , or =; li is an integer (1<=li<=109), while logical_value is one of the words: Yes or No. The hint is considered correct if logical_value is the correct reply to the question: "Does the relation: n operator li hold?", and is considered to be false (a lie) otherwise. -----Output----- For each test case output a line containing a single integer, equal to the minimal possible number of Johnny's lies during the game. -----Example----- Input: 3 2 < 100 No > 100 No 3 < 2 Yes > 4 Yes = 3 No 6 < 2 Yes > 1 Yes = 1 Yes = 1 Yes > 1 Yes = 1 Yes Output: 0 1 2 Explanation: for the respective test cases, the number picked by Johnny could have been e.g. n=100, n=5, and n=1.
["# cook your dish here\ndef guessingGame (l):\n a = []\n m = 1000000001\n for i in range (len(l)):\n k=int(l[i][1])\n if (l[i][0]=='<' and l[i][2]=='Yes'):\n a.append((1,1))\n a.append((k,-1))\n \n if (l[i][0]=='<' and l[i][2]=='No'):\n a.append((k,1))\n a.append((m,-1))\n \n if (l[i][0]=='=' and l[i][2]=='Yes'):\n a.append((k,1))\n a.append((k+1,-1))\n\n if (l[i][0]=='=' and l[i][2]=='No'):\n a.append((1,1))\n a.append((k,-1))\n a.append((k+1,1))\n a.append((m,-1))\n\n if (l[i][0]=='>' and l[i][2]=='Yes'):\n a.append((k+1,1))\n a.append((m,-1))\n\n if (l[i][0]=='>' and l[i][2]=='No'):\n a.append((1,1))\n a.append((k+1,-1))\n\n a.sort()\n w=0\n r=0\n\n for i in range (len(a)):\n w+=a[i][1]\n r=max(w,r)\n\n return len(l)-r\n \n \ndef __starting_point():\n\n T = int(input())\n answer = []\n\n for _ in range (T):\n e = int(input())\n temp = []\n for q_t in range (e):\n q = list(map(str,input().rstrip().split()))\n temp.append(q)\n result = guessingGame(temp)\n print(result)\n__starting_point()"]
{"inputs": [["3", "2", "< 100 No", "> 100 No", "3", "< 2 Yes", "> 4 Yes", "= 3 No", "6", "< 2 Yes", "> 1 Yes", "= 1 Yes", "= 1 Yes", "> 1 Yes", "= 1 Yes"]], "outputs": [["0", "1", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
1,059
69d35f28914862f7fbdab2b44e4e735f
UNKNOWN
Ms. E.T. came from planet Hex. She has 8 fingers in each hand which makes her count in hexadecimal way. When she meets you, she tells you that she came from 7E light years from the planet Earth. You see she means that it is 126 light years far away and she is telling you the numbers in hexadecimal. Now, you are in trouble to understand what those numbers really mean. Therefore, you have to convert the hexadecimal numbers to decimals. Input: First line of code contain T test cases. every line of text case contain a Hex-value Output: Every line of output contain a decimal conversion of given nunmber Sample Input: 3 A 1A23 2C2A Sample Output: 10 6691 11306
["# cook your dish here\ntry:\n t=int(input())\n for i in range(t):\n s=input()\n i=int(s,16)\n print(i)\nexcept EOFError as e:\n print(e)", "# cook your dish here\ntry:\n n=int(input())\n for i in range(n):\n a=input()\n \n print(int(a, 16))\nexcept EOFError as e:\n pass", "t=int(input())\nfor i in range(t):\n s=input()\n print(int(s,16))", "t=int(input())\nfor i in range(t):\n str1=input()\n str1=str1[::-1]\n ans=0\n for j in range(len(str1)):\n if(str1[j]==\"0\" or str1[j]==\"1\" or str1[j]==\"2\" or str1[j]==\"3\" or str1[j]==\"4\" or str1[j]==\"5\" or str1[j]==\"6\" or str1[j]==\"7\" or str1[j]==\"8\" or str1[j]==\"9\"):\n ans+=int(str1[j])*(16**j)\n elif(str1[j]==\"A\"):\n ans+=10*(16**j)\n elif(str1[j]==\"B\"):\n ans+=11*(16**j)\n elif(str1[j]==\"C\"):\n ans+=12*(16**j)\n elif(str1[j]==\"D\"):\n ans+=13*(16**j)\n elif(str1[j]==\"E\"):\n ans+=14*(16**j)\n elif(str1[j]==\"F\"):\n ans+=15*(16**j)\n print(ans)", "try:\n t=int(input())\n for _ in range(t):\n f=str(input())\n res = int(f, 16) \n print(str(res))\nexcept:\n pass\n", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n n=input()\n print(int(n,16))", "t=int(input())\nfor _ in range(t):\n s=input()\n l=len(s)\n d,j=0,0\n for i in range(l-1,-1,-1):\n if s[i]=='A':\n d+=10*16**j\n elif s[i]=='B':\n d+=11*16**j\n elif s[i]=='C':\n d+=12*16**j\n elif s[i]=='D':\n d+=13*16**j\n elif s[i]=='E':\n d+=14*16**j\n elif s[i]=='F':\n d+=15*16**j \n else:\n d+=int(s[i])*16**j\n j+=1 \n print(d) ", "# cook your dish here\ntest_case=int(input())\nfor _ in range(test_case):\n str=input()\n i=int(str,16)\n print(i)", "# cook your dish here\ntest_case=int(input())\nfor _ in range(test_case):\n str=input()\n i=int(str,16)\n print(i)", "T = int(input())\nfor _ in range(T):\n a = str(input())\n x = int(a, 16)\n print(x)\n", "# cook your dish here\ntest_case=int(input())\nfor _ in range(test_case):\n str=input()\n i=int(str,16)\n print(i)", "# cook your dish here\nT = int(input())\n\nfor _ in range(T):\n value = input()\n print(int(value, 16))", "# cook your dish here\nfor _ in range(int(input())):\n print(int(input(),16))\n", "# cook your dish here\nfor _ in range(int(input())):\n s = input()\n print(int(s, 16))", "t=int(input())\nfor i in range(t):\n s=input()\n res = int(s, 16)\n print(res)", "t=int(input())\r\nfor _ in range(t):\r\n test_string =input()\r\n res = int(test_string, 16) \r\n print(str(res)) ", "# cook your dish here\nt=int(input())\nwhile(t):\n t=t-1\n hex=list(input())\n dec=0\n l=len(hex)-1\n for i in range(len(hex)):\n if(hex[i] not in \"ABCDEF\"):\n dec=dec+(16**(l-i))*int(hex[i])\n else:\n if(hex[i]=='A'):\n dec=dec+(16**(l-i))*10\n elif(hex[i]=='B'):\n dec=dec+(16**(l-i))*11\n elif(hex[i]=='C'):\n dec=dec+(16**(l-i))*12\n elif(hex[i]=='D'):\n dec=dec+(16**(l-i))*13\n elif(hex[i]=='E'):\n dec=dec+(16**(l-i))*14\n else:\n dec=dec+(16**(l-i))*15\n print(dec)", "# cook your dish here\nt=int(input())\nfor i in range(t):\n h=list(reversed(list(input())))\n v=\"ABCDEF\"\n r=[10,11,12,13,14,15] \n x=0\n for j in range(len(h)): \n if h[j] not in v:\n x=x+(int(h[j])*(16**j))\n else:\n y=r[v.index(h[j])]\n x=x+(int(y)*(16**j))\n \n print(x) \n", "# cook your dish here\nfor i in range(int(input())):\n x = input()\n\n hex = int(x, 16) \n print(str(hex)) ", "for t in range(int(input())):\n print(int(input(),16))", "# cook your dish here\nfor _ in range(int(input())):\n s=input()\n print(int(s,16))", "# cook your dish here\nfor i in range(int(input())):\n a=input()\n b=int(a,16)\n print(b)", "h={\"1\":1,\"2\":2,\"3\":3,\"4\":4,\"5\":5,\"6\":6,\"7\":7,\"8\":8,\"9\":9,\"A\":10,\"B\":11,\"C\":12,\"D\":13,\"E\":14,\"F\":15}\r\nres=[]\r\nT=int(input())\r\nfor i in range (T):\r\n s=input()\r\n l=len(s)\r\n count=0\r\n for j in range (l):\r\n count+=h[s[j]]*(16**(l-j-1))\r\n res.append(count)\r\nfor num in res:\r\n print(num)", "# cook your dish here\ntry:\n t = int(input())\n while t>0:\n hexdec = input()\n if hexdec == 'x':\n return\n else:\n dec = int(hexdec, 16)\n print(str(dec))\n t -= 1\nexcept EOFError:\n pass"]
{"inputs": [["3", "A", "1A23", "2C2A"]], "outputs": [["10", "6691", "11306"]]}
INTERVIEW
PYTHON3
CODECHEF
4,886
12f53c68dc80badf68c57f28271c42d3
UNKNOWN
The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 2 23 34 234 345 456 2345 3456 4567 5678 -----EXPLANATION:----- No need, else pattern can be decode easily.
["try:\r\n tc=int(input())\r\n for _ in range(tc):\r\n n=int(input())\r\n st=\"\"\r\n b=1\r\n for i in range(1,n+1):\r\n b+=1\r\n a=b\r\n for j in range(1,n+1):\r\n print(a,end='')\r\n a+=1\r\n print() \r\nexcept:\r\n pass", "for _ in range(int(input())):\n\tn = int(input())\n\tmain = 2\n\tcount = 2\n\tfor i in range(n):\n\t\tcount = main\n\t\tfor j in range(n):\n\t\t\tprint(count, end=\"\")\n\t\t\tcount += 1\n\t\tmain += 1\n\t\tprint()\n\t\n\t\t", "for i in range(int(input())):\n n= int(input())\n \n for j in range(0,n):\n s=\"\"\n for k in range(1,n+1):\n s+=str(j+k+1)\n print(s)", "# cook your dish here\nfor i in range(int(input())):\n k=int(input())\n count=2\n for j in range(k):\n count=2+j\n for l in range(k):\n print(count,end='')\n count+=1\n print()", "t=int(input())\nfor t in range(t):\n n=int(input())\n k=2\n for i in range(1,n+1):\n s=k\n for j in range(1,n+1):\n print(s,end=\"\")\n s+=1\n k+=1\n print()", "t=int(input())\nfor t in range(t):\n n=int(input())\n k=2\n for i in range(1,n+1):\n s=k\n for j in range(1,n+1):\n print(s,end=\"\")\n s+=1\n k+=1\n print()\n \n", "# cook your dish here\n# cook your dish here\nfor case in range(int(input())):\n n=int(input())\n \n k=1\n i=1\n for i in range(0,n):\n for j in range(0,n):\n k+=1\n print(k,end=\"\")\n k=2\n k+=i\n i+=1\n print(\"\")\n \n", "from sys import*\r\ninput=stdin.readline\r\nt=int(input())\r\nfor _ in range(t):\r\n k=int(input())\r\n x=2\r\n for i in range(k):\r\n y=x\r\n for j in range(k):\r\n print(y,end=\"\")\r\n y+=1\r\n x+=1\r\n print()\r\n", "try:\n for i in range(int(input())):\n n=int(input())\n \n for j in range(1,n+1):\n s=j+1\n for k in range(1,n+1):\n \n print(s,end=\"\")\n s+=1\n print(\"\\r\")\nexcept Exception:\n pass\n\n", "def solve(n):\r\n for i in range(1,n+1):\r\n j=i+1\r\n for k in range(1,n+1):\r\n print(j,end='')\r\n j+=1\r\n print()\r\nfor _ in range(int(input())):\r\n n=int(input())\r\n #s=input()\r\n #a,b=map(int,input().split())\r\n #l=list(map(int,input().split()))\r\n solve(n)\r\n", "# cook your dish here\nfor _ in range(int(input())):\n k = int(input())\n x = 2\n for i in range(k):\n for j in range(k):\n print(x+j, end = '')\n x = x + 1\n print()", "t = int(input())\r\n\r\nfor i in range(t):\r\n k = int(input()) \r\n ini = [x + 2 for x in range(0, k)]\r\n for j in range(0, k):\r\n print(\"\".join([str(x + j) for x in ini]))", "t = int(input())\r\n\r\nfor i in range(t):\r\n k = int(input()) \r\n ini = [x + 2 for x in range(0, k)]\r\n for j in range(0, k):\r\n print(\"\".join([str(x + j) for x in ini]))", "t=int(input())\nfor i in range(0,t):\n n=int(input())\n for j in range(2,n+2):\n for k in range(j,n+j):\n print(k,end=\"\")\n print(\" \")\n ", "# cook your dish here\n\n# cook your dish here\nt = int(input())\n\nwhile t:\n n = int(input())\n k = 2\n\n for i in range(1, n + 1):\n for j in range(1, n + 1):\n print(k, end='')\n k += 1\n\n k = i + 2\n print()\n \n t -= 1", "try:\r\n # def solve(n):\r\n # res=\"\"\r\n # while(n!=0):\r\n # if(n%2==0):\r\n # res=\"0\"+res\r\n # else:\r\n # res=\"1\"+res\r\n # n>>=1\r\n # return res\r\n t=int(input())\r\n for _ in range(t):\r\n n = int(input())\r\n c=2\r\n for i in range(n):\r\n z=c\r\n for j in range(n):\r\n print(z,end=\"\")\r\n z+=1\r\n c+=1\r\n print()\r\nexcept EOFError:\r\n pass", "# cook your dish here\n\n# t = [input() for i in range(n)]\n\n\ndef fk(s: str):\n s = int(s)\n for i in range(2, 1+1+int(s)):\n for k in range(i, i+s):\n print(k, end='')\n print()\n # t = 0\n # for i in range(1, s*s+1):\n # print(i*2, end='')\n # t += 1\n # if t == s:\n # print()\n # t = 0\n\n\nn = input()\nn = int(n)\nt = [fk(input()) for i in range(n)]\n\n# fk(3)\n", "# cook your dish here\nfor _ in range(int(input())):\n k = int(input())\n count = 2\n s = \"\"\n for i in range(k):\n for j in range(k):\n s += str(count+j)\n print(s)\n s = \"\"\n count += 1", "# cook your dish here\n\n# cook your dish here\nt = int(input())\n\nwhile t:\n n = int(input())\n k = 2\n\n for i in range(1, n + 1):\n for j in range(1, n + 1):\n print(k, end='')\n k += 1\n\n k = i + 2\n print()\n \n t -= 1", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n if n==1:\n print(\"2\")\n else:\n p=2\n for i in range(n):\n f=p\n for j in range(n):\n print(f,end='')\n f+=1\n p+=1\n print()", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n c=1\n for i in range(2,n+2):\n for j in range(0,n):\n print(i+j,end=\"\")\n c+=2\n print()\n ", "from sys import stdin, stdout\nfrom math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log\nfrom collections import defaultdict as dd, deque\nfrom heapq import merge, heapify, heappop, heappush, nsmallest\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nmod = pow(10, 9) + 7\nmod2 = 998244353\ndef inp(): return stdin.readline().strip()\ndef out(var, end=\"\\n\"): stdout.write(str(var)+\"\\n\")\ndef outa(*var, end=\"\\n\"): stdout.write(' '.join(map(str, var)) + end)\ndef lmp(): return list(mp())\ndef mp(): return map(int, inp().split())\ndef smp(): return map(str, inp().split())\ndef l1d(n, val=0): return [val for i in range(n)]\ndef l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]\ndef remadd(x, y): return 1 if x%y else 0\ndef ceil(a,b): return (a+b-1)//b\n\ndef isprime(x):\n if x<=1: return False\n if x in (2, 3): return True\n if x%2 == 0: return False\n for i in range(3, int(sqrt(x))+1, 2):\n if x%i == 0: return False\n return True\n\nfor _ in range(int(inp())):\n n = int(inp())\n for i in range(1, n+1):\n for j in range(1, n+1):\n print(i+j, end=\"\")\n print()", "for _ in range(int(input())):\r\n n = int(input())\r\n\r\n for i in range(1, n+1):\r\n l = []\r\n\r\n for _ in range(i+1, n+i+1):\r\n l.append(_)\r\n\r\n print(*l, sep='')\r\n"]
{"inputs": [["4", "1", "2", "3", "4"]], "outputs": [["2", "23", "34", "234", "345", "456", "2345", "3456", "4567", "5678"]]}
INTERVIEW
PYTHON3
CODECHEF
7,209
9f31984b13d95e539b5fafce3ef13eac
UNKNOWN
Ahmed Gafer failed to pass the test, but he got the job because of his friendship with Said and Shahhoud. After working in the kitchen for a while, he blew it. The customers didn't like the food anymore and one day he even burned the kitchen. Now the master Chef is very upset. Ahmed isn't useful anymore at being a co-Chef, so S&S decided to give him a last chance. They decided to give Ahmed a new job, and make him work as the cashier of the restaurant. Nevertheless, in order not to repeat their previous mistake, they decided to give him a little test to check if his counting skills are good enough for the job. The problem is as follows: Given a string A of lowercase English letters, Ahmad was asked to find the number of good substrings. A substring A[L, R] is good if: - The length of the substring is exactly 2 and AL = AR, OR - The length of the substring is greater than 2,AL = AR and the substring A[L + 1, R - 1] has only one distinct letter. Anyways, Ahmed struggled trying to find a solution for the problem. Since his mathematical skills are very poor as it turned out, he decided to cheat and contacted you asking for your help. Can you help him in this challenge? -----Input----- The first line of the input contains the integer T, indicating the number of test cases. Each of the following T lines, contains a string A. -----Output----- For each test case, output a single line containing a single number, indicating the number of good substrings. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ |A| ≤ 105 - It's guaranteed that the sum of |A| over all test cases doesn't exceed 5x105. -----Example----- Input: 2 a abba Output: 0 2 -----Explanation----- Example case 2. The good substrings of abba are: { bb } and { abba }.
["# cook your dish here\nfor _ in range(int(input())):\n s=input()\n count=0\n i=0\n while i<len(s)-1:\n ch=s[i]\n j=i+1 \n while j<len(s) and s[j]==ch:\n j+=1 \n l=j-i\n if i!=0 and j!=len(s) and s[i-1]==s[j] :\n count+=1\n count+=l*(l-1)//2\n #print(s[i:j],count)\n i=j\n print(count) \n", "# cook your dish here\nfor _ in range(int(input())):\n s=input()\n count=0\n i=0\n while i<len(s)-1:\n ch=s[i]\n j=i+1 \n while j<len(s) and s[j]==ch:\n j+=1 \n l=j-i\n if i!=0 and j!=len(s) and s[i-1]==s[j] :\n count+=1\n count+=l*(l-1)//2\n #print(s[i:j],count)\n i=j\n print(count) ", "for _ in range(int(input())):\n a = input()\n if len(a) == 1:\n print(0)\n continue\n comp = 0\n cont = 1\n ans = 0\n for i in range(1,len(a)):\n if a[i] == a[comp]:\n cont += 1\n if i == len(a) - 1:\n ans += (cont*(cont-1))//2\n else:\n ans += (cont*(cont-1))//2\n if comp != 0 and a[comp-1] == a[i]:\n ans += 1\n comp = i\n cont = 1\n print(ans)", "e=10**9+7\nt=int(input())\nfor _ in range(t):\n s=input()\n ans=0\n n=len(s)\n i=1\n m=1\n while i<n and s[i]==s[i-1]:\n m+=1\n i+=1\n ans+=(m*(m-1))//2\n while i<n:\n x=s[i-1]\n i+=1\n m=1\n while i<n and s[i]==s[i-1]:\n m+=1\n i+=1\n ans+=(m*(m-1))//2\n if i!=n and s[i]==x:\n ans+=1\n print(ans)\n"]
{"inputs": [["2", "a", "abba"]], "outputs": [["0", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
1,295
89ca3b8091f8098c73d6953b06639441
UNKNOWN
In Chefland, there is a monthly robots competition. In the competition, a grid table of N rows and M columns will be used to place robots. A cell at row i and column j in the table is called cell (i, j). To join this competition, each player will bring two robots to compete and each robot will be placed at a cell in the grid table. Both robots will move at the same time from one cell to another until they meet at the same cell in the table. Of course they can not move outside the table. Each robot has a movable range. If a robot has movable range K, then in a single move, it can move from cell (x, y) to cell (i, j) provided (|i-x| + |j-y| <= K). However, there are some cells in the table that the robots can not stand at, and because of that, they can not move to these cells. The two robots with the minimum number of moves to be at the same cell will win the competition. Chef plans to join the competition and has two robots with the movable range K1 and K2, respectively. Chef does not know which cells in the table will be used to placed his 2 robots, but he knows that there are 2 cells (1, 1) and (1, M) that robots always can stand at. Therefore, he assumes that the first robot is at cell (1, 1) and the other is at cell (1, M). Chef wants you to help him to find the minimum number of moves that his two robots needed to be at the same cell and promises to give you a gift if he wins the competition. -----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 4 space-separated integers N M K1 K2 denoting the number of rows and columns in the table and the movable ranges of the first and second robot of Chef. - The next N lines, each line contains M space-separated numbers either 0 or 1 denoting whether the robots can move to this cell or not (0 means robots can move to this cell, 1 otherwise). It makes sure that values in cell (1, 1) and cell (1, M) are 0. -----Output----- For each test case, output a single line containing the minimum number of moves that Chef’s 2 robots needed to be at the same cell. If they can not be at the same cell, print -1. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N, M ≤ 100 - 0 ≤ K1, K2 ≤ 10 ----- Subtasks ----- Subtask #1 : (25 points) - K1 = K2 = 1 Subtask # 2 : (75 points) Original Constraints -----Example----- Input: 2 4 4 1 1 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 4 4 1 1 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 1 Output: 5 -1 -----Explanation----- Example case 1. Robot 1 can move (1, 1) -> (2, 1) -> (3, 1) -> (4, 1) -> (4, 2) -> (4, 3), and robot 2 can move (1, 4) -> (2, 4) -> (3, 4) -> (4, 4) -> (4, 3) -> (4, 3), they meet at cell (4, 3) after 5 moves. Example case 2. Because the movable range of both robots is 1, robot 1 can not move from (3, 1) to (4, 2), and robot 2 can not move from (3, 4) to (4, 3. Hence, they can not meet each other.
["import sys\n\ndef spaces(a,n,m,k,visit1,visit2,dist,position):\n queue = [position]\n lastedit = []\n dist[position[0]][position[1]] = 0 \n while queue!=[]:\n point = queue[0]\n i = point[0]\n j = point[1]\n #print 'point',i,j\n if visit1[i][j]==False:\n visit1[i][j] = True\n startx = max(i-k,0)\n endx = min(i+k,n-1)\n for x in range(startx,endx+1):\n starty = max(0,j+abs(x-i)-k)\n endy = min(m-1,j-abs(x-i)+k)\n for y in range(starty,endy+1):\n if (a[x][y]==0 and visit1[x][y]==False):\n if visit2[x][y]==True:\n lastedit.append([x,y])\n #print x,y,\n if dist[x][y]>dist[i][j]+1:\n dist[x][y]=dist[i][j]+1\n queue.append([x,y])\n #print queue,dist\n queue = queue[1:]\n #print\n return lastedit\n\nfor t in range(int(input())):\n n,m,k1,k2 = list(map(int,input().split()))\n a = []\n for i in range(n):\n a.append(list(map(int,input().split())))\n #print a\n value = sys.maxsize\n listing = []\n visit1 = [[False for i in range(m)]for j in range(n)]\n visit2 = [[False for i in range(m)]for j in range(n)]\n dist1 = [[sys.maxsize for i in range(m)]for j in range(n)]\n dist2 = [[sys.maxsize for i in range(m)]for j in range(n)]\n if k1>=k2:\n spaces(a,n,m,k1,visit1,visit2,dist1,[0,0])\n else:\n spaces(a,n,m,k2,visit1,visit2,dist1,[0,m-1])\n listing = spaces(a,n,m,k1,visit2,visit1,dist2,[0,0])\n if k1>k2:\n listing = spaces(a,n,m,k2,visit2,visit1,dist2,[0,m-1])\n #print visit1\n #sprint visit2\n if k1==k2:\n if dist1[0][m-1]==sys.maxsize:\n print('-1')\n else:\n print(int((dist1[0][m-1]+1)/2))\n else:\n d = len(listing)\n for i in range(d-1,-1,-1):\n x = listing[i][0]\n y = listing[i][1]\n if visit1[x][y]==True and dist2[x][y]<value:\n value = dist2[x][y]\n if value!=sys.maxsize:\n print(value)\n else:\n print('-1')\n\n\n\n\n \n \n", "import sys\n\ndef spaces(a,n,m,k,visit1,visit2,dist,position):\n queue = [position]\n lastedit = []\n dist[position[0]][position[1]] = 0 \n while queue!=[]:\n point = queue[0]\n i = point[0]\n j = point[1]\n #print 'point',i,j\n if visit1[i][j]==False:\n visit1[i][j] = True\n startx = max(i-k,0)\n endx = min(i+k,n-1)\n for x in range(startx,endx+1):\n starty = max(0,j+abs(x-i)-k)\n endy = min(m-1,j-abs(x-i)+k)\n for y in range(starty,endy+1):\n if (a[x][y]==0 and visit1[x][y]==False):\n if visit2[x][y]==True:\n lastedit.append([x,y])\n #print x,y,\n if dist[x][y]>dist[i][j]+1:\n dist[x][y]=dist[i][j]+1\n queue.append([x,y])\n #print queue,dist\n queue = queue[1:]\n #print\n return lastedit\n\nfor t in range(int(input())):\n n,m,k1,k2 = list(map(int,input().split()))\n a = []\n for i in range(n):\n a.append(list(map(int,input().split())))\n #print a\n value = sys.maxsize\n listing = []\n visit1 = [[False for i in range(m)]for j in range(n)]\n visit2 = [[False for i in range(m)]for j in range(n)]\n dist1 = [[sys.maxsize for i in range(m)]for j in range(n)]\n dist2 = [[sys.maxsize for i in range(m)]for j in range(n)]\n if k1<=k2:\n spaces(a,n,m,k1,visit1,visit2,dist1,[0,0])\n else:\n spaces(a,n,m,k2,visit1,visit2,dist1,[0,m-1])\n listing = spaces(a,n,m,k1,visit2,visit1,dist2,[0,0])\n if k1<k2:\n listing = spaces(a,n,m,k2,visit2,visit1,dist2,[0,m-1])\n #print visit1\n #sprint visit2\n if k1==k2:\n if dist1[0][m-1]==sys.maxsize:\n print('-1')\n else:\n print(int((dist1[0][m-1]+1)/2))\n else:\n d = len(listing)\n for i in range(d-1,-1,-1):\n x = listing[i][0]\n y = listing[i][1]\n if visit1[x][y]==True and dist2[x][y]<value:\n value = dist2[x][y]\n if value!=sys.maxsize:\n print(value)\n else:\n print('-1')\n\n\n\n\n \n \n", "from queue import Queue\n\ndef run_bfs(x,y,k1,a):\n visited=[]\n n=len(a)\n m=len(a[0])\n inf=float(\"inf\")\n for i in range(0,n):\n visited=visited+[[inf]*m]\n visited[x][y]=0\n qu=Queue()\n qu.put((x,y,0))\n while(not(qu.empty())):\n (x1,y1,a1)=qu.get()\n # x1 in range(min(x1-k1,0),min(x1+k1+1,n))\n for x_n in range(max(x1-k1,0),min(x1+k1+1,n)):\n t1=abs(x_n-x1)\n for y_n in range(max(y1-k1+t1,0),min(y1+k1-t1+1,m)):\n if (a[x_n][y_n]==0):\n if (visited[x_n][y_n]==inf):\n visited[x_n][y_n]=a1+1\n qu.put((x_n,y_n,a1+1))\n return visited\n\nt=eval(input())\nfor i in range(0,t):\n inp=input()\n inp=[int(j) for j in inp.split()]\n [n,m,k1,k2]=inp\n a=[]\n for t1 in range(0,n):\n inp=input()\n inp=[int(j) for j in inp.split()]\n a=a+[inp]\n visited1=run_bfs(0,0,k1,a)\n visited2=run_bfs(0,m-1,k2,a)\n m1=float(\"inf\")\n for x in range(0,n):\n for y in range(0,m):\n m_l=max(visited1[x][y],visited2[x][y])\n m1=min(m_l,m1)\n if (m1<float(\"inf\")):\n print(m1)\n else:\n print(\"-1\")", "import sys\nsys.setrecursionlimit(10000000)\n \nflag = []\n \ndef isSafe(l, x, y, n, m):\n if x >= 0 and x < n and y >= 0 and y < m and l[x][y] == 0:\n return True\n return False\n \ndef solveMaze(l, n, m): \n x = solveMazeUtil(l, n, m, 0, 0, 0) \n return x\n \ndef solveMazeUtil(l, n, m, x, y, count):\n\n nonlocal flag\n\n count += 1\n if x == 0 and y == m-1:\n if l[x][y] == 0:\n return count\n return -1\n \n \n if isSafe(l, x, y, n, m) == True:\n\n if flag[x][y] == 0:\n\n flag[x][y] = 1\n\n #if flag[x-1][y] == 0:\n z = solveMazeUtil(l, n, m, x-1, y, count)\n if z != -1:\n return z\n\n #if flag[x][y-1] == 0:\n z = solveMazeUtil(l, n, m, x, y-1, count)\n if z != -1:\n return z\n\n #if flag[x][y+1] == 0:\n z = solveMazeUtil(l, n, m, x, y+1, count)\n if z != -1:\n return z\n\n #if flag[x+1][y] == 0:\n z = solveMazeUtil(l, n, m, x+1, y, count)\n if z != -1:\n return z\n\n return -1\n\n return -1 \n \ndef main():\n\n nonlocal flag\n for q in range(eval(input())):\n \n a = [int(i) for i in input().split()]\n n = a[0]\n m = a[1]\n l = []\n \n if a[2] == 0 and a[3] == 0:\n print(-1)\n \n else:\n\n flag = [[0 for i in range(m)] for j in range(n)]\n for i in range(n):\n l.append([int(i) for i in input().split()])\n \n ans = solveMaze(l, n, m)\n \n if a[2] == 0 or a[3] == 0:\n print(ans)\n \n else:\n if ans >= 0:\n print(ans/2)\n else:\n print(ans) \n\nmain()", "#!/bin/python\nfrom heapq import heappush, heappop\nimport sys\ndef getDistancesBot1(a,n,m,mat,k):\n for i in range(n):\n mat.append([20000 for j in range(m)])\n heap = []\n mat[0][0]=0;\n heappush(heap, (0,0,0))\n while len(heap)!=0:\n item=heappop(heap);\n top=max(0,item[2]-k)\n bottom=min(n-1,item[2]+k)\n for y in range(top,bottom+1):\n c=k-abs(y-item[2]) \n left=max(0,item[1]-c)\n right=min(m-1,item[1]+c)\n for x in range(left,right+1):\n if a[y][x]==0:\n if mat[y][x]>(item[0]+1):\n mat[y][x]=item[0]+1\n heappush(heap, (mat[y][x],x,y))\ndef getDistancesBot2(a,n,m,mat,k):\n for i in range(n):\n mat.append([20000 for j in range(m)])\n heap = []\n mat[0][m-1]=0;\n heappush(heap, (0,m-1,0))\n while len(heap)!=0:\n item=heappop(heap);\n top=max(0,item[2]-k)\n bottom=min(n-1,item[2]+k)\n for y in range(top,bottom+1):\n c=k-abs(y-item[2]) \n left=max(0,item[1]-c)\n right=min(m-1,item[1]+c)\n for x in range(left,right+1):\n if a[y][x]==0:\n if mat[y][x]>(item[0]+1):\n mat[y][x]=item[0]+1\n heappush(heap, (mat[y][x],x,y))\n \nt=int(input().strip())\nfor i in range(t):\n [n,m,k1,k2] = list(map(int,input().strip().split(' ')));\n arr=[]\n for j in range(n):\n arr.append(list(map(int,input().strip().split(' '))))\n mat1=[]\n mat2=[]\n getDistancesBot1(arr,n,m,mat1,k1)\n getDistancesBot2(arr,n,m,mat2,k2)\n minSteps=2000\n for x in range(n):\n for y in range(m):\n minSteps=min(minSteps,max(mat1[x][y],mat2[x][y]))\n if minSteps==2000:\n print(-1)\n else:\n print(minSteps)\n"]
{"inputs": [["2", "4 4 1 1", "0 1 1 0", "0 1 1 0", "0 1 1 0", "0 0 0 0", "4 4 1 1", "0 1 1 0", "0 1 1 0", "0 1 1 0", "1 0 0 1"]], "outputs": [["5", "-1"]]}
INTERVIEW
PYTHON3
CODECHEF
7,738
54078897a043438922c09e82b3177d83
UNKNOWN
$Neha$ is given a number $N$. She always looks for special thing , this time she is looking for $Special$ $Number$ and $Partial$ $Special$ $Number$. A $Special$ $Number$ is a number whose product of its digits is equal to number itself i.e. $N $, and in this number there is no digit $1$. $Partial$ $Special$ is a number having all the condition same as $Special$ except that it can also have digit $1$ in it .Neha have to count the number of $Special$ and $Partial$ $Special$ $Numbers $for a given $N$ . She is not so good in programming , so go and help her. -----Input:----- - Integers $N$ is taken as input from input stream. -----Output:----- - Print the number of $Special$ and $Partial$ $Special$ $Numbers $for a given $N$. -----Constraints----- - $1 \leq N \leq 100$ numbers to be count will be less then 10^6 -----Sample Input:----- 3 -----Sample Output:----- 1 20 -----EXPLANATION:----- There are only one natural numbers, the product of the digits of which is 3 :- {3}. There are 20 natural numbers with digit 1 , whose product of the digits is 3 :-{13, 31, 113 ,131 311 ,1113 ,1131 ,1311, 3111 ,11113, 11131, 11311 ,13111, 31111, 111113, 111131, 111311,113111, 131111 ,311111}
["n=int(input())\r\na=[]\r\nb=[]\r\nfor i in range(1,1000001):\r\n s = str(i)\r\n p=1\r\n flag=0\r\n for e in s:\r\n if e=='1':\r\n flag=1\r\n p=p*int(e)\r\n if p==n:\r\n if flag!=1:\r\n a.append(i)\r\n else:\r\n b.append(i)\r\nprint(len(a),len(b))", "n=int(input())\nc1,c2=0,0\nfor i in range(1000001):\n j=i\n p=1\n f=0\n while(j>0):\n p*=(j%10)\n if(j%10==1):\n f=1\n j=j//10\n if(p==n):\n if(f==0):\n c1+=1\n else:\n c2+=1\nprint(c1,c2)", "x=[0, 0, 1, 1, 2, 1, 3, 1, 4, 2, 2, 0, 7, 0, 2, 2, 7, 0, 7, 0, 5, 2, 0, 0, 17, 1, 0, 3, 5, 0, 8, 0, 13, 0, 0, 2, 21, 0, 0, 0, 12, 0, 8, 0, 0, 5, 0, 0, 38, 1, 3, 0, 0, 0, 15, 0, 12, 0, 0, 0, 24, 0, 0, 5, 24, 0, 0, 0, 0, 0, 6, 0, 58, 0, 0, 3, 0, 0, 0, 0, 26, 5, 0, 0, 24, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 82, 0, 3, 0, 9]\ny=[0, 6, 20, 20, 54, 20, 88, 20, 122, 54, 68, 0, 238, 0, 68, 68, 224, 0, 238, 0, 170, 68, 0, 0, 522, 34, 0, 102, 170, 0, 272, 0, 358, 0, 0, 68, 630, 0, 0, 0, 352, 0, 272, 0, 0, 170, 0, 0, 928, 34, 102, 0, 0, 0, 454, 0, 352, 0, 0, 0, 648, 0, 0, 170, 502, 0, 0, 0, 0, 0, 204, 0, 1300, 0, 0, 102, 0, 0, 0, 0, 576, 156, 0, 0, 648, 0, 0, 0, 0, 0, 648, 0, 0, 0, 0, 0, 1380, 0, 102, 0, 222]\nn= int(input())\nprint(x[n],y[n])", "def digitp(s,p):\n\tfor i in range(len(s)):p *= int(s[i])\n\treturn p\nn = int(input());cs = 0;cps = 0\nfor i in range(1,10**6+1):\n\ts = str(i)\n\tif digitp(s,1) == n:\n\t\tif '1' in s:cps += 1\n\t\telse:cs += 1\nprint(cs,cps)", "x=[0, 0, 1, 1, 2, 1, 3, 1, 4, 2, 2, 0, 7, 0, 2, 2, 7, 0, 7, 0, 5, 2, 0, 0, 17, 1, 0, 3, 5, 0, 8, 0, 13, 0, 0, 2, 21, 0, 0, 0, 12, 0, 8, 0, 0, 5, 0, 0, 38, 1, 3, 0, 0, 0, 15, 0, 12, 0, 0, 0, 24, 0, 0, 5, 24, 0, 0, 0, 0, 0, 6, 0, 58, 0, 0, 3, 0, 0, 0, 0, 26, 5, 0, 0, 24, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 82, 0, 3, 0, 9]\ny=[0, 6, 20, 20, 54, 20, 88, 20, 122, 54, 68, 0, 238, 0, 68, 68, 224, 0, 238, 0, 170, 68, 0, 0, 522, 34, 0, 102, 170, 0, 272, 0, 358, 0, 0, 68, 630, 0, 0, 0, 352, 0, 272, 0, 0, 170, 0, 0, 928, 34, 102, 0, 0, 0, 454, 0, 352, 0, 0, 0, 648, 0, 0, 170, 502, 0, 0, 0, 0, 0, 204, 0, 1300, 0, 0, 102, 0, 0, 0, 0, 576, 156, 0, 0, 648, 0, 0, 0, 0, 0, 648, 0, 0, 0, 0, 0, 1380, 0, 102, 0, 222]\nn= int(input())\nprint(x[n],y[n])", "def digitp(s):\n\tp = 1\n\tfor i in range(len(s)):\n\t\tp *= int(s[i])\n\treturn p\nn = int(input())\ncs = 0\ncps = 0\nfor i in range(1,10**6+1):\n\ts = str(i)\n\tif digitp(s) == n:\n\t\tif '1' in s:\n\t\t\tcps += 1\n\t\telse:\n\t\t\tcs += 1\nprint(cs,cps)\n\t\n\n\n", "# cook your dish here\nn=int(input())\ns,p=0,0\nfor i in range(10**6):\n x=list(str(i))\n m=1\n for j in x:\n if m==0:\n break\n m*=int(j)\n if m==n:\n if \"1\" in x:\n p+=1\n else:\n s+=1\nprint(s,p)\n", "n = int(input())\n\ndef getProduct(n): \n \n product = 1\n \n while (n != 0): \n product = product * (n % 10) \n n = n // 10\n \n return product \n \nc=0\ncp=0\n\nfor i in range(1, 1000000):\n count=0\n s=str(i)\n for j in s:\n if j == '1':\n count+=1\n if count>0 and getProduct(i)==n:\n cp+=1\n elif count==0 and getProduct(i)==n:\n c+=1\nprint(c,cp)", "# cook your dish here\n\ne=[[6 ,0],\n[20, 1],\n[20, 1],\n[54, 2],\n[20, 1],\n[88, 3],\n[20, 1],\n[122, 4],\n[54, 2],\n[68 ,2],\n[0 ,0],\n[238, 7],\n[0 ,0],\n[68, 2],\n[68, 2],\n[224, 7],\n[0, 0],\n[238, 7],\n[0 ,0],\n[170 ,5],\n[68 ,2],\n[0 ,0],\n[0, 0],\n[522 ,17],\n[34 ,1],\n[0, 0],\n[102 ,3],\n[170 ,5],\n[0 ,0],\n[272 ,8],\n[0 ,0],\n[358 ,13],\n[0 ,0],\n[0 ,0],\n[68 ,2],\n[630 ,21],\n[0 ,0],\n[0 ,0],\n[0 ,0],\n[352, 12],\n[0 ,0],\n[272 ,8],\n[0 ,0],\n[0 ,0],\n[170 ,5],\n[0 ,0],\n[0 ,0],\n[928 ,38],\n[34 ,1],\n[102 ,3],\n[0 ,0],\n[0 ,0],\n[0 ,0],\n[454 ,15],\n[0 ,0],\n[352 ,12],\n[0 ,0],\n[0 ,0],\n[0 ,0],\n[648 ,24],\n[0 ,0],\n[0 ,0],\n[170 ,5],\n[502 ,24],\n[0 ,0],\n[0 ,0],\n[0 ,0],\n[0 ,0],\n[0 ,0],\n[204 ,6],\n[0 ,0],\n[1300 ,58],\n[0, 0],\n[0, 0],\n[102, 3],\n[0, 0],\n[0, 0],\n[0, 0],\n[0, 0],\n[576 ,26],\n[156, 5],\n[0, 0],\n[0, 0],\n[648, 24],\n[0 ,0],\n[0, 0],\n[0 ,0],\n[0, 0],\n[0 ,0],\n[648, 24],\n[0, 0],\n[0, 0],\n[0, 0],\n[0 ,0],\n[0 ,0],\n[1380, 82],\n[0 ,0],\n[102, 3],\n[0 ,0],\n[222, 9]\n]\n\nn=int(input())\n\nprint(e[n-1][1],e[n-1][0])", "# Coder : Hakesh D #\nimport sys\n#input=sys.stdin.readline\n\nfrom collections import deque\nfrom math import ceil,sqrt,gcd,factorial\nfrom bisect import bisect_right,bisect_left\n\nmod = 1000000007\nINF = 10**18\nNINF = -INF\ndef I():return int(input())\ndef MAP():return map(int,input().split())\ndef LIST():return list(map(int,input().split()))\ndef modi(x):return pow(x,mod-2,mod)\ndef lcm(x,y):return (x*y)//gcd(x,y)\ndef write(l):\n for i in l:\n print(i,end=' ') \n print()\n########################################################################################\nn = int(input())\nans = out = 0\nfor i in range(1,1000000):\n p = 1\n pres = 0\n while(i != 0):\n digit = i % 10\n if(digit == 1):pres = 1\n p *= digit\n i = i//10\n if(p == n and pres == 0):ans+=1\n if(p == n and pres == 1):out+=1\n\nprint(ans,out)\n\n\n", "# cook your dish here\nn = int(input().lstrip())\n\n\nsn = 0\npsn = 0\n\nfor i in range(1,1000000):\n\tprod = 1\n\tfor j in str(i):\n\t\tprod*= int(j)\n\n\tif prod==n:\n\t\tif \"1\" in str(i):\n\t\t\tpsn+=1\n\t\telse:\n\t\t\tsn+=1\n\nprint(sn,psn)", "# cook your dish here\ndef getProduct(n):\n product = 1\n\n while (n != 0):\n product = product * (n % 10)\n n = n // 10\n\n return product\nspecial=0\npartially=0\nn=int(input())\nfor i in range(1,(10**6)+1):\n if getProduct(i)==n:\n str_i=str(i)\n if str_i.count(\"1\")>0:\n partially+=1\n else:\n special+=1\nprint(special,partially)", "#dt = {} for i in x: dt[i] = dt.get(i,0)+1\r\nimport sys;input = sys.stdin.readline\r\ninp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]\r\n\r\nn = inp()\r\na,b = 0,0\r\nfrom functools import reduce\r\ncal = lambda i: reduce((lambda x,y: x*y),map(int,i))\r\nfor i in range(1,10**6+1):\r\n t = str(i)\r\n if '0' not in t:\r\n if cal(t) == n:\r\n if '1' in t:\r\n b += 1\r\n else:\r\n a += 1\r\nprint(a,b)", "# cook your dish here\nN = int(input())\nsp, psp = 0, 0\n\nfor i in range(1,10**6+1):\n l = [int(j) for j in str(i)]\n prod = 1\n for k in l:\n prod *= k\n \n if prod == N and '1' not in str(i):\n sp += 1\n elif prod == N and '1' in str(i):\n psp += 1\n\nprint(sp, psp)"]
{"inputs": [["3"]], "outputs": [["1 20"]]}
INTERVIEW
PYTHON3
CODECHEF
6,723
efe407ed8fea096e718256dd457703d0
UNKNOWN
Many things in this first paragraph are references to some pretty famous YouTube stars, so be careful about rephrasing. Thanks! Michael, Kevin and Jake are sharing a cake, in celebration of their Webby award. They named it VCake. Unlike other cakes they considered, this one has finite volume and surface area. It's shaped as a normal rectangular cake with dimensions R centimeters by C centimeters. For the purposes of this problem, we can forget about three dimensions and think of a cake as just a 2D rectangle. Chef will now cut the cake into three pieces, one for each person. However, the cake's shape and Chef's really old tools pose a few restrictions: - Chef can only cut the cake, or a cake piece, across a line parallel to one of its sides. - Chef can only cut the cake, or a cake piece, from end to end. That is, she cannot cut the cake partially. - Chef can only cut the cake, or a cake piece, such that the sides of the resulting pieces (which will be rectangular in shape) are integers. In addition, Michael, Kevin and Jake also have a few preferences of their own: - They want their pieces to be connected (in one piece), and rectangular in shape. - Michael wants his piece to have an area exactly M square centimeters. (Again, forget about a third dimension.) - Kevin wants his piece to have an area exactly K square centimeters. - Jake wants his piece to have an area exactly J square centimeters. With these restrictions, Chef is at a loss. Is it possible for Chef to accomplish this task? Please note that the entire cake should be used. There should be no leftover cake. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case consists of a single line containing five space separated integers R, C M, K and J. -----Output----- For each test case, output a single line containing either “Yes” or “No” (without quotes), denoting whether Chef can accomplish the task or not. -----Constraints----- - 1 ≤ T ≤ 105 - 1 ≤ R, C ≤ 109 - 1 ≤ M, K, J ≤ 1018 -----Example----- Input:4 4 5 10 4 6 4 5 6 10 4 4 5 4 6 10 2 2 2 2 2 Output:Yes Yes Yes No -----Explanation----- Example case 1. In this case, Chef can accomplish the task by doing the following slicing. pre tt _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _________ | | | | | | |M M M M M| | | -- |_ _ _ _ _| -- |_ _ _ _ _| -- |M_M_M_M_M| | | | | | | | |J J J|K K| |_ _ _ _ _| |_ _ _ _ _| |_ _ _|_ _| |J_J_J|K_K| /tt /pre I'll make an image if I have time Example case 4. Here, Michael, Kevin and Jake each wants a piece with area 2, but the total area of the cake is only 2×2 = 4. This means the task is impossible.
["#!/usr/bin/python\nimport sys\n\ndef __starting_point():\n t = int(input())\n for iteration in range(t):\n r,c,m_inp,k_inp,j_inp = input().strip().split(\" \")\n r=int(r)\n c=int(c)\n m_inp=int(m_inp)\n k_inp=int(k_inp)\n j_inp=int(j_inp)\n\n ans = \"\"\n if (r*c) != (m_inp+k_inp+j_inp):\n print(\"No\")\n continue\n else:\n flag = False\n for i in range(6):\n if flag:\n break\n if i==0:\n m = m_inp\n k = k_inp\n j = j_inp\n elif i==1:\n m = j_inp\n k = m_inp\n j = k_inp\n elif i==2:\n m = k_inp\n k = j_inp\n j = m_inp\n elif i==3:\n m = m_inp\n k = j_inp\n j = k_inp\n elif i==4:\n m = k_inp\n k = m_inp\n j = j_inp\n elif i==5:\n m = j_inp\n k = k_inp\n j = m_inp\n if m%r == 0:\n r_remain_1 = r\n c_remain_1 = c-(m/r)\n \n if k%r_remain_1 == 0:\n r_remain_2 = r_remain_1\n c_remain_2 = c_remain_1 - (k/r_remain_1)\n if r_remain_2*c_remain_2 == j:\n print(\"Yes\")\n flag = True\n continue\n if k%c_remain_1 == 0:\n c_remain_2 = c_remain_1\n r_remain_2 = r_remain_1 - (k/c_remain_1)\n if r_remain_2*c_remain_2 == j:\n print(\"Yes\")\n flag = True\n continue\n \n if j%r_remain_1 == 0:\n r_remain_2 = r_remain_1\n c_remain_2 = c_remain_1 - (j/r_remain_1)\n if r_remain_2*c_remain_2 == k:\n print(\"Yes\")\n flag = True\n continue\n if j%c_remain_1 == 0:\n c_remain_2 = c_remain_1\n r_remain_2 = r_remain_1 - (j/c_remain_1)\n if r_remain_2*c_remain_2 == k:\n print(\"Yes\")\n flag = True\n continue\n \n if m%c == 0:\n c_remain_1 = c\n r_remain_1 = r-(m/c) \n\n if k%r_remain_1 == 0:\n r_remain_2 = r_remain_1\n c_remain_2 = c_remain_1 - (k/r_remain_1)\n if r_remain_2*c_remain_2 == j:\n print(\"Yes\")\n flag = True\n continue\n if k%c_remain_1 == 0:\n c_remain_2 = c_remain_1\n r_remain_2 = r_remain_1 - (k/c_remain_1)\n if r_remain_2*c_remain_2 == j:\n print(\"Yes\")\n flag = True\n continue\n \n if j%r_remain_1 == 0:\n r_remain_2 = r_remain_1\n c_remain_2 = c_remain_1 - (j/r_remain_1)\n if r_remain_2*c_remain_2 == k:\n print(\"Yes\")\n flag = True\n continue\n if j%c_remain_1 == 0:\n c_remain_2 = c_remain_1\n r_remain_2 = r_remain_1 - (j/c_remain_1)\n if r_remain_2*c_remain_2 == k:\n print(\"Yes\")\n flag = True\n continue\n if not flag:\n print(\"No\")\n__starting_point()", "import string\n\nT=eval(input())\nfor x in range(T):\n #a=raw_input()\n b=input().split()\n R=int(b[0])\n C=int(b[1])\n M=int(b[2])\n K=int(b[3])\n J=int(b[4])\n if (R*C)!=M+K+J:\n print(\"No\")\n continue\n if R==1 or C==1:\n print(\"Yes\")\n continue\n\n if M%R==0:\n C=C-M/R\n if K%R==0 or K%C==0 or J%R==0 or J%C==0:\n print(\"Yes\")\n continue\n else:\n C=C+M/R\n if M%C==0:\n R=R-M/C\n if K%R==0 or K%C==0 or J%R==0 or J%C==0:\n print(\"Yes\")\n continue\n else:\n R=R+M/C\n if K%R==0:\n C=C-K/R\n if M%R==0 or M%C==0 or J%R==0 or J%C==0:\n print(\"Yes\")\n continue\n else:\n C=C+K/R \n if K%C==0:\n R=R-K/C\n if M%R==0 or M%C==0 or J%R==0 or J%C==0:\n print(\"Yes\")\n continue\n else:\n R=R+K/C\n if J%R==0:\n C=C-J/R\n if K%R==0 or K%C==0 or M%R==0 or M%C==0:\n print(\"Yes\")\n continue\n C=C+J/R\n if J%C==0:\n R=R-J/C\n if K%R==0 or K%C==0 or M%R==0 or M%C==0:\n print(\"Yes\")\n continue\n else:\n R=R+J/C\n \n \n print(\"No\")", "def twonocheck(k, j, r, c):\n if k % r == 0:\n nc = c - k / r\n if r * nc == j:\n return True\n if k % c == 0:\n nr = r - k / c\n if nr * c == j:\n return True\n return False\n\n\ndef nocheck(m, k, j, r, c):\n if m % r == 0:\n nc = c - m / r\n if nc != 0:\n if twonocheck(k, j, r, nc):\n return True\n elif twonocheck(j, k, r, nc):\n return True\n if m % c == 0:\n nr = r - m / c\n if nr != 0:\n if twonocheck(k, j, nr, c):\n return True\n elif twonocheck(j, k, nr, c):\n return True\n return False\n\nfor i in range(int(input())):\n inp = input().split()\n r = int(inp[0])\n c = int(inp[1])\n m = int(inp[2])\n k = int(inp[3])\n j = int(inp[4])\n # r, c, m, k, j = map(int, raw_input().split())\n\n if nocheck(m, k, j, r, c):\n print(\"Yes\")\n # elif nocheck(m, j, k, r, c):\n # print \"Yes\"\n elif nocheck(j, m, k, r, c):\n print(\"Yes\")\n # elif nocheck(j, k, m, r, c):\n # print \"Yes\"\n elif nocheck(k, m, j, r, c):\n print(\"Yes\")\n # elif nocheck(k, j, m, r, c):\n # print \"Yes\"\n else:\n print(\"No\")\n", "for t in range(int(input())):\n r,c,p1,p2,p3=list(map(int,input().split()))\n if ((r*c)!=(p1+p2+p3)):\n print('No')\n continue\n if (p1%r)==0:\n nc=c-(p1/r)\n if ((p2%r==0 and p3%r==0) or (p2%nc==0 and p3%nc==0)):\n print('Yes')\n continue\n if (p2%r)==0:\n nc=c-(p2/r)\n if ((p1%r==0 and p3%r==0) or (p1%nc==0 and p3%nc==0)):\n print('Yes')\n continue\n if (p3%r)==0:\n nc=c-(p3/r)\n if ((p2%r==0 and p1%r==0) or (p2%nc==0 and p1%nc==0)):\n print('Yes')\n continue\n if (p1%c)==0:\n nr=r-(p1/c)\n if ((p2%nr==0 and p3%nr==0) or (p2%c==0 and p3%c==0)):\n print('Yes')\n continue\n if (p2%c)==0:\n nr=r-(p2/c)\n if ((p1%nr==0 and p3%nr==0) or (p1%c==0 and p3%c==0)):\n print('Yes')\n continue\n if (p3%c)==0:\n nr=r-(p3/c)\n if ((p2%nr==0 and p1%nr==0) or (p2%c==0 and p1%c==0)):\n print('Yes')\n continue\n print('No')\n", "def cut2(x, y, m, n):\n #print \"got {}*{} and {},{}\".format(x,y,m,n)\n return m%x==0 or n%y==0\n \ndef cut1(x, y, l, m, n):\n if x*y != l+m+n:\n return False\n else:\n if l%x==0:\n if cut2(x, y-l/x, m, n):\n return True\n if l%y==0:\n if cut2(x-l/y, y, m, n):\n return True\n if m%x==0:\n if cut2(x, y-m/x, l, n):\n return True\n if not m%y:\n if cut2(x-m/y, y, l, n):\n return True\n if not n%x:\n if cut2(x, y-n/x, m, l):\n return True\n if not n%y:\n if cut2(x-n/y, y, m, l):\n return True\n return False\n\nt = int(input())\n\nfor i in range(t):\n x, y, l, m, n = list(map(int, input().strip().split()))\n print(\"Yes\" if cut1(x,y,l,m,n) else \"No\")\n", "# your code goes here\nn = input()\nn = int(n)\nfor i in range (0,n):\n r,c,j,k,m = list(map(int, input().split(\" \")))\n if(r*c != m + k +j):\n print(\"No\")\n elif m%c == 0 and (k*c)%(j+k)==0 and (j*c)%(j+k)==0:\n print(\"Yes\")\n elif j%c == 0 and (k*c)%(m+k)==0 and (m*c)%(m+k)==0:\n print(\"Yes\")\n elif k%c == 0 and (j*c)%(j+m)==0 and (m*c)%(j+m)==0:\n print(\"Yes\")\n elif m%r == 0 and (k*r)%(j+k)==0 and (j*r)%(j+k)==0:\n print(\"Yes\")\n elif j%r == 0 and (k*r)%(m+k)==0 and (m*r)%(m+k)==0:\n print(\"Yes\")\n elif k%r == 0 and (j*r)%(m+j)==0 and (m*r)%(m+j)==0:\n print(\"Yes\")\n elif k%c == 0 and j%c==0 and m%c==0:\n print(\"Yes\")\n elif k%r == 0 and j%r==0 and m%r==0:\n print(\"Yes\")\n else:\n print(\"No\")", "def check2(r,x,k,j):\n if k%r==0 and j%r==0:\n return True\n if (k%x ==0) and j%x ==0:\n return True\n return False\n\ndef check(r,c,m,k,j):\n if m%r!=0:\n return False\n x = c - (m/r)\n return check2(r,x,k,j)\n\nfor _ in range(int(input())):\n r,c,m,k,j = list(map(int, input().split()))\n if r*c != m+k+j :\n print(\"No\")\n else:\n b =False\n b|=check(r,c,m,k,j);\n b|=check(r,c,k,m,j);\n b|=check(r,c,j,m,k);\n b|=check(c,r,m,k,j);\n b|=check(c,r,k,m,j);\n b|=check(c,r,j,m,k);\n if b:\n print('Yes')\n else:\n print('No') \n", "try:\n t=eval(input())\nexcept EOFError:\n t=0\nwhile(t):\n try:\n r,c,m,k,j=list(map(int,input().split()))\n except EOFError:\n r=1\n c=1\n m=1\n k=1\n j=1\n area1=r*c\n area2=m+k+j\n if(area1!=area2):\n print(\"No\")\n else:\n f=0\n if(m%r==0):\n rr=r\n cc=c\n a=m//rr\n cc=cc-a\n if(((k%rr==0) and (j%rr==0)) or ((k%cc==0) and (j%cc==0))):\n f=1\n if(m%c==0 and f==0):\n rr=r\n cc=c\n a=m//cc\n rr-=a\n if(((k%rr==0) and (j%rr==0)) or ((k%cc==0) and (j%cc==0))):\n f=1\n if(k%r==0 and f==0):\n rr=r\n cc=c\n a=k//rr\n cc=cc-a\n if(((m%rr==0) and (j%rr==0)) or ((m%cc==0) and (j%cc==0))):\n f=1\n if(k%c==0 and f==0):\n rr=r\n cc=c\n a=k//cc\n rr=rr-a\n if(((m%rr==0) and (j%rr==0)) or ((m%cc==0) and (j%cc==0))):\n f=1\n if(j%r==0 and f==0):\n rr=r\n cc=c\n a=j//rr\n cc=cc-a\n if(((k%rr==0) and (m%rr==0)) or ((k%cc==0) and (m%cc==0))):\n f=1\n if(j%c==0 and f==0):\n rr=r\n cc=c\n a=j//cc\n rr=rr-a\n if(((k%rr==0) and (m%rr==0)) or ((k%cc==0) and (m%cc==0))):\n f=1\n if(f==1):\n print(\"Yes\")\n else:\n print(\"No\")\n t=t-1\n", "import sys\nimport itertools as it\n\ndef main():\n t=int(input())\n while t>0:\n t-=1\n r,c,m,k,j=list(map(int,input().split()))\n if m+k+j!=r*c:\n print('No')\n else:\n l=[m,k,j]\n l=list(it.permutations(l))\n chk=0\n for x in l:\n f=test(x,r,c) + test(x,c,r)\n if f>=1:\n chk=1\n break\n if chk==1:\n print('Yes')\n else:\n print('No')\ndef test(l,r,c):\n for x in range(2,0,-1):\n if l[x]%r==0:\n d=l[x]/r\n c=c-d\n elif l[x]%c==0:\n d=l[x]/c\n r=r-d\n else:\n return 0\n #print r,l,c\n if r==0 or c==0:\n return 0\n if r*c==l[0]:\n return 1\n else:\n return 0\n\ndef __starting_point():\n main()\n\n__starting_point()", "for _ in range(eval(input())):\n r,c,x,y,z=list(map(int,input().split()))\n s=r*c\n f=x+y+z\n if x+y+z==s:\n m=x\n if m%r==0 or m%c==0:\n if m%r==0:\n p=m//r\n q=c-p\n if p<c:\n g=z\n h=y\n k=r\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n q,k=k,q\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n if m%c==0:\n p=m//c\n q=r-p\n if p<r:\n g=z\n h=y\n k=c\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n q,k=k,q\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n m=y\n if m%r==0 or m%c==0:\n if m%r==0:\n p=m//r\n q=c-p\n if p<c:\n g=x\n h=z\n k=r\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n q,k=k,q\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n if m%c==0:\n p=m//c\n q=r-p\n if p<r:\n g=x\n h=z\n k=c\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n q,k=k,q\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n m=z\n if m%r==0 or m%c==0:\n if m%r==0:\n p=m//r\n q=c-p\n if p<c:\n g=x\n h=y\n k=r\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n q,k=k,q\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n if m%c==0:\n p=m//c\n q=r-p\n if p<r:\n g=x\n h=y\n k=c\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n q,k=k,q\n if g%q==0:\n if((k-(g/q))*q)==h:\n print(\"Yes\")\n continue\n \n print(\"No\")\n", "\ndef solve(r, c, ar ):\n \n if not r*c == ar[0] + ar[1] + ar[2]:\n return \"No\"\n \n if ar[0] % r == ar[1] % r == ar[2] % r == 0:\n if ar[0] / r + ar[1] / r + ar[2] / r == c:\n return \"Yes\"\n \n if ar[0] % c == ar[1] % c == ar[2] % c == 0:\n if ar[0] / c + ar[1] / c + ar[2] / c == r:\n return \"Yes\" \n \n for i in range(len(ar)):\n if ar[i] % r == 0:\n cnt = 0\n sml = 0\n sma = 0\n c2 = c - ar[i] / r\n for j in range(len(ar)):\n if i == j:\n continue\n if ar[j] % c2 == 0:\n cnt += 1\n sml += ar[j] / c2\n sma += ar[j]\n if cnt == 2 and sml == r:\n if sma + ar[i] == r * c:\n return \"Yes\"\n if ar[i] % c == 0:\n cnt = 0\n sml = 0\n sma = 0\n r2 = r - ar[i] / c\n for j in range(len(ar)):\n if i == j:\n continue\n if ar[j] % r2 == 0:\n cnt += 1\n sml += ar[j] / r2\n sma += ar[j]\n if cnt == 2 and sml == c:\n if sma + ar[i] == r * c:\n return \"Yes\" \n return \"No\"\n \n \nfor cas in range(eval(input())):\n r, c, m, k, j = list(map(int, input().strip().split()))\n print(solve( r, c, [m, k, j] ))\n \n \n", "def get_result(r,c,dims):\n if len(dims)==0:\n if r*c==0:\n return 1\n return 0;\n for i in range(len(dims)):\n newdims = [dims[x] for x in range(len(dims)) if x!=i]\n if r!=0 and dims[i]%r==0:\n if get_result(r,c-dims[i]/r,newdims):\n return 1\n if c!=0 and dims[i]%c==0:\n if get_result(r-dims[i]/c,c,newdims):\n return 1\n return 0\n \n \n \n\ndef __starting_point():\n num = int(input())\n for i in range(num):\n R,C,M,K,J = [int(x) for x in input().split()]\n if(get_result(R,C,[M,K,J])):\n print(\"Yes\")\n else:\n print(\"No\")\n__starting_point()", "for x in range(0,eval(input())):\n p=list(map(int,input().split()))\n a=p[2]\n b=p[3]\n c=p[4]\n ro=p[0]\n co=p[1]\n ar=ro*co\n if a==ar or b==ar or c==ar:\n print(\"No\")\n continue\n if a%ro==0:\n nr=ro\n nc=co-(a/ro)\n if b%nr==0 and c%nr==0 and (b/nr)+(c/nr)==nc:\n print(\"Yes\")\n continue\n else:\n if b%nc==0 and c%nc==0 and (b/nc)+(c/nc)==nr:\n print(\"Yes\")\n continue\n if b%ro==0:\n nr=ro\n nc=co-(b/ro)\n if a%nr==0 and c%nr==0 and (a/nr)+(c/nr)==nc:\n print(\"Yes\")\n continue\n else:\n if a%nc==0 and c%nc==0 and (a/nc)+(c/nc)==nr:\n print(\"Yes\")\n continue\n if c%ro==0:\n nr=ro\n nc=co-(c/ro)\n if b%nr==0 and a%nr==0 and (b/nr)+(a/nr)==nc:\n print(\"Yes\")\n continue\n else:\n if b%nc==0 and a%nc==0 and (b/nc)+(a/nc)==nr:\n print(\"Yes\")\n continue\n if a%co==0:\n nr=ro-(a/co)\n nc=co\n if b%nr==0 and c%nr==0 and (b/nr)+(c/nr)==nc:\n print(\"Yes\")\n continue\n else:\n if b%nc==0 and c%nc==0 and (b/nc)+(c/nc)==nr:\n print(\"Yes\")\n continue\n if b%co==0:\n nr=ro-(b/co)\n nc=co\n if a%nr==0 and c%nr==0 and (a/nr)+(c/nr)==nc:\n print(\"Yes\")\n continue\n else:\n if a%nc==0 and c%nc==0 and (a/nc)+(c/nc)==nr:\n print(\"Yes\")\n continue\n if c%co==0:\n nr=ro-(c/co)\n nc=co\n if b%nr==0 and a%nr==0 and (b/nr)+(a/nr)==nc:\n print(\"Yes\")\n continue\n else:\n if b%nc==0 and a%nc==0 and (b/nc)+(a/nc)==nr:\n print(\"Yes\")\n continue\n print(\"No\")\n", "t = int (eval(input()))\n\nwhile t > 0:\n t -= 1\n r, c, m, j, k = list(map (int, input().split()))\n area = r * c\n if m + j + k != area:\n print(\"No\")\n continue\n l = r\n b = c\n if m % l == 0:\n b -= m/l\n if b > 0 and (j % l == 0 or j % b == 0 or k %l == 0 or k % b == 0):\n print(\"Yes\")\n continue\n l = r\n b = c\n if m % b == 0:\n l -= m/b\n if l > 0 and (j % l == 0 or j % b == 0 or k %l == 0 or k % b == 0):\n print(\"Yes\")\n continue\n \n l = r\n b = c\n if j % l == 0:\n b -= j/l\n if b > 0 and (m % l == 0 or m % b == 0 or k %l == 0 or k % b == 0):\n print(\"Yes\")\n continue\n l = r\n b = c\n if j % b == 0:\n l -= j/b\n if l > 0 and (m % l == 0 or m % b == 0 or k %l == 0 or k % b == 0):\n print(\"Yes\")\n continue\n \n l = r\n b = c\n if k % l == 0:\n b -= k/l\n if b > 0 and (j % l == 0 or j % b == 0 or m %l == 0 or m % b == 0):\n print(\"Yes\")\n continue\n l = r\n b = c\n if k % b == 0:\n l -= k/b\n if l > 0 and (j % l == 0 or j % b == 0 or m % l == 0 or m % b == 0):\n print(\"Yes\")\n continue\n \n print(\"No\")\n", "def do_cut(r,c,m,k,j,mask):\n if mask==0:\n if (r==0 or c==0):\n return True\n else:\n return False\n\n if r<=0 or c<=0:\n return False\n\n cut_poss = False\n ans = False\n\n if m%r==0 and (mask&4):\n ans = ans or do_cut(r,c-(m/r),m,k,j,mask&3)\n cut_poss = True\n if m%c==0 and (mask&4):\n ans = ans or do_cut(r-(m/c),c,m,k,j,mask&3)\n cut_poss = True\n\n if k%r==0 and (mask&2):\n ans = ans or do_cut(r,c-(k/r),m,k,j,mask&5)\n cut_poss = True\n if k%c==0 and (mask&2):\n ans = ans or do_cut(r-(k/c),c,m,k,j,mask&5)\n cut_poss = True\n\n if j%r==0 and (mask&1):\n ans = ans or do_cut(r,c-(j/r),m,k,j,mask&6)\n cut_poss = True\n if j%c==0 and (mask&1):\n ans = ans or do_cut(r-(j/c),c,m,k,j,mask&6)\n cut_poss = True\n\n if not cut_poss:\n return False\n\n return ans\n\n\ndef solve():\n t = eval(input())\n while t>0:\n r,c,m,k,j = list(map(int,input().split()))\n ans = do_cut(r,c,m,k,j,7)\n print('Yes' if ans else 'No')\n t-=1\n\n\nsolve() ", "\n\ndef can_cut_h(r,c, s1, s2):\n if (s1 % r != 0) and (s2 % r != 0):\n return None\n c1, c2 = s1 / r, s2 / r\n if c1 + c2 != c:\n return None\n\n return c2\n\ndef can_cut_v(r,c, s1, s2):\n if (s1 % c != 0) and (s2 % c != 0):\n return None\n r1, r2 = s1 / c, s2 / c\n if r1 + r2 != r:\n return None\n\n return r2\n\ndef solve(r,c,m,k,j):\n if (m + k + j) != r*c:\n return \"No\",\n in_order = [m, k, j]\n for _ in range(3):\n s1 = in_order.pop()\n s2, s3 = in_order\n in_order.insert(0, s1)\n v_r = can_cut_v(r, c, s1, s2 + s3)\n h_c = can_cut_h(r, c, s1, s2 + s3)\n if v_r is not None:\n if can_cut_h(v_r, c, s2, s3) or can_cut_v(v_r, c, s2, s3):\n return \"Yes\",\n if h_c is not None:\n if can_cut_h(r, h_c, s2, s3) or can_cut_v(r, h_c, s2, s3):\n return \"Yes\",\n return \"No\",\n\n\ndef read_input():\n r,c,m,k,j = list(map(int, input().split()))\n return r,c,m,k,j\n\ndef __starting_point():\n t = int(input())\n for i in range(t):\n res = list(map(str, solve(*read_input())))\n print(\" \".join(res))\n # print solve(4, 5, 10, 4, 6)\n # print solve(4, 5, 6, 10, 4)\n # print solve(4, 5, 4, 6, 10)\n # print solve(2, 2, 2, 2, 2)\n\n__starting_point()"]
{"inputs": [["4", "4 5 10 4 6", "4 5 6 10 4", "4 5 4 6 10", "2 2 2 2 2"]], "outputs": [["Yes", "Yes", "Yes", "No"]]}
INTERVIEW
PYTHON3
CODECHEF
17,631
8cdef539b502c583079ffe00091a80fb
UNKNOWN
Stuart is obsessed to numbers. He like all type of numbers in fact he is having a great collection of numbers in his room. His collection includes N different large numbers. But today he is searching for a number which is having maximum frequency of digit X. Numbers are large so he can’t do the task on his own. Help him to find a number having maximum frequency of digit X. -----Input----- First Line contains number of test cases T. First Line of each test case contains N. Next line contains N space separated integers A1,A2,A3,....,AN. Where Ai integer indicates ith number in Stuart's room. Next Line contains digit X. -----Output----- Output the number which is having maximum frequency of digit X. If two or more numbers are having same maximum frequency then output the first occurred number among them in A1,A2,A3,....,AN -----Constraints----- - 1 ≤ T ≤ 30 - 1 ≤ N ≤ 100 - 1 ≤ Ai ≤ 10200 - 0 ≤ X ≤ 9 -----Example----- Input: 2 5 345 1323 165 98 456 3 5 335 876 98 1323 349 3 Output: 1323 335 -----Explanation----- Example case 1. 1323 number is having maximum occurrence of digit 3. Example case 2. 335 & 1323 are having maximum occurrence of digit 3 so output must be first occurred number in the array i.e. 335.
["T = int(input())\n\ndef call_me(N,A,X):\n max = 0\n ans = ''\n for i in A:\n if i.count(X) > max:\n max = i.count(X)\n ans = i\n return ans\n\n\n\nfor i in range(T):\n N = int(input())\n A = list(map(str,input().split()))\n X = input()\n print(call_me(N,A,X))\n", "for _ in range(int(input())):\n n=int(input())\n a=input().split()\n x=input()\n lis=[]\n for i in range(n):\n count=0\n for j in range(len(a[i])):\n if a[i][j]==x:\n count+=1\n lis.append(count)\n val=max(lis)\n for i in range(len(lis)):\n if lis[i]==val:\n print(a[i])\n break", "#write your code in next line. \na=int(input())\nwhile(a):\n b=int(input())\n c=str(input()).split()\n d=str(input())\n l=0\n m=0\n x=0\n while(b):\n l=c[b-1].count(d)\n if l>=x:\n x=l\n m=c[b-1]\n b-=1\n a-=1\n print(m)\n", "for t in range(int(input())):\n n=input()\n s=input().split()\n d=input()\n f=0\n num=0\n for i in s:\n z=i.count(d)\n if (z>f):\n f=z\n num=i\n print(num)", "for t in range(eval(input())):\n N = eval(input())\n A = input().split(' ')\n B = eval(input())\n count = [0 for x in range(N)]\n \n for i in range(N):\n A[i] = list(A[i])\n for j in range(len(A[i])):\n if A[i][j] == str(B):\n count[i] += 1\n A[i] = ''.join(A[i])\n print(A[count.index(max(count))])\n", "tc=int(input())\nfor i in range(tc):\n m=-1\n n=int(input())\n p=list(map(str,input().split()))\n q=input()\n # print p\n no=0\n for j in p:\n if q in j:\n l=j.count(q)\n if m<l:\n m=l\n no=j\n print(no)\n\n \n", "t=int(input())\nfor case in range(t):\n n=int(input())\n a=list(map(str,input().split()))\n k=input()\n max=-1\n for i in a :\n if max<i.count(k):\n max=i.count(k)\n num=i\n print(num) \n \n \n", "__author__ = 'ravi'\nfor i in range(int(input())):\n p=int(input())\n n=input().split()\n c=input()\n mx=0\n for j in range(p):\n if(mx<n[j].count(c)):\n mx=n[j].count(c)\n for k in n:\n if(mx==k.count(c)):\n print(k)\n break;\n", "for tc in range(int(input())):\n n=int(input())\n a=list(map(str,input().split()))\n k=input()\n cur=-1\n tmp=-2\n for i in a:\n if k in i:\n if i.count(k)>cur:\n cur=i.count(k)\n tmp=i\n print(tmp)\n"]
{"inputs": [["2", "5", "345 1323 165 98 456", "3", "5", "335 876 98 1323 349", "3"]], "outputs": [["1323", "335"]]}
INTERVIEW
PYTHON3
CODECHEF
2,160
8b11782a82f70f7ddaa30c8fe7c4b07c
UNKNOWN
Abhiram needs to search for an antidote. He comes to know that clue for finding the antidote is carefully hidden by KrishnaMurthy in the form of a puzzle. The puzzle consists of a string S and a keywordK. Abhiram needs to find the string of position of anagrams R of the keyword in the string which is the clue. The antidote is found in the box numbered R. Help him find his clue R. Anagram: A word or phrase that is made by arranging the letters of another word or phrase in a different order. Eg: 'elvis' and 'lives' are both anagrams of each other. Note: Consider, Tac and act are not anagrams(case sensitive). -----Input:----- The first line contains a string S of length land the second line contains a keyword K. -----Output:----- Output contains a line"The antidote is found in R." Where R= string of positions of anagrams.(the position of the first word in the string is 1). -----Constraints:----- 1<=l<=500 1<=k<=50 -----Example:----- Input: cat is the act of tac cat Output: The antidote is found in 46.
["x = input().split(\" \")\ny = input()\nans = ''\nl = 1\nfor i in x:\n if i!=y and sorted(i) == sorted(y):\n ans = ans + (str)(l)\n l=l+1\nans+='.'\nprint(\"The antidote is found in\",ans)", "s = input().split(\" \")\nk = input()\nl = \"abcdefghijklmnopqrstuvwxyz\"\na = 26*[0]\nn = len(k)\nc = \"\"\nfor i in range(n):\n a[l.find(k[i])]+=1\nfor i in range(len(s)):\n x = s[i]\n m = len(x)\n if (m==n):\n b = 26*[0] \n for j in range(m):\n b[l.find(x[j])]+=1\n if a==b and x!=k:\n c += str(i+1)\nprint(\"The antidote is found in \"+c+\".\")", "from collections import Counter\n\ndef is_anagram(str1, str2):\n return Counter(str1) == Counter(str2)\n\n\ns=input()\nst=input()\ns=s.split(\" \")\ni=0\nc=\"\"\nfor sr in s:\n i+=1\n if sr!=st and is_anagram(sr, st):\n c=c+str(i)\n\nprint(\"The antidote is found in \" + c+ '.')", "# your code goes here\nfrom sys import stdin, stdout\ns = stdin.readline().strip().split(' ')\nk = stdin.readline().strip()\nfor i in k:\n countk = list(k)\n countk.sort()\n countk = \"\".join(countk)\nans = \"\"\nfor i in range(len(s)):\n if s[i] != k:\n counti = list(s[i])\n counti.sort()\n counti = \"\".join(counti)\n if counti == countk:\n ans += str(i+1)\nstdout.write(\"The antidote is found in \"+ans+\".\")", "s = input()\nk = input()\nwrds = s.split(' ')\nkl = len(k)\ncomp = sorted(k)\n\ndef ana(an):\n if an == k:\n return False\n return comp == sorted(an)\n\nans = ''\nfor x in range(0, len(wrds)):\n word = wrds[x]\n if len(word) == kl and ana(word):\n ans += str(x+1)\n\nprint('The antidote is found in %s.' %ans)"]
{"inputs": [["cat is the act of tac", "cat"]], "outputs": [["The antidote is found in 46."]]}
INTERVIEW
PYTHON3
CODECHEF
1,576
870f6807b532f440bde921e558290ad8
UNKNOWN
Chef has decided to retire and settle near a peaceful beach. He had always been interested in literature & linguistics. Now when he has leisure time, he plans to read a lot of novels and understand structure of languages. Today he has decided to learn a difficult language called Smeagolese. Smeagolese is an exotic language whose alphabet is lowercase and uppercase roman letters. Also every word on this alphabet is a meaningful word in Smeagolese. Chef, we all know is a fierce learner - he has given himself a tough exercise. He has taken a word and wants to determine all possible anagrams of the word which mean something in Smeagolese. Can you help him ? -----Input----- Input begins with a single integer T, denoting the number of test cases. After that T lines follow each containing a single string S - the word chef has chosen. You can assume that 1 <= T <= 500 and 1 <= |S| <= 500. You can also assume that no character repeats more than 10 times in the string. -----Output----- Output one line per test case - the number of different words that are anagrams of the word that chef has chosen. As answer can get huge, print it modulo 10^9 + 7 -----Example----- Input: 4 ab aa aA AAbaz Output: 2 1 2 60 Description: In first case "ab" & "ba" are two different words. In third case, note that A & a are different alphabets and hence "Aa" & "aA" are different words.
["# cook your dish here\nfrom collections import Counter\nfrom math import factorial\nfor _ in range(int(input())):\n s=input()\n c=Counter(s)\n k=factorial(len(s))\n for value in c.values():\n if value>1:\n k=k//factorial(value)\n print(k%(10**9+7))", "# cook your dish here\nfrom collections import Counter\nfrom math import factorial\nfor _ in range(int(input())):\n \n s=input()\n c=Counter(s)\n k=factorial(len(s))\n \n for value in list(c.values()):\n \n if value>1:\n k=k//factorial(value)\n \n print(k%(10**9+7))\n \n \n \n", "from collections import Counter\nimport math\n\ntc=int(input())\nwhile tc>0:\n s=input()\n k=math.factorial(s.__len__())\n x=Counter(s)\n for val in x.values():\n if val>1:\n k=k//math.factorial(val)\n k=int(k)\n k=k%(10**9+7)\n print(k)\n tc-=1", "# cook your dish here\nfrom collections import Counter\nfrom math import factorial\nfor _ in range(int(input())):\n \n s=input()\n c=Counter(s)\n k=factorial(len(s))\n \n for value in list(c.values()):\n \n if value>1:\n k=k//factorial(value)\n \n print(k%(10**9+7))\n \n \n \n", "# cook your dish here\nMOD = 1000000007\n\nfact_mods = [0] * (501)\nfact_mods[0], fact_mods[1] = 1, 1\nfor i in range(2, 501):\n fact_mods[i] = (fact_mods[i-1]*i) % MOD\n\nfor _ in range(int(input())):\n s = input()\n n = len(s)\n counts = {}\n denom = 1\n for ch in s:\n counts[ch] = counts.get(ch,0) + 1\n for ch in counts:\n denom = (denom * fact_mods[counts[ch]]) % MOD\n print(fact_mods[n]*pow(denom,MOD-2,MOD) % MOD)\n \n \n", "def fact(N):\n c=1\n if N==0:\n return 1\n for i in range(1,N+1):\n c*=i\n return c\nfrom collections import Counter\nfor _ in range(int(input())):\n s=list(input())\n d=Counter(s)\n p=10**9+7\n m=1\n for v in d.values():\n m*=fact(v)\n print((fact(len(s))//m)%p)", "from collections import Counter\nimport math\nmod = (pow(10,9) + 7)\nt = int(input())\n\nfor _ in range(t):\n s = input()\n p = 1\n count = dict(Counter(s))\n for i in count:\n p = p * math.factorial(count[i]) \n x = math.factorial(len(s)) // p\n x = x % mod\n print(x)", "mod = 1000000007\ndef fact(n):\n if n <= 1:\n return 1\n else:\n i = 1\n for p in range(2,n+1):\n i *= p\n return i\nfor _ in range(int(input())):\n S = input()\n N = len(S)\n dic = {}\n for i in range(N):\n p = S[i]\n if p in list(dic.keys()):\n dic[p] += 1\n else:\n dic[p] = 1\n f = fact(N)\n for t in list(dic.keys()):\n f = f // fact(dic[t])\n print(f % mod)\n", "import math\nt=int(input())\n#print(math.factorial(5))\nfor i in range(t):\n s=input()\n l=list(s)\n seti=set(l)\n deno=1\n for i in seti:\n deno*=(math.factorial(l.count(i)))\n leng=len(l)\n ans=((math.factorial(leng))//deno)%1000000007\n print(int(ans))", "from math import factorial\nfrom collections import Counter\nfrom functools import reduce\nimport operator as op\n\nmod = 1000000007\n\n\n\nfor _ in range(int(input())): \n s=input()\n c=Counter(s)\n num = factorial(len(s))\n den = 1\n for k in c:\n den*=factorial(c[k])\n print(num//den%mod)\n", "# cook your dish here\nimport math\nfor _ in range(int(input())):\n s=input()\n a=[0]*26\n b=[0]*26\n for i in range(len(s)):\n if s[i].islower():\n a[97-ord(s[i])]+=1\n else:\n b[65-ord(s[i])]+=1\n sum = math.factorial(len(s))\n su=sum\n for i in range(26):\n su=su//(math.factorial(a[i])*math.factorial(b[i]))\n print(su%(10**9+7)) ", "# your code goes here\nfrom collections import defaultdict\n\n\ndef fac(num, modulo):\n ans = 1\n for i in range(2, num+1):\n ans = (ans*i)%modulo\n return ans\n \n \ndef fast_expo(num, p, modulo):\n num %= modulo\n exxp = 1\n while(p>0):\n if p&1:\n exxp = exxp * num%modulo\n num = num * num % modulo\n p >>= 1\n return exxp\n \n\nmodulo = 1000000007\n\nt = int(input())\n\nfor i in range(t):\n s = input().strip()\n \n mp = defaultdict(int)\n for si in s:\n mp[si] += 1\n \n denom = 1\n for alpha in mp.keys():\n denom = denom*fac(mp[alpha], modulo)\n denom = fast_expo(denom, modulo-2, modulo)\n \n nfac = fac(len(s), modulo)\n nfac = nfac*denom%modulo\n \n print(int(nfac))", "e=10**9+7\n\ndef fact(n):\n ans=1\n for i in range(2,n+1):\n ans=(ans*i)%e\n return ans\n \nt=int(input())\nfor _ in range(t):\n s=input()\n n=len(s)\n d={}\n for i in s:\n try:\n d[i]+=1\n except:\n d[i]=1\n x=fact(n)\n for i in list(d.values()):\n y= fact(i)\n y = pow(y,e-2,e)\n x=(x*y)%e\n print(x)\n", "# your code goes here\nfrom collections import Counter\ndef fact(num):\n factorial = 1\n for i in range(1,num + 1):\n factorial = factorial*i\n return factorial;\nt=int(input())\nfor _ in range(t):\n done = dict()\n strr=str(input())\n ans=fact(len(strr))\n res=Counter(strr)\n for i in res:\n done[i]=0;\n l=1\n for i in res:\n if(done[i]==0):\n l=l*fact(res[i])\n done[i]=1\n ans=ans//l\n ans=ans%1000000007\n print(ans)", "# cook your dish here\nfrom math import factorial\n\nt = int(input())\n\nfor i in range(t):\n\n s = input()\n hash = {}\n\n for j in s:\n try:\n hash[j]\n except:\n hash[j] = 1\n else:\n hash[j]+=1\n z = factorial(len(s))\n m = 10**9 + 7\n k = 1\n \n for j in list(hash.keys()):\n k*=factorial(hash[j])\n print((z//k)%m)\n\n\n", "import collections\n\nfact = [1] * 510\nMOD = 10 ** 9 + 7\nfor i in range(2, 502):\n fact[i] = (fact[i - 1] * i) % MOD\n\nfor _ in range(int(input())):\n s = input()\n d = collections.Counter(s)\n n = len(s)\n \n num = fact[n]\n den = 1\n for i in d:\n den = (den * fact[d[i]]) % MOD\n \n inv = pow(den, MOD - 2, MOD)\n \n print((num * inv) % MOD)", "from math import factorial\n\nt = int(input())\nfor _ in range(t):\n line = input()\n char = []\n for i in line:\n char.append(i)\n char.sort()\n same = []\n current = char[0]\n k = 0\n for i in char:\n if i == current:\n k += 1\n else:\n current = i\n if k > 1:\n same.append(k)\n k = 1\n same.append(k)\n ans = factorial(len(char))\n for i in same:\n ans //= factorial(i)\n print(ans % 1000000007)"]
{"inputs": [["4", "ab", "aa", "aA", "AAbaz"]], "outputs": [["2", "1", "2", "60", "Description:", "In first case \"ab\" & \"ba\" are two different words. In third case, note that A & a are different alphabets and hence \"Aa\" & \"aA\" are different words."]]}
INTERVIEW
PYTHON3
CODECHEF
5,905
0bf755731d0b4965b581a5e0fbd5700f
UNKNOWN
You have a string S consisting of N uppercase English letters. You are allowed to perform at most one operation of following kind: Choose any position in the string, remove the character at that position and insert it back to any other place in the string. Find the lexicographically smallest string you can achieve. -----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 the single integer N denoting length of string S. The second line contains the string S. -----Output----- For each test case, output a single line containing the answer to the corresponding test case. -----Constraints----- - 1 ≤ T ≤ 50 - 1 ≤ N ≤ 50 - S will consist of uppercase English letters. -----Example----- Input: 2 4 DCBA 7 XYZZYZZ Output: ADCB XYYZZZZ -----Explanation----- Example case 1. The optimal solution here is to choose the last character and put it in the beginning of the string. So the answer will be ADCB Example case 2. The optimal solution here is to choose the 5-th character (1-based index) and put it between the 2-nd and the 3-rd characters. So the answer will be XYYZZZZ
["for _1 in range(int(input())):\n n=int(input())\n s=input().strip()\n answer=s\n for i in range(len(s)):\n c=s[i]\n string=s[:i]+s[i+1:]\n for j in range(len(string)+1):\n answer=min(answer, string[:j]+c+string[j:])\n print(answer)", "def modify(s,i,j):\n k = s[i]\n del(s[i])\n s.insert(j,k)\n return(str(s))\n \nfor _ in range(int(input())):\n n = int(input())\n s = list(input())\n\n ans = str(s)\n for i in range(n):\n for j in range(n):\n if i != j:\n ans = min(ans,modify(s,i,j))\n k = s[j]\n del(s[j])\n s.insert(i,k)\n \n for i in ans:\n if ord(i) >= 65 and ord(i) <=90:\n print(i,end=\"\")\n print()", "def Min_str():\n n = int(input())\n s = input()\n temp = s[:]\n for i in range(n):\n s1 = [ord(x) for x in s]\n t = s1[i]\n del s1[i]\n j = 0\n while(j<n-1):\n if(s1[j]>t):\n s1[j:j]=[t]\n break\n j+=1\n if(len(s1)!=n):\n s1+=[t]\n T=\"\".join([chr(x) for x in s1])\n if(T<temp):\n temp = T\n print(temp)\nt= int(input())\nfor i in range(t):\n Min_str()", "t=int(input())\nfor _ in range(t):\n n=int(input())\n s=list(input())\n ans=s\n for i in range(n):\n x=s.copy()\n del(x[i])\n for j in range(n):\n ans=min(ans,x[:j]+[s[i]]+x[j:])\n print(\"\".join(ans))\n \n \n \n", "for _ in range(int(input())):\n n=int(input())\n #s=input() \n mini=[\"z\"]*100\n s=list(input().strip())\n # n=len(s)\n ans=min(mini,s)\n t=s[:]\n for i in range(n):\n #remove s[i]\n curr=s[i]\n f=0\n ind=0 \n temp=s[i]\n for j in range(i):\n if s[j]>curr:\n s.pop(i)\n s.insert(j,temp)\n ans=min(ans,s)\n break \n s=t[:] \n # print(s)\n for i in range(n):\n #remove s[i]\n curr=s[i]\n f=0\n ind=n-1 \n temp=s[i]\n for j in range(i+1,n):\n if s[j]>curr:\n s.pop(i)\n s.insert(j-1,temp)\n ans=min(ans,s)\n break \n else:\n s.pop(i)\n s.insert(n-1,temp)\n ans=min(ans,s)\n s=t[:] \n #ans=min(pzbl)\n print(*ans,sep='')\n ", "t = int(input())\nfor T in range(t):\n n = int(input())\n s = input()\n ans = s\n val, val1 = '', ''\n for i in range(n):\n ##print(val, end = \" \")\n val = s[:i] + s[i + 1:]\n for j in range(n):\n val1 = val[:j] + s[i] + val[j:]\n ##print(val1)\n ans = min(ans, val1)\n print(ans)\n", "# cook your dish here\nt = int(input())\n\nfor _ in range(t):\n n = int(input())\n s = input()\n l = []\n for i in range(n):\n yo = s[:i] + s[i+1:]\n for j in range(n):\n k1 = yo[:j] + s[i] + yo[j:]\n l.append(k1)\n l.sort()\n print(l[0])\n", "t=int(input())\nfor _ in range(t):\n n=int(input())\n s=input()\n wow=s\n for i in range(n):\n now=s[:i]+s[i+1:]\n for j in range(n):\n wow=min(wow,now[:j]+s[i]+now[j:])\n \n print(wow)\n''' c=True\n ind=0\n while c and ind<n:\n m=min(s[ind:])\n if m==s[ind:][0]:\n ind+=1\n continue\n else:\n c=False\n if True:\n l=[j+ind for j,i in enumerate(s[ind:]) if i==m]\n #print([s[:ind]+s[aa]+s[ind:aa]+s[aa+1:] for aa in l])\n mm=min(l,key=lambda aa:s[:ind]+s[aa]+s[ind:aa]+s[aa+1:])\n s=s[:ind]+m+s[ind:mm]+s[mm+1:]\n print(s)'''\n \n \n \n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n s=input()\n ans=s\n for i in range(n):\n new_s=s[:i]+s[i+1:]\n for j in range(n):\n ans=min(ans,new_s[:j]+s[i]+new_s[j:])\n \n print(ans)", "for _ in range(int(input())):\n n=int(input())\n s=input()\n ans=s\n for i in range(n):\n new_s=s[:i]+s[i+1:]\n for j in range(n):\n ans=min(ans,new_s[:j]+s[i]+new_s[j:])\n \n print(ans)"]
{"inputs": [["2", "4", "DCBA", "7", "XYZZYZZ"]], "outputs": [["ADCB", "XYYZZZZ"]]}
INTERVIEW
PYTHON3
CODECHEF
3,486
784e19ec33177a7926dbdf578bb9533e
UNKNOWN
Let's define a periodic infinite sequence S$S$ (0$0$-indexed) with period K$K$ using the formula Si=(i%K)+1$S_i = (i \% K) + 1$. Chef has found a sequence of positive integers A$A$ with length N$N$ buried underground. He suspects that it is a contiguous subsequence of some periodic sequence. Unfortunately, some elements of A$A$ are unreadable. Can you tell Chef the longest possible period K$K$ of an infinite periodic sequence which contains A$A$ (after suitably filling in the unreadable elements) as a contiguous subsequence? -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \dots, A_N$. Unreadable elements are denoted by −1$-1$. -----Output----- For each test case, print a single line. - If the period can be arbitrarily large, this line should contain a single string "inf". - Otherwise, if A$A$ cannot be a contiguous subsequence of a periodic sequence, it should contain a single string "impossible". - Otherwise, it should contain a single integer — the maximum possible period. -----Constraints----- - 1≤T≤100$1 \le T \le 100$ - 2≤N≤105$2 \le N \le 10^5$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ - for each valid i$i$, 1≤Ai≤106$1 \le A_i \le 10^6$ or Ai=−1$A_i = -1$ -----Subtasks----- Subtask #1 (50 points): - 2≤N≤1,000$2 \le N \le 1,000$ - the sum of N$N$ over all test cases does not exceed 10,000$10,000$ Subtask #2 (50 points): original constraints -----Example Input----- 3 3 -1 -1 -1 5 1 -1 -1 4 1 4 4 6 7 -1 -----Example Output----- inf 4 impossible
["# cook your dish here\nfrom math import gcd\nfor _ in range(int(input())):\n n,a,k,min_k,e = int(input()),[int(i) for i in input().split()],0,0,-1 \n for j in range(n):\n if(a[j] != -1):break \n for i in range(j,n):\n if min_k==0:min_k,e = a[i],a[i]+1 \n else:\n if min_k < a[i]:min_k = a[i] \n if(a[i] == -1):pass\n else:\n if(a[i] == e):pass\n else:\n if( k == 0):k = e-a[i]\n else:\n new_k = e-a[i]\n if(new_k < 0):k = -1\n else:k = gcd(k,new_k)\n if(k<min_k or k<0): k = -1; break\n if k != 0 and a[i]!=-1: e = a[i]%k+1\n else:e += 1 \n if(k == -1):print(\"impossible\")\n elif k == 0 :print(\"inf\")\n else:print(k) ", "# cook your dish here\nfrom math import gcd\nfor _ in range(int(input())):\n n,a,k,min_k,e = int(input()),[int(i) for i in input().split()],0,0,-1 \n for j in range(n):\n if(a[j] != -1):break \n for i in range(j,n):\n if min_k==0:min_k,e = a[i],a[i]+1 \n else:\n if min_k < a[i]:min_k = a[i] \n if(a[i] == -1):pass\n else:\n if(a[i] == e):pass\n else:\n if( k == 0):k = e-a[i]\n else:\n new_k = e-a[i]\n if(new_k < 0):k = -1\n else:k = gcd(k,new_k)\n if(k<min_k or k<0): k = -1; break\n if k != 0 and a[i]!=-1: e = a[i]%k+1\n else:e += 1 \n if(k == -1):print(\"impossible\")\n elif k == 0 :print(\"inf\")\n else:print(k) ", "from math import gcd\nfor _ in range(int(input())):\n n,a,k,min_k,e = int(input()),[int(i) for i in input().split()],0,0,-1 \n for j in range(n):\n if(a[j] != -1):break \n for i in range(j,n):\n if min_k==0:min_k,e = a[i],a[i]+1 \n else:\n if min_k < a[i]:min_k = a[i] \n if(a[i] == -1):pass\n else:\n if(a[i] == e):pass\n else:\n if( k == 0):k = e-a[i]\n else:\n new_k = e-a[i]\n if(new_k < 0):k = -1\n else:k = gcd(k,new_k)\n if(k<min_k or k<0): k = -1; break\n if k != 0 and a[i]!=-1: e = a[i]%k+1\n else:e += 1 \n if(k == -1):print(\"impossible\")\n elif k == 0 :print(\"inf\")\n else:print(k) "]
{"inputs": [["3", "3", "-1 -1 -1", "5", "1 -1 -1 4 1", "4", "4 6 7 -1"]], "outputs": [["inf", "4", "impossible"]]}
INTERVIEW
PYTHON3
CODECHEF
2,311
db80229d59f88c801deea9b6f6cfbd46
UNKNOWN
You are participating in a contest which has $11$ problems (numbered $1$ through $11$). The first eight problems (i.e. problems $1, 2, \ldots, 8$) are scorable, while the last three problems ($9$, $10$ and $11$) are non-scorable ― this means that any submissions you make on any of these problems do not affect your total score. Your total score is the sum of your best scores for all scorable problems. That is, for each scorable problem, you look at the scores of all submissions you made on that problem and take the maximum of these scores (or $0$ if you didn't make any submissions on that problem); the total score is the sum of the maximum scores you took. You know the results of all submissions you made. Calculate your total score. -----Input----- - The first line of the input contains a single 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 integer $N$ denoting the number of submissions you made. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains two space-separated integers $p_i$ and $s_i$, denoting that your $i$-th submission was on problem $p_i$ and it received a score $s_i$. -----Output----- For each test case, print a single line containing one integer ― your total score. -----Constraints----- - $1 \le T \le 10$ - $1 \le N \le 1,000$ - $1 \le p_i \le 11$ for each valid $i$ - $0 \le s_i \le 100$ for each valid $i$ -----Subtasks----- Subtask #1 (15 points): all submissions are on the same problem, i.e. $p_1 = p_2 = \ldots = p_N$ Subtask #2 (15 points): there is at most one submission made on each problem, i.e. $p_i \neq p_j$ for each valid $i, j$ ($i \neq j$) Subtask #3 (70 points): original constraints -----Example Input----- 2 5 2 45 9 100 8 0 2 15 8 90 1 11 1 -----Example Output----- 135 0 -----Explanation----- Example case 1: The scorable problems with at least one submission are problems $2$ and $8$. For problem $2$, there are two submissions and the maximum score among them is $45$. For problem $8$, there are also two submissions and the maximum score is $90$. Hence, the total score is $45 + 90 = 135$. Example case 2: No scorable problem is attempted, so the total score is $0$.
["# cook your dish here\np=int(input())\nfor z in range(p):\n n=int(input())\n a=[]\n for i in range(8):\n a.append(0)\n for i in range(n):\n x,y=list(map(int,input().split()))\n if x<=8 and y>a[x-1]:\n a[x-1]=y\n print(sum(a))\n", "# cook your dish here\nn=int(input())\nfor i in range(n):\n m=int(input())\n l=[0]*8\n for i in range(m):\n a,b=map(int,input().split())\n if a>0 and a<=8:\n if l[a-1]<b:\n l[a-1]=b\n print(sum(l))", "for t in range(int(input())):\n n=int(input())\n l=[0]*8\n for i in range(n):\n p,s=list(map(int,input().split()))\n if p<=8 and l[p-1]<s:\n l[p-1]=s\n print(sum(l))\n\n \n \n \n \n \n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n s=[0]*8 \n for i in range(n):\n p,m=list(map(int,input().split()))\n if p<9 and s[p-1]<m:\n s[p-1]=m \n print(sum(s))\n \n \n \n \n \n \n \n \n", "n=int(input())\nwhile n>0:\n t=int(input())\n a=[]\n for i in range(8):\n a.append(0)\n for i in range(t):\n b,c=[int(x) for x in input().split()]\n if b<=8 and c>a[b-1]:\n a[b-1]=c\n print(sum(a))\n n=n-1\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n d={1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[]}\n l=[]\n su=0\n for i in range(n):\n p,s=list(map(int,input().split()))\n if p<=8:\n d[p]=d[p]+[s]\n for j in d:\n if len(d[j])!=0:\n su=su+max(d[j])\n print(su)\n \n \n", "import collections\nT=int(input())\nwhile(T>0):\n N=int(input())\n a=[]\n for i in range(8):\n a.append(0)\n for i in range(N):\n b,c=[int(i) for i in input().split()]\n if b<=8 and c>a[b-1]:\n a[b-1]=c\n print(sum(a))\n T-=1", "# cook your dish here\nn=int(input())\nwhile n>0:\n t=int(input())\n a=[]\n for i in range(8):\n a.append(0)\n for i in range(t):\n b,c=[int(x) for x in input().split()]\n if b<=8 and c>a[b-1]:\n a[b-1]=c\n print(sum(a))\n n=n-1", "# cook your dish here\nt=int(input())\nfor i in range(t):\n n=int(input())\n a=[0]\n b=[0]\n c=[0]\n d=[0]\n e=[0]\n f=[0]\n g=[0]\n h=[0]\n for j in range(n):\n q,s=list(map(int,input().split()))\n if q<9:\n if q==1:\n a.append(s)\n elif q==2:\n b.append(s)\n elif q==3:\n c.append(s)\n elif q==4:\n d.append(s)\n elif q==5:\n e.append(s)\n elif q==6:\n f.append(s)\n elif q==7:\n g.append(s)\n elif q==8:\n h.append(s)\n print(max(a)+max(b)+max(c)+max(d)+max(e)+max(f)+max(g)+max(h))\n\n", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n d = {}\n for i in range(n):\n m,n = map(int,input().split())\n if m>=1 and m<=8:\n if m not in d:\n d[m]=n\n elif d[m]<n:\n d[m]=n\n print(sum(d.values())) ", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n l=[]\n d=dict()\n for i in range(n):\n l=[int(i) for i in input().split()]\n if l[0] in [1,2,3,4,5,6,7,8]:\n if l[0] in list(d.keys()):\n if d[l[0]]<l[1]:\n d[l[0]]=l[1]\n else:\n d[l[0]]=l[1]\n s=0\n for i,j in list(d.items()):\n s=s+j\n print(s)\n", "for _ in range(int(input())):\n n = int(input())\n hsh = [0] * 9\n for _ in range(n):\n q, s = map(int, input().split())\n if q in range(1, 9):\n if hsh[q] < s: hsh[q] = s\n\n print(sum(hsh))", "for _ in range(int(input())):\n n = int(input())\n res = [0]*9\n for i in range(n):\n p, s = map(int, input().split())\n if 1 <= p <= 8:\n if res[p] < s:\n res[p] = s\n print(sum(res))", "# cook your dish here\nt = int(input())\nfor z in range(t) :\n n = int(input())\n d = {}\n for i in range(n):\n k,v = [int(x) for x in input().split()]\n if k<9 :\n if k not in d:\n d[k] = v\n else:\n if v > d[k] :\n d[k] = v\n if not d :\n print('0')\n else :\n x = d.values()\n x = list(x)\n print(sum(x))", "# cook your dish here\nfor _ in range(int(input())):\n d={}\n n=int(input())\n for i in range(n):\n k,v=map(int,input().split())\n if(k<=8):\n if(k not in d.keys()):\n d[k]=v \n else:\n if(d[k]<v):\n d[k]=v \n print(sum(d.values()))", "for _ in range(int(input())):\n Score=[0]*8\n for _ in range(int(input())):\n P,S=list(map(int,input().split()))\n if P>=1 and P<=8:\n if Score[P-1]<S:\n Score[P-1]=S\n print(sum(Score))\n", "T=int(input())\nfor i in range (T):\n n=int(input())\n l=[0]*11\n for i in range (n):\n X,Y=map(int,input().split())\n if X<9 and l[X-1]<Y:\n l[X-1]=Y\n print(sum(l)) ", "t=int(input())\nfor i in range (t):\n n = int(input())\n a = [0] * 8\n for i in range (n):\n x , y = list(map(int , input().split()))\n if x <= 8:\n if a[x-1] < y:\n a[x-1] = y\n sum = 0\n for j in a:\n sum += j\n print(sum)\n", "# cook your dish here\nrii = lambda : list(map(int, input().strip().split(\" \")))\nril = lambda : list(map(int, input().strip().split(\" \")))\nri = lambda : int(input().strip())\nrs = lambda : eval(input())\nT = ri()\nfor _ in range(T):\n N = ri()\n l = [0 for i in range(8)]\n for i in range(N):\n nb, S = rii()\n if nb < 9:\n l[nb-1] = max(S, l[nb-1])\n print(sum(l))\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n a=[0,0,0,0,0,0,0,0]\n for i in range(n):\n n,m=list(map(int,input().split()))\n if n==9 or n==10 or n==11:\n continue\n elif a[n-1]<m:\n a[n-1]=m \n print(sum(a))\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n arr=[]\n for i in range(11):\n arr.append(0)\n for i in range(n):\n p,s=list(map(int,input().split()))\n if p<=8:\n arr[p-1]=max(arr[p-1],s)\n print(sum(arr))\n"]
{"inputs": [["2", "5", "2 45", "9 100", "8 0", "2 15", "8 90", "1", "11 1"]], "outputs": [["135", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
5,435
249c86c4e684a627ea77d4b771173c92
UNKNOWN
There are n cabs in a city numbered from 1 to n. The city has a rule that only one cab can run in the city at a time. Cab picks up the customer and drops him to his destination. Then the cab gets ready to pick next customer. There are m customers in search of cab. First customer will get the taxi first. You have to find the nearest cab for each customer. If two cabs have same distance then the cab with lower number is preferred. Your task is to find out minimum distant cab for each customer. 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 two space-separated integers N and M, denoting the number of cabs and the number of customers. The next N lines contain two space-separated integers x[i] and y[i], denoting the initial position of the ith cab. Next line contains an integer M denoting number of customers. The next M lines contain four space seperated integers sx[i], sy[i], dx[i], dy[i], denoting the current location and the destination of the ith customer. Output: Output the nearest cab number for each customer. Constraints: 1<=t<=10 1<=n,m<=1000 -10^9<=x[i] , y[i] , sx[i] , sy[i] , dx[i] , dy[i]<=10^9 Example: Input: 1 3 2 1 3 3 2 3 5 2 3 3 4 5 3 4 1 Output: 1 1 Explanation: The distance of cab1 from customer1 = sqrt((1-2)^2 + (3-3)^2) = 1 The distance of cab2 from customer1 = sqrt(2) The distance of cab3 from customer1 = sqrt(5) So output for customer1 is 1 Now location of cab1 is (3,4) The distance of cab1 from customer2 = sqrt((3-5)^2 + (4-3)^2) = sqrt(5) The distance of cab2 from customer2 = sqrt(5) The distance of cab3 from customer2 = sqrt(8) So output for customer2 is 1
["import math\ndef dist(w,x,y,z):\n return math.hypot(y - w, z - x)\n\nt = int(input())\nwhile (t>0):\n t = t -1\n n, m = list(map(int,input().split()))\n a = []\n for i in range(0,n):\n x,y = list(map(int,input().split()))\n a.append([x,y])\n for j in range(0,m):\n p,q,r,s = list(map(int,input().split()))\n nearest = -1\n distance = 10000000000\n for i in range(0,n):\n way = dist(a[i][0],a[i][1],p,q)\n if way < distance:\n distance = way\n nearest = i\n print(nearest + 1)\n a[nearest][0] = r\n a[nearest][1] = s\n\n"]
{"inputs": [["1", "3 2", "1 3", "3 2", "3 5", "2 3 3 4", "5 3 4 1"]], "outputs": [["1", "1"]]}
INTERVIEW
PYTHON3
CODECHEF
542
cac4fe86af59e78be3b886d571a7b33b
UNKNOWN
A binary string is called a self-destructing string if it can reduced to an empty string by performing the following operation some number of times (possibly zero): Choose a valid integer $i$ such that the $i$-th character of the current string is different from the $i+1$-th character, and remove these two characters from the string. You are given a binary string $s$. Your task is to convert $s$ to a self-destructing string. To do that, you may perform the following operation any number of times (possibly zero): Choose an integer $i$ ($1 \le i \le |s|-1$) such that the $i$-th character of $s$ is different from the $i+1$-th character, and invert one of these characters (inverting a character means changing '0' to '1' or '1' to '0', e.g. the string "01" can be changed to "00"). Find the smallest number of operations required to convert $s$ to a self-destructing string or determine that it is impossible. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains a single string $s$. -----Output----- For each test case, print a single line containing one integer ― the smallest required number of operations or $-1$ if it is impossible to convert $s$ to a self-destructing string. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le |s| \le 10^6$ - $s$ contains only characters '0' and '1' - the sum of $|s|$ over all test cases does not exceed $10^6$ -----Example Input----- 3 001101 1101 110 -----Example Output----- 0 1 -1 -----Explanation----- Example case 1: We already have a self-destructing string because "001101" can be converted to "0101", to "01" and finally to an empty string. Example case 2: We can choose $i=3$ (since $s_3 \neq s_4$) and then invert $s_4$ to obtain "1100", which is a self-destructing string. Example case 3: It can be shown that "110" cannot be converted to a self-destructing string.
["t=int(input())\nfor i in range(t):\n s=input()\n zeroes=s.count('0')\n ones=s.count('1')\n if (len(s)%2==1 or zeroes==0 or ones==0):\n ans= -1\n else:\n ans=abs(zeroes-ones)//2\n print(ans) ", "a = int(input())\nfor i in range(a):\n b = list(map(int,str(input())))\n if len(b)%2!=0:\n print(-1)\n else:\n if sum(b)==len(b) or sum(b)==0:\n print(-1)\n else:\n print(abs((len(b)//2)-sum(b)))", "# cook your dish here\nfor u in range(int(input())):\n s = input()\n cnt0 = s.count('0')\n cnt1 = s.count('1')\n if len(s)%2 != 0 or cnt0==len(s) or cnt1==len(s):\n print(-1)\n else:\n d = abs(cnt1-cnt0)\n if d == 0:\n print(0)\n else:\n print(d//2) ", "# cook your dish here\nt=int(input())\nwhile t>0 :\n s=input()\n n=len(s) \n x=s.count('1')\n if n%2==1 or x==0 or x==n :\n print(-1)\n else :\n print(abs(n//2-x))\n t-=1", "for _ in range(int(input())):\n s = input()\n \n \n d = {'0':0, '1':0}\n for i in s:\n d[i] += 1 \n \n if(len(s)%2 or d['1']==0 or d['0'] == 0):\n print(-1)\n else:\n print(abs(d['1'] - d['0']) //2)\n \n \n", "t=int(input())\nfor i in range(1,t+1):\n st=input()\n a=st.count('0',0,len(st))\n b=st.count('1',0,len(st))\n if len(st)%2==1 or a==0 or b==0:\n ans=-1\n else:\n ans=abs(a-b)//2\n print(ans)\n", "for _ in range(int(input())):\n sg = input().strip()\n l = len(sg)\n if l % 2 != 0:\n print(-1)\n else:\n ones = 0; zeros = 0;\n for i in range(l):\n if sg[i] == '1':\n ones += 1\n else:\n zeros += 1\n diff = abs(ones - zeros)\n if diff == l:\n print(-1)\n else:\n diff //= 2\n print(diff)\n", "for t in range(int(input())):\n s=input()\n z=s.count(\"0\")\n o=s.count(\"1\")\n if len(s)%2==0:\n if z==0 or o==0:\n print(-1)\n else:\n print(len(s)//2-min(o,z))\n else:\n print(-1)", "for _ in range(int(input())):\n s = str(input())\n if len(s)&1==1:\n print(-1)\n else:\n z = 0\n o = 0\n for i in range(len(s)):\n if s[i]=='0':\n z+=1\n else:\n o+=1\n if z==o:\n print(0)\n elif (z or o)!=len(s):\n print(int(abs(z-o)/2))\n else:\n print(-1)", "# cook your dish here\nt=int(input())\nfor i in range(t):\n l=input()\n if len(l)%2==1:\n print(-1)\n else:\n z=l.count(\"1\")\n s=len(l)\n if s==z or z==0:\n print(-1)\n else:\n print(abs(s-2*z)//2)\n", "for _ in range(int(input())):\n s = input()\n l = len(s)\n if l%2==1:\n print(-1)\n else:\n c1 = s.count('1')\n c0 = l-c1\n if c0 == 0 or c1 == 0:\n print(-1)\n elif c1==c0:\n print(0)\n else:\n print(abs(c1-c0)//2)", "# cook your dish here\nfor _ in range(int(input())):\n s = input()\n lisS = list(s)\n \n if(len(set(lisS))==1 or len(s) % 2 != 0):\n print(-1)\n else:\n mid = len(s) // 2\n numZeros = 0\n for i in range(len(s)):\n if(s[i] == \"0\"):\n numZeros += 1\n \n print(abs(mid - numZeros))", "# 10 + 20 + 2:28\n\n\n\ndef solve(b,n):\n x=0\n y=0\n for i in range(n):\n if b[i]=='1':\n x+=1\n else:\n y+=1\n if x==n or y==n:\n return -1\n\n return abs(x-y)//2\n\n\nfor tests in range(int(input())):\n b=input()\n n=len(b)\n if n%2==1:\n print(-1)\n else:\n print(solve(b,n))", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n s=list(input())\n n=len(s)\n if(n%2==1):\n print(-1)\n else:\n i=0\n k1=s.count('1')\n k2=s.count('0')\n if(k1==k2):\n print(0)\n elif(k1==0 or k2==0):\n print(-1)\n else:\n kmin=min(k1,k2)\n required=n//2\n print(required-kmin)", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n s=list(input())\n n=len(s)\n if(n%2==1):\n print(-1)\n else:\n i=0\n k1=s.count('1')\n k2=s.count('0')\n if(k1==k2):\n print(0)\n elif(k1==0 or k2==0):\n print(-1)\n else:\n kmin=min(k1,k2)\n required=n//2\n print(required-kmin)", "for _ in range(int(input())):\n s = input()\n n =len(s)\n if len(s)== s.count(\"0\") or len(s)== s.count(\"1\"):\n print(-1)\n else:\n if n%2==0:\n x =abs(n-2*(s.count(\"0\")))\n print(x//2)\n else:\n print(-1)\n \n\n", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n s = input()\n x = s.count('0')\n y = s.count('1')\n if (len(s)%2 == 1 or x == 0 or y == 0):\n print(-1)\n else:\n print(abs(x-y)//2)", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n s = input();\n a = s.count('0')\n b = s.count('1');\n if(len(s)%2==1 or a==0 or b==0):\n ans = -1\n else:\n ans = abs(a-b)//2;\n print(ans)", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n s = input();\n a = s.count('0')\n b = s.count('1');\n if(len(s)%2==1 or a==0 or b==0):\n ans = -1\n else:\n ans = abs(a-b)//2;\n print(ans)", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n s = input();\n a = s.count('0')\n b = s.count('1');\n if(len(s)%2==1 or a==0 or b==0):\n ans = -1\n else:\n ans = abs(a-b)//2;\n print(ans)", "# cook your dish here\nfor _ in range(int(input())):\n n=list(input())\n if len(n)%2==1:\n print(-1)\n else:\n s1=n.count('0')\n s2=n.count('1')\n if s1>0 and s2>0:\n print(abs(s1-s2)//2)\n else:\n print(-1)", "# cook your dish here\nfor _ in range(int(input())):\n n=list(input())\n if len(n)%2==1:\n print(-1)\n else:\n s1=n.count('0')\n s2=n.count('1')\n if abs(s1-s2)%2==0 and s1>0 and s2>0:\n print(abs(s1-s2)//2)\n else:\n print(-1)", "# cook your dish here\ntry:\n t = int(input())\n for k in range(t):\n s = input()\n ans = 0\n if((len(s)%2!=0) or (not '0'in s) or (not '1' in s)):\n ans = -1\n else:\n zeros = 0\n ones = 0\n for i in s:\n if i=='1':\n ones += 1 \n else :\n zeros += 1 \n ans = abs(zeros - ones)//2\n print(ans)\nexcept:\n pass\n \n", "a = int(input())\nfor x in range(a):\n b = input()\n c = b.count(\"1\")\n d = b.count(\"0\")\n if c == len(b) or d == len(b):\n print(-1)\n elif len(b)&1 == 1:\n print(-1)\n else:\n print(int(len(b)/2)-min(c,d))", "# cook your dish here\nt=int(input())\nfor i in range(t):\n s=input()\n n=len(s)\n if(n%2==1):\n print(-1)\n else:\n o=s.count('1')\n z=s.count('0')\n if(o==n or z==n):\n print(-1)\n else:\n \n print(n//2-min(o,z))"]
{"inputs": [["3", "001101", "1101", "110"]], "outputs": [["0", "1", "-1"]]}
INTERVIEW
PYTHON3
CODECHEF
6,010
73af08261b86a1a1146247dca487aac5
UNKNOWN
Chef's company wants to make ATM PINs for its users, so that they could use the PINs for withdrawing their hard-earned money. One of these users is Reziba, who lives in an area where a lot of robberies take place when people try to withdraw their money. Chef plans to include a safety feature in the PINs: if someone inputs the reverse of their own PIN in an ATM machine, the Crime Investigation Department (CID) are immediately informed and stop the robbery. However, even though this was implemented by Chef, some people could still continue to get robbed. The reason is that CID is only informed if the reverse of a PIN is different from that PIN (so that there wouldn't be false reports of robberies). You know that a PIN consists of $N$ decimal digits. Find the probability that Reziba could get robbed. Specifically, it can be proven that this probability can be written as a fraction $P/Q$, where $P \ge 0$ and $Q > 0$ are coprime integers; you should compute $P$ and $Q$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains a single integer $N$ denoting the length of each PIN. -----Output----- For each test case, print a single line containing two space-separated integers — the numerator $P$ and denominator $Q$ of the probability. -----Constraints----- - $1 \le T \le 100$ - $1 \le N \le 10^5$ -----Subtasks----- Subtask #1 (10 points): $N \le 18$ Subtask #2 (20 points): $N \le 36$ Subtask #3 (70 points): original constraints -----Example Input----- 1 1 -----Example Output----- 1 1 -----Explanation----- Example case 1: A PIN containing only one number would fail to inform the CID, since when it's input in reverse, the ATM detects the same PIN as the correct one. Therefore, Reziba can always get robbed — the probability is $1 = 1/1$.
["for i in range(int(input())):\n n = int(input())\n q = \"1\"+\"0\"*(n//2)\n print(1,q)", "# cook your dish here\nfor i in range(int(input())):\n n = int(input())\n q = \"1\"+\"0\"*(n//2)\n print(1,q)", "# cook your dish here\nfor _ in range(int(input())):\n n = int(input())\n q = \"1\"+\"0\"*(n//2)\n print(1,q)\n \n", "for _ in range(int(input())):\n n = int(input())\n q = \"1\"+\"0\"*(n//2)\n print(1,q)\n \n", "# cook your dish he\ndef gcd(a,b):\n \n # Everything divides 0 \n if (b == 0):\n return a\n return gcd(b, a%b)\n\nt = int(input())\n\nfor _ in range(t):\n n = int(input())\n # p=9\n # #tot=9\n # q=0\n # flag=0\n # num=0\n # if n==1:\n # print(1 , end=\" \")\n # flag=1\n # print(1)\n # else:\n # for i in range(2,n+1):\n # if i%2==0:\n # pass\n # else:\n # p*=10\n \n # if flag==0:\n # #print(gcd(3,4))\n # q= 10**n - 10**(n-1)\n # print(str(p//gcd(p,q)) + \" \" + str(q//gcd(p,q)))\n \n q=\"1\"+\"0\"*(n//2)\n print(1, q)\n", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n q=\"1\"+\"0\"*(n//2)\n print(1,q)", "# cook your dish he\ndef gcd(a,b):\n \n # Everything divides 0 \n if (b == 0):\n return a\n return gcd(b, a%b)\n\nt = int(input())\n\ndef isPalindrome(n: int) -> bool: \n \n # Find reverse of n \n rev = 0\n i = n \n while i > 0: \n rev = rev * 10 + i % 10\n i //= 10\n \n # If n and rev are same, \n # then n is palindrome \n return (n == rev)\n\nfor _ in range(t):\n n = int(input())\n p=9\n tot=9\n q=0\n flag=0\n num=0\n if n==1:\n print(1 , end=\" \")\n flag=1\n print(1)\n else:\n for i in range(2,n+1):\n if i%2==0:\n pass\n else:\n p*=10\n \n if flag==0:\n #print(gcd(3,4))\n q= 10**n - 10**(n-1)\n print(str(p//gcd(p,q)) + \" \" + str(q//gcd(p,q)))", "# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n print(1,10**(n//2))", "# cook your dish here\nt=int(input())\nwhile(t>0):\n t-=1\n n=int(input())\n #n,m,k=map(int,input().split())\n #l=list(map(int,input().split()))\n q=10**(n-int((n+1)/2))\n p=1\n '''if(n==1):\n q=1'''\n print(p,q)\n", "for _ in range(int(input())):\n N = int(input())\n d = '1' + '0'*(N//2)\n print(1, d)", "# cook your dish here\n\nt=int(input())\nwhile(t>0):\n t-=1\n n=int(input())\n #n,m,k=map(int,input().split())\n #l=list(map(int,input().split()))\n q=10**(n-int((n+1)/2))\n p=1\n '''if(n==1):\n q=1'''\n print(p,q)\n", "# cook your dish he\nt=int(input())\nfor i in range(t):\n n=int(input())\n q=10**(n-int((n+1)/2))\n p=1\n if n==1:\n q=1\n print(p,q)\n", "# cook your dish here\nfrom sys import stdin, stdout\nfrom math import floor\nans = []\n\nfor _ in range(int(stdin.readline())):\n n = int(stdin.readline())\n ans.append(f'1 {\"1\" + \"0\" * floor(n / 2)}')\nstdout.write('\\n'.join(ans))\n", "# cook your dish here\nimport math\nfor _ in range(int(input())):\n n = int(input())\n r = n-math.ceil(n/2)\n print(1,10 ** r)", "# cook your dish here\nimport math\nfor _ in range(int(input())):\n n = int(input())\n a=n-math.ceil(n/2)\n print(1,10**a)", "# cook your dish here\n# cook your dish here\nimport math\ntt=int(input())\nfor i in range(tt):\n n=int(input())\n a=n-math.ceil(n/2)\n print(1,10**a)", "# cook your dish here\nimport math\nfor h in range(int(input())):\n n=int(input())\n a=math.ceil(n/2)\n a=n-a\n print(\"1 1\",end=\"\")\n for i in range(1,a+1):\n print(0,end=\"\")\n print()", "t=int(input())\nfor i in range(t):\n n=int(input())\n def compute_hcf(x, y):\n while(y):\n x, y = y, x % y\n return x\n if(n==1):\n print(1,end=\" \")\n print(1)\n elif(n>1):\n a=0\n b=10**n\n if(n%2==0):\n a+=10**(n//2)\n elif(n%2!=0):\n a+=10**((n//2)+1)\n hcf=0\n hcf=compute_hcf(a,b)\n a1=0\n b1=0\n a1=a//hcf\n b1=b//hcf\n print(a1,end=\" \")\n print(b1)\n \n \n", "for _ in range(int(input())):\n n=int(input())\n q=10**(n//2)\n print(1,q)", "# cook your dish here\nimport math\nfor i in range(int(input())):\n n=int(input())\n p=1\n q=10**(n//2)\n print(p,\" \",q)", "# cook your dish here\nimport math\nt=int(input())\nfor i in range(t):\n n=int(input())\n q=10**(n//2)\n print(1,q)\n \n", "t=int(input()) \nfor _ in range(t):\n n=int(input()) \n str1='1 '+'1'+(n//2)*'0' \n print(str1) \n", "# cook your dish here\nimport math\ntt=int(input())\nfor i in range(tt):\n n=int(input())\n a=n-math.ceil(n/2)\n print(1,10**a)", "# cook your dish here\nimport math\ntt=int(input())\nfor i in range(tt):\n n=int(input())\n b=math.ceil(n/2)\n nu=10**b\n de=10**n\n q=math.gcd(nu,de)\n print(nu//q,de//q)"]
{"inputs": [["1", "1"]], "outputs": [["1 1"]]}
INTERVIEW
PYTHON3
CODECHEF
4,572
f483f091a2a5e1ebf49e08636cbd499b
UNKNOWN
As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eats as much of it as it can (or wants), then stretches out to its full length to reach a new leaf with its front end, and finally "hops" to it by contracting its back end to that leaf. We have with us a very long, straight branch of a tree with leaves distributed uniformly along its length, and a set of caterpillars sitting on the first leaf. (Well, our leaves are big enough to accommodate upto $20$ caterpillars!). As time progresses our caterpillars eat and hop repeatedly, thereby damaging many leaves. Not all caterpillars are of the same length, so different caterpillars may eat different sets of leaves. We would like to find out the number of leaves that will be undamaged at the end of this eating spree. We assume that adjacent leaves are a unit distance apart and the length of the caterpillars is also given in the same unit. For example suppose our branch had $20$ leaves (placed $1$ unit apart) and $3$ caterpillars of length $3, 2$ and $5$ units respectively. Then, first caterpillar would first eat leaf $1$, then hop to leaf $4$ and eat it and then hop to leaf $7$ and eat it and so on. So the first caterpillar would end up eating the leaves at positions $1,4,7,10,13,16$ and $19$. The second caterpillar would eat the leaves at positions $1,3,5,7,9,11,13,15,17$ and $19$. The third caterpillar would eat the leaves at positions $1,6,11$ and $16$. Thus we would have undamaged leaves at positions $2,8,12,14,18$ and $20$. So the answer to this example is $6$. -----Input:----- The first line of the input contains two integers $N$ and $K$, where $N$ is the number of leaves and $K$ is the number of caterpillars. Lines $2,3,...,K+1$ describe the lengths of the $K$ caterpillars. Line $i+1$ ($1 \leq i \leq K$) contains a single integer representing the length of the $i^{th}$ caterpillar. -----Output:----- A line containing a single integer, which is the number of leaves left on the branch after all the caterpillars have finished their eating spree. -----Constraints:----- - $1 \leq N \leq 1000000000$. - $1 \leq K \leq 20$. - The length of the caterpillars lie between $1$ and $N$. - $50 \%$ of test cases will also satisfy $1 \leq N \leq 10000000$ and $1 \leq K \leq 16$. -----Sample Input:----- 20 3 3 2 5 -----Sample Output:----- 6 -----Hint:----- You may use $64$-bit integers (long long in C/C++) to avoid errors while multiplying large integers. The maximum value you can store in a $32$-bit integer is $2^{31}-1$, which is approximately $2 \cdot 10^9$. $64$-bit integers can store values greater than $10^{18}$.
["from math import gcd\r\nn, k = list(map(int, input().split()))\r\na = []\r\nfor i in range(k):\r\n try:\r\n a += list(map(int, input().split()))\r\n except:\r\n pass\r\nans = n\r\nfor i in range(1, 2**k):\r\n b = bin(i)[2:].rjust(k, \"0\")\r\n c = []\r\n for j in range(k):\r\n if(b[j] == '1'):\r\n c.append(a[j])\r\n lcm = c[0]\r\n for j in c[1:]:\r\n lcm *= j // gcd(lcm, j)\r\n temp = ((n - 1) // lcm) + 1\r\n if(b.count('1')&1):\r\n ans -= temp\r\n else:\r\n ans += temp\r\nprint(ans)\r\n\r\n"]
{"inputs": [["20 3", "3", "2", "5"]], "outputs": [["6", "Hint:", "You may use 64 -bit integers ( long long in C/C++) to avoid errors while multiplying large integers. The maximum value you can store in a 32 -bit integer is 2 31 \u2212 1 , which is approximately 2 \u22c5 10 9 . 64 -bit integers can store values greater than 10 18 ."]]}
INTERVIEW
PYTHON3
CODECHEF
583
3ce51b46bd15e9bcb8662fbf7ecbef49
UNKNOWN
Young Sheldon is given the task to teach Chemistry to his brother Georgie. After teaching him how to find total atomic weight, Sheldon gives him some formulas which consist of $x$, $y$ and $z$ atoms as an assignment. You already know that Georgie doesn't like Chemistry, so he want you to help him solve this assignment. Let the chemical formula be given by the string $S$. It consists of any combination of x, y and z with some value associated with it as well as parenthesis to encapsulate any combination. Moreover, the atomic weight of x, y and z are 2, 4 and 10 respectively. You are supposed to find the total atomic weight of the element represented by the given formula. For example, for the formula $(x_2y_2)_3z$, given string $S$ will be: $(x2y2)3z$. Hence, substituting values of x, y and z, total atomic weight will be $(2*2+4*2)*3 + 10 = 46$. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input $S$. -----Output:----- For each testcase, output in a single line, the total atomic weight. -----Constraints----- - $1 \leq T \leq 100$ - Length of string $S \leq 100$ - String contains $x, y, z, 1, 2,..., 9$ and parenthesis -----Sample Input:----- 2 (xy)2 x(x2y)3(z)2 -----Sample Output:----- 12 46
["for _ in range(int(input())):\n s = list(input().strip())\n\n i = 0\n\n while i < len(s) - 1:\n if s[i].isalpha() or s[i] == ')':\n if s[i + 1].isdigit():\n if i + 2 >= len(s) or s[i + 2] == ')':\n s = s[:i+1] + ['*', s[i+1]] + s[i+2:]\n else:\n s = s[:i+1] + ['*', s[i+1], '+'] + s[i+2:]\n i += 1\n elif s[i + 1].isalpha() or s[i + 1] == '(':\n s = s[:i+1] + ['+'] + s[i+1:]\n\n i += 1\n\n s = ''.join(s)\n\n s = s.strip('+')\n\n x = 2\n y = 4\n z = 10\n\n print(eval(s))\n"]
{"inputs": [["2", "(xy)2", "x(x2y)3(z)2"]], "outputs": [["12", "46"]]}
INTERVIEW
PYTHON3
CODECHEF
504
a72aa6ce50902748aa8e2022fd7dcdfd
UNKNOWN
Chef has a pepperoni pizza in the shape of a $N \times N$ grid; both its rows and columns are numbered $1$ through $N$. Some cells of this grid have pepperoni on them, while some do not. Chef wants to cut the pizza vertically in half and give the two halves to two of his friends. Formally, one friend should get everything in the columns $1$ through $N/2$ and the other friend should get everything in the columns $N/2+1$ through $N$. Before doing that, if Chef wants to, he may choose one row of the grid and reverse it, i.e. swap the contents of the cells in the $i$-th and $N+1-i$-th column in this row for each $i$ ($1 \le i \le N/2$). After the pizza is cut, let's denote the number of cells containing pepperonis in one half by $p_1$ and their number in the other half by $p_2$. Chef wants to minimise their absolute difference. What is the minimum value of $|p_1-p_2|$? -----Input----- - The first line of the input contains a single 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 integer $N$. - $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains a string with length $N$ describing the $i$-th row of the grid; this string contains only characters '1' (denoting a cell with pepperonis) and '0' (denoting a cell without pepperonis). -----Output----- For each test case, print a single line containing one integer — the minimum absolute difference between the number of cells with pepperonis in the half-pizzas given to Chef's friends. -----Constraints----- - $1 \le T \le 1,000$ - $2 \le N \le 1,000$ - $N$ is even - the sum of $N \cdot N$ over all test cases does not exceed $2 \cdot 10^6$ -----Example Input----- 2 6 100000 100000 100000 100000 010010 001100 4 0011 1100 1110 0001 -----Example Output----- 2 0 -----Explanation----- Example case 1: Initially, $|p_1-p_2| = 4$, but if Chef reverses any one of the first four rows from "100000" to "000001", $|p_1-p_2|$ becomes $2$. Example case 2: Initially, $|p_1-p_2| = 0$. We cannot make that smaller by reversing any row.
["# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n l1=[]\n l2=[]\n \n for i in range(n):\n s=input()\n a=s[ :n//2].count('1')\n b=s[n//2: ].count('1')\n if a>b:\n l1.append(a-b)\n \n elif a<b:\n l2.append(b-a)\n \n p=sum(l1)\n q=sum(l2)\n \n if p==q:\n print(0)\n \n elif p>q:\n diff=p-q\n flag=0\n for i in range(diff//2, 0, -1):\n a=diff-i\n if (i in l1) or (a in l1):\n print(abs(a-i))\n flag=1\n break\n \n if flag==0:\n print(diff)\n \n else:\n diff=q-p\n flag=0\n for i in range(diff//2, 0, -1):\n a=diff-i\n if (i in l2) or (a in l2):\n print(abs(a-i))\n flag=1\n break\n \n if flag==0:\n print(diff)", "t=int(input())\nfor i in range(t):\n n=int(input())\n a=[]\n c1=0\n c2=0\n for i in range(n):\n s=input()\n a.append([s[0:n//2].count(\"1\"),s[n//2:].count(\"1\")])\n c1+=a[i][0]\n c2+=a[i][1]\n tp1=c1\n tp2=c2\n m=abs(c1-c2)\n for i in range(n):\n tp1+=a[i][1]\n tp1-=a[i][0]\n tp2+=a[i][0]\n tp2-=a[i][1]\n if(m>abs(tp1-tp2)):\n m=abs(tp1-tp2)\n tp1=c1\n tp2=c2\n print(m)\n \n \n \n \n \n \n \n \n \n \n", "def row(alist):\n a,n=0,len(alist)\n for i in range(n//2):\n if alist[i]=='1':\n a+=1\n for i in range(n//2,n):\n if alist[i]=='1':\n a-=1\n return(a)\nt=int(input())\nfor count in range(t):\n n=int(input())\n mat=[input() for i in range(n)]\n A=[]\n for i in range(n):\n A.append(row(mat[i]))\n s=sum(A)\n B=[abs(s)]\n if s>0:\n for i in range(n):\n if A[i]>0:\n B.append(abs(s-2*A[i]))\n else:\n for i in range(n):\n if A[i]<0:\n B.append(abs(s-2*A[i]))\n print(min(B))", "for _ in range(int(input())):\n n=int(input())\n aux=[0]*n\n tot=0\n for i in range(n):\n s=list(map(int,list(input())))\n aux[i]=sum(s[:n//2])-sum(s[n//2:])\n tot=sum(aux)\n if tot==0:\n print(0)\n else:\n x=[]\n for i in range(len(aux)):\n x.append(abs(tot-(2*aux[i])))\n print(min(x))", "for t in range(int(input())):\n n=int(input())\n p1=0\n p2=0\n ans=500\n diff=[]\n for i in range(n):\n a=input()\n x=0\n y=0\n for j in range(n//2):\n if a[j]=='1':\n p1+=1\n x+=1\n for j in range(n//2,n):\n if a[j]=='1':\n p2+=1\n y+=1\n diff.append(2*(y-x))\n for k in range(n):\n ans=min(ans,abs(p1-p2+diff[k]))\n print(min(ans,abs(p1-p2)))\n \n", "for t in range(int(input())):\n n=int(input())\n matrix=[]\n ans=500\n p1=0\n p2=0\n diff=[]\n for i in range(n):\n num=[]\n a=input()\n x=0\n y=0\n for j in range(n//2):\n num.append(int(a[j]))\n if a[j]=='1':\n p1+=1\n x+=1\n for j in range(n//2,n):\n num.append(int(a[j]))\n if a[j]=='1':\n p2+=1\n y+=1\n diff.append(2*(y-x))\n matrix.append(num)\n for k in range(n):\n ans=min(ans,abs(p1-p2+diff[k]))\n print(min(ans,abs(p1-p2)))\n \n", "# cook your dish here\ntest=int(input())\nfor i in range(test):\n n=int(input())\n a=[]\n p1=p2=0\n for j in range(n):\n s=input()\n a.append([s[:n//2].count('1'),s[n//2:].count('1')])\n p1+=a[j][0]\n p2+=a[j][1]\n m=abs(p1-p2)\n tp2=p2\n tp1=p1\n for j in range(n):\n tp1-=a[j][0]\n tp1+=a[j][1]\n tp2-=a[j][1]\n tp2+=a[j][0]\n if m>abs(tp1-tp2):\n m=abs(tp1-tp2)\n tp1=p1\n tp2=p2\n print(m)\n", "for i in range(int(input())):\n n=int(input())\n a=[]\n p1=p2=0\n for j in range(n):\n s=input()\n a.append([s[:n//2].count('1'),s[n//2:].count('1')])\n p1+=a[j][0]\n p2+=a[j][1]\n m=abs(p1-p2)\n tp2=p2\n tp1=p1\n for j in range(n):\n tp1-=a[j][0]\n tp1+=a[j][1]\n tp2-=a[j][1]\n tp2+=a[j][0]\n if m>abs(tp1-tp2):\n m=abs(tp1-tp2)\n tp1=p1\n tp2=p2\n print(m)\n", "for i in range(int(input())):\n n=int(input())\n a=[]\n p1=p2=0\n for j in range(n):\n s=input()\n a.append([s[:n//2].count('1'),s[n//2:].count('1')])\n p1+=a[j][0]\n p2+=a[j][1]\n m=abs(p1-p2)\n tp2=p2\n tp1=p1\n for j in range(n):\n tp1-=a[j][0]\n tp1+=a[j][1]\n tp2-=a[j][1]\n tp2+=a[j][0]\n if m>abs(tp1-tp2):\n m=abs(tp1-tp2)\n tp1=p1\n tp2=p2\n print(m)\n", "t=int(input())\nfor _ in range(t):\n n=int(input())\n lr=[0]*n\n for i in range(n):\n s=input()\n lr[i]=s[:n//2].count(\"1\")-s[n//2:].count(\"1\")\n lrsum=sum(lr)\n\n if(lrsum==0):\n print(\"0\")\n\n else:\n x=[abs(lrsum-2*lr[i]) for i in range(n)]\n print(min(x))\n\n\n", "t=int(input())\nfor _ in range(t):\n n=int(input())\n l=[0]*n\n r=[0]*n\n for i in range(n):\n s=input()\n l[i]=s[:n//2].count(\"1\")\n r[i]=s[n//2:].count(\"1\")\n if(l[i]>r[i]):\n l[i]=l[i]-r[i]\n r[i]=0\n elif(l[i]==r[i]):\n l[i]=0\n r[i]=0\n else:\n r[i]=r[i]-l[i]\n l[i]=0\n lsum=sum(l)\n rsum=sum(r)\n x=abs(lsum-rsum)\n if(x==0):\n print(\"0\")\n\n else:\n m=1000000000000000\n if(lsum>rsum):\n for j in range(n):\n k=abs(x-2*l[j])\n if(k<m):\n m=k\n if(m==0):\n break\n elif (rsum > lsum):\n for j in range(n):\n k = abs(x - 2 * r[j])\n if (k < m):\n m = k\n if (m == 0):\n break\n print(m)\n\n\n", "# cook your dish here\n\nfor _ in range(int(input())):\n n=int(input())\n l=[]\n for i in range(n):\n a=input()\n l.append(a)\n x=0\n y=0\n p=0\n q=0\n li=[]\n for i in range(n):\n p=0\n q=0\n for j in range(n):\n if j<(n/2) and l[i][j]==\"1\":\n x+=1\n p+=1\n elif j>=(n/2) and l[i][j]==\"1\":\n y+=1\n q+=1\n a=[]\n a=(p,q)\n li.append(a)\n mi=abs(x-y)\n if x-y==0:\n print(x-y)\n else:\n mi=abs(x-y)\n d=x-y\n for i in range(n):\n c=x-li[i][0]+li[i][1]\n b=y+li[i][0]-li[i][1]\n z=abs(c-b)\n if mi>abs(z):\n mi=abs(z)\n print(mi)\n", "t = int(input())\nfor i in range(t):\n diff = []\n n = int(input())\n for j in range(n):\n c1 = 0\n c2 = 0\n l1 = list(map(int,input()))\n for k in range(n):\n if l1[k] == 1 and k < n//2:\n c1 += 1\n elif l1[k] and k >= n//2:\n c2 += 1\n diff.append(c1-c2)\n # print(diff)\n current_diff = sum(diff)\n # print(current_diff)\n new = []\n for kk in range(len(diff)):\n new.append(abs(current_diff - 2*diff[kk]))\n print(min(min(new),abs(current_diff)))\n\n", "t=int(input())\nfor you in range(t):\n n=int(input())\n lo=[]\n for i in range(n):\n l=input()\n li=[int(i) for i in l]\n lo.append(li)\n lrowhalf1=[0 for i in range(n)]\n lrowhalf2=[0 for i in range(n)]\n \n for i in range(n):\n lrowhalf1[i]=sum(lo[i][0:n//2])\n lrowhalf2[i]=sum(lo[i][n//2:n])\n \n mina=abs(sum(lrowhalf1)-sum(lrowhalf2))\n for i in range(n):\n mini=abs(sum(lrowhalf1)-2*(lrowhalf1[i]-lrowhalf2[i])-sum(lrowhalf2))\n if(mini<mina):\n mina=mini\n print(mina)", "for _ in range(int(input())):\n n=int(input())\n left=0\n right=0\n Dict={}\n for i in range(n):\n s=input()\n curr_left=s[0:n//2].count(\"1\")\n curr_right=s[n//2:n].count(\"1\")\n left+=curr_left\n right+=curr_right\n Dict[i]=curr_left-curr_right\n ans=abs(left-right)\n org_ans=left-right\n\n for i in range(n):\n ans=min(ans,abs(org_ans-2*Dict[i]))\n print(ans)", "# cook your dish here\nT = int(input())\nfor _ in range(T):\n N = int(input())\n mini = 10000000\n count1,count2=0,0\n u = []\n save = []\n for i in range(N):\n a = input()\n u.append(a)\n x = a[:N//2].count('1')\n y = a[N//2:].count('1')\n count1+=x\n count2+=y\n save.append(x-y)\n if(count1==count2):\n print(0)\n continue\n for i in save:\n mini = min(abs(count1-2*i-count2),mini)\n print(mini)\n \n", "\nfor _ in range(int(input())):\n N = int(input())\n array = []\n for i in range(N):\n a = input()\n first = 0\n second = 0\n for j in range(N//2):\n first += int(a[j])\n for j in range(N//2,N):\n second += int(a[j])\n array.append([first,second])\n sm1 = 0\n sm2 = 0\n for i in array:\n sm1 += i[0]\n sm2 += i[1]\n diff = abs(sm1 - sm2)\n for i in array:\n diff = min(diff, abs(sm1 - sm2 - i[0] * 2 + i[1] * 2))\n print(diff)", "# cook your dish here\nfor __ in range(int(input())):\n n=int(input());a=[];ans=10**9;\n for _ in range(n):\n a.append(list(input()))\n l1,l2,acnt,bcnt,diff=[],[],0,0,[];\n for i in range(n):\n acnt,bcnt=0,0;\n for j in range(len(a[i])//2):\n if(a[i][j]=='1'):acnt+=1\n if(a[i][n-1-j]=='1'):bcnt+=1\n l1.append(acnt)\n l2.append(bcnt)\n acnt,bcnt=sum(l1),sum(l2);\n if(acnt==bcnt):print(\"0\")\n elif(abs(bcnt-acnt)==1):print(\"1\")\n else:\n if(bcnt>acnt):\n l1,l2=l2,l1\n acnt,bcnt=bcnt,acnt\n for i in range(n):\n val=l1[i]-l2[i]\n diff.append(val if(val>0) else 0)\n for i in range(len(diff)):\n ans=min(ans,abs(acnt-diff[i]-bcnt-diff[i]))\n print(ans)\n", "t = int(input())\n\nfor _ in range(t):\n n = int(input())\n\n row_count = {}\n\n for i in range(n):\n s = input()\n row_count[i+1] = [s[:(n//2)].count('1'), s[(n//2):].count('1')]\n\n left_sum, right_sum = 0, 0\n for val in list(row_count.values()):\n left_sum += val[0]\n right_sum += val[1]\n\n min_abs_diff = abs(left_sum - right_sum)\n\n for row, val in list(row_count.items()):\n diff_swap_row = abs(\n (left_sum - val[0] + val[1]) - (right_sum - val[1] + val[0]))\n if diff_swap_row < min_abs_diff:\n min_abs_diff = diff_swap_row\n\n print(min_abs_diff)\n", "for T in range(int(input())):\n N=int(input())\n P=[0]*N\n Q=[0]*N\n R=[0]*N\n X=N//2\n for I in range(N):\n P[I]=input()\n for I in range(N):\n for J in range(X):\n if P[I][J]=='1':\n Q[I]+=1\n if P[I][J+X]=='1':\n R[I]+=1\n S=sum(Q)-sum(R)\n Z={abs(S)}\n for I in range(N):\n if Q[I]!=R[I]:\n Z.add(abs((S+2*(R[I]-Q[I]))))\n print(min(Z))"]
{"inputs": [["2", "6", "100000", "100000", "100000", "100000", "010010", "001100", "4", "0011", "1100", "1110", "0001"]], "outputs": [["2", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
9,280
0323456a1d9bafb3a011dc7f12a07093
UNKNOWN
You are provided with the marks of entire class in Data structures exam out of 100. You need to calculate the number of students having backlog (passing marks is >=31) and the average of the class. But this average is not a normal average, for this average marks of students having backlog are not considered but they will be considered in number of students. Also print the index of topper’s marks and print the difference of everyone’s marks with respect to the topper. In first line print the number of students having backlog and average of the class. In second line print indices of all the toppers(in case of more than 1 topper print index of all toppers in reverse order). Next N lines print the difference of everyone’s marks with respect to topper(s). Note- if all students have backlog than average will be 0. INPUT The first line of the input contains an integer T denoting the number of test cases. Next line contains N denoting the no of students in the class. The line contains N space seprated integers A1,A2,A3….AN denoting the marks of each student in exam. OUTPUT First line contains the number of students having backlog and the special average of marks as described above. Average must have 2 decimal places. Next line contains the indices of all the toppers in given array in reverse order. Next N lines contain the difference of every student’s marks with respect to toppers. Constraints 1<= T <= 100 1<= N <= 30 0<= Ai<= 100 Example Input 1 9 56 42 94 25 74 99 27 52 83 Output 2 55.55 5 43 57 5 74 25 0 72 47 16
["for j in range(int(input())):\n input()\n a = list(map(int,input().split()))\n marks = 0\n backlok = 0\n top_marks = max(a)\n topper = []\n for i in range(len(a)):\n if(a[i] >= 31):\n marks+=a[i]\n if(a[i]<31):\n backlok+=1\n if(a[i] == top_marks):\n topper.append(i)\n print(backlok, \"{:0.2f}\".format(marks/len(a),2))\n topper.sort(reverse=True)\n for i in topper:\n print(i,\" \")\n for i in a:\n print(top_marks-i)\n\n", "t=int(input())\nfor i in range(t):\n n=int(input())\n l=list(map(int,input().strip().split()))\n bl=0\n jod=0\n big=0\n for j in l:\n if j<31:\n bl+=1\n else:\n jod+=j\n if j>big:\n big=j\n avg=jod/n\n dif=[0 for i in range(n)]\n top=[]\n for j in range(n):\n dif[j]=big-l[j]\n if l[j]==big:\n top.append(j)\n print(bl,end=' ')\n print('%.2f'%avg)\n for j in range(len(top)-1,-1,-1):\n print(top[j],end=' ')\n print()\n for j in range(n):\n print(dif[j])\n \n"]
{"inputs": [["1", "9", "56 42 94 25 74 99 27 52 83"]], "outputs": [["2 55.56", "5", "43", "57", "5", "74", "25", "0", "72", "47", "16"]]}
INTERVIEW
PYTHON3
CODECHEF
916
86a4084ff8565d4e07db5c12623a243a
UNKNOWN
Chef has $N$ boxes arranged in a line.Each box has some candies in it,say $C[i]$. Chef wants to distribute all the candies between of his friends: $A$ and $B$, in the following way: $A$ starts eating candies kept in the leftmost box(box at $1$st place) and $B$ starts eating candies kept in the rightmost box(box at $N$th place). $A$ eats candies $X$ times faster than $B$ i.e. $A$ eats $X$ candies when $B$ eats only $1$. Candies in each box can be eaten by only the person who reaches that box first. If both reach a box at the same time, then the one who has eaten from more number of BOXES till now will eat the candies in that box. If both have eaten from the same number of boxes till now ,then $A$ will eat the candies in that box. Find the number of boxes finished by both $A$ and $B$. NOTE:- We assume that it does not take any time to switch from one box to another. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains three lines of input. The first line of each test case contains $N$, the number of boxes. - The second line of each test case contains a sequence,$C_1$ ,$C_2$ ,$C_3$ . . . $C_N$ where $C_i$ denotes the number of candies in the i-th box. - The third line of each test case contains an integer $X$ . -----Output:----- For each testcase, print two space separated integers in a new line - the number of boxes eaten by $A$ and $B$ respectively. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq N \leq 200000$ - $1 \leq C_i \leq 1000000$ - $1 \leq X \leq 10$ - Sum of $N$ over all test cases does not exceed $300000$ -----Subtasks----- Subtask 1(24 points): - $1 \leq T \leq 10$ - $1 \leq N \leq 1000$ - $1 \leq C_i \leq 1000$ - $1 \leq X \leq 10$ Subtask 2(51 points): original constraints -----Sample Input:----- 1 5 2 8 8 2 9 2 -----Sample Output:----- 4 1 -----EXPLANATION:----- $A$ will start eating candies in the 1st box(leftmost box having 2 candies) . $B$ will start eating candies in the 5th box(rightmost box having 9 candies) .As $A's$ speed is 2 times as that of $B's$, $A$ will start eating candies in the 2nd box while $B$ would still be eating candies in the 5th box.$A$ will now start eating candies from the 3rd box while $B$ would still be eating candies in the 5th box.Now both $A$ and $B$ will finish candies in their respective boxes at the same time($A$ from the 3rd box and $B$ from 5th box). Since $A$ has eaten more number of boxes($A$ has eaten 3 boxes while $B$ has eaten 1 box) till now,so $A$ will be eating candies in the 4th box. Hence $A$ has eaten 4 boxes and $B$ has eaten 1 box.
["import sys\n\n# stdin = open(\"testdata.txt\", \"r\")\nip = sys.stdin \n\ndef solve(C, n, x):\n if n==1:\n return (1, 0)\n\n b1, b2 = 1, 1\n a , b = C[0], C[-1]\n while b1 + b2 < n:\n if a < b*x:\n a += C[b1]\n b1 += 1\n elif a > b*x:\n b2 += 1\n b += C[n-b2] \n else:\n if b1 >= b2:\n a += C[b1]\n b1 += 1\n else:\n b2 += 1\n b += C[b2]\n return (b1, b2)\n\nt = int(ip.readline())\n\nfor _ in range(t):\n n = int(ip.readline())\n C = list(map(int, ip.readline().split()))\n x = int(ip.readline())\n\n ans = solve(C, n, x)\n print(*ans)", "for _ in range(int(input())):\n n = int(input())\n arr = list(map(int,input().split()))\n x = int(input())\n abox = 0\n bbox = 0\n i = 0\n j = n-1\n while n>0:\n if j!=i:\n v = x*arr[j]\n n-=1\n summ = 0\n for f in range(i,j):\n if summ+arr[f]<=v:\n summ+=arr[f]\n abox+=1\n n-=1\n else:\n arr[f] = summ+arr[f] - v\n i = f\n break\n bbox+=1\n j-=1\n elif j==i:\n if abox>=bbox:\n abox+=1\n n-=1\n else:\n bbox+=1\n n-=1\n\n print(abox,bbox)", "for _ in range(int(input())):\n n = int(input())\n arr = list(map(int,input().split()))\n x = int(input())\n abox = 0\n bbox = 0\n i = 0\n j = n-1\n while n>0:\n if j!=i:\n v = x*arr[j]\n n-=1\n summ = 0\n for f in range(i,j):\n if summ+arr[f]<=v:\n summ+=arr[f]\n abox+=1\n n-=1\n else:\n arr[f] = summ+arr[f] - v\n i = f\n break\n bbox+=1\n j-=1\n elif j==i:\n if abox>=bbox:\n abox+=1\n n-=1\n else:\n bbox+=1\n n-=1\n\n print(abox,bbox)", "try:\n for i in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n x=int(input())\n i=0\n j=n-1\n bi=0\n bj=1\n count1=l[j]*x\n while(i<j):\n if(count1>0 and i!=j):\n #print(count1,i)\n if(l[i]<=count1):\n count1=count1-l[i]\n i=i+1\n bi=bi+1\n else:\n l[i]=l[i]-count1\n count1=0\n if(count1<=0):\n #print(count1,i)\n j=j-1\n count1=count1+l[j]*x\n #print(count1,i,j)\n if(i<j):\n bj=bj+1\n if(i==j):\n if(bi>bj):\n bi=bi+1\n elif(bi<bj):\n bj=bj+1\n else:\n bi=bi+1\n print(bi,bj)\nexcept:\n pass\n", "try:\n for i in range(int(input())):\n n=int(input())\n l=list(map(int,input().split()))\n x=int(input())\n i=0\n j=n-1\n bi=0\n bj=1\n count1=l[j]*x\n while(i<j):\n if(count1>0 and i!=j):\n #print(count1,i)\n if(l[i]<=count1):\n count1=count1-l[i]\n i=i+1\n bi=bi+1\n else:\n l[i]=l[i]-count1\n count1=0\n if(count1<=0):\n #print(count1,i)\n j=j-1\n count1=count1+l[j]*x\n #print(count1,i,j)\n if(i!=j):\n bj=bj+1\n else:\n if(bi>bj):\n bi=bi+1\n elif(bi<bj):\n bj=bj+1\n else:\n bi=bi+1\n print(bi,bj)\nexcept:\n pass\n", "t=int(input())\nfor _ in range(t):\n n=int(input())\n l=list(map(int,input().split()))\n x=int(input())\n j=n-1\n i=0\n a=0\n b=0\n remain = 0\n while True:\n if i>j:\n break\n if i==j:\n if remain:\n a+=1\n else:\n if b>a:\n b+=1\n else:\n a+=1\n break\n \n b+=1\n d = x*l[j]+remain\n while i<j:\n d-=l[i]\n if d==0:\n remain = 0\n a+=1\n i+=1\n break\n if d<0:\n remain = d+l[i]\n break\n a+=1\n i+=1\n j-=1\n print(a,b)\n \n \n \n \n \n \n \n \n", "try:\n for _ in range(int(input())):\n n = int(input())\n arr = list(map(int,input().split()))\n x = int(input())\n\n left = 0\n right = n-1\n count1 = count2 = 0\n sum1=sum2=0\n \n while left<=right:\n if sum1<x*sum2:\n sum1 =sum1+arr[left]\n left +=1\n count1 +=1\n \n elif sum1>x*sum2:\n sum2 =sum2+ arr[right]\n right -=1\n count2 +=1\n \n else:\n if left!=right:\n sum1 =sum1+arr[left]\n left +=1\n count1 +=1\n \n elif count1<count2:\n sum2 =sum2+ arr[right]\n right -=1\n count2 =count2+1\n \n else:\n sum1 =sum1+arr[left]\n left +=1\n count1 +=1\n \n print(count1,count2)\nexcept:\n pass", "t=int(input())\nfor _ in range(t):\n n=int(input())\n a=[int(y) for y in input().split()]\n x=int(input())\n ca=[0]*n\n cb=[0]*n\n psum,psumr,aeats,beats=0,0,0,0\n tie=False\n for i in range(n):\n psum=psum+a[i]\n psumr=psumr+a[n-i-1]\n if i<n-1:\n ca[i+1]=psum/x\n cb[n-i-2]=psumr\n \n for i in range(n):\n if cb[i]>ca[i]:\n aeats+=1\n elif cb[i]<ca[i]:\n beats+=1\n else:\n tie=True\n if (tie):\n if beats>aeats:\n beats+=1\n else:\n aeats+=1\n print(\"{} {}\".format(aeats,beats))", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n n = int(input())\n a = list(map(int,input().split()))\n x = int(input())\n if n==1:\n print(1,0)\n continue\n else:\n sum1 = sum(a)\n lim = x*(int((sum1/(x+1))))\n sumt = 0\n f = -1\n c1 = 0\n while lim > sumt:\n f += 1\n sumt += a[f]\n c1 += 1\n c1 = c1-1\n #print(f,c1)\n for i in range(f,n-1):\n t1 = sum(a[:i])/x\n t2 = sum(a[i+1:])\n #print(c1,i,t1,t2)\n if t1 < t2:\n c1 += 1\n elif t1==t2:\n if i >= (n-i)-1:\n c1 += 1\n break\n else:\n break\n print(c1,n-c1)", "# cook your dish here\nt = int(input())\nfor _ in range(t):\n n = int(input())\n a = list(map(int,input().split()))\n x = int(input())\n c1 = 1\n if n==1:\n print(1,0)\n continue\n else:\n for i in range(1,n-1):\n t1 = sum(a[:i])/x\n t2 = sum(a[i+1:])\n if t1 < t2:\n c1 += 1\n elif t1==t2:\n if i >= (n-i)-1:\n c1 += 1\n break\n else:\n break\n print(c1,n-c1)", "for _ in range(int(input())):\n n=int(input())\n arr = list(map(int,input().split()))\n X = int(input())\n if n==1:\n print(1,0)\n continue\n l,r = 0,n-1\n a,b=0,0\n sumA,sumB=0,0\n while l<r:\n if sumA<=sumB*X:\n sumA+=arr[l]\n l+=1\n a+=1\n else:\n sumB+=arr[r]\n r-=1\n b+=1\n if l==r:\n if sumA-sumB*X==0:\n if a>=b:\n a+=1\n else:\n b+=1\n elif sumA-sumB*X<0:\n a+=1\n else:\n b+=1\n print(a,b)\n", "for _ in range(int(input())):\n N = int(input())\n candies = list(map(int, input().split()))\n x = int(input())\n sum = 0\n for a in range(N):\n sum += candies[a]\n lftSum = candies[0]\n rgtSum = sum - lftSum\n lftans = 1\n rgtans = N - 1\n for a in range(1, N):\n if rgtSum - candies[a] == lftSum/x:\n if lftans >= rgtans - 1:\n lftans += 1\n rgtans -= 1\n break\n else:\n break\n elif rgtSum - candies[a] < lftSum/x:\n break\n else:\n lftans += 1\n rgtans -= 1\n lftSum += candies[a]\n rgtSum -= candies[a]\n\n print(lftans, rgtans)", "for _ in range(int(input())):\n n = int(input())\n lst = list(map(int, input().split()))\n x = int(input())\n\n A, B = 0, 0\n i, j = 0, n - 1\n\n while i < j:\n B += 1\n chocsA = x * lst[j]\n\n while i < j and chocsA >= lst[i]:\n chocsA -= lst[i]\n i += 1\n A += 1\n \n lst[i] -= chocsA\n j -= 1\n\n if i == j:\n if chocsA > 0:\n A += 1\n\n else:\n if A >= B:\n A += 1\n else:\n B += 1\n\n print(A, B)\n \n\n \n", "T=int(input())\nfor _ in range(T):\n N=int(input())\n l=list(map(int,input().split()))\n X=int(input())\n A=l[:]\n B=l[:]\n for i in range(1,N):\n A[i]=A[i-1]+A[i]\n B[N-1-i]=B[N-1-i]+B[N-i]\n #print(A,B)\n i=0\n j=N-1\n while i<j and j-i>1:\n if A[i]/X<B[j]:\n i+=1\n elif A[i]/X>B[j]:\n j-=1\n else:\n if i+1<N-j:\n j-=1\n else:\n i+=1\n print(i+1,N-j)\n", "for _ in range(int(input())):\n n = int(input())\n arr = list(map(int, input().split()))\n x = int(input())\n left, right = 0, n-1\n c1, c2 = 0, 0\n b1, b2 = 0, 0\n while left <= right:\n if c1 < x * c2:\n # print('h1')\n c1 += arr[left]\n left += 1\n b1 += 1\n elif c1 > x * c2:\n # print('h2')\n c2 += arr[right]\n right -= 1\n b2 += 1\n else:\n if left != right:\n # print('h3')\n c1 += arr[left]\n left += 1\n b1 += 1\n else:\n if b1 < b2:\n # print('h4')\n c2 += arr[right]\n right -= 1\n b2 += 1\n else:\n # print('h5')\n c1 += arr[left]\n left += 1\n b1 += 1\n print(b1, b2)", "#oimv\nd=int(input())\nfor tt in range(d):\n n=int(input())\n s=list(map(int,input().split()))\n x=int(input())\n sum=[]\n count=0\n low=-1\n high=n\n if(n==1):\n print(\"1 0\")\n continue\n counta=0\n countb=0\n timea=0\n timeb=0\n while(1):\n if(low>=high-1):\n break\n if timea<timeb:\n low+=1\n timea+=s[low]/x\n counta+=1\n elif timea>timeb:\n high-=1\n timeb+=s[high]\n countb+=1\n else:\n if counta>=countb:\n low+=1\n timea+=s[low]/x\n counta+=1\n else:\n high-=1\n timeb+=s[high]\n countb+=1\n print(counta,countb)", "#https://www.codechef.com/problems/BIT2B\nimport math\nimport bisect\nt=int(input())\nwhile t!=0:\n t-=1\n n=int(input())\n candies=[int(x) for x in input().split()]\n rate=int(input())\n a_start_time=[0 for x in range(n)]\n b_start_time=[0 for x in range(n)]\n a_start_time[0]=0\n b_start_time[-1]=0\n sum_candies=candies[0]\n for i in range(1,n):\n a_start_time[i]=sum_candies/rate\n sum_candies+=candies[i]\n \n\n for i in range(n-2,-1,-1):\n b_start_time[i]=b_start_time[i+1]+candies[i+1]\n \n a_count=0\n b_count=0\n if n==1:\n print(1, 0)\n continue\n elif n==2:\n print(1, 1)\n continue\n for i in range(n):\n if(a_start_time[i]<b_start_time[i]):\n a_count+=1\n elif(b_start_time[i]==a_start_time[i]):\n if(a_count>=n-a_count-1):\n a_count+=1\n print(a_count,n-a_count)", "#https://www.codechef.com/problems/BIT2B\nimport math\nimport bisect\nt=int(input())\nwhile t!=0:\n t-=1\n n=int(input())\n candies=[int(x) for x in input().split()]\n rate=int(input())\n a_start_time=[0 for x in range(n)]\n b_start_time=[0 for x in range(n)]\n a_start_time[0]=0\n b_start_time[-1]=0\n sum_candies=candies[0]\n for i in range(1,n):\n a_start_time[i]=sum_candies/rate\n sum_candies+=candies[i]\n \n\n for i in range(n-2,-1,-1):\n b_start_time[i]=b_start_time[i+1]+candies[i+1]\n \n a_count=0\n b_count=0\n if n==1:\n print(1, 0)\n continue\n elif n==2:\n print(1, 1)\n continue\n for i in range(n):\n if(a_start_time[i]<b_start_time[i]):\n a_count+=1\n elif(b_start_time[i]==a_start_time[i]):\n if(a_count>=n-a_count-1):\n a_count+=1\n print(a_count,n-a_count)", "#https://www.codechef.com/problems/BIT2B\nimport math\nimport bisect\nt=int(input())\nwhile t!=0:\n t-=1\n n=int(input())\n candies=[int(x) for x in input().split()]\n rate=int(input())\n a_start_time=[0 for x in range(n)]\n b_start_time=[0 for x in range(n)]\n a_start_time[0]=0\n b_start_time[-1]=0\n sum_candies=candies[0]\n for i in range(1,n):\n a_start_time[i]=sum_candies//rate\n sum_candies+=candies[i]\n \n\n for i in range(n-2,-1,-1):\n b_start_time[i]=b_start_time[i+1]+candies[i+1]\n \n a_count=0\n b_count=0\n if n==1:\n print(1, 0)\n continue\n elif n==2:\n print(1, 1)\n continue\n for i in range(n):\n if(a_start_time[i]<b_start_time[i]):\n a_count+=1\n elif(b_start_time[i]==a_start_time[i]):\n if(a_count>=n-a_count-1):\n a_count+=1\n print(a_count,n-a_count)", "#https://www.codechef.com/problems/BIT2B\nimport math\nimport bisect\nt=int(input())\nwhile t!=0:\n t-=1\n n=int(input())\n candies=[int(x) for x in input().split()]\n rate=int(input())\n a_start_time=[0 for x in range(n)]\n b_start_time=[0 for x in range(n)]\n a_start_time[0]=0\n b_start_time[-1]=0\n sum_candies=candies[0]\n for i in range(1,n):\n a_start_time[i]=sum_candies//rate\n sum_candies+=candies[i]\n \n\n for i in range(n-2,-1,-1):\n b_start_time[i]=b_start_time[i+1]+candies[i+1]\n \n a_count=0\n b_count=0\n if n==1:\n print(1, 0)\n continue\n elif n==2:\n print(1, 1)\n continue\n for i in range(n):\n if(a_start_time[i]<b_start_time[i]):\n a_count+=1\n elif(b_start_time[i]<a_start_time[i]):\n b_count+=1\n else:\n if(a_count<n-a_count-1):\n b_count+=1\n else:\n a_count+=1\n print(a_count,n-a_count)", "import math\nimport bisect\nt=int(input())\nwhile t!=0:\n t-=1\n n=int(input())\n candies=[int(x) for x in input().split()]\n rate=int(input())\n leftover=0\n a_start_time=[0 for x in range(n)]\n b_start_time=[0 for x in range(n)]\n a_start_time[0]=0\n b_start_time[-1]=0\n sum_candies=candies[0]\n for i in range(1,n):\n a_start_time[i]=sum_candies//rate\n sum_candies+=candies[i]\n \n\n for i in range(n-2,-1,-1):\n b_start_time[i]=b_start_time[i+1]+candies[i+1]\n \n a_count=0\n b_count=0\n if n==1:\n print(1, 0)\n continue\n elif n==2:\n print(1, 1)\n continue\n for i in range(n):\n if(a_start_time[i]<b_start_time[i]):\n a_count+=1\n elif(b_start_time[i]<a_start_time[i]):\n b_count+=1\n else:\n if(a_count<n-a_count-1):\n b_count+=1\n else:\n a_count+=1\n print(a_count,n-a_count)"]
{"inputs": [["1", "5", "2 8 8 2 9", "2"]], "outputs": [["4 1"]]}
INTERVIEW
PYTHON3
CODECHEF
12,618
c4f5eeb7ef015181c3d1b49823a35a44
UNKNOWN
You have a sequence $a$ with length $N$ created by removing some elements (possibly zero) from a permutation of numbers $(1, 2, \dots, N)$. When an element is removed, the length of the sequence doesn't change, but there is an empty spot left where the removed element was. You also have an integer $K$. Let's call a permutation $p_1, p_2, \dots, p_N$ good if: - it is possible replace empty spots in $a$ by numbers in such a way that we obtain the permutation $p$ - the number of positions $i$ ($1 < i \le N$) such that $p_i > p_{i-1}$ is equal to $K$ Your task is to find the number of good permutations. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$. - The second line contains $N$ space-separated integers $a_1, a_2, \dots, a_N$. Each element of this sequence is either $0$ (indicating an empty spot previously occupied by a removed element) or an integer between $1$ and $N$ inclusive. -----Output----- For each test case, print a single line containing one integer — the number of good permutations. -----Constraints----- - $1 \le T \le 300$ - $0 \le K < N \le 8$ - each integer between $1$ and $N$ inclusive appears in $a$ at most once -----Example Input----- 1 3 1 2 0 0 -----Example Output----- 2 -----Explanation----- Example case 1: The two possible good permutations are $(2,3,1)$ and $(2,1,3)$.
["from itertools import permutations\n\nfor _ in range(int(input())):\n N,K=list(map(int,input().split()))\n arr=list(map(int,input().split()))\n arr1=[]\n arr2=[]\n for i in range(1,len(arr)+1):\n arr1.append(i)\n indexzero=[]\n for i in range(0,len(arr)):\n if(arr[i]==0):\n indexzero.append(i)\n else:\n arr2.append(arr[i])\n # arr3 = [x for x in arr1 if x not in arr2]\n arr3= list(set(arr1)-set(arr2))\n result=permutations(arr3)\n perm=[]\n for i in result:\n perm.append(i)\n step=0\n count=0\n for p in range(0,len(perm)):\n temp=[]\n for q in range(0,len(arr)):\n if(arr[q]==0):\n temp.append(perm[p][step])\n step+=1 \n else:\n temp.append(arr[q])\n k=0\n step=0\n for m in range(0,len(temp)-1):\n if(temp[m]<temp[m+1]):\n k+=1\n if(k==K):\n count+=1 \n print(count)\n \n \n \n \n \n \n \n", "from itertools import permutations\nk=[]\nd=[]\ndef abc(l):\n ans=0\n for i in range(1,len(l)):\n if l[i]>l[i-1]:\n ans+=1\n return ans\n\nfor i in range(8):\n k.append(i+1)\n x=list(permutations(k))\n c=[]\n for j in range(8):\n c.append([])\n d.append(c)\n for j in x:\n d[i][abc(j)].append(j)\n\nt=int(input())\nfor _ in range(t):\n n,k=list(map(int,input().split()))\n l=list(map(int,input().split()))\n ans=0\n c=d[n-1][k]\n for i in c:\n flag=1\n for j in range(n):\n if l[j]!=0 and l[j]!=i[j]:\n flag=0\n break\n if flag:\n ans+=1\n print(ans)\n \n", "from itertools import permutations\nk=[]\nd=[]\ndef abc(l):\n ans=0\n for i in range(1,len(l)):\n if l[i]>l[i-1]:\n ans+=1\n return ans\n\nfor i in range(8):\n k.append(i+1)\n x=list(permutations(k))\n c=[]\n for j in range(8):\n c.append([])\n d.append(c)\n for j in x:\n d[i][abc(j)].append(j)\n\nt=int(input())\nfor _ in range(t):\n n,k=list(map(int,input().split()))\n l=list(map(int,input().split()))\n ans=0\n c=d[n-1][k]\n for i in c:\n flag=1\n for j in range(n):\n if l[j]!=0 and l[j]!=i[j]:\n flag=0\n break\n if flag:\n m=0\n for j in range(1,n):\n if i[j]>i[j-1]:\n m+=1\n if m==k:\n ans+=1\n print(ans)\n \n", "from itertools import permutations\nfor _ in range(int(input())):\n n,k=list(map(int,input().split()))\n a=list(map(int,input().split()))\n b=[False]*(n+1)\n for i in a:\n if i!=0:b[i]=True\n g=[]\n grd=0\n for i in range(1,n+1):\n if not b[i]:g.append(i)\n for i in permutations(g):\n b=a[:]\n f=0\n m=0\n ff=list(i[:])\n for j in range(n):\n if b[j]==0:\n b[j]=ff[-1]\n ff.pop()\n for j in range(1,n):\n if b[j]>b[j-1]:\n f+=1\n if f>k:\n m=1\n break\n if f==k and m==0:grd+=1\n print(grd)\n", "# cook your dish here\nfrom itertools import permutations\nfor _ in range(int(input())):\n n,k=list(map(int,input().split()))\n a=list(map(int,input().split()))\n b=[False]*(n+1)\n for i in a:\n if i!=0:b[i]=True\n g=[]\n grd=0\n for i in range(1,n+1):\n if not b[i]:g.append(i)\n for i in permutations(g):\n b=a[:]\n f=0\n ff=list(i[:])\n for j in range(n):\n if b[j]==0:\n b[j]=ff[-1]\n ff.pop()\n for j in range(1,n):\n if b[j]>b[j-1]:f+=1\n if f==k:grd+=1\n print(grd)\n", "def cal(a, count, k, poss, ind):\n temp = a.copy()\n if len(poss) == 0:\n #print(a)\n for i in range(1, len(a)):\n if temp[i]>temp[i-1]:\n k -= 1\n if k == 0 and 0 not in temp:\n count += 1\n return count\n if temp[ind] == 0:\n for item in poss:\n temp[ind] = item\n count = cal(temp, count, k, poss-set([item]), ind+1)\n else:\n count = cal(temp, count, k, poss, ind+1)\n return count\nt = int(input())\nwhile t>0:\n t -= 1\n n, k = map(int, input().split())\n a = [int(x) for x in input().split()]\n poss = set()\n for i in range(1, len(a)+1):\n if i not in a:\n poss.add(i)\n count = cal(a, 0, k, poss, 0)\n print(count)", "t=int(input())\nimport itertools\nfor _ in range(t):\n n,kkk=list(map(int,input().split()))\n it=list(map(int,input().split()))\n kk=set(list(range(1,n+1)))\n s=set([i for i in it if i!=0])\n left=kk-s\n #print(left)\n mm=list(itertools.permutations(list(left)))\n tot=0\n #print(mm)\n for perm in mm:\n #print(perm)\n k=0\n mt=it[:]\n ind=0\n for j,i in enumerate(it):\n if i==0:\n mt[j]=perm[ind]\n ind+=1\n if j>0:\n if mt[j]>mt[j-1]:\n k+=1\n if kkk==k:\n tot+=1\n print(tot)\n \n \n \n \n \n", "import itertools\nt=int(input())\nfor _ in range(t):\n n,k=map(int,input().split())\n a=list(map(int,input().split()))\n p=[i for i in range(1,n+1)]\n good=0\n for i in a:\n if(i != 0 ):\n p.remove(i)\n\n x=list(itertools.permutations(p))\n for i in x:\n j=0\n b=[l for l in a]\n for z in range(n):\n if(b[z]==0):\n b[z]=i[j]\n j+=1\n loc_cnt=0\n for z in range(1,n):\n if(b[z]>b[z-1]):\n loc_cnt+=1\n if(loc_cnt==k):\n good+=1\n \n print(good) "]
{"inputs": [["1", "3 1", "2 0 0"]], "outputs": [["2"]]}
INTERVIEW
PYTHON3
CODECHEF
4,751
24095a7bd03b4e960b2f09faa4c92183
UNKNOWN
$Harshad$ $Mehta$ is planning a new scam with the stocks he is given a stock of integer price S and a number K . $harshad$ has got the power to change the number $S$ at most $K$ times In order to raise the price of stock and now cash it for his benefits Find the largest price at which $harshad$ can sell the stock in order to maximize his profit -----Input:----- - First line will contain $S$ and $K$ , the price of the stock and the number K -----Output:----- Print the largest profit he can make in a single line. -----Constraints----- - S can take value upto 10^18 NOTE: use 64 int number to fit range - K can take value from [0.. 9] -----Sample Input:----- 4483 2 -----Sample Output:----- 9983 -----EXPLANATION:----- First two digits of the number are changed to get the required number.
["a,b=[int(_) for _ in input().split()]\r\nif b==0:\r\n print(a)\r\nelse: \r\n l=[]\r\n a=str(a)\r\n for i in range(len(a)):\r\n l.append(a[i])\r\n for i in range(len(l)):\r\n if b==0:\r\n break\r\n if l[i]=='9':\r\n continue\r\n else:\r\n l[i]='9'\r\n b-=1\r\n s=''\r\n for i in l:\r\n s+=i\r\n print(s) \r\n \r\n", "# cook your dish here\ndef maxnum(s, k):\n l=len(s)\n if l==0 or k==0:\n return s\n if l==1:\n return \"9\"\n if s[0]!='9':\n s='9'+s[1:]\n k-=1\n i=1\n while k>0 and i<l:\n if s[i]!='9':\n s=s[:i]+'9'+s[i+1:]\n k-=1\n i+=1\n return s\nn,k=list(map(int,input().split()))\ns=f'{n}'\nprint(maxnum(s, k))\n", "n,k = input().split()\na = [int(i) for i in n]\nk = int(k)\ni = 0;n = len(a)\nwhile(k>0 and i<n):\n if a[i]!=9:\n a[i]=9\n k-=1\n i+=1\nprint(''.join(map(str,a)))", "s,k = map(int,input().split())\na = str(s)\nl = [int(d) for d in str(a)]\nwhile k>0:\n for j in range(len(l)):\n if l[j]==9:\n continue\n else:\n l[j] = 9\n k-=1\n if k<1:\n break\nprint(*l,sep=\"\") \n \n ", "st,k=input().split()\n\nst=list(st)\n\nk=int(k)\nfor i in range(len(st)): \n if (k < 1): \n break\n\n if (st[i] != '9'): \n\n st[i]='9'\n\n k -= 1\n \nprint(''.join(st))", "st,k=input().split()\n\n\nk=int(k)\nfor i in range(len(st)): \n if (k < 1): \n break\n\n if (st[i] != '9'): \n\n st = st[0:i] + '9' + st[i + 1:] \n\n k -= 1\n \nprint(st)", "num,k=input().split()\r\nk=int(k)\r\n# print(num)\r\n# print(num[0])\r\nl=[]\r\nold=''\r\nfor x in range(len(num)):\r\n if num[x]!='9' and k>0:\r\n old='9'\r\n l.append(old)\r\n k=k-1\r\n else:\r\n l.append(num[x])\r\n \r\n\r\nfor x in l:\r\n print(x,end='')\r\n", "num,k=input().split()\r\nk=int(k)\r\n# print(num)\r\n# print(num[0])\r\nl=[]\r\nold=''\r\nfor x in range(len(num)):\r\n if num[x]!='9' and k>0:\r\n old='9'\r\n l.append(old)\r\n k=k-1\r\n else:\r\n l.append(num[x])\r\n \r\n\r\nfor x in l:\r\n print(x,end='')\r\n", "N, K = input().split()\r\nN, K = list(N), int(K)\r\n\r\nfor i in range(len(N)):\r\n\tif K and N[i] != '9':\r\n\t\tN[i] = '9'\r\n\t\tK -= 1\r\nprint(''.join(N))", "# cook your dish here\r\ns,k = list(map(int,input().split()))\r\nstrS = str(s)\r\nres = ''\r\nfor i in range(0,len(strS)):\r\n if k>0 and strS[i] != '9':\r\n res += '9'\r\n k -= 1 \r\n else:\r\n res += strS[i]\r\n\r\nprint(int(res))\r\n", "s,k=map(int,input().split())\ni=0\nc=0\nst=list(str(s))\nwhile(i<k):\n if i>=len(st):\n break\n if st[i]!=\"9\":\n st[i]=\"9\"\n c=c+1\n i=i+1\nif c<k:\n for i in range(len(st)):\n if c>=k:\n break\n if st[i]!='9':\n st[i]='9'\n c=c+1\nst=\"\".join(st)\ns=int(st)\nprint(s)", "# cook your dish here\nn,k = map(int, input().split())\nn = str(n)\nfor i in range(len(n)):\n if(k == 0):\n break\n if(n[i]!='9'):\n n = n[:i]+'9'+n[i+1:]\n k-=1\n \nprint(n)", "# # cook your dish here\n# n,k=map(int,input().split())\n# x=[int(y) for y in str(n)]\n# if k<=len(x):\n# for i in range(k):\n# x[i]=9\n# else:\n# k=len(x)\n# for i in range(k):\n# x[i]=9\n# c=\"\".join(str(x)for x in x)\n# print(c)\n# print(max(int(c),n*k))\nn,k=list(map(int,input().split()))\n# x=[int(y) for y in str(n)]\nx=str(n)\na=[]\n# if k<=len(x):\n# for i in range(k):\n# x[i]=9\n# else:\n# k=len(x)\n# for i in range(k):\n# x[i]=9\n# print(\"\".join(str(x)for x in x))\nfor i in range(len(x)):\n if k<1:\n break\n if x[i]!='9':\n x=x[0:i]+'9'+x[i+1:]\n\n k-=1\nprint(x)\n", "# cook your dish here\nn,k=list(map(int,input().split()))\nn=str(n)\nl=list(n)\nn=len(l)\nfor i in range(n):\n if(k>0):\n if(l[i]!='9'):\n l[i]='9'\n k-=1\nprint(''.join(l))\n", "# cook your dish here\ndef findMaximumNum(st, n, k): \n\tfor i in range(n): \n \n\t\tif (k < 1): \n\t\t\tbreak\n \n\t\tif (st[i] != '9'): \n\n \n\t\t\tst = st[0:i] + '9' + st[i + 1:] \n\n\n\t\t\tk -= 1\n\n\treturn st \n\ns,k=list(map(int, input().split()))\nst=str(s)\nn=len(st)\nprint(findMaximumNum(st, n, k)) \n", "# cook your dish here\ndef findMaximumNum(st, n, k): \n\tfor i in range(n): \n \n\t\tif (k < 1): \n\t\t\tbreak\n \n\t\tif (st[i] != '9'): \n\n \n\t\t\tst = st[0:i] + '9' + st[i + 1:] \n\n\n\t\t\tk -= 1\n\n\treturn st \n\ns,k=list(map(int, input().split()))\nst=str(s)\nn=len(st)\nprint(findMaximumNum(st, n, k)) \n", "# cook your dish here\ndef findMaximumNum(st, k):\n\tfor i in range(len(st)):\n\t\tif (k < 1): \n\t\t\tbreak \n\t\tif (st[i] != '9'):\n\t\t\tst = st[0:i] + '9' + st[i + 1:]\n\t\t\tk -= 1\n\n\treturn st \nst,m = input().split()\nk = int(m)\nprint(findMaximumNum(st, k)) ", "# cook your dish here\ntry:\n s,k=map(int,input().split())\n num=str(s)\n l=[]\n for i in range(len(num)):\n l.append(int(num[i]))\n cnt=0\n for i in range(len(l)):\n if k==0:\n break\n if l[i]!=9:\n l[i]=9\n cnt+=1\n if cnt==k:\n break\n for i in l:\n print(i,end=\"\")\n print()\nexcept EOFError as e: pass\n \n \n ", "\r\n\r\ndef findMaximumNum(st, n, k): \r\n \r\n for i in range(n): \r\n \r\n if (k < 1): \r\n break\r\n \r\n if (st[i] != '9'): \r\n \r\n st = st[0:i] + '9' + st[i + 1:] \r\n \r\n \r\n k -= 1\r\n \r\n return st \r\n \r\n\r\nst,k = input().split()\r\nn = len(st) \r\nk = int(k)\r\nprint(findMaximumNum(st, n, k)) ", "x,y = list(map(str, input().split(\" \")))\nl = list(x)\ny = int(y)\ni = 0\nwhile i<int(y): \n if(l[i]!=\"9\"):\n l[i] = \"9\"\n else:\n y+=1\n i+=1\nprint((''.join(l)))# cook your dish here\n", "n,k=input().split()\nk=int(k)\nl=len(n)\nans=\"\"\nfor i in range(l):\n if k==0:\n break\n if n[i]!='9':\n ans+='9'\n k-=1\n else:\n ans+=n[i]\nif len(ans)==l:\n print(ans)\nelse:\n for j in range(len(ans),l):\n ans+=n[j]\n print(ans)", "n,k=input().split()\nk=int(k)\nres = [x for x in str(n)]\ni=0\nwhile(k>0 and i<len(res)):\n if(res[i]!=\"9\"):\n res[i]=\"9\"\n k=k-1\n i=i+1\n else:\n i=i+1\nst=\"\"\nx=st.join(res)\nprint(x)\n \n \n", "x,y = list(map(str, input().split(\" \")))\nl = list(x)\ny = int(y)\ni = 0\nwhile i<int(y): \n if(l[i]!=\"9\"):\n l[i] = \"9\"\n else:\n y+=1\n i+=1\nprint(''.join(l))\n", "n,k=map(int,input().split())\nx=str(n)\nj=0\nv=[]\nfor i in range(0,len(x)):\n if x[i]=='9':\n v.append(x[i])\n continue\n else:\n if j<k:\n j=j+1\n v.append('9')\n else:\n v.append(x[i])\nprint(\"\".join(v))", "s, k = [int(s) for s in input().split(\" \")]\r\na = str(s)\r\nans = \"\"\r\nfor ch in a:\r\n if k > 0:\r\n if int(ch) < 9:\r\n ans += \"9\"\r\n k-=1\r\n else:\r\n ans += ch\r\n else:\r\n ans += ch\r\nprint(ans)\r\n"]
{"inputs": [["4483 2"]], "outputs": [["9983"]]}
INTERVIEW
PYTHON3
CODECHEF
7,460
7533fe99aea3e440d4129b7621519831
UNKNOWN
Rahul is a serial killer. Rahul has been betrayed by his lover in the past and now he want to eliminate entire Universe.He has already Eliminated majority of the population and now only a handful number of people are left. Like other Serial killers, he has an interesting pattern of killing people. He either kill one individual at a time or if he find two individuals of different heights,he eliminates both of them simultaneously. Now Rahul wants to eliminate them as quickly as he can. So given $N$ as the number of people left and an array containing height of those $N$ people,tell the minimum number of kills Rahul require to eliminate the entire universe. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each test case constitutes of Two lines. - First line contains $N$, representing the number of people left in the universe - The second line contains an array $a[i]$ of size $N$ containing heights of those $N$ people. -----Output:----- For each testcase, you have to output a Single line Containing the minimum number of kills required by Rahul to eliminate the Universe. -----Constraints----- - $1 \leq T \leq 50000$ - $1 \leq N \leq 50000$ - $100 \leq a[i] \leq 10^5$ -----Sample Input:----- 1 10 178 184 178 177 171 173 171 183 171 175 -----Sample Output:----- 5
["from math import ceil\r\nt=int(input())\r\nfor i in range(t):\r\n p=int(input())\r\n l=list(map(int,input().split()))\r\n maxx=1\r\n for i in range(len(l)):\r\n maxx=max(maxx,l.count(l[i]))\r\n if(maxx*2>p):\r\n print(maxx)\r\n else:\r\n q=p-maxx*2\r\n maxx+=ceil(q/2)\r\n print(maxx)\r\n \r\n \r\n \r\n\r\n", "#dt = {} for i in x: dt[i] = dt.get(i,0)+1\r\nimport sys;input = sys.stdin.readline\r\ninp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]\r\n\r\nfor _ in range(inp()):\r\n n = inp()\r\n x = ip()\r\n dt = {} \r\n for i in x: dt[i] = dt.get(i,0)+1\r\n ans = max(max(dt.values()),(n-1)//2+1)\r\n print(ans)"]
{"inputs": [["1", "10", "178 184 178 177 171 173 171 183 171 175"]], "outputs": [["5"]]}
INTERVIEW
PYTHON3
CODECHEF
772
75438b511245353451ce12b2918b4f5f
UNKNOWN
Chef recently learned about ratios and proportions. He wrote some positive integers a, b, c, d on a paper. Chef wants to know whether he can shuffle these numbers so as to make some proportion? Formally, four numbers x, y, z, w are said to make a proportion if ratio of x : y is same as that of z : w. -----Input----- Only line of the input contains four space separated positive integers - a, b, c, d. -----Output----- Print "Possible" if it is possible to shuffle a, b, c, d to make proportion, otherwise "Impossible" (without quotes). -----Constraints----- - 1 ≤ a, b, c, d ≤ 1000 -----Example----- Input: 1 2 4 2 Output: Possible -----Explanation----- By swapping 4 and the second 2, we get 1 2 2 4. Note that 1 2 2 4 make proportion as 1 : 2 = 2 : 4. Hence answer is "Possible"
["def permutate(arr):\n if len(arr) == 1:\n yield arr\n for x in range(len(arr)):\n for perm in permutate(arr[:x] + arr[x+1:]):\n yield [arr[x]] + perm\n\nvals = [int(x) for x in input().split()]\n\nfounded = False\nfor val in permutate(vals):\n if (val[0] / float(val[1]) == val[2] / float(val[3])):\n print(\"Possible\")\n founded = True \n break\nif not founded:\n print(\"Impossible\")\n", "a = input().split()\na = [int(x) for x in a]\na.sort()\nif (float(a[0])/float(a[1]))==(float(a[2])/float(a[3])):\n print(\"Possible\")\nelse:\n print(\"Impossible\")", "a,b,c,d=list(map(int,input().split()))\nif(a*b==c*d or a*c==b*d or a*d==b*c):\n print(\"Possible\")\nelse:\n print(\"Impossible\")", "import sys\n\na, b, c, d = list(map(float, input().split()))\n\nif a/b == c/d:\n print(\"Possible\")\n return\n\nif a/b == d/c:\n print(\"Possible\")\n return\n\nif a/c == b/d:\n print(\"Possible\")\n return\n\nif a/c == d/b:\n print(\"Possible\")\n return\n\nif a/d == c/b:\n print(\"Possible\")\n return\n\nif a/d == b/c:\n print(\"Possible\")\n return\n\nif a/b == c/d:\n print(\"Possible\")\n return\n\nif a/b == c/d:\n print(\"Possible\")\n return\n\nprint(\"Impossible\")", "import sys\n\na, b, c, d = list(map(float, input().split()))\n\nif a/b == c/d:\n print(\"Possible\")\n return\n\nif a/b == d/c:\n print(\"Possible\")\n return\n\nif a/c == b/d:\n print(\"Possible\")\n return\n\nif a/c == d/b:\n print(\"Possible\")\n return\n\nif a/d == c/b:\n print(\"Possible\")\n return\n\nif a/d == b/c:\n print(\"Possible\")\n return\n\nif a/b == c/d:\n print(\"Possible\")\n return\n\nif a/b == c/d:\n print(\"Possible\")\n return\n\nprint(\"Impossible\")", "a,b,c,d = list(map(float,input().split()))\nli = []\nli.append(a)\nli.append(b)\nli.append(c)\nli.append(d)\nli = sorted(li)\nif li[1]/li[0] == li[3]/li[2]:\n print(\"Possible\")\nelse:\n print(\"Impossible\")", "a,b,c,d = list(map(int , input().split()))\n\nif(a*d == c*b or a*c == d*b or b*d == c*a or b*c == d*a):\n print(\"Possible\")\nelif(a*d == b*c or a*b ==d*c or c*d == b*a or c*b == d*a):\n print(\"Possible\")\nelif(a*c == b*d or a*b == c*d or d*c == b*a or d*b == c*a):\n print(\"Possible\")\nelse:\n print(\"Impossible\") ", "a=list(map(float,input().split()))\na.sort()\nif(a[0]/a[1]==a[2]/a[3]):\n print('Possible')\nelse:\n print('Impossible')", "a, b , c, d = list(map(int , input().split()))\n\nab = a*b\nac = a*c\nad = a*d\nbc = b*c\nbd = b*d\ncd = c*d\n\nif ab == cd or ac == bd or ad == bc:\n print('Possible')\nelse:\n print('Impossible')\n", "def min(a,b):\n if(a<b):\n return a;\n return b;\n\ndef max(a,b):\n if(a<b):\n return b;\n return a;\n\n\n \n[a,b,c,d]=[float(x) for x in input().split()];\nflag=False;\nif(min(a,b)/max(a,b)==min(c,d)/max(c,d)):\n flag=True;\n\nif(min(a,c)/max(a,c)==min(b,d)/max(b,d)):\n flag=True;\n\nif(min(a,d)/max(a,d)==min(b,c)/max(b,c)):\n flag=True;\n\nif(flag):\n print (\"Possible\");\nelse:\n print (\"Impossible\");\n \n \n\n \n \n \n", "a,b,c,d = input().split(\" \")\nA = []\nA.append(float(a))\nA.append(float(b))\nA.append(float(c))\nA.append(float(d))\n\nA.sort()\n\nif A[0]/A[1] == A[2]/A[3]:\n print(\"Possible\")\nelif A[0]/A[2] == A[1]/A[3]:\n print(\"Possible\")\nelif A[0]/A[3] == A[1]/A[2]:\n print(\"Possible\")\nelse:\n print(\"Impossible\")", "a=list(map(int,input().split(\" \")))\na.sort()\nif a[1]*a[2]==a[0]*a[3]:\n print(\"Possible\")\nelse :\n print(\"Impossible\")", "\ndef __starting_point():\n a, b, c, d = sorted(map(int, input().split()))\n if d * a == b * c:\n print('Possible')\n else:\n print('Impossible')\n\n__starting_point()", "import sys\n\ndef check(a,b,c,d) :\n return (a==c and b==d) or (a==d and b==c)\n\na,b,c,d = list(map(int,sys.stdin.readline().split()))\ngcd = lambda x,y : x if y==0 else gcd(y,x%y)\nflag=False\nlg0,rg0 = gcd(a,b),gcd(c,d)\nif check(a/lg0,b/lg0,c/rg0,d/rg0) :\n flag = True\n\nlg0,rg0 = gcd(a,c),gcd(b,d)\nif check(a/lg0,c/lg0,b/rg0,d/rg0) :\n flag = True\n\nlg0,rg0 = gcd(a,d),gcd(c,b)\nif check(a/lg0,d/lg0,c/rg0,b/rg0) :\n flag = True\n\nprint(\"Possible\" if flag else \"Impossible\")", "from itertools import permutations\na = [int(num) for num in input().split()]\n\nl = list(permutations(a))\nflag = False\nfor e in l:\n e0 = float(e[0])\n e1 = float(e[1])\n e2 = float(e[2])\n e3 = float(e[3])\n if e0 / e1 == e2 / e3:\n print(\"Possible\")\n flag = True\n break\nif flag == False:\n print(\"Impossible\")", "a,b,c,d = list(map(float, input().strip().split()))\nif a/b == c/d or a/b == d/c:\n print('Possible')\nelif a/c == b/d or a/c == d/b:\n print('Possible')\nelif a/d == c/b or a/d == b/c:\n print('Possible')\nelse:\n print('Impossible')\n", "\na = list(map(int,input().split()))\nout = \"Impossible\"\nfor i in range(4):\n if out == \"Possible\":\n break\n for j in range(1,4):\n a[i],a[j] = a[j],a[i]\n if a[0]/a[1] == a[2]/a[3]:\n out = \"Possible\"\n break\n else:\n a[i],a[j] = a[j],a[i]\nprint(out)", "\na = list(map(int,input().split()))\nout = \"Impossible\"\nfor i in range(4):\n if out == \"Possible\":\n break\n for j in range(1,4):\n a[i],a[j] = a[j],a[i]\n if a[0]/a[1] == a[2]/a[3]:\n out = \"Possible\"\n break\n else:\n a[i],a[j] = a[j],a[i]\nprint(out)", "a=[int(x) for x in input().split()]\na.sort()\nif a[0]*a[3]==a[2]*a[1]:\n print(\"Possible\")\nelse:\n print(\"Impossible\") ", "li=list(map(int,input().split()))\nli =sorted(li)\na,b,c,d=li\n#print float(b/float(a))\n#print float(d/float(c))\nif float(b/float(a)) == float(d/float(c)):\n print(\"Possible\")\nelse:\n print(\"Impossible\")", "import itertools\na,b,c,d = list(map(float , input().split()))\nli = list(itertools.permutations([a,b,c,d]))\nflag = False\nfor i in li:\n if i[0]/i[1] == i[2]/i[3]:\n flag = True\n break\nif flag == True :\n print('Possible')\nelse :\n print('Impossible')", "abcd = input()\nabcd = abcd.split()\nabcd = list(map(int, abcd))\nprod = 1\ncnt = 0\nfor item in abcd:\n prod*=item\nflo = prod**(0.5)\nif int(flo) == flo:\n for i in range(3):\n for j in range(i+1,4):\n if abcd[i]*abcd[j] == flo:\n cnt = 1\n break\n if cnt==1:\n break\n if cnt == 1:\n print(\"Possible\")\n else:\n print(\"Impossible\")\nelse:\n print(\"Impossible\")"]
{"inputs": [["1 2 4 2"]], "outputs": [["Possible"]]}
INTERVIEW
PYTHON3
CODECHEF
6,195
e2cc78f3f007e25a62bae2f1d548150a
UNKNOWN
Devu is a disastrous oracle: his predictions about various events of your life are horrifying. Instead of providing good luck, he "blesses" you with bad luck. The secret behind his wickedness is a hidden omen which is a string of length m. On your visit to him, you can ask a lot of questions about your future, each of which should be a string of length m. In total you asked him n such questions, denoted by strings s1, s2, ... , sn of length m each. Each of the question strings is composed of the characters 'a' and 'b' only. Amount of bad luck this visit will bring you is equal to the length of longest common subsequence (LCS) of all the question strings and the hidden omen string. Of course, as the omen string is hidden, you are wondering what could be the least value of bad luck you can get. Can you find out what could be the least bad luck you can get? Find it fast, before Devu tells you any bad omens. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. First line of each test case contains a single integer n denoting number of strings. For each of next n lines, the ith line contains the string si. -----Output----- For each test case, output a single integer corresponding to the answer of the problem. -----Constraints----- - All the strings (including the hidden omen) contain the characters 'a' and 'b' only. Subtask #1: (40 points) - 1 ≤ T, n, m ≤ 14 Subtask #2: (60 points) - 1 ≤ T, n, m ≤ 100 -----Example----- Input:3 2 ab ba 2 aa bb 3 aabb abab baab Output:1 0 2 -----Explanation----- In the first example, the minimum value of LCS of all the strings is 1, the string by oracle can be one of these {aa, ab, ba, bb}. In the second example, whatever string oracle has does not matter, LCS will always be zero.
["t = int(input())\nfor j in range(0, t):\n n = int(input())\n m = 100\n for i in range(0, n):\n str = input()\n p = min(str.count(\"a\",0,len(str)),str.count(\"b\",0,len(str)))\n if (m > p):\n m = p\n print(m)\n t = t-1", "for i in range(int(input())):\n n=int(input())\n li_a=li_b=[]\n for i in range(n):\n s=input()\n li_a.append(s.count('a'))\n li_b.append(s.count('b'))\n print(min(min(li_a),min(li_b)))\n", "# your code goes here\n\nt = int(input())\nwhile (t>0):\n t = t-1\n n = int(input())\n a = 100000000\n b = 100000000\n for i in range (0,n):\n s = str(input())\n l = s.count('a')\n if (l < a):\n a = l\n l = s.count('b')\n if (l < b):\n b = l\n print(min(a, b))", "# cook your code here\nt=int(input())\nfor unused in range(t):\n n=int(input())\n minA=200\n minB=200\n for used in range(n):\n inputString=input()\n countA=0\n countB=0\n for i in inputString:\n if i=='a':\n countA+=1\n else:\n countB+=1\n if countA<minA:\n minA=countA\n if countB<minB:\n minB=countB\n print(min(minA,minB))\n \n \n \n\n \n \n", "t = eval(input())\nfor i in range(t):\n n = eval(input())\n d = {'a':1000,'b':1000}\n for j in range(n):\n str = input()\n d['a']= min(str.count('a'),d['a'])\n d['b']= min(str.count('b'),d['b'])\n print(min(d['a'],d['b'])) ", "# cook your code here\nT = int(input())\nfor _ in range(T):\n N = int(input())\n A = [0]*N\n for i in range(N):\n s = input()\n ca = 0\n cb = 0\n for x in s:\n if x == 'a':\n ca += 1\n else:\n cb += 1\n A[i] = min(ca,cb)\n print(min(A))\n", "import sys\n\nt = int(input())\n\nwhile t > 0:\n n = int(input())\n minimum = sys.maxsize\n while n > 0:\n s = input()\n minimum = min(minimum, s.count('a'), s.count('b'))\n n = n - 1\n print(minimum)\n t = t - 1", "import sys\n\nT = int(input())\n\nresult = \"\"\n\ndef getMin(matrix, N, M):\n smallest = sys.maxsize\n for i in range(0, N):\n for j in range(0, M):\n if matrix[i][j] < smallest:\n smallest = matrix[i][j]\n return smallest\n\nfor i in range(0, T):\n N = int(input())\n matrix = [[0 for x in range(2)] for x in range(N)]\n for j in range(0, N):\n text = input()\n for c in text:\n if(c == \"a\"):\n matrix[j][0] += 1\n else:\n matrix[j][1] += 1\n result += str(getMin(matrix, N, 2)) + \"\\n\"\n\nprint(result)", "def f(n):\n s=list(input().strip())\n mina=len([x for x in s if x=='a'])\n ln=len(s)\n minb=ln-mina\n for i in range(n-1):\n s=list(input().strip())\n curra=len([x for x in s if x=='a'])\n currb=len([x for x in s if x=='b'])\n if mina>curra:\n mina=curra\n if minb>currb:\n minb=currb\n return min(mina,minb)\nt=int(input())\nfor i in range(t):\n n=int(input())\n print(f(n)) ", "for _ in range(int(input())):\n lst=[]\n for nn in range(int(input())):\n stg=input()\n chck=list(set(stg))\n if chck==['a'] or chck==['b']:\n lst.append(0)\n continue\n else:\n lst.append(min(stg.count('a'),stg.count('b')))\n print(min(lst))\n \n", "def lcs(A,B):\n a,b=len(A),len(B)\n lst=[[None]*(b+1) for _ in range(a+1)]\n for x in range(a+1):\n for y in range(b+1):\n if x==0 or y==0:\n lst[x][y]=0\n elif A[x-1] == B[y-1]:\n lst[x][y]= 1+lst[x-1][y-1]\n else:\n lst[x][y]=max(lst[x][y-1],lst[x-1][y])\n return lst[a][b]\n\nfor _ in range(int(input())):\n n=int(input())\n lst=[]\n for i in range(n):\n stg=input()\n axx=list(set(stg))\n if axx == ['a'] or axx == ['b']:\n lst.append(0)\n continue\n m=len(stg)\n cnta=stg.count('a')\n cntb=stg.count('b')\n if cnta>cntb:\n lst.append(lcs(stg,m*'b'))\n else:\n lst.append(lcs(stg,m*'a'))\n print(min(lst))\n", "t=eval(input())\nwhile t:\n m=101\n n=eval(input())\n while n:\n s=input()\n a=s.count('a')\n b=s.count('b')\n m=min(a, b, m)\n n-=1\n print(m)\n t-=1", "def lcs(A,B):\n a,b=len(A),len(B)\n lst=[[None]*(b+1) for _ in range(a+1)]\n for x in range(a+1):\n for y in range(b+1):\n if x==0 or y==0:\n lst[x][y]=0\n elif A[x-1] == B[y-1]:\n lst[x][y]= 1+lst[x-1][y-1]\n else:\n lst[x][y]=max(lst[x][y-1],lst[x-1][y])\n return lst[a][b]\n\nfor _ in range(int(input())):\n n=int(input())\n lst=[]\n for i in range(n):\n stg=input()\n axx=list(set(stg))\n if axx == ['a'] or axx == ['b']:\n lst.append(0)\n continue\n m=len(stg)\n cnta=stg.count('a')\n cntb=stg.count('b')\n if cnta>cntb:\n lst.append(lcs(stg,m*'b'))\n elif cntb>cnta:\n lst.append(lcs(stg,m*'a'))\n else:\n lst.append(min(lcs(stg,m*'a'),lcs(stg,m*'b')))\n print(min(lst))\n", "def lcs(A,B):\n a,b=len(A),len(B)\n lst=[[None]*(b+1) for _ in range(a+1)]\n for x in range(a+1):\n for y in range(b+1):\n if x==0 or y==0:\n lst[x][y]=0\n elif A[x-1] == B[y-1]:\n lst[x][y]= 1+lst[x-1][y-1]\n else:\n lst[x][y]=max(lst[x][y-1],lst[x-1][y])\n return lst[a][b]\n\nfor _ in range(int(input())):\n n=int(input())\n lst=[]\n for i in range(n):\n stg=input()\n m=len(stg)\n cnta=stg.count('a')\n cntb=stg.count('b')\n if cnta>cntb:\n lst.append(lcs(stg,m*'b'))\n elif cntb>cnta:\n lst.append(lcs(stg,m*'a'))\n else:\n lst.append(min(lcs(stg,m*'a'),lcs(stg,m*'b')))\n print(min(lst))\n", "def lcs(A,B):\n a,b=len(A),len(B)\n lst=[[0]*(b+1) for _ in range(a+1)]\n for x in range(1,a+1):\n for y in range(1,b+1):\n if A[x-1] == B[y-1]:\n lst[x][y]= 1+lst[x-1][y-1]\n else:\n lst[x][y]=max(lst[x][y-1],lst[x-1][y])\n return lst[a][b]\n\nfor _ in range(int(input())):\n n=int(input())\n lst=[]\n for i in range(n):\n stg=input()\n m=len(stg)\n lst.append(min(lcs(stg,m*'a'),lcs(stg,m*'b')))\n print(min(lst))\n", "t=eval(input())\nfor qq in range(t):\n n=eval(input())\n s=input()\n amin=0\n bmin=0\n for x in s:\n if x ==\"a\":amin+=1\n else:bmin+=1\n #print s,amin,bmin\n for cc in range(n-1):\n s=input()\n ca=0\n cb=0\n for x in s:\n if x==\"a\":ca+=1\n else:cb+=1\n amin=min(ca,amin)\n bmin=min(cb,bmin)\n # print s,amin,bmin\n print(min(amin,bmin))", "t = int(input())\nfor i in range(t):\n n = int(input())\n min_a = 1000\n min_b = 1000\n for j in range(n):\n tmp_a = 0\n tmp_b = 0\n str1 = input()\n for e in str1:\n if(e == 'a'):\n tmp_a+=1\n elif(e == 'b'):\n tmp_b+=1\n min_a = min(min_a, tmp_a)\n min_b = min(min_b, tmp_b)\n print(min(min_b, min_a))\n", "t = int(input())\nfor i in range(t):\n n = int(input())\n min_a = 1000\n min_b = 1000\n for j in range(n):\n tmp_a = 0\n tmp_b = 0\n str1 = input()\n for e in str1:\n if(e == 'a'):\n tmp_a+=1\n elif(e == 'b'):\n tmp_b+=1\n min_a = min(min_a, tmp_a)\n min_b = min(min_b, tmp_b)\n print(min(min_b, min_a))\n", "for x in range(0,int(input())):\n k,q=int(input()),100000\n for y in range(0,k):\n s=input()\n q=min(min(s.count('a'),s.count('b')),q)\n print(q)", "for x in range(0,int(input())):\n k=int(input())\n ctra,ctrb=[None]*k,[None]*k\n for y in range(0,k):\n s=input()\n ctra[y],ctrb[y]=s.count('a'),s.count('b')\n print(min(min(ctra),min(ctrb)))\n"]
{"inputs": [["3", "2", "ab", "ba", "2", "aa", "bb", "3", "aabb", "abab", "baab"]], "outputs": [["1", "0", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
6,907
c63ed3a364fedea94464c128dad4b03e
UNKNOWN
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl. Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc. Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i ≠ j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j − i|type. Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one. -----Input----- The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow. The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students. Note that the integer n is not given explicitly in input. -----Output----- For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost. -----Constraints and Subtasks-----Subtask 1: 25 points - 1 ≤ T ≤ 105 - 1 ≤ n ≤ 105 - type = 0 - Sum of n over all the test cases in one test file does not exceed 106. Subtask 2: 25 points - 1 ≤ T ≤ 105 - 1 ≤ n ≤ 105 - type = 1 - Sum of n over all the test cases in one test file does not exceed 106. Subtask 3: 25 points - 1 ≤ T ≤ 105 - 1 ≤ n ≤ 105 - type = 2 - Sum of n over all the test cases in one test file does not exceed 106. Subtask 4: 25 points - 1 ≤ T ≤ 102 - 1 ≤ n ≤ 103 - type can be 0, 1 or 2, that is type ∈ {0, 1, 2}. -----Example----- Input: 8 0 BB 0 BG 0 BBGG 1 BGG 1 BGGB 1 BBBGG 2 BBGG 2 BGB Output: -1 0 1 1 1 3 1 0 -----Explanation----- Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed. Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1. Example case 2. Arrangement is already valid. No swap is needed. So answer is 0. Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1. Now type of the next 3 test cases is 1. So c(i, j) = |j − i|, that is, the absolute value of the difference between i and j. Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1. Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1. Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3. Then type of the last 2 test cases is 2. So c(i, j) = (j − i)2 Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1. Example case 8. Arrangement is already valid. No swap is needed. So answer is 0.
["def outOfIndex(boys,girls,COST):\n if COST == 0:\n return len(boys)\n else:\n total_cost = [ abs(x-y) for x,y in zip(boys,girls)]\n total_cost = sum(total_cost)\n return total_cost\n\nfor _ in range(int(input())):\n COST = int(input())\n queue = input()\n B = queue.count('B')\n G = queue.count('G')\n boys=[]\n girls = []\n if (abs(B-G)>1):\n print(-1)\n else:\n if B > G:\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='B':\n boys.append(c)\n if c%2==0 and queue[c] =='G':\n girls.append(c)\n print(outOfIndex(boys,girls,COST))\n boys.clear()\n girls.clear()\n elif B < G:\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='G':\n girls.append(c)\n if c%2==0 and queue[c] =='B':\n boys.append(c)\n print(outOfIndex(boys,girls,COST))\n boys.clear()\n girls.clear()\n else:\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='B':\n boys.append(c)\n if c%2==0 and queue[c] =='G':\n girls.append(c)\n attempt1 = outOfIndex(boys,girls,COST)\n boys.clear()\n girls.clear()\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='G':\n girls.append(c)\n if c%2==0 and queue[c] =='B':\n boys.append(c)\n attempt2 = outOfIndex(boys,girls,COST)\n print(min(attempt1,attempt2))\n boys.clear()\n girls.clear() \n", "def outOfIndex(boys,girls,COST):\n if COST == 0:\n return len(boys)\n else:\n total_cost = [ abs(x-y) for x,y in zip(boys,girls)]\n total_cost = sum(total_cost)\n return total_cost\n\nfor _ in range(int(input())):\n COST = int(input())\n queue = input()\n B = queue.count('B')\n G = queue.count('G')\n boys=[]\n girls = []\n if (abs(B-G)>1):\n print((-1))#Impossible condition\n else:\n if B > G:\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='B':\n boys.append(c)\n if c%2==0 and queue[c] =='G':\n girls.append(c)\n #After genarating list pass it to the function\n print(outOfIndex(boys,girls,COST))\n boys.clear()\n girls.clear()\n \n elif B < G:\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='G':\n girls.append(c)\n if c%2==0 and queue[c] =='B':\n boys.append(c)\n print(outOfIndex(boys,girls,COST))\n boys.clear()\n girls.clear()\n\n else:\n #Start with boys\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='B':\n boys.append(c)\n if c%2==0 and queue[c] =='G':\n girls.append(c)\n #After genarating list pass it to the function\n attempt1 = outOfIndex(boys,girls,COST)\n boys.clear()\n girls.clear()\n for c in range(len(queue)):\n if c%2!=0 and queue[c]=='G':\n girls.append(c)\n if c%2==0 and queue[c] =='B':\n boys.append(c)\n attempt2 = outOfIndex(boys,girls,COST)\n print(min(attempt1,attempt2))\n boys.clear()\n girls.clear() \n", "def actual_generator(a,N,start):\n result = []\n if a == 'B':\n result = ['B', 'G'] * (N // 2)\n result.append('B')\n elif a == 'G':\n result = ['G', 'B'] * (N // 2)\n result.append('G')\n else:\n if start == 'B':\n result = ['B', 'G'] * (N // 2)\n elif start == 'G':\n result = ['G', 'B'] * (N // 2)\n return result\n\n\nfor _ in range(int(input())):\n typ = int(input())\n s = input()\n N = len(s)\n array = [s[i] for i in range(N)]\n countG = array.count('G')\n countB = array.count('B')\n if abs(countG - countB) > 1:\n print(-1)\n else:\n defectB = []\n defectG = []\n if countG == countB:\n actual1 = actual_generator('X', N, 'B')\n actual2 = actual_generator('X', N, 'G')\n defect1B = []\n defect1G = []\n defect2B = []\n defect2G = []\n for i in range(N):\n if actual1[i] != array[i] and actual1[i] == 'B':\n defect1B.append(i)\n elif actual1[i] != array[i] and actual1[i] == 'G':\n defect1G.append(i)\n for i in range(N):\n if actual2[i] != array[i] and actual2[i] == 'B':\n defect2B.append(i)\n elif actual2[i] != array[i] and actual2[i] == 'G':\n defect2G.append(i)\n if len(defect1B) > len(defect2B):\n defectG = defect2G\n defectB = defect2B\n else :\n defectG = defect1G\n defectB = defect1B\n elif countG > countB:\n actual = actual_generator('G', N, 'X')\n defectB = []\n defectG = []\n for i in range(N):\n if actual[i] != array[i] and actual[i] == 'B':\n defectB.append(i)\n elif actual[i] != array[i] and actual[i] == 'G':\n defectG.append(i)\n\n elif countB > countG:\n actual = actual_generator('B', N, 'X')\n defectB = []\n defectG = []\n for i in range(N):\n if actual[i] != array[i] and actual[i] == 'B':\n defectB.append(i)\n elif actual[i] != array[i] and actual[i] == 'G':\n defectG.append(i)\n\n if typ == 0 :\n print(len(defectB))\n elif typ == 1 :\n print(abs(sum(defectG)-sum(defectB)))", "for _ in range(int(input())):\n t=int(input())\n s=input()\n b=[]\n a=[]\n c=0\n lb=0\n la=0\n r=[]\n ls=len(s)\n for i in (s):\n c+=1\n if i=='B':\n lb+=1\n else:\n la+=1\n if (la-lb)==0 or abs(la-lb)==1:\n if la < lb:\n for i in range(ls):\n if i % 2 == 1 and s[i] == 'B':\n b.append(i)\n if i % 2 == 0 and s[i] == 'G':\n a.append(i)\n elif la > lb:\n for i in range(ls):\n if i % 2 == 0 and s[i] == 'B':\n b.append(i)\n if i % 2 == 1 and s[i] == 'G':\n a.append(i)\n cs = 0\n sw = 0\n for i in range(len(a)):\n cs += abs(a[i] - b[i])\n if t == 0:\n cs = len(a)\n if la==lb:\n for i in range(len(s)):\n if i % 2 == 0 and s[i] == 'B':\n b.append(i)\n if i % 2 == 1 and s[i] == 'G':\n a.append(i)\n cs=0\n sw=0\n for i in range(len(a)):\n cs+=abs(a[i]-b[i])\n if t==0:\n cs=len(a)\n x=cs\n b=[]\n a=[]\n for i in range(len(s)):\n if i % 2 == 1 and s[i] == 'B':\n b.append(i)\n if i % 2 == 0 and s[i] == 'G':\n a.append(i)\n cs=0\n sw=0\n for i in range(len(a)):\n if i<len(b):\n cs+=abs(a[i]-b[i])\n if t==0:\n cs=len(a)\n cs=min(x,cs)\n print(cs)\n else:\n print(-1)\n", "for _ in range(int(input())):\n t = int(input())\n s = input()\n b = 0\n g = 0\n for i in s:\n if i=='B':\n b+=1\n else:\n g+=1\n if abs(g-b)>1:\n print(\"-1\")\n elif (g-b)>0:\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==0:\n bl.append(i)\n elif s[i]=='G' and i%2==1:\n gl.append(i)\n c=0\n for i in range(len(bl)):\n if t==0:\n c+=1\n else:\n c+=abs(gl[i]-bl[i])\n print(c)\n elif (b-g)>0:\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==1:\n bl.append(i)\n elif s[i]=='G' and i%2==0:\n gl.append(i)\n c=0\n for i in range(len(bl)):\n if t==0:\n c+=1\n else:\n c+=abs(gl[i]-bl[i])\n print(c)\n else:\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==0:\n bl.append(i)\n elif s[i]=='G' and i%2==1:\n gl.append(i)\n c1=0\n for i in range(len(bl)):\n if t==0:\n c1+=1\n else:\n c1+=abs(gl[i]-bl[i])\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==1:\n bl.append(i)\n elif s[i]=='G' and i%2==0:\n gl.append(i)\n c2=0\n for i in range(len(bl)):\n if t==0:\n c2+=1\n else:\n c2+=abs(gl[i]-bl[i])\n if c2<c1:\n print(c2)\n else:\n print(c1)", "for _ in range(int(input())):\n t = int(input())\n s = input()\n n = len(s)\n b = 0\n for i in s:\n if i == 'B':\n b+=1\n g = n-b\n if abs(b-g)>1:\n print('-1')\n elif b==1 and g==1:\n print('0')\n elif b>g:\n BG = 'BG'*(n//2)\n if n&1: BG += 'B'\n B, G = [], []\n for i in range(n):\n if s[i]!=BG[i]:\n if s[i]=='B':\n B.append(i)\n else:\n G.append(i)\n if t==0:\n print(len(B))\n else:\n print(sum([abs(B[i]-G[i]) for i in range(len(B))]))\n elif b<g:\n BG = 'GB'*(n//2)\n if n&1: BG += 'G'\n B, G = [], []\n for i in range(n):\n if s[i]!=BG[i]:\n if s[i]=='B':\n B.append(i)\n else:\n G.append(i)\n if t==0:\n print(len(B))\n else:\n print(sum([abs(B[i]-G[i]) for i in range(len(B))]))\n else:\n BG = 'GB'*(n//2)\n if n&1: BG += 'G'\n B, G = [], []\n for i in range(n):\n if s[i]!=BG[i]:\n if s[i]=='B':\n B.append(i)\n else:\n G.append(i)\n if t==0:\n v1 = len(B)\n else:\n v1 = sum([abs(B[i]-G[i]) for i in range(len(B))])\n \n BG = 'BG'*(n//2)\n if n&1: BG += 'B'\n B, G = [], []\n for i in range(n):\n if s[i]!=BG[i]:\n if s[i]=='B':\n B.append(i)\n else:\n G.append(i)\n if t==0:\n v2 = len(B)\n else:\n v2 = sum([abs(B[i]-G[i]) for i in range(len(B))])\n print(min(v1,v2))", "for _ in range(int(input())):\n t = int(input())\n s = input()\n b = 0\n g = 0\n for i in s:\n if i=='B':\n b+=1\n else:\n g+=1\n if abs(g-b)>1:\n print(\"-1\")\n elif (g-b)>0:\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==0:\n bl.append(i)\n elif s[i]=='G' and i%2==1:\n gl.append(i)\n c=0\n for i in range(len(bl)):\n if t==0:\n c+=1\n else:\n c+=abs(gl[i]-bl[i])\n print(c)\n elif (b-g)>0:\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==1:\n bl.append(i)\n elif s[i]=='G' and i%2==0:\n gl.append(i)\n c=0\n for i in range(len(bl)):\n if t==0:\n c+=1\n else:\n c+=abs(gl[i]-bl[i])\n print(c)\n else:\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==0:\n bl.append(i)\n elif s[i]=='G' and i%2==1:\n gl.append(i)\n c1=0\n for i in range(len(bl)):\n if t==0:\n c1+=1\n else:\n c1+=abs(gl[i]-bl[i])\n gl = []\n bl = []\n for i in range(len(s)):\n if s[i]=='B' and i%2==1:\n bl.append(i)\n elif s[i]=='G' and i%2==0:\n gl.append(i)\n c2=0\n for i in range(len(bl)):\n if t==0:\n c2+=1\n else:\n c2+=abs(gl[i]-bl[i])\n if c2<c1:\n print(c2)\n else:\n print(c1)", "def compute(s,c):\n bcount = s.count('B')\n gcount = s.count('G')\n if(abs(bcount-gcount)>1):\n return -1\n stack = [(0,s[0])]\n bans = 0\n gans = 0\n if(c!=0):\n c=1\n for i in range(1,len(s)):\n if(len(stack)>0 and stack[len(stack)-1][1]!=s[i]):\n idx,char = stack[len(stack)-1]\n if((idx%2 and s[i]=='B') or (i%2 and char=='B')):\n bans += abs(i-idx)**c\n elif((idx%2 and s[i]=='G') or (i%2 and char=='G')):\n gans += abs(i-idx)**c\n stack.pop(len(stack)-1)\n else:\n stack.append((i,s[i]))\n if(gcount==bcount):\n return min(bans,gans)\n elif(gcount>bcount):\n return bans\n return gans\nfor t in range(int(input())):\n c = int(input())\n print(compute(input(),c))", "def compute(s,c):\n bcount = s.count('B')\n gcount = s.count('G')\n if(abs(bcount-gcount)>1):\n return -1\n stack = [(0,s[0])]\n bans = 0\n gans = 0\n beven = 0\n geven = 0\n if(c!=0):\n c=1\n for i in range(1,len(s)):\n if(len(stack)>0 and stack[len(stack)-1][1]!=s[i]):\n idx,char = stack[len(stack)-1]\n if((idx%2 and s[i]=='B') or (i%2 and char=='B')):\n bans += abs(i-idx)**c\n beven+=1\n elif((idx%2 and s[i]=='G') or (i%2 and char=='G')):\n gans += abs(i-idx)**c\n geven+=1\n stack.pop(len(stack)-1)\n else:\n stack.append((i,s[i]))\n #print(stack,end=' ')\n \"\"\"\n if(len(stack)>0):\n idx,char = stack[len(stack)-1]\n if(char=='G'):\n geven+=1\n else:\n beven+=1\n print(\"geven =\",geven,\"beven =\",beven,end=' ')\n \"\"\"\n if(gcount==bcount):\n return min(bans,gans)\n elif(gcount>bcount):\n return bans\n return gans\nfor t in range(int(input())):\n c = int(input())\n print(compute(input(),c))\n", "def compute(s,c):\n bcount = s.count('B')\n gcount = s.count('G')\n if(abs(bcount-gcount)>1):\n return -1\n stack = [(0,s[0])]\n bans = 0\n gans = 0\n for i in range(1,len(s)):\n if(len(stack)>0 and stack[len(stack)-1][1]!=s[i]):\n idx,char = stack[len(stack)-1]\n if((idx%2 and s[i]=='B') or (i%2 and char=='B')):\n bans += abs(i-idx)**c\n elif((idx%2 and s[i]=='G') or (i%2 and char=='G')):\n gans += abs(i-idx)**c\n stack.pop(len(stack)-1)\n else:\n stack.append((i,s[i]))\n \n if(gcount==bcount):\n return min(bans,gans)\n elif(gcount>bcount):\n return bans\n return gans\nfor t in range(int(input())):\n c = int(input())\n print(compute(input(),c))\n", "\"\"\"\narrange string in a form that no two 'B' or two 'G' come together, if can't \u00a0\u00a0\u00a0\u00a0return -1\ncost = |i-j|**t\nminimize cost\n\"\"\"\n\ndef solve(s,c):\n bcount = s.count('B')\n gcount = s.count('G')\n if(abs(bcount-gcount)>1):\n return -1\n B = []\n G = []\n start = 'B'\n cost=0\n if(bcount<gcount):\n start = 'G'\n iterate=1\n temp=start\n if(bcount==gcount):\n iterate = 2\n for y in range(iterate):\n for i in range(len(s)):\n if(s[i]!=start):\n if(s[i]=='B'):\n if(len(G)>0):\n t = G.pop(0)\n cost+=abs(t-i)**c\n else:\n B.append(i)\n else:\n if(len(B)>0):\n t = B.pop(0)\n cost+=pow(abs(t-i),c,10**6)\n else:\n G.append(i) \n if(start=='B'):\n start='G'\n else:\n start='B'\n if(temp=='B'):\n start='G'\n else:\n start='B'\n if(y==1):\n break\n eqcost = cost\n cost=0\n B=[]\n G=[]\n #print(eqcost,cost)\n if(bcount==gcount):\n return min(eqcost,cost)\n return eqcost \n \n \n\n\nfor t in range(int(input())):\n c = int(input())\n s = input()\n print(solve(s,c))\n", "\"\"\"\narrange string in a form that no two 'B' or two 'G' come together, if can't \u00a0\u00a0\u00a0\u00a0return -1\ncost = |i-j|**t\nminimize cost\n\"\"\"\n\ndef solve(s,c):\n bcount = s.count('B')\n gcount = s.count('G')\n if(abs(bcount-gcount)>1):\n return -1\n B = []\n G = []\n start = 'B'\n cost=0\n if(bcount<gcount):\n start = 'G'\n iterate=1\n temp=start\n if(bcount==gcount):\n iterate = 2\n for y in range(iterate):\n for i in range(len(s)):\n if(s[i]!=start):\n if(s[i]=='B'):\n if(len(G)>0):\n t = G.pop(0)\n cost+=abs(t-i)**c\n else:\n B.append(i)\n else:\n if(len(B)>0):\n t = B.pop(0)\n cost+=abs(t-i)**c\n else:\n G.append(i) \n if(start=='B'):\n start='G'\n else:\n start='B'\n if(temp=='B'):\n start='G'\n else:\n start='B'\n if(y==1):\n break\n eqcost = cost\n cost=0\n B=[]\n G=[]\n #print(eqcost,cost)\n if(bcount==gcount):\n return min(eqcost,cost)\n return eqcost \n \n \n\n\nfor t in range(int(input())):\n c = int(input())\n s = input()\n print(solve(s,c))\n"]
{"inputs": [["8", "0", "BB", "0", "BG", "0", "BBGG", "1", "BGG", "1", "BGGB", "1", "BBBGG", "2", "BBGG", "2", "BGB"]], "outputs": [["-1", "0", "1", "1", "1", "3", "1", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
14,237
4480764bafe2b8f69908e23b995b35d3
UNKNOWN
Rohit has n empty boxes lying on the ground in a line. The size of the boxes is given in the form of an array $a$. The size of the ith box is denoted by $a[i]$. Since Rohit has a tiny room, there is a shortage of space. Therefore, he has to reduce the number of boxes on the ground by putting a box into another box that is at least twice the size of the current box i.e if we have to put the ith box into the jth box then $( 2*a[i] ) <= a[j]$. Each box can contain a maximum of one box and the box which is kept in another box cannot hold any box itself. Find the minimum number of boxes that will remain on the ground after putting boxes into each other. -----Input:----- - The first line contains a single integer n. - The next n lines contain the integer a[i] - the size of the i-th box. -----Output:----- Output a single integer denoting the minimum number of boxes remaining on the ground. -----Constraints----- - $1 \leq n \leq 5*10^5$ - $1 \leq a[i] \leq 10^5$ -----Subtasks----- - 30 points : $1 \leq n \leq 10^2$ - 70 points : $1 \leq n \leq 5*10^5$ -----Sample Input:----- 5 16 1 4 8 2 -----Sample Output:----- 3
["n=int(input())\r\nl=[0]*n\r\nfor i in range(n):\r\n l[i]=int(input())\r\nl.sort()\r\ns=0\r\ni=n-1\r\nwhile i>=0:\r\n x=2*l[i]\r\n if l[-1]>=x:\r\n j=i\r\n while j<len(l):\r\n if l[j]>=x:\r\n l.pop(j)\r\n l.pop(i)\r\n s+=1\r\n break\r\n j+=1\r\n i-=1\r\ns+=len(l)\r\nprint(s)", "n=int(input())\nl=[0]*n\nfor i in range(n):\n l[i]=int(input())\nl.sort()\ns=0\ni=n-1\nwhile i>=0:\n x=2*l[i]\n if l[-1]>=x:\n j=i\n while j<len(l):\n if l[j]>=x:\n l.pop(j)\n l.pop(i)\n s+=1\n break\n j+=1\n i-=1\ns+=len(l)\nprint(s)", "def remove_values_from_list(the_list, val):\r\n return [value for value in the_list if value != val]\r\nn=int(input())\r\nl=[]\r\nbox=0\r\nfor i in range(n):\r\n a=int(input())\r\n l.append(a) \r\nl.sort(reverse=True)\r\nfor i in range(n):\r\n if(l[i]!=0):\r\n for j in range(1,n):\r\n if(l[j]!=0):\r\n if(l[j]*2<=l[i]):\r\n box=box+1\r\n l[i]=0\r\n l[j]=0\r\n break \r\nl = remove_values_from_list(l, 0) \r\nprint(box+len(l))", "#!/usr/bin/env python\r\nimport sys\r\n\r\nboxes_number = int(sys.stdin.readline().rstrip())\r\nboxes_sizes = list(map(int, sys.stdin.readlines()))\r\n\r\nboxes_sizes.sort()\r\n\r\nlower_bound, upper_bound = 0, boxes_number // 2 + 1\r\n\r\nwhile lower_bound + 1 < upper_bound:\r\n middle = (lower_bound + upper_bound) >> 1\r\n\r\n possible = not middle or all(\r\n 2 * boxes_sizes[i] <= boxes_sizes[boxes_number - middle + i]\r\n for i in range(middle)\r\n )\r\n\r\n if not possible:\r\n upper_bound = middle\r\n else:\r\n lower_bound = middle\r\n\r\nprint(boxes_number - lower_bound)\r\n", "n=int(input())\r\nl=[]\r\nfor _ in range(n):\r\n l.append(int(input()))\r\nl.sort()\r\nl.reverse()\r\ni=0\r\ncount=0\r\nwhile i<len(l):\r\n s=l[i]\r\n l1=l[i+1:len(l)]\r\n if len(l1)!=0 and s>=2*l1[len(l1)-1]:\r\n for j in l1:\r\n if s>=2*j:\r\n l.remove(j)\r\n break\r\n count+=1\r\n i+=1\r\nprint(count)\r\n", "def remove_values_from_list(the_list, val):\n return [value for value in the_list if value != val]\nn=int(input())\nl=[]\nbox=0\nfor i in range(n):\n a=int(input())\n l.append(a) \nl.sort(reverse=True)\n\nfor i in range(n):\n if(l[i]!=0):\n for j in range(1,n):\n if(l[j]!=0):\n if(l[j]*2<=l[i]):\n box=box+1\n l[i]=0\n l[j]=0\n break \n \nl = remove_values_from_list(l, 0) \nprint(box+len(l))", "def remove_values_from_list(the_list, val):\r\n return [value for value in the_list if value != val]\r\nn=int(input())\r\nl=[]\r\nbox=0\r\nfor i in range(n):\r\n a=int(input())\r\n l.append(a) \r\nl.sort(reverse=True)\r\nfor i in range(n):\r\n if(l[i]!=0):\r\n for j in range(1,n):\r\n if(l[j]!=0):\r\n if(l[j]*2<=l[i]):\r\n box=box+1\r\n l[i]=0\r\n l[j]=0\r\n break \r\nl = remove_values_from_list(l, 0) \r\nprint(box+len(l))", "from collections import defaultdict\r\nn=int(input())\r\nl=[]\r\nfor i in range(n):\r\n l.append([int(input()),0])\r\nl.sort(key=lambda x:x[0],reverse=True)\r\nans=0\r\ny=0\r\nfor i in range(n):\r\n if l[i][1]==1:\r\n continue\r\n for j in range(max(y,i+1),n):\r\n if l[j][1]==0 and 2*l[j][0]<=l[i][0]:\r\n l[j][1]=1\r\n l[i][1]=1\r\n ans+=1\r\n break\r\n y=j\r\nfor i in range(n):\r\n if l[i][1]==0:\r\n ans+=1\r\nprint(ans)\r\n#print(l)\r\n\r\n", "# n=int(input())\n# lst=[]\n# for i in range(n):\n# lst.append(int(input()))\n# lst.sort()\n# l=0\n# r=1\n# count=0\n# while r<len(lst):\n# if lst[l]*2<=lst[r]:\n# count+=1\n# l+=1\n# r+=1\n# else:\n# r+=1\n# print(len(lst)-count)\n\n# cook your dish here\nn = int(input())\nlst = []\nfor i in range(n):\n lst.append(int(input()))\nlst.sort()\nmini = abs(-n//2)\nf_l,s_l = lst[:mini],lst[mini:]\n\nans = 0\nj = 0\ni = 0\nextra=0\nwhile(i<len(f_l) and j<len(s_l)):\n if(f_l[i]*2<=s_l[j]):\n ans+=1\n i+=1\n j+=1\n else:\n extra+=1\n j+=1\n# print(ans)\nans+=len(s_l)-j + len(f_l)-i + extra\n# print(i,j,ans)\nprint(ans)", "n=int(input())\r\nlis=[]\r\nfor _ in range (n):\r\n size=int(input())\r\n lis.append(size)\r\nlis.sort()\r\nlis.reverse()\r\nj=1\r\nval=[True for _ in range (n)]\r\ni=0\r\ncnt=n\r\nif (n==1):\r\n print(1)\r\nelif (lis[0]==lis[n-1]):\r\n print(n)\r\nelse:\r\n while (i<n and j<n):\r\n if (val[i]!=True):\r\n i+=1\r\n continue\r\n if (val[j]!=True):\r\n j+=1\r\n continue\r\n if (lis[j]*2<=lis[i]):\r\n val[i]=False\r\n val[j]=False\r\n i+=1\r\n j+=1\r\n cnt-=1\r\n else:\r\n j+=1\r\n print(cnt)"]
{"inputs": [["5", "16", "1", "4", "8", "2"]], "outputs": [["3"]]}
INTERVIEW
PYTHON3
CODECHEF
5,262
2d7d124114aa689a11a05ff822bb97ce
UNKNOWN
After completing some serious investigation, Watson and Holmes are now chilling themselves in the Shimla hills. Very soon Holmes became bored. Holmes lived entirely for his profession. We know he is a workaholic. So Holmes wants to stop his vacation and get back to work. But after a tiresome season, Watson is in no mood to return soon. So to keep Holmes engaged, he decided to give Holmes one math problem. And Holmes agreed to solve the problem and said as soon as he solves the problem, they should return back to work. Watson too agreed. The problem was as follows. Watson knows Holmes’ favorite numbers are 6 and 5. So he decided to give Holmes N single digit numbers. Watson asked Holmes to form a new number with the given N numbers in such a way that the newly formed number should be completely divisible by 5 and 6. Watson told Holmes that he should also form the number from these digits in such a way that the formed number is maximum. He may or may not use all the given numbers. But he is not allowed to use leading zeros. Though he is allowed to leave out some of the numbers, he is not allowed to add any extra numbers, which means the maximum count of each digit in the newly formed number, is the same as the number of times that number is present in those given N digits. -----Input----- The first line of input contains one integers T denoting the number of test cases. Each test case consists of one integer N, number of numbers. Next line contains contains N single digit integers -----Output----- For each test case output a single number, where the above said conditions are satisfied. If it is not possible to create such a number with the given constraints print -1.If there exists a solution, the maximised number should be greater than or equal to 0. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N ≤ 10000 - 0 ≤ Each digit ≤ 9 -----Subtasks----- Subtask #1 : (90 points) - 1 ≤ T ≤ 100 - 1 ≤ N ≤ 10000 Subtask 2 : (10 points) - 1 ≤ T ≤ 10 - 1 ≤ N≤ 10 -----Example----- Input: 2 12 3 1 2 3 2 0 2 2 2 0 2 3 11 3 9 9 6 4 3 6 4 9 6 0 Output: 33322222200 999666330
["t=int(input())\nfor i in range(0,t):\n n=int(input())\n lis=list(map(int,input().split()))\n lis2=[]\n for j in range(0,10):\n lis2.append(0)\n for j in range(0,len(lis)):\n lis2[lis[j]]+=1;\n s=sum(lis)\n while s%3!=0:\n if s%3==2:\n if lis2[2]>=1:\n lis2[2]-=1\n s=s-2\n elif lis2[5]>=1:\n lis2[5]-=1\n s=s-5\n elif lis2[8]>=1:\n lis2[8]-=1\n s=s-8\n elif lis2[1]>=2:\n lis2[1]-=2\n s=s-2\n elif lis2[1]>=1 and lis2[4]>=1:\n lis2[1]-=1\n lis2[4]-=1\n s=s-5\n elif lis2[4]>=2:\n lis2[4]-=2\n s=s-8\n elif lis2[1]>=1 and lis2[7]>=1:\n lis2[1]-=1\n lis2[7]-=1\n s=s-8\n elif lis2[4]>=1 and lis2[7]>=1:\n lis2[4]-=1\n lis2[7]-=1\n s=s-11\n elif lis2[7]>=2:\n lis2[7]-=2\n s=s-14\n elif s%3==1:\n if lis2[1]>=1:\n lis2[1]-=1\n s=s-1\n elif lis2[4]>=1:\n lis2[4]-=1\n s=s-4\n elif lis2[7]>=1:\n lis2[7]-=1\n s=s-7\n elif lis2[2]>=2:\n lis2[2]-=2\n s=s-4\n elif lis2[5]>=1 and lis2[2]>=1:\n lis2[2]-=1\n lis2[5]-=1\n s=s-7\n elif lis2[5]>=2:\n lis2[5]-=2\n s=s-10\n elif lis2[2]>=1 and lis2[8]>=1:\n lis2[2]-=1\n lis2[8]-=1\n s=s-10\n elif lis2[8]>=1 and lis2[5]>=1:\n lis2[8]-=1\n lis2[5]-=1\n s=s-13\n elif lis2[8]>=2:\n lis2[8]-=2\n s=s-16\n lis3=[]\n for j in range(1,10):\n if lis2[j]>=1:\n for k in range(0,lis2[j]):\n lis3.append(j)\n lis3.reverse()\n for k in range(0,lis2[0]):\n lis3.append(0)\n sol=''\n for k in range(0,len(lis3)):\n sol+=str(lis3[k])\n print(sol)", "def mul3(ip): \n q0=[]\n q1=[]\n q2=[]\n ip.sort()\n sums=sum(ip)\n for i in ip:\n if (i % 3) == 0:\n q0.insert(0,i)\n if (i % 3) == 1:\n q1.insert(0,i)\n if (i % 3) == 2:\n q2.insert(0,i)\n if(sums%3==1):\n if(len(q1)):\n q1.pop()\n elif(len(q2)>=2):\n q2.pop()\n q2.pop()\n else:\n return -1\n elif(sums%3==2):\n if(len(q2)):\n q2.pop()\n elif(len(q1)>=2):\n q1.pop()\n q1.pop()\n else:\n return -1\n \n q0.extend(q1)\n q0.extend(q2)\n if(len(q0)<=0):\n return -1\n q0.sort()\n q0.reverse()\n return q0\n\nt = int(input())\nfor z in range(t):\n n = int(input())\n a = list(map(int, input().split()))\n if 0 not in a or mul3(a)==-1:\n print(-1)\n else:\n out=''\n temp = mul3(a)\n for p in temp:\n out+=str(p)\n print(out)", "def qsort(inlist):\n if inlist == []: \n return []\n else:\n pivot = inlist[0]\n lesser = qsort([x for x in inlist[1:] if x < pivot])\n greater = qsort([x for x in inlist[1:] if x >= pivot])\n return lesser + [pivot] + greater\ndef mul3(ip): \n q0=[]\n q1=[]\n q2=[]\n qsort(ip)\n sums=sum(ip)\n for i in ip:\n if (i % 3) == 0:\n q0.insert(0,i)\n if (i % 3) == 1:\n q1.insert(0,i)\n if (i % 3) == 2:\n q2.insert(0,i)\n if(sums%3==1):\n if(len(q1)):\n q1.pop()\n elif(len(q2)>=2):\n q2.pop()\n q2.pop()\n else:\n return -1\n elif(sums%3==2):\n if(len(q2)):\n q2.pop()\n elif(len(q1)>=2):\n q1.pop()\n q1.pop()\n else:\n return -1\n \n q0.extend(q1)\n q0.extend(q2)\n if(len(q0)<=0):\n return -1\n q0.sort()\n q0.reverse()\n return q0\n\nt = int(input())\nfor z in range(t):\n n = int(input())\n a = list(map(int, input().split()))\n if 0 not in a or mul3(a)==-1:\n print(-1)\n else:\n out=''\n temp = mul3(a)\n for p in temp:\n out+=str(p)\n print(out)", "def mul3(ip): \n q0=[]\n q1=[]\n q2=[]\n ip.sort()\n sums=sum(ip)\n for i in ip:\n if (i % 3) == 0:\n q0.insert(0,i)\n if (i % 3) == 1:\n q1.insert(0,i)\n if (i % 3) == 2:\n q2.insert(0,i)\n if(sums%3==1):\n if(len(q1)):\n q1.pop()\n elif(len(q2)>=2):\n q2.pop()\n q2.pop()\n else:\n return -1\n elif(sums%3==2):\n if(len(q2)):\n q2.pop()\n elif(len(q1)>=2):\n q1.pop()\n q1.pop()\n else:\n return -1\n \n q0.extend(q1)\n q0.extend(q2)\n if(len(q0)<=0):\n return -1\n q0.sort()\n q0.reverse()\n return q0\n\nt = int(input())\nfor z in range(t):\n n = int(input())\n a = list(map(int, input().split()))\n if 0 not in a or mul3(a)==-1:\n print(-1)\n else:\n out=''\n temp = mul3(a)\n for p in temp:\n out+=str(p)\n print(out)", "t = eval(input())\nwhile t:\n n = eval(input())\n z = list(map(int,input().split()))\n a = [0]*10\n for i in z:\n a[i] += 1\n if a[0] == 0:\n print(-1)\n \n else:\n no_1 = a[1] + a[4] + a[7]\n no_2 = a[2] + a[5] + a[8]\n s = ''\n if (no_1 + no_2*2)%3 == 0:\n s = ''\n elif no_2 > no_1:\n no = (no_2 - no_1)%3\n if no_2 == 1 and no_1 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n elif no == 2:\n if no_1 > 0:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 2\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n elif no_2 < no_1:\n no = (no_1 - no_2)%3\n if no_1 == 1 and no_2 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n elif no == 2:\n if no_2 > 0:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 1\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n for i in range(9,-1,-1):\n s += str(i)*a[i]\n \n if len(s) == 0:\n print(-1)\n else:\n print(int(s))\n t -= 1", "n=int(input())\nwhile n>0:\n num=0\n t=int(input())\n a=list(map(int,input().split()))\n a.sort(reverse=True)\n sum1=sum(a)%3\n if(sum1==0):\n flag=1\n elif(sum1 in a and sum1!=0):\n c=a.index(sum1)\n del a[c]\n elif(sum1+3 in a):\n c=a.index(sum1+3)\n del a[c]\n elif(sum1+6 in a):\n c=a.index(sum1+6)\n del a[c]\n elif(sum1==1):\n if(2 in a):\n c=a.index(2)\n del a[c]\n if(2 in a):\n c=a.index(2)\n del a[c]\n elif(5 in a):\n c=a.index(5)\n del a[c]\n else:\n c=a.index(8)\n del a[c]\n elif(5 in a):\n c=a.index(5)\n del a[c]\n if(5 in a):\n c=a.index(5)\n del a[c]\n elif(8 in a):\n c=a.index(8)\n del a[c]\n elif(8 in a):\n c=a.index(8)\n del a[c]\n if(8 in a):\n c=a.index(8)\n del a[c]\n elif(sum1==2):\n if(1 in a):\n c=a.index(1)\n del a[c]\n if(1 in a):\n c=a.index(1)\n del a[c]\n elif(4 in a):\n c=a.index(4)\n del a[c]\n else:\n c=a.index(7)\n del a[c]\n elif(4 in a):\n c=a.index(4)\n del a[c]\n if(4 in a):\n c=a.index(4)\n del a[c]\n elif(7 in a):\n c=a.index(7)\n del a[c]\n elif(7 in a):\n c=a.index(7)\n del a[c]\n if(7 in a):\n c=a.index(7)\n del a[c]\n num=int(''.join(map(str,a)))\n if(0 not in a):\n num=-1\n print(num)\n n-=1 ", "n=int(input())\nwhile n>0:\n num=0\n t=int(input())\n a=list(map(int,input().split()))\n a.sort(reverse=True)\n sum1=sum(a)%3\n if(sum1==0):\n flag=1\n elif(sum1 in a and sum1!=0):\n c=a.index(sum1)\n del a[c]\n elif(sum1+3 in a):\n c=a.index(sum1+3)\n del a[c]\n elif(sum1+6 in a):\n c=a.index(sum1+6)\n del a[c]\n elif(sum1==1):\n if(2 in a):\n c=a.index(2)\n del a[c]\n if(2 in a):\n c=a.index(2)\n del a[c]\n elif(5 in a):\n c=a.index(5)\n del a[c]\n else:\n c=a.index(8)\n del a[c]\n elif(5 in a):\n c=a.index(5)\n del a[c]\n if(5 in a):\n c=a.index(5)\n del a[c]\n elif(8 in a):\n c=a.index(8)\n del a[c]\n elif(8 in a):\n c=a.index(8)\n del a[c]\n if(8 in a):\n c=a.index(8)\n del a[c]\n elif(sum1==2):\n if(1 in a):\n c=a.index(1)\n del a[c]\n if(1 in a):\n c=a.index(1)\n del a[c]\n elif(4 in a):\n c=a.index(4)\n del a[c]\n else:\n c=a.index(7)\n del a[c]\n elif(4 in a):\n c=a.index(4)\n del a[c]\n if(4 in a):\n c=a.index(4)\n del a[c]\n elif(7 in a):\n c=a.index(7)\n del a[c]\n elif(7 in a):\n c=a.index(7)\n del a[c]\n if(7 in a):\n c=a.index(7)\n del a[c]\n num=int(''.join(map(str,a)))\n if(0 not in a):\n num=-1\n print(num)\n n-=1 ", "t = eval(input())\nwhile t:\n n = eval(input())\n z = list(map(int,input().split()))\n a = [0]*10\n for i in z:\n a[i] += 1\n if a[0] == 0:\n print(-1)\n \n else:\n no_1 = a[1] + a[4] + a[7]\n no_2 = a[2] + a[5] + a[8]\n s = ''\n if (no_1 + no_2*2)%3 == 0:\n s = ''\n elif no_2 > no_1:\n no = (no_2 - no_1)%3\n if no_2 == 1 and no_1 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n elif no == 2:\n if no_1 > 0:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 2\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n elif no_2 < no_1:\n no = (no_1 - no_2)%3\n if no_1 == 1 and no_2 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n elif no == 2:\n if no_2 > 0:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 1\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n for i in range(9,-1,-1):\n s += str(i)*a[i]\n \n if len(s) == 0:\n print(-1)\n else:\n print(s)\n t -= 1", "t = eval(input())\nwhile t:\n n = eval(input())\n z = list(map(int,input().split()))\n a = [0]*10\n for i in z:\n a[i] += 1\n if a[0] == 0:\n print(-1)\n \n else:\n no_1 = a[1] + a[4] + a[7]\n no_2 = a[2] + a[5] + a[8]\n s = ''\n if no_2 > no_1:\n no = (no_2 - no_1)%3\n if no_2 == 1 and no_1 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n elif no == 2:\n if no_1 > 0:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 2\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n elif no_2 < no_1:\n no = (no_1 - no_2)%3\n if no_1 == 1 and no_2 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n elif no == 2:\n if no_2 > 0:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 1\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n for i in range(9,-1,-1):\n s += str(i)*a[i]\n \n if len(s) == 0:\n print(-1)\n else:\n print(s)\n t -= 1", "t = eval(input())\nwhile t:\n n = eval(input())\n z = list(map(int,input().split()))\n a = [0]*10\n for i in z:\n a[i] += 1\n if a[0] == 0:\n print(-1)\n else:\n no_1 = a[1] + a[4] + a[7]\n no_2 = a[2] + a[5] + a[8]\n s = ''\n if no_2 > no_1:\n no = (no_2 - no_1)%3\n if no_2 == 1 and no_1 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n else:\n if no_1 > 0:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 2\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n \n elif no_2 < no_1:\n no = (no_1 - no_2)%3\n if no_1 == 1 and no_2 == 0:\n a[2],a[5],a[8],a[1],a[4],a[7] = 0,0,0,0,0,0\n elif no == 1:\n for k in range(1,9,3):\n if a[k] != 0:\n a[k] -= 1\n break \n else:\n if no_2 > 0:\n for k in range(2,9,3):\n if a[k] != 0:\n a[k] -= 1\n break\n else:\n j = 2\n i = 1\n while j > 0:\n if a[i] >= j:\n a[i] -= j\n j = 0\n else:\n j -= a[i]\n a[i] = 0\n i+= 3\n for i in range(9,-1,-1):\n s += str(i)*a[i]\n if len(s) == 0:\n print(-1)\n else:\n print(int(s))\n t -= 1", "t=int(input())\nfor i in range(t):\n n=int(input())\n x=list(map(int,input().split()))\n total=sum(x)\n x.sort()\n x.reverse()\n dic={}\n b1,b2,b0=[],[],[]\n for j in range(n):\n if x[j]!=0:\n if x[j]%3==0:\n b0.append(x[j])\n elif x[j]%3==1:\n b1.append(x[j])\n elif x[j]%3==2:\n b2.append(x[j])\n if x[j] in dic:\n dic[x[j]]+=1\n else:\n dic[x[j]]=1\n if 0 in dic:\n if total%3==0:\n ans=int(''.join(str(y) for y in x))\n print(ans)\n elif total%3==1:\n flag=1\n if len(b1)>=1:\n x.remove(b1[-1])\n elif len(b2)>=2:\n x.remove(b2[-1])\n x.remove(b2[-2])\n else:\n flag=0\n if flag==1:\n ans=int(''.join(str(y) for y in x))\n print(ans)\n else:\n print(\"-1\")\n elif total%3==2:\n flag=1\n if len(b2)>=1:\n x.remove(b2[-1])\n elif len(b1)>=2:\n x.remove(b1[-1])\n x.remove(b1[-2])\n else:\n flag=0\n if flag==1:\n ans=int(''.join(str(y) for y in x))\n print(ans)\n else:\n print(\"-1\")\n else:\n print(\"-1\")", "t=int(input())\nfor i in range(t):\n n=int(input())\n x=list(map(int,input().split()))\n total=sum(x)\n x.sort()\n x.reverse()\n dic={}\n b1,b2,b0=[],[],[]\n for j in range(n):\n if x[j]!=0:\n if x[j]%3==0:\n b0.append(x[j])\n elif x[j]%3==1:\n b1.append(x[j])\n elif x[j]%3==2:\n b2.append(x[j])\n if x[j] in dic:\n dic[x[j]]+=1\n else:\n dic[x[j]]=1\n if 0 in dic:\n if total%3==0:\n print(''.join(str(y) for y in x))\n elif total%3==1:\n flag=1\n if len(b1)>=1:\n x.remove(b1[-1])\n elif len(b2)>=2:\n x.remove(b2[-1])\n x.remove(b2[-2])\n else:\n flag=0\n if flag==1:\n print(''.join(str(y) for y in x))\n else:\n print(\"-1\")\n elif total%3==2:\n flag=1\n if len(b2)>=1:\n x.remove(b2[-1])\n elif len(b1)>=2:\n x.remove(b1[-1])\n x.remove(b1[-2])\n else:\n flag=0\n if flag==1:\n print(''.join(str(y) for y in x))\n else:\n print(\"-1\")\n else:\n print(\"-1\")", "def make_div_3(a):\n d = {\n 1 : [1, 4, 7],\n 2 : [2, 5, 8]\n }\n rem = sum(a) % 3\n while rem:\n l1 = d[rem]\n i = sorted(list(set(a).intersection(l1)))\n if i:\n a.remove(i[0])\n else:\n l2 = d[2 if rem == 1 else 1]\n i = sorted(list(set(a).intersection(l2)))\n if i:\n a.remove(i[0])\n else:\n return -1\n rem = sum(a) % 3\n if len(a) == a.count(0):\n return [0]\n return a\n\ndef __starting_point():\n t = int(input())\n while t:\n n = int(input())\n a = [int(i) for i in input().split()]\n if not 0 in a:\n print(-1)\n else:\n sa = sorted(a)\n sa.reverse()\n a = make_div_3(sa)\n if a == -1:\n print(a)\n else:\n print(''.join([str(i) for i in a]))\n t -= 1\n\n__starting_point()", "def check1(array):\n last=[0 for i in range(10)]\n for i in range(10):\n last[i]=array[i]-array[i]%3\n array[i]=array[i]%3\n j=9\n while j>=0:\n if j%3==0:\n last[j]+=array[j]\n else:\n for i in range(j-1,0,-1):\n if (j+i)%3==0:\n while array[j]!=0 and array[i]!=0:\n last[j]+=1\n last[i]+=1\n array[j]-=1\n array[i]-=1\n j-=1\n ans=[str(i)*last[i] for i in range(10)]\n return int(''.join(ans[::-1]))\n\ndef check2(array):\n last=[0 for i in range(10)]\n j=9\n while j>=0:\n if j%3==0:\n last[j]=array[j]\n array[j]=0\n else:\n for i in range(j-1,0,-1):\n if (j+i)%3==0:\n while array[j]!=0 and array[i]!=0:\n last[j]+=1\n last[i]+=1\n array[j]-=1\n array[i]-=1\n j-=1\n for i in range(10):\n last[i]+=array[i]-array[i]%3\n ans=[str(i)*last[i] for i in range(10)]\n return int(''.join(ans[::-1]))\n \n\n\n\nT=int(input().strip())\nfor i in range(T):\n N=int(input().strip())\n array=[0 for i in range(10)]\n arr=list(map(int,input().strip().split()))\n for j in arr:\n array[j]+=1\n if array[0]==0:\n print(-1)\n else:\n array1=array[:]\n a=check1(array)\n b=check2(array1)\n print(max(a,b))\n \n", "def check1(array):\n last=[0 for i in range(10)]\n last[8]+=min(array[8],array[1])\n last[1]=min(array[8],array[1])\n array[1]-=min(array[8],array[1])\n last[7]+=min(array[7],array[2])\n last[2]+=min(array[7],array[2])\n array[2]-=min(array[7],array[2])\n while array[5]!=0 and array[4]!=0:\n last[5]+=1\n last[4]+=1\n array[5]-=1\n array[4]-=1\n while array[5]!=0 and array[1]!=0:\n last[5]+=1\n last[1]+=1\n array[5]-=1\n array[1]-=1\n last[4]+=min(array[4],array[2])\n last[2]+=min(array[4],array[2])\n array[2]-=min(array[4],array[2])\n last[2]+=min(array[1],array[2])\n last[1]+=min(array[1],array[2])\n temp=array[1]\n array[1]-=min(array[1],array[2])\n array[2]-=min(temp,array[2])\n for i in range(10):\n if i%3==0:\n last[i]+=array[i]\n else:\n last[i]+=(array[i]-array[i]%3)\n ans=[str(i)*last[i] for i in range(10)]\n return int(''.join(ans[::-1]))\n\ndef check2(array):\n last=[0 for i in range(10)]\n for i in range(10):\n if i%3==0:\n last[i]+=array[i]\n else:\n last[i]+=(array[i]-array[i]%3)\n array[i]-=last[i]\n last[8]+=min(array[8],array[1])\n last[1]+=min(array[8],array[1])\n array[1]-=min(array[8],array[1])\n last[7]+=min(array[7],array[2])\n last[2]+=min(array[7],array[2])\n array[2]-=min(array[7],array[2])\n while array[5]!=0 and array[4]!=0:\n last[5]+=1\n last[4]+=1\n array[5]-=1\n array[4]-=1\n while array[5]!=0 and array[1]!=0:\n last[5]+=1\n last[1]+=1\n array[5]-=1\n array[1]-=1\n last[4]+=min(array[4],array[2])\n last[2]+=min(array[4],array[2])\n array[2]-=min(array[4],array[2])\n last[2]+=min(array[1],array[2])\n last[1]+=min(array[1],array[2])\n temp=array[1]\n array[1]-=min(array[1],array[2])\n array[2]-=min(temp,array[2])\n ans=[str(i)*last[i] for i in range(10)]\n return int(''.join(ans[::-1]))\n\n\nT=int(input().strip())\nfor i in range(T):\n N=int(input().strip())\n array=[0 for i in range(10)]\n arr=list(map(int,input().strip().split()))\n for j in arr:\n array[j]+=1\n if array[0]==0:\n print(-1)\n else:\n array1=array[:]\n a=check1(array)\n b=check2(array1)\n print(max(a,b))\n \n \n", "t = int(input())\nl1 = [[8,7],[8,4],[8,1],[7,5],[7,2],[5,4],[5,1],[4,2],[2,1]]\nl2 = [[8,5],[8,2],[7,4],[7,1],[5,8],[5,2],[4,7],[4,1],[2,8],[2,5],[1,7],[1,4]]\nlength = 9\nwhile t > 0 :\n t -= 1\n d = {}\n d2 = {}\n fin = {}\n n = int(input())\n a = list(map(int,input().split()))\n bol = True\n for k in range(10) :\n d[k] = 0\n d2[k] = 0\n fin[k] = 0\n for i in range(n) :\n d[a[i]] += 1\n if bol and a[i] == 0 :\n bol = False\n if bol :\n print('-1')\n else :\n ans = ''\n for a,b in l1 :\n z = min(d[a],d[b])\n if z % 2 == 1 :\n z -= 1\n fin[a] += z\n fin[b] += z\n d[a] -= z\n d[b] -= z\n for j in range(10) :\n if j % 3 != 0 :\n d2[j] = d[j]%3\n d[j] -= d2[j]\n\n for a,b in l2 :\n if d2[a] == 2 and d2[b] >= 1 :\n d[a] += 2\n d[b] += 1\n d2[a] -= 2\n d2[b] -= 1\n for a,b in l1 :\n z = min(d2[a],d2[b])\n if z > 0 :\n d[a] += z\n d[b] += z\n d2[a] -= z\n d2[b] -= z\n\n for _ in range(9,-1,-1) :\n ans += str(_)*(d[_]+fin[_])\n if ans == '0'*d[0] :\n print(0)\n else :\n print(ans)\n", "t = int(input())\nl1 = [[8,7],[8,4],[8,1],[7,5],[7,2],[5,4],[5,1],[4,2],[2,1]]\nl2 = [[8,5],[8,2],[7,4],[7,1],[5,8],[5,2],[4,7],[4,1],[2,8],[2,5],[1,7],[1,4]]\nlength = 9\nwhile t > 0 :\n t -= 1\n d = {}\n d2 = {}\n n = int(input())\n a = list(map(int,input().split()))\n bol = True\n for k in range(10) :\n d[k] = 0\n d2[k] = 0\n for i in range(n) :\n d[a[i]] += 1\n if bol and a[i] == 0 :\n bol = False\n if bol :\n print('-1')\n else :\n ans = ''\n for j in range(10) :\n if j % 3 != 0 :\n d2[j] = d[j]%3\n d[j] -= d2[j]\n for a,b in l1 :\n z = min(d2[a],d2[b])\n if z == 2 :\n d[a] += z\n d[b] += z\n d2[a] -= z\n d2[b] -= z\n\n for a,b in l2 :\n if d2[a] == 2 and d2[b] >= 1 :\n d[a] += 2\n d[b] += 1\n d2[a] -= 2\n d2[b] -= 1\n for a,b in l1 :\n z = min(d2[a],d2[b])\n if z > 0 :\n d[a] += z\n d[b] += z\n d2[a] -= z\n d2[b] -= z\n\n for _ in range(9,-1,-1) :\n ans += str(_)*d[_]\n if ans == '0'*d[0] :\n print(0)\n else :\n print(ans)\n", "t = int(input())\nl1 = [[8,7],[8,4],[8,1],[7,5],[7,2],[5,4],[5,1],[4,2],[2,1]]\nl2 = [[8,5],[8,2],[7,4],[7,1],[5,8],[5,2],[4,7],[4,1],[2,8],[2,5],[1,7],[1,4]]\nlength = 9\nwhile t > 0 :\n t -= 1\n d = {}\n d2 = {}\n n = int(input())\n a = list(map(int,input().split()))\n bol = True\n for k in range(10) :\n d[k] = 0\n d2[k] = 0\n for i in range(n) :\n d[a[i]] += 1\n if bol and a[i] == 0 :\n bol = False\n if bol :\n print('-1')\n else :\n ans = ''\n for j in range(10) :\n if j % 3 != 0 :\n d2[j] = d[j]%3\n d[j] -= d2[j]\n for a,b in l1 :\n z = min(d2[a],d2[b])\n if z == 2 :\n d[a] += z\n d[b] += z\n d2[a] -= z\n d2[b] -= z\n\n for a,b in l2 :\n if d2[a] == 2 and d2[b] >= 1 :\n d[a] += 2\n d[b] += 1\n d2[a] -= 2\n d2[b] -= 1\n for a,b in l1 :\n z = min(d2[a],d2[b])\n if z > 0 :\n d[a] += z\n d[b] += z\n d2[a] -= z\n d2[b] -= z\n\n for _ in range(9,-1,-1) :\n ans += str(_)*d[_]\n print(ans)\n", "# your code goes here\n# your code goes here\ntest_cases = int(input())\nfor i in range(0,test_cases):\n num_digits = int(input())\n input_str = input()\n digits_old = input_str.split()\n digits = []\n num_zero = 0\n for num in digits_old:\n if num == '0':\n num_zero += 1 \n digits.append(int(num))\n if num_zero == 0:\n print(-1)\n else:\n digits.sort(reverse = True)\n flag = 1\n que0 = []\n que1 = []\n que2 = []\n sum_digits = 0\n for num in digits:\n if num%3 == 0:\n que0.append(num)\n if num%3 == 1:\n que1.append(num)\n if num%3 == 2:\n que2.append(num)\n sum_digits += num\n if sum_digits%3 == 0:\n number = 0\n elif sum_digits%3 == 1:\n if len(que1) > 0:\n digits.remove(que1[len(que1)-1])\n elif len(que2) > 1:\n digits.remove(que2[len(que2)-1])\n digits.remove(que2[len(que2)-2])\n else:\n print(-1)\n flag = 0\n else:\n if len(que2) > 0:\n digits.remove(que2[len(que2)-1])\n elif len(que1) > 1:\n digits.remove(que1[len(que1)-1])\n digits.remove(que1[len(que1)-2])\n else:\n print(-1)\n flag = 0\n if flag == 1:\n number = 0\n for num in digits:\n number = number*10 + num\n print(number)", "# your code goes here\ntest_cases = int(input())\nfor i in range(0,test_cases):\n num_digits = int(input())\n input_str = input()\n digits_old = input_str.split()\n digits = []\n num_zero = 0\n for num in digits_old:\n if num == '0':\n num_zero += 1 \n digits.append(int(num))\n if num_zero == 0:\n print(-1)\n else:\n digits.sort(reverse = True)\n start = True\n flag = 1\n while start or number != 0:\n start = False\n number = 0\n sum_digits = 0\n for num in digits:\n sum_digits += num\n number = number*10 + num\n if sum_digits%3 == 0:\n print(number)\n flag = 0\n break\n if digits[num_digits-1-num_zero] in [0,3,6,9]:\n num_zero += 1\n continue\n del digits[num_digits-1-num_zero]\n num_digits -= 1\n if flag == 1:\n print(0)"]
{"inputs": [["2", "12", "3 1 2 3 2 0 2 2 2 0 2 3", "11", "3 9 9 6 4 3 6 4 9 6 0"]], "outputs": [["33322222200", "999666330"]]}
INTERVIEW
PYTHON3
CODECHEF
23,354
1d9aa9c585e8f140147654c582156641
UNKNOWN
Chef has a nice complete binary tree in his garden. Complete means that each node has exactly two sons, so the tree is infinite. Yesterday he had enumerated the nodes of the tree in such a way: - Let's call the nodes' level a number of nodes that occur on the way to this node from the root, including this node. This way, only the root has the level equal to 1, while only its two sons has the level equal to 2. - Then, let's take all the nodes with the odd level and enumerate them with consecutive odd numbers, starting from the smallest levels and the leftmost nodes, going to the rightmost nodes and the highest levels. - Then, let's take all the nodes with the even level and enumerate them with consecutive even numbers, starting from the smallest levels and the leftmost nodes, going to the rightmost nodes and the highest levels. - For the better understanding there is an example: 1 / \ 2 4 / \ / \ 3 5 7 9 / \ / \ / \ / \ 6 8 10 12 14 16 18 20 Here you can see the visualization of the process. For example, in odd levels, the root was enumerated first, then, there were enumerated roots' left sons' sons and roots' right sons' sons. You are given the string of symbols, let's call it S. Each symbol is either l or r. Naturally, this sequence denotes some path from the root, where l means going to the left son and r means going to the right son. Please, help Chef to determine the number of the last node in this path. -----Input----- The first line contains single integer T number of test cases. Each of next T lines contain a string S consisting only of the symbols l and r. -----Output----- Per each line output the number of the last node in the path, described by S, modulo 109+7. -----Constraints----- - 1 ≤ |T| ≤ 5 - 1 ≤ |S| ≤ 10^5 - Remember that the tree is infinite, so each path described by appropriate S is a correct one. -----Example----- Input: 4 lrl rll r lllr Output: 10 14 4 13 -----Explanation----- See the example in the statement for better understanding the samples.
["# cook your dish here\nMOD=10**9+7\nfor _ in range(int(input())):\n s=input()\n ind=1\n level=1\n for i in range(len(s)):\n if s[i]=='l':\n if level%2==1:\n ind=ind*2\n else:\n ind=ind*2-1\n if s[i]=='r':\n if level%2==1:\n ind=ind*2+2\n else:\n ind=ind*2+1\n level+=1\n ind%=MOD\n print(ind)\n", "\r\n\r\nMOD = 10 ** 9 + 7\r\nfor _ in range(int(input())):\r\n s = input()\r\n ind = 1\r\n level = 1\r\n for i in range(len(s)):\r\n if s[i] == 'l':\r\n if level % 2 == 1:\r\n ind = ind * 2\r\n else:\r\n ind = ind * 2 - 1\r\n if s[i] == 'r':\r\n if level % 2 == 1:\r\n ind = ind * 2 + 2\r\n else:\r\n ind = ind * 2 + 1\r\n level += 1\r\n ind %= MOD\r\n print(ind)\r\n", "mod = 1000000007\r\nfor _ in range(int(input())):\r\n string = list(input().strip())\r\n idx = 1\r\n lev = 1\r\n for i in (string):\r\n if i == 'l':\r\n if lev % 2 == 1:\r\n idx = (idx * 2)\r\n else:\r\n idx = (idx * 2 - 1)\r\n else:\r\n if lev % 2 == 1:\r\n idx = (idx * 2 + 2)\r\n else:\r\n idx = (idx * 2 + 1)\r\n lev += 1\r\n idx = idx % mod\r\n print(idx)", "t = int(input())\r\nfor i in range(t):\r\n s = input().strip()\r\n\r\n level =1\r\n ans = 1\r\n for i in s:\r\n\r\n if(level%2==0):\r\n if(i==\"l\"):\r\n ans = 2*ans-1\r\n else:\r\n ans = 2*ans+1\r\n\r\n else:\r\n if(i==\"l\"):\r\n ans = 2*ans\r\n\r\n else:\r\n\r\n ans = 2*ans +2\r\n ans = ans%1000000007\r\n level +=1\r\n\r\n print(ans)\r\n \r\n", "mod=1000000007\r\nfor _ in range(int(input())):\r\n s=input().strip()\r\n level=1\r\n ans=1\r\n for a in s:\r\n if level%2!=0:\r\n if a==\"l\":\r\n ans=2*ans\r\n else:\r\n ans=2*ans+2\r\n else:\r\n if a==\"l\":\r\n ans=2*ans-1\r\n else:\r\n ans=2*ans+1\r\n ans=ans%mod\r\n level+=1 \r\n\r\n\r\n print(ans)", "mod=1000000007\r\nfor _ in range(int(input())):\r\n s=input().strip()\r\n level=1\r\n ans=1\r\n for a in s:\r\n if level%2!=0:\r\n if a==\"l\":\r\n ans=2*ans\r\n else:\r\n ans=2*ans+2\r\n else:\r\n if a==\"l\":\r\n ans=2*ans-1\r\n else:\r\n ans=2*ans+1\r\n ans=ans%mod\r\n level+=1 \r\n\r\n\r\n print(ans)", "T=int(input())\r\nfor i in range(T):\r\n\ts=input().strip()\r\n\tini=1\r\n\tfor j in range(len(s)):\r\n\t\tini*=2\r\n\t\tif j%2==0:\t\t\t\r\n\t\t\tif(s[j]=='r'):\r\n\t\t\t\tini+=2\r\n\t\telse:\r\n\t\t\tif(s[j]=='l'):\r\n\t\t\t\tini-=1\r\n\t\t\telse:\r\n\t\t\t\tini+=1\r\n\t\tini%=(10**9+7)\r\n\tprint(ini%(10**9+7))", "t=int(input())\r\nfor i in range(t):\r\n s=input().strip()\r\n x=1\r\n a=1000000007\r\n for j in range(len(s)):\r\n if(s[j]=='l' and j%2==0):\r\n x=(x*2)%a\r\n elif(s[j]=='l' and j%2!=0):\r\n x=(x*2-1)%a\r\n elif(s[j]=='r' and j%2==0):\r\n x=(x*2+2)%a\r\n else:\r\n x=(x*2+1)%a\r\n print(x%a) \r\n \r\n "]
{"inputs": [["4", "lrl", "rll", "r", "lllr", ""]], "outputs": [["10", "14", "4", "13"]]}
INTERVIEW
PYTHON3
CODECHEF
3,643
6a6ba32420fe2445951534860fc213f0
UNKNOWN
Divya's watch of worth Rs10 cr is abducted by N thieves(1,2....i...N). The fight over the watch leads to a final decision that it should belong to the thief who wins a simple game. The rules of the game state that every thief registers a time in the format HH:MM:SS . Accordingly the average A of three clockwise angles between the hours , minutes and seconds hands is calculated . Thus the ith thief with the maximum A wins the game and gets to keep the watch. The thieves are poor in mathematics and will need your help . Given the number of thieves and their registered time resolves the conflict and help them in choosing the winner -----Input----- First line of input contains T which denotes the number of test cases. The first line of each test case consists of an integer which denotes the number of thieves thereby N line follow which give the time choosen by each thieve in the format HH:MM:SS. -----Output:----- Output single integer i which denotes the ith thief. -----Constraints:----- 1<=T<=100 1<=N<=50 01<=HH<=12 00<=MM<=60 00<=SS<=60 -----Example:----- Input: 2 3 12:28:26 07:26:04 11:23:17 2 07:43:25 06:23:34 Output: 3 1
["for t in range(int(input())):\n n = int(input())\n mx = -1\n for i in range(n):\n h, m, s = list(map(int,input().split(\":\")))\n h %= 12\n m %= 60\n s %= 60\n ha = h*30 + m*0.5 + s*0.5/60\n ma = m*6 + s*0.1\n sa = s*6\n \n hm1 = abs(ha - ma)\n hm2 = 360 - hm1\n hm3 = abs(hm1 - hm2)\n hm = min(hm1, hm2, hm3)\n \n ms1 = abs(ma - sa)\n ms2 = 360 - ms1\n ms3 = abs(ms1 - ms2)\n ms = min(ms1, ms2, ms3)\n \n sh1 = abs(sa - ha)\n sh2 = 360 - sh1\n sh3 = abs(sh1 - sh2)\n sh = min(sh1, sh2, sh3)\n \n avg = (hm + ms + sh) / 3\n if (mx < avg):\n ans = i+1\n mx = avg\n print(ans)", "# your code goes here\nfrom sys import stdin, stdout\nt = int(stdin.readline())\nwhile t:\n t -= 1\n n = int(stdin.readline())\n maxi = -1\n index = 0\n for i in range(n):\n hh,mm,ss = list(map(int, stdin.readline().strip().split(':')))\n ssa = ss * 360.0 / 60.0\n mma = (mm + (ss / 60.0)) * 360.0 / 60.0\n hha = (hh + (mm + (ss / 60.0)) / 60.0) * 360.0 / 12.0\n avg = (abs(ssa-hha) + abs(mma-ssa) + abs(mma-hha)) / 3\n # print hha, mma, ssa\n if avg > maxi:\n maxi = avg\n index = i\n stdout.write(str(index+1)+'\\n')\n"]
{"inputs": [["2", "3", "12:28:26", "07:26:04", "11:23:17", "2", "07:43:25", "06:23:34"]], "outputs": [["3", "1"]]}
INTERVIEW
PYTHON3
CODECHEF
1,142
619349a52ded51453fe9ced7fbd5a74d
UNKNOWN
Ash is on his way to becoming the Pokemon Master. His pokemon can perform the following moves: - Tackle - Deal damage worth $X$ points - Grow - Increase damage by $Y$ points i.e. $X$ = $X$ + $Y$ But, it can only perform Grow first (0 or more times) and then tackle (0 or more) times after which it cannot perform Grow again. That is, it cannot perform the Grow operation once it has performed the tackle operation. A pokemon can be caught only if it’s health is exactly 1. A wild pokemon has appeared and has health worth $H$ points. Find the minimum number of moves required to catch it or say that it is not possible. -----Input:----- - The first line of the input consists of a single integer $T$ denoting the number of test cases. - Each test case consists of 3 space-separated integers $H$, $X$ and $Y$. -----Output:----- - For each test case, print a single line containing one integer - the minimum number of moves required to catch the pokemon if it is possible to catch it else print -1. -----Constraints----- - 1 <= $T$ <= 103 - 1 <= $X$, $Y$ < $H$ <= 109 -----Subtasks----- Subtask #1 (30 points): - 1 <= $X$, $Y$ < $H$ <= 1000 Subtask #2 (70 points): - Original Constraints -----Sample Input:----- 2 101 10 10 11 3 3 -----Sample Output:----- 6 -1 -----EXPLANATION:----- - Example Case 1: Ash can make use of Grow once. So $X$ = 10 + 10 = 20 Then he can do Tackle 5 times to decrease $H$ to 1. OR Ash can make use of Grow 4 times. So $X$ = 10 + 4*10 = 50 Then he can do Tackle 2 times to decrease $H$ to 1. Hence, a total of 6 moves are required either way which is minimum. - Example Case 2: No matter how many times Ash uses Grow or Tackle, pokemon can never be caught.
["def Testcase():\n h,x,y = [int(x) for x in input().strip().split()]\n \n h = h-1\n yt = h//y +1\n # print(yt)\n flag=0\n ans = 100000000009\n \n for i in range(0,yt):\n temp = x+i*y\n if h%temp==0:\n flag = 1\n cl =i+int(h/temp)\n # print(temp,cl)\n ans = min(ans,cl)\n # print(temp,ans,i)\n print(ans if flag==1 else '-1')\n \n \nt = int(input())\nwhile t>0:\n Testcase()\n \n t-=1", "# cook your dish here\n\nt = int(input())\nfor rep in range(t):\n h,x,y = list(map(int,input().split()))\n req = h - 1\n mini = 1000000000\n g = 0\n t = 0\n base = x\n f = 0\n while base <= req:\n if req % base == 0:\n f = 1\n t = req // base\n if g + t < mini:\n mini = g + t\n base = base + y\n g = g + 1\n if f == 1:\n print(mini)\n else:\n print(-1)\n", "# cook your dish here\ntry:\n from functools import reduce\n\n def factors(n): \n return set(reduce(list.__add__, \n ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))\n b = ()\n test = int(input())\n for i in range (test):\n flag = 0\n move = []\n h , x , y = list(map(int,input().split(' ')))\n b = factors(h-1)\n h -= 1\n if(h == x):\n print(\"0\")\n else:\n for j in b:\n if(j >= x):\n if(j==x ):\n flag = 1 \n move.append(h//j)\n elif((j-x)%y == 0 ):\n flag = 1\n move.append(((j-x)//y + h/j ))\n \n \n if(flag == 0):\n print(\"-1\")\n else:\n print(int(min(move)%((10**9)+7)))\n \n \nexcept EOFError:\n\tpass", "# cook your dish here\ntry:\n from functools import reduce\n\n def factors(n): \n return set(reduce(list.__add__, \n ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))\n b = ()\n test = int(input())\n for i in range (test):\n flag = 0\n move = []\n h , x , y = list(map(int,input().split(' ')))\n b = factors(h-1)\n h -= 1\n if(h == x):\n print(\"0\")\n else:\n for j in b:\n if(j >= x):\n if(j==x ):\n flag = 1 \n move.append(h//j)\n elif((j-x)%y == 0 ):\n flag = 1\n move.append(((j-x)//y + h/j ))\n \n \n if(flag == 0):\n print(\"-1\")\n else:\n print(int(min(move)%((10**9)+7)))\n \n \nexcept EOFError:\n\tpass", "# cook your dish here\ntry:\n from functools import reduce\n\n def factors(n): \n return set(reduce(list.__add__, \n ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))\n b = ()\n test = int(input())\n for i in range (test):\n flag = 0\n move = []\n h , x , y = list(map(int,input().split(' ')))\n b = factors(h-1)\n h -= 1\n if(h == x):\n print(\"0\")\n else:\n for j in b:\n if(j >= x):\n if(j==x ):\n flag = 1 \n move.append(h//j)\n elif((j-x)%y == 0 ):\n flag = 1\n move.append(((j-x)//y + h/j ))\n \n \n if(flag == 0):\n print(\"-1\")\n else:\n print(int(min(move)%((10**9)+7)))\n \n \nexcept EOFError:\n\tpass", "# cook your dish here\ntry:\n from functools import reduce\n\n def factors(n): \n return set(reduce(list.__add__, \n ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))\n b = ()\n test = int(input())\n for i in range (test):\n flag = 0\n move = 1000\n h , x , y = list(map(int,input().split(' ')))\n b = factors(h-1)\n h -= 1\n if(h == x):\n print(\"0\")\n move = 999\n else:\n for j in b:\n if(j >= x):\n if(j==x ):\n flag = 1 \n if(move > h//j):\n move = h//j\n elif((j-x)%y == 0 ):\n flag = 1\n if(move > ((j-x)//y + h/j )):\n move = ((j-x)//y + h/j )\n \n \n if(flag == 0):\n print(\"-1\")\n else:\n print(int(move%((10**9)+7)))\n \n \nexcept EOFError:\n\tpass", "# cook your dish here\ntry:\n from functools import reduce\n\n def factors(n): \n return set(reduce(list.__add__, \n ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))\n b = ()\n test = int(input())\n for i in range (test):\n flag = 0\n move = 1000\n h , x , y = list(map(int,input().split(' ')))\n b = factors(h-1)\n h -= 1\n if(h == x):\n print(\"0\")\n move = 999\n else:\n for j in b:\n if(j >= x):\n if(j==x ):\n flag = 1 \n if(move > h//j):\n move = h//j\n elif((j-x)%y == 0 ):\n flag = 1\n if(move > ((j-x)//y + h/j )):\n move = ((j-x)//y + h/j )\n \n \n if(flag == 0):\n print(\"-1\")\n else:\n print(int(move)%((10**9)+7))\n \n \nexcept EOFError:\n\tpass", "# cook your dish here\ntry:\n from functools import reduce\n\n def factors(n): \n return set(reduce(list.__add__, \n ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))\n b = ()\n test = int(input())\n for i in range (test):\n flag = 0\n move = 1000\n h , x , y = list(map(int,input().split(' ')))\n b = factors(h-1)\n h -= 1\n if(h == x):\n print(\"0\")\n move = 999\n else:\n for j in b:\n if(j >= x):\n if(j==x ):\n flag = 1 \n if(move > h//j):\n move = h//j\n elif((j-x)%y == 0 ):\n flag = 1\n if(move > ((j-x)//y + h/j )):\n move = ((j-x)//y + h/j )\n \n \n if(flag == 0):\n print(\"-1\")\n else:\n print(int(move))\n \n \nexcept EOFError:\n\tpass", "# cook your dish here\n\nt = int(input())\nfor rep in range(t):\n h,x,y = list(map(int,input().split()))\n req = h - 1\n mini = 1000000000\n g = 0\n t = 0\n base = x\n f = 0\n while base <= req:\n if req % base == 0:\n f = 1\n t = req // base\n if g + t < mini:\n mini = g + t\n base = base + y\n g = g + 1\n if f == 1:\n print(mini)\n else:\n print(-1)\n", "from sys import stdin\r\nfrom functools import reduce\r\n\r\ndef factors(n): \r\n return list(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))\r\n\r\nfor _ in range(int(stdin.readline())):\r\n h, x, y = list(map(int, stdin.readline().split()))\r\n arr = factors(h-1)\r\n ans = 1000000001\r\n cond = False\r\n for i in range(len(arr)):\r\n if arr[i]>=x:\r\n m = arr[i]-x\r\n if m%y == 0:\r\n cond = True\r\n ans = min(ans, m//y + (h-1)//arr[i])\r\n if cond == True:\r\n print(ans)\r\n else:\r\n print(-1)\r\n \r\n \r\n", "# cook your dish here\nimport math \n \ndef printDivisors(n,X,l) : \n list = [] \n for i in range(1, int(math.sqrt(n) + 1)) : \n if (n % i == 0) : \n if (n / i == i) : \n list.append(i)\n a=0\n else : \n list.append(i)\n list.append(int(n / i)) \n \n for i in list[::-1] :\n if(i-X>=0):\n l.append(i)\n \n\nT=int(input())\nfor _ in range(T):\n H,X,Y=list(map(int,input().split()))\n l=[]\n printDivisors(H-1,X,l)\n mv=2*H\n for i in l:\n a=(i-X)//Y\n if(a*Y==(i-X)):\n v=((H-1)//i)+a\n if(v<mv):\n mv=v\n\n if(mv>H):\n print(-1)\n\n else:\n print(mv)\n \n", "from math import sqrt\r\n\r\nINF = float('inf')\r\n\r\ndef divs(n):\r\n d = []\r\n\r\n for i in range(1, int(sqrt(n)) + 1):\r\n if n % i == 0:\r\n d.append(i)\r\n\r\n if n // i != i:\r\n d.append(n // i)\r\n\r\n return d\r\n\r\nfor _ in range(int(input())):\r\n h, x, y = map(int, input().split())\r\n h -= 1\r\n\r\n d = divs(h)\r\n\r\n mini = INF\r\n\r\n for i in d:\r\n if i - x >= 0 and (i - x) % y == 0:\r\n f = (i - x) // y + h // i\r\n\r\n mini = min(mini, f)\r\n\r\n if mini == INF:\r\n print(-1)\r\n else:\r\n print(mini)"]
{"inputs": [["2", "101 10 10", "11 3 3"]], "outputs": [["6", "-1"]]}
INTERVIEW
PYTHON3
CODECHEF
9,967
f59c1801f26eb3e916fc3a687cc21d72
UNKNOWN
Given $N *M$ matrix containing elements either $1$ or $0$ and string S of length $N+M-1$ containing characters $0$ or $1$. Your task is to make all the paths from top left corner to the bottom right corner of the matrix same as the given string .You can perform two types of operations any time .Path means you can only allow it to take right or down. Operations : - Changing the matrix elements from $1$ to $0$ or vice versa will cost P rupees per element. - Changing the character of string from $1$ to $0$ or vice versa will cost Q rupees per character. You have to minimize the cost, (possibly 0) . -----Input:----- - First line of input contains the total no. of test cases $T$. - For every test case, first line of input contains two spaced positive integers, $N$ and $M$. - Next $N$ lines contains $M$-spaced integers which can be only $0$ or $1$. - Next line of input contains a string $S$ of length $N+M-1$. - Last line of input contains two spaced integers, $P$ and $Q$. -----Output:----- - $You$ $have$ $to$ $print$ $the$ $minimum$ $cost .$ -----Constraints----- - $1 \leq T \leq 20$ - $1 \leq N, M \leq 1000$ - $|S| = N+M-1$ - $0 \leq P, Q \leq 1000$The input/output is quite large, please use fast reading and writing methods. -----Sample Input----- 2 3 3 1 0 1 0 1 1 1 1 0 10111 10 5 3 3 0 0 1 0 1 1 0 1 1 00011 2 9 -----Sample Output----- 5 4 -----Explanation----- - You can change the last element of the matrix and also can change the last element of string but the minimum cost will produce by changing string element , therefore it will cost 5 rupees.
["from sys import stdin,stdout\nimport math,bisect\nfrom collections import Counter,deque,defaultdict\nL=lambda:list(map(int, stdin.readline().strip().split()))\nM=lambda:list(map(int, stdin.readline().strip().split()))\nI=lambda:int(stdin.readline().strip())\nS=lambda:stdin.readline().strip()\nC=lambda:stdin.readline().strip().split()\ndef pr(a):return(\" \".join(list(map(str,a))))\n#_________________________________________________#\n\n\ndef solve():\n n, m = M()\n a = []\n for i in range(n):\n a += [L()]\n s = S()\n p, q = M()\n ans = [[0,0] for i in range(n+m)]\n for i in range(n):\n for j in range(m):\n if a[i][j]==0:\n ans[i+j][0]+=1\n else:\n ans[i+j][1]+=1\n c = 0\n for i in range(n+m-1):\n A,B,C,D = 0,0,0,0\n if s[i]=='0':\n A = ans[i][1]*p\n B = q + ans[i][0]*p\n c+=min(A,B)\n else:\n C = ans[i][0]*p\n D = q + ans[i][1]*p\n c+=min(C,D)\n print(c)\nfor _ in range(I()):\n solve()\n", "# cook your dish here\n# code by RAJ BHAVSAR\nimport sys\ndef get_ints(): return map(int, sys.stdin.readline().strip().split())\ndef get_list(): return list(map(int, sys.stdin.readline().strip().split()))\ndef get_string(): return sys.stdin.readline().strip()\n\ndef row_helper(data,s,p,q):\n t1 = 0\n for i in range(len(data[0])):\n if(data[0][i] != int(s[i])):\n t1 += min(p,q)\n return t1\n\ndef col_helper(data,s,p,q):\n t1 = 0\n for i in range(len(data)):\n if(data[i][0] != int(s[i])):\n t1 += min(p,q)\n return t1\n\nfor _ in range(int(input())):\n row,col = get_ints()\n data = []\n for i in range(row):\n data.append(get_list())\n s = get_string()\n p,q = get_ints()\n if(row == 1):\n print(row_helper(data,s,p,q))\n else:\n if(col == 1):\n print(col_helper(data,s,p,q))\n else:\n ans = 0\n i = 0\n while(i < row):\n temp = []\n x,y = i,0\n while(x >= 0 and y < col):\n temp.append(data[x][y])\n x -= 1\n y += 1\n t1 = 0\n for l in temp:\n if(l != int(s[i])):\n t1 += p\n t2 = q\n for l in temp:\n if(l == int(s[i])):\n t2 += p\n ans += min(t1,t2)\n i += 1\n loop = i\n i -= 1\n j = 1\n while(j < col):\n temp = []\n x,y = i,j\n while(y < col and x >= 0):\n temp.append(data[x][y])\n y += 1\n x -= 1\n t1 = 0\n for l in temp:\n if(l != int(s[loop])):\n t1 += p\n t2 = q\n for l in temp:\n if(l == int(s[loop])):\n t2 += p\n ans += min(t1,t2)\n j += 1\n loop += 1\n print(ans)", "# cook your dish here\nimport math\n\nread = lambda : list(map(int,input().strip().split()))\nread_arr = lambda x: [read() for _ in range(x)]\nans = []\n\nt = int(input().strip())\n\ndef mincost(n,m,arr,s,p,q):\n cost = 0\n for t in range(n+m -1):\n cc = s[t]\n curr = 0\n ct = 0\n start = max(0,t-(m-1))\n end = min(n,t+1)\n for i in range(start,end):\n j = t - i;\n if j >= m:\n break;\n curr += (cc^(arr[i][j]))\n ct += 1\n cost1 = q + ((ct - curr) * p)\n cost2 = (curr * p)\n cost += min(cost1,cost2)\n return cost;\n \n\nfor i in range(t):\n cn,cm = read()\n carr = read_arr(cn)\n cs = [int(x) for x in input().strip()]\n cp,cq = read()\n ans.append(mincost(cn,cm,carr,cs,cp,cq))\nprint(\"\\n\".join([str(x) for x in ans]))\n", "from sys import *\ninput=stdin.readline\nfor u in range(int(input())):\n n,m=list(map(int,input().split()))\n l=[]\n for i in range(n):\n l.append(list(map(int,input().split())))\n s=input()\n x,y=list(map(int,input().split()))\n d=[0]*len(s)\n k=[0]*len(s)\n for i in range(n):\n for j in range(m):\n if(str(l[i][j])!=s[(i+j)%(n+m-1)]):\n d[(i+j)%(n+m-1)]+=1\n k[i+j]+=1\n c=0\n for i in range(len(s)):\n c+=min(d[i]*x,y+(k[i]-d[i])*x)\n print(c)\n", "# cook your dish here\nfor _ in range(int(input())):\n n, m = map(int, input().split())\n\n ml = []\n for i in range(n):\n ml.append(list(map(int, input().split())))\n s = input()\n p, q = map(int, input().split())\n nl = [0]*(n+m-1)\n pl = [0]*(n+m-1)\n for i in range(n):\n for j in range(m):\n if ml[i][j] != int(s[i+j]):\n nl[i+j] += 1\n else:\n pl[i+j] += 1\n c = 0\n for i in range(len(nl)):\n c += min(nl[i]*p, pl[i]*p + q)\n print(c)", "# cook your dish here\nt=int(input())\nwhile(t):\n n,m=map(int, input().split())\n a=[]\n for i in range(n):\n a.append(list(map(int, input().split())))\n # print(a)\n s=input()\n p,q=map(int, input().split())\n a0=[]\n a1=[]\n for i in range(n+m-1):\n a0.append(0)\n a1.append(0)\n for i in range(n):\n for j in range(m):\n if(a[i][j]==0):\n a0[i+j]+=1\n else:\n a1[i+j]+=1\n cost=0\n for i in range(len(s)):\n if(s[i]=='0'):\n cost+= min(p*a1[i] , (p*a0[i]+q))\n else:\n cost+= min(p*a0[i] , (p*a1[i]+q))\n print(cost)\n t-=1", "# cook your dish here\nimport sys\nfrom collections import defaultdict\ndef get_array(): return list(map(int , sys.stdin.readline().strip().split()))\ndef get_ints(): return list(map(int, sys.stdin.readline().strip().split()))\ndef input(): return sys.stdin.readline().strip()\n\n\nfor _ in range(int(input())):\n c=[]\n n,m=get_ints()\n d0=defaultdict(int)\n d1=defaultdict(int)\n for _ in range(n):\n a=get_array()\n c.append(a)\n s=input()\n p,q=list(map(int,input().split()))\n for i in range(n):\n for j in range(m):\n\n if c[i][j]==0:\n d0[i+j]+=1\n else:\n d1[i+j]+=1\n\n\n su=0\n case1='0'\n case2='1'\n for i in range(len(s)):\n x=d0[i]\n y=d1[i]\n\n if s[i]=='0':\n cost1=y*p\n cost2=q+(x*p)\n if s[i]=='1':\n cost1=(y*p)+q\n cost2=x*p\n su+=(min(cost1,cost2))\n print(su)\n\n\n\n\n\n\n\n", "#dt = {} for i in x: dt[i] = dt.get(i,0)+1\nimport sys;input = sys.stdin.readline\n#import io,os; input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #for \u00a0\u00a0\u00a0\u00a0pypy\ninp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]\n\nfor _ in range(inp()):\n n,m = ip()\n x = [ip() for i in range(n)]\n s = input().strip()\n p,q = ip()\n ans = 0\n for i in range(n+m-1):\n dt = {0:0,1:0}\n if i < m:\n row,col = 0,i\n else:\n row,col = i-m+1,m-1\n while col >= 0 and row < n:\n dt[x[row][col]] += 1\n col -= 1\n row += 1\n if s[i] == '0':\n t = min(dt[1]*p,q+dt[0]*p)\n elif s[i] == '1':\n t = min(dt[0]*p,q+dt[1]*p)\n ans += t\n print(ans)\n", "for _ in range(int(input())):\n n, m = list(map(int, input().split()))\n a = [input().split() for _ in range(n)]\n s = input()\n p, q = list(map(int, input().split()))\n\n t = 0\n\n for i, j in enumerate(s):\n c = 0\n\n if i >= m:\n x = i-m+1\n y = m-1\n else:\n x = 0\n y = i\n\n f = 0\n\n while 0 <= x < n and 0 <= y < m:\n c += a[x][y] != j\n f += 1\n\n x += 1\n y -= 1\n\n t += min(c * p, p * (f-c) + q)\n\n print(t)\n", "t=int(input())\nfor _ in range(t):\n a,b=list(map(int,input().split()))\n l=[]\n for i in range(a):\n l.append(list(map(int,input().split())))\n k=[]\n for i in range(b):\n x=0\n y=0\n n,m = 0,i\n while n<a and m>=0:\n if l[n][m]==0:\n x+=1\n else:\n y+=1\n n+=1\n m-=1\n k.append([x,y])\n for i in range(1,a):\n x=0\n y=0\n n,m=i,b-1\n while n<a and m>=0:\n if l[n][m]==0:\n x+=1\n else:\n y+=1\n n+=1\n m-=1\n k.append([x,y])\n ans=0\n s=input()\n p,q=list(map(int,input().split()))\n for i in range(a+b-1):\n if s[i]=='0':\n ans+=min(k[i][1]*p, q+k[i][0]*p)\n else:\n ans+=min(k[i][0]*p, q+k[i][1]*p)\n print(ans)\n \n \n \n \n \n"]
{"inputs": [["2", "3 3", "1 0 1", "0 1 1", "1 1 0", "10111", "10 5", "3 3", "0 0 1", "0 1 1", "0 1 1", "00011", "2 9"]], "outputs": [["5", "4"]]}
INTERVIEW
PYTHON3
CODECHEF
7,358
2afd18cce01f36b8721129070ca63377
UNKNOWN
A valid parentheses sequence is a non-empty string where each character is either '(' or ')', which satisfies the following constraint: You can find a way to repeat erasing adjacent pairs of parentheses '()' until it becomes empty. For example, '(())' and '()((()()))' are valid parentheses sequences, but ')()(' and '(()' are not. Mike has a valid parentheses sequence. He really likes everything about his sequence, except the fact that it is quite long. So Mike has recently decided that he will replace his parentheses sequence with a new one in the near future. But not every valid parentheses sequence will satisfy him. To help you understand his requirements we'll introduce the pseudocode of function F(S): FUNCTION F( S - a valid parentheses sequence ) BEGIN balance = 0 max_balance = 0 FOR index FROM 1 TO LENGTH(S) BEGIN if S[index] == '(' then balance = balance + 1 if S[index] == ')' then balance = balance - 1 max_balance = max( max_balance, balance ) END RETURN max_balance END In other words, F(S) is equal to the maximal balance over all prefixes of S. Let's denote A as Mike's current parentheses sequence, and B as a candidate for a new one. Mike is willing to replace A with B if F(A) is equal to F(B). He would also like to choose B with the minimal possible length amongst ones satisfying the previous condition. If there are several such strings with the minimal possible length, then Mike will choose the least one lexicographically, considering '(' to be less than ')'. Help Mike! -----Input----- The first line of the input contains one integer T denoting the number of testcases to process. The only line of each testcase contains one string A denoting Mike's parentheses sequence. It is guaranteed that A only consists of the characters '(' and ')'. It is also guaranteed that A is a valid parentheses sequence. -----Output----- The output should contain exactly T lines, one line per each testcase in the order of their appearance. The only line of each testcase should contain one string B denoting the valid parentheses sequence that should be chosen by Mike to replace A. -----Constraints----- 1 ≤ T ≤ 5; 1 ≤ |A| ≤ 100000(105). -----Example----- Input: 1 ()((()())) Output: ((()))
["try:\n for i in range(int(input())):\n s=input()\n balance=0\n max_balance=0\n for i in s:\n if i=='(':balance+=1\n else:\n balance-=1\n max_balance=max(max_balance,balance)\n print('('*max_balance,')'*max_balance,sep=\"\")\nexcept Exception as e:\n print(e)\n ", "# cook your dish here\nfor _ in range(int(input())):\n b = 0\n mx = 0\n for i in input():\n if i == \"(\":\n b += 1\n else:\n b -= 1\n mx = max(b,mx)\n print(\"(\"*mx + \")\"*mx)", "# cook your dish here\ndef F(S):\n\tbalance=0\n\tmax_balance=0\n\tfor i in S:\n\t\tif (i=='('):\n\t\t balance+=1\n\t\telif(i== ')'):\n\t\t balance-=1\n\t\tmax_balance=max(max_balance,balance)\n\t\n\treturn max_balance\n\nfor _ in range(int(input())):\n x=input()\n maxi=F(x)\n tot='('*maxi\n tot+=')'*maxi\n print(tot)", "# cook your dish here\ndef fun(s):\n bal=0\n maxbal=0\n for i in range(len(s)):\n if s[i]=='(':\n bal+=1\n else:\n bal-=1\n maxbal=max(maxbal,bal)\n return maxbal\nt=int(input())\nwhile t>0:\n s=input()\n maxbal=fun(s)\n print('('*maxbal+')'*maxbal)\n t-=1", "# cook your dish here\nfor s in range(int(input())):\n A=input()\n balance=0\n max_balance=0\n for i in range(len(A)):\n if(A[i]=='('):\n balance+=1\n if(A[i]==')'):\n balance-=1\n max_balance=max(max_balance,balance)\n for i in range(max_balance):\n print(\"(\",end='')\n for i in range(max_balance):\n print(\")\",end='')\n print()", "for i in range(int(input())):\r\n j=input()\r\n balance = 0\r\n max_balance = 0\r\n for i in range(len(j)):\r\n if j[i] == '(' :balance+=1\r\n if j[i] == ')' :balance-=1\r\n max_balance = max(max_balance, balance)\r\n for i in range(max_balance):\r\n print(\"(\",end='')\r\n for i in range(max_balance):\r\n print(\")\",end='')\r\n print()", "try:\n for i in range(int(input())):\n st = input()\n balance = 0\n max_balance = 0\n for i in st:\n if i=='(': balance+=1\n else: balance-=1\n max_balance = max(max_balance, balance)\n print('('*max_balance,')'*max_balance, sep=\"\")\nexcept Exception as e:\n print(e)", "try:\n for i in range(int(input())):\n st = input()\n balance = 0\n max_balance = 0\n for i in st:\n if i=='(': balance+=1\n else: balance-=1\n max_balance = max(max_balance, balance)\n print('('*max_balance,')'*max_balance, sep=\"\")\nexcept Exception as e:\n print(e)", "# cook your dish here\n# cook your dish here\ntry:\n \n def F(S):\n balance = 0\n max_balance = 0\n for i in S:\n if i == '(':\n balance += 1 \n elif i == ')':\n balance -= 1 \n max_balance = max(balance, max_balance)\n return max_balance\n \n for _ in range(int(input())):\n S = input()\n max_balance = F(S)\n for i in range(max_balance):\n print('(', end='')\n for i in range(max_balance):\n print(')', end='')\n print('')\n \nexcept:\n pass\n", "# cook your dish here\ndef F(S):\n\tbalance=0\n\tmax_balance=0\n\tfor i in S:\n\t\tif (i=='('):\n\t\t balance+=1\n\t\telif(i== ')'):\n\t\t balance-=1\n\t\tmax_balance=max(max_balance,balance)\n\t\n\treturn max_balance\n\nfor _ in range(int(input())):\n S=input()\n max_balance=F(S)\n resStr='('*max_balance\n resStr+=')'*max_balance\n print(resStr)# cook your dish here\n", "# cook your dish here\ndef F(S):\n\tbalance=0\n\tmax_balance=0\n\tfor i in S:\n\t\tif (i=='('):\n\t\t balance+=1\n\t\telif(i== ')'):\n\t\t balance-=1\n\t\tmax_balance=max(max_balance,balance)\n\t\n\treturn max_balance\n\nfor _ in range(int(input())):\n S=input()\n max_balance=F(S)\n resStr='('*max_balance\n resStr+=')'*max_balance\n print(resStr)", "\r\nfor i in range(int(input())):\r\n s = input()\r\n c = 0\r\n mc = 0\r\n for i in s:\r\n if i == \"(\":\r\n c+=1\r\n else:\r\n c-=1\r\n mc = max(mc,c)\r\n print(\"(\"*mc+\")\"*mc)", "# cook your dish here\ntry:\n \n def F(S):\n balance = 0\n max_balance = 0\n for i in S:\n if i == '(':\n balance += 1 \n elif i == ')':\n balance -= 1 \n max_balance = max(balance, max_balance)\n return max_balance\n \n for _ in range(int(input())):\n S = input()\n max_balance = F(S)\n for i in range(max_balance):\n print('(', end='')\n for i in range(max_balance):\n print(')', end='')\n print('')\n \nexcept:\n pass\n", "# cook your dish here\nfor i in range(int(input())):\n s = input()\n c = 0\n mc = 0\n for i in s:\n if i == \"(\":\n c+=1\n else:\n c-=1\n mc = max(mc,c)\n print(\"(\"*mc+\")\"*mc)", "T = int(input())\nfor j in range(0,T):\n s= input()\n maxbalance = 0\n balance = 0\n for i in range(len(s)):\n a = s[i]\n if a == '(':\n balance += 1\n else:\n balance -= 1\n if balance > maxbalance:\n maxbalance = balance\n print('('*maxbalance,')'*maxbalance,sep = '')", "# cook your dish here\nfor _ in range(int(input())):\n s = input()\n maxcount = 0\n count = 0\n for i in s:\n if i == '(':\n count+=1\n else:\n count-=1\n maxcount = max(maxcount,count)\n print('('*maxcount+')'*maxcount)", "# cook your dish here\nfor _ in range(int(input())):\n s = input()\n maxcount = 0\n count = 0\n for i in s:\n if i == '(':\n count+=1\n else:\n count-=1\n maxcount = max(maxcount,count)\n print('('*maxcount+')'*maxcount)", "# cook your dish here\nfor _ in range(int(input())):\n maxcount,count=0,0\n s = input()\n for i in s:\n if i == '(':\n count += 1\n else:\n count -= 1\n maxcount = max(maxcount,count)\n print('('*maxcount+')'*maxcount)", "for i in range(int(input())):\r\n x=input()\r\n maxlen=0\r\n balance=0\r\n for i in x:\r\n if i=='(':\r\n balance+=1\r\n else:\r\n balance-=1\r\n maxlen=max(maxlen,balance)\r\n print('('*maxlen +')'*maxlen)"]
{"inputs": [["1", "()((()()))", "", ""]], "outputs": [["((()))"]]}
INTERVIEW
PYTHON3
CODECHEF
6,696
cb26dcda763bfea022cdeda68e8f4274
UNKNOWN
Given a Complete Binary Tree of ‘n’ depth, you can perform 4 types of mirror operation on the tree:- Mirror on the right. The tree is mirrored to the right and rightmost node on every level is connected with the mirrored corresponding node. Mirror on the left. The tree is mirrored to the left and leftmost node on every level is connected with the mirrored corresponding node. Mirror on the top. The tree is mirrored to the top and topmost nodes are connected with corresponding nodes. Mirror on the bottom. The tree is mirrored to the bottom and bottom most nodes are connected with the corresponding nodes. See the image for details. Mirror Right: Mirror Bottom: You are given ‘q’ queries, each performing this type of operation or asking for the no of edges in the produced graph. Queries are of the form “1 x” or “2” where x is 1 for right, 2 for left, 3 for top or 4 for bottom. 1 x: Perform x operation on the result graph. 2: Print the no of edges in the graph. Since it can be very large, print it modulo 1000000007. -----Input:----- - First line will contain $n$, the depth of the initial tree and $q$, the number of queries. - Next $q$ lines contain queries of the form "1 $x$" or "2". -----Output:----- For each query of type "2", output a single line containing the no of edges in the graph modulo 1000000007. -----Constraints----- - $1 \leq n \leq 1000$ - $1 \leq q \leq 10^5$ - $1 \leq x \leq 4$ -----Sample Input:----- 2 3 1 1 1 4 2 -----Sample Output:----- 38 -----EXPLANATION:----- Initial no of edges = 6 After the operation 1 1, no of edges = 15 After the operation 1 4, no of edges = 38 At operation 2, we print the no of edges that is 38.
["import os,sys\nfrom io import BytesIO, IOBase\n\ndef ii(): return int(input())\ndef si(): return input()\ndef mi(): return list(map(int,input().split()))\ndef li(): return list(mi())\nimport math \n\nimport collections \n\ndef CountFrequency(arr): \n return collections.Counter(arr) \n\nfor i in range(1):\n n,q=mi()\n p=pow(2,n+1)-2 \n t=1 \n b=pow(2,n)\n s=n+1\n for i in range(q):\n a=li()\n if len(a)==2:\n if a[1]==1 or a[1]==2:\n p*=2 \n p+=s\n t*=2 \n b*=2\n else:\n p*=2 \n if a[1]==3:\n p+=t\n t=b \n s*=2\n else:\n p+=b\n b=t\n s*=2\n else:\n print(p%1000000007)\n", "# cook your dish here\nn,q=map(int,input().split())\nedges=2*(2**n-1)\nside_nodes=n+1\ntop_nodes=1\nbottom_nodes=2**n\nfor i in range(q):\n query=list(map(int,input().split()))\n if query[0]==1:\n if query[1]==1 or query[1]==2:\n edges=edges*2+side_nodes\n top_nodes*=2\n bottom_nodes*=2\n elif query[1]==3:\n edges=edges*2+top_nodes\n top_nodes=bottom_nodes\n side_nodes*=2\n else:\n edges=edges*2+bottom_nodes\n bottom_nodes=top_nodes\n side_nodes*=2\n else:\n print(edges%1000000007)", "# cook your dish here\nMOD=1000000007\nlevels,q=input().split()\nlevels=int(levels)\nq=int(q)\nleft=levels+1\nright=levels+1\ntop=1\nbottom=pow(2,levels)\nedges=0\nf=1\nfor i in range(1,levels+1):\n f=((2%MOD)*(f%MOD))%MOD\n edges+=f\n\nlevels+=1\n\nfor i in range(1,q+1):\n l1=list(map(int,input().split()))\n if(l1[0]==1):\n y=l1[1]\n if(y==1):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(levels%MOD))%MOD\n top=((top%MOD)*(2%MOD))%MOD\n bottom=((bottom%MOD)*(2%MOD))%MOD\n elif(y==2):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(levels%MOD))%MOD\n top=((top%MOD)*(2%MOD))%MOD\n bottom=((bottom%MOD)*(2%MOD))%MOD\n elif(y==3):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(top%MOD))%MOD\n top=bottom;\n levels=((levels%MOD)*(2%MOD))\n elif(y==4):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(bottom%MOD))%MOD\n bottom=top\n levels=((levels%MOD)*(2%MOD))\n elif(l1[0]==2):\n print(edges%MOD)\n", "MOD=1000000007\nlevels,q=input().split()\nlevels=int(levels)\nq=int(q)\nleft=levels+1\nright=levels+1\ntop=1\nbottom=pow(2,levels)\nedges=0\nf=1\nfor i in range(1,levels+1):\n f=((2%MOD)*(f%MOD))%MOD\n edges+=f\n\nlevels+=1\n\nfor i in range(1,q+1):\n l1=list(map(int,input().split()))\n if(l1[0]==1):\n y=l1[1]\n if(y==1):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(levels%MOD))%MOD\n top=((top%MOD)*(2%MOD))%MOD\n bottom=((bottom%MOD)*(2%MOD))%MOD\n elif(y==2):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(levels%MOD))%MOD\n top=((top%MOD)*(2%MOD))%MOD\n bottom=((bottom%MOD)*(2%MOD))%MOD\n elif(y==3):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(top%MOD))%MOD\n top=bottom;\n levels=((levels%MOD)*(2%MOD))\n elif(y==4):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(bottom%MOD))%MOD\n bottom=top\n levels=((levels%MOD)*(2%MOD))\n elif(l1[0]==2):\n print(edges%MOD)\n", "n, q = list(map(int, input().split()))\n\ntop = 1\nbottom = 2 ** n\ndepth = n + 1\nedges = 2 ** (n + 1) - 2\n\nfor _ in range(q):\n x = list(map(int, input().split()))\n\n if x[0] == 2:\n print(edges % (10 ** 9 + 7))\n else:\n t = x[1]\n\n edges *= 2\n\n if t == 1 or t == 2:\n top *= 2\n bottom *= 2\n\n edges += depth\n\n if t == 3:\n depth *= 2\n edges += top\n top = bottom\n\n if t == 4:\n depth *= 2\n edges += bottom\n bottom = top\n", "MOD=1000000007\nlevels,q=input().split()\nlevels=int(levels)\nq=int(q)\nleft=levels+1\nright=levels+1\ntop=1\nbottom=pow(2,levels)\nedges=0\nf=1\nfor i in range(1,levels+1):\n f=((2%MOD)*(f%MOD))%MOD\n edges+=f\n\nlevels+=1\n\nfor i in range(1,q+1):\n l1=list(map(int,input().split()))\n if(l1[0]==1):\n y=l1[1]\n if(y==1):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(levels%MOD))%MOD\n top=((top%MOD)*(2%MOD))%MOD\n bottom=((bottom%MOD)*(2%MOD))%MOD\n elif(y==2):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(levels%MOD))%MOD\n top=((top%MOD)*(2%MOD))%MOD\n bottom=((bottom%MOD)*(2%MOD))%MOD\n elif(y==3):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(top%MOD))%MOD\n top=bottom;\n levels=((levels%MOD)*(2%MOD))\n elif(y==4):\n edges=((edges%MOD)*(2%MOD))%MOD\n edges=((edges%MOD)+(bottom%MOD))%MOD\n bottom=top\n levels=((levels%MOD)*(2%MOD))\n elif(l1[0]==2):\n print(edges%MOD)\n", "def mirror_down(e,r,l,u,d):\n edge=(2*e)+d\n right=r*2\n left=l*2\n up=u\n down=u\n #print(edge,right,left,up,down)\n return [edge,right,left,up,down]\ndef mirror_up(e,r,l,u,d):\n edge=(2*e)+u\n right=r*2\n left=l*2\n up=d\n down=d\n #print(edge,right,left,up,down)\n return [edge,right,left,up,down]\ndef mirror_left(e,r,l,u,d): \n edge=(2*e)+l\n right=r\n left=l\n up=u*2\n down=d*2\n #print(edge,right,left,up,down)\n return [edge,right,left,up,down]\ndef mirror_right(e,r,l,u,d):\n edge=(2*e)+r\n right=r\n left=l\n up=u*2\n down=d*2\n #print(edge,right,left,up,down)\n return [edge,right,left,up,down]\ns=input('').split(' ')\nn,q=(int(s[0]),int(s[1]))\ne=(2**(n+1))-2\nl=n+1\nr=n+1\nu=1\nd=2**n\nwhile(q!=0):\n s=input('').split(' ')\n if(len(s)==2):\n c=int(s[1])\n if(c==1):\n a=mirror_right(e,r,l,u,d)\n e=a[0]\n r=a[1]\n l=a[2]\n u=a[3]\n d=a[4]\n if(c==2):\n a=mirror_left(e,r,l,u,d)\n e=a[0]\n r=a[1]\n l=a[2]\n u=a[3]\n d=a[4]\n if(c==3):\n a=mirror_up(e,r,l,u,d)\n e=a[0]\n r=a[1]\n l=a[2]\n u=a[3]\n d=a[4]\n if(c==4):\n a=mirror_down(e,r,l,u,d)\n e=a[0]\n r=a[1]\n l=a[2]\n u=a[3]\n d=a[4]\n \n else:\n print(e%1000000007)\n q-=1\n", "mod=1000000007\nn,q=map(int,input().split())\npot=pow(2,n,mod) #2 to the power n and then mod mod\ne=((pot-1)*2)%mod\nright=n+1\nleft=n+1\ntop=1\nbottom=pot\nfor _ in range(q):\n l=input()\n add=0\n if l[0]=='1':\n if l[2]=='1':\n add=right\n top*=2\n bottom*=2\n elif l[2]=='2':\n add=left\n top*=2\n bottom*=2\n elif l[2]=='3':\n add=top\n right*=2\n left*=2\n top=bottom\n else:\n add=bottom\n left*=2\n right*=2\n bottom=top\n e=(e*2+add)%mod\n else:\n print(e)", "def main():\n n,q = list(map(int,input().split()))\n edges = ((pow(2,n,1000000007)-1)*2)%1000000007\n bottom = pow(2,n,1000000007)\n top = 1\n right = n+1\n left = n+1\n\n for i in range(q):\n query = list(map(int,input().split()))\n if len(query) == 1:\n print(edges)\n else:\n op = query[1]\n if op == 1:\n edges *= 2\n edges += right\n edges = edges%1000000007\n bottom *= 2\n top *= 2\n elif op == 2:\n edges *= 2\n edges += left\n edges = edges%1000000007\n bottom *= 2\n top *= 2\n elif op == 3:\n edges *= 2\n edges += top\n edges = edges%1000000007\n left *= 2\n right *= 2\n top = bottom\n else:\n edges *= 2\n edges += bottom\n edges = edges%1000000007\n left *= 2\n right *= 2\n bottom = top\n\n left = left%1000000007\n right = right%1000000007\n bottom = bottom%1000000007\n top = top%1000000007\n\nmain()\n", "import sys\nmod=10**9+7\nn,q=list(map(int,input().split()))\nu,l,r,d=1,n+1,n+1,pow(2,n,mod)\ne=(2*(d-1))%mod\nfor a in sys.stdin:\n a=list(a.split())\n if a[0]==\"1\":\n e=(e*2)%mod\n if a[1]==\"1\":\n e=(e+r)%mod\n r=l\n u=(u*2)%mod\n d=(d*2)%mod\n elif a[1]==\"2\":\n e=(e+l)%mod\n l=r\n u=(u*2)%mod\n d=(d*2)%mod\n elif a[1]==\"3\": \n e=(e+u)%mod\n u=d\n l=(l*2)%mod\n r=(r*2)%mod\n else:\n e=(e+d)%mod\n d=u\n l=(l*2)%mod\n r=(r*2)%mod\n else:\n print(e)\n \n", "# cook your dish here\ndepth,q=list(map(int,input().split()))\nr=1000000007\nedges=0\ntop=1\nbottom=2**(depth)\nfor i in range(1,depth+1):\n edges=edges+(2**i)\nfor i1 in range(0,q):\n a=list(map(int,input().split()))\n if len(a)==1:\n print(edges%r)\n else:\n x=a[1]\n if x==1:\n top=2*top\n bottom=2*bottom\n edges=(2*edges)+depth+1\n \n elif x==2:\n top=top*2\n bottom=2*bottom\n edges=(2*edges)+depth+1\n elif x==3:\n depth=(2*depth)+1\n edges=(2*edges)+top\n top=bottom\n else:\n depth=(2*depth)+1\n edges=(2*edges)+bottom\n bottom=top\n \n \n \n", "n, q = list(map(int, input().split()))\nb, e = 2 ** n, 2 ** (n + 1) - 2\n\nc = 1\nn += 1\n\nfor _ in range(q):\n l = input().split()\n\n if l[0] == '1':\n x = l[1]\n\n if x == '1' or x == '2':\n e = e * 2 + n\n b *= 2\n c *= 2\n\n if x == '3':\n e = e * 2 + c\n c = b\n n *= 2\n\n if x == '4':\n e = e * 2 + b\n b = c\n n *= 2\n\n else:\n print(e % 1000000007)\n", "mod=1000000007\nn,q=map(int,input().split())\nl=n+1%mod\nr=n+1%mod\nt=1%mod\nb=pow(2,n)%mod\ne=(pow(2,n+1)-2)%mod\nfor _ in range(q):\n lst=list(map(int,input().split()))\n if (len(lst)!=1) :\n if(lst[1]==1):\n e=(2*e+l)%mod\n l=l\n r=r\n t=(2*t)%mod\n b=(2*b)%mod\n if lst[1]==2:\n e=(2*e+r)%mod\n l=l\n r=r\n t=(2*t)%mod\n b=(2*b)%mod\n if lst[1]==3:\n e=((2*e)+t)%mod\n t=b%mod\n l=2*l%mod\n r=2*r%mod\n b=b%mod\n if lst[1]==4:\n e=((2*e)+b)%mod\n b=t%mod\n l=2*l%mod\n r=2*r%mod\n b=b%mod\n else:\n print(e)"]
{"inputs": [["2 3", "1 1", "1 4", "2"]], "outputs": [["38"]]}
INTERVIEW
PYTHON3
CODECHEF
9,196
2bd5d3acbad9c9fbcbb4c676dba9e588
UNKNOWN
Your are given a string $S$ containing only lowercase letter and a array of character $arr$. Find whether the given string only contains characters from the given character array. Print $1$ if the string contains characters from the given array only else print $0$. Note: string contains characters in lower case only. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains- a string $S$ of lowercase letter a integer $n$ denoting length of character array $arr$ next line contains $n$ space separated characters. -----Output:----- For each testcase, Print $1$ if the string contains characters from the given array only else print $0$. -----Constraints----- - $1 \leq T \leq 1000$ - $0 \leq n \leq 10^5$ -----Sample Input:----- 3 abcd 4 a b c d aabbbcccdddd 4 a b c d acd 3 a b d -----Sample Output:----- 1 1 0
["t=int(input())\r\nfor _ in range(t):\r\n S=set(input().strip())\r\n n=int(input().strip())\r\n a=set(input().strip().split(\" \"))\r\n g=True\r\n for i in S:\r\n if(i not in a):\r\n g=False\r\n if(g):\r\n print(1)\r\n else:\r\n print(0)", "t=int(input())\r\nfor _ in range(t):\r\n S=set(input().strip())\r\n n=int(input().strip())\r\n a=set(input().strip().split(\" \"))\r\n g=True\r\n for i in S:\r\n if(i not in a):\r\n g=False\r\n if(g):\r\n print(1)\r\n else:\r\n print(0)", "# cook your dish here\nfor _ in range(int(input())):\n string = input().strip()\n n = int(input())\n lst = input().split()\n #print(lst)\n status = True\n for x in string:\n #print(x)\n if x not in lst:\n status = False\n break\n if status:\n print(1)\n else:\n print(0)", "# cook your dish here\nT= int(input())\nfor i in range(T):\n S=input()\n n=int(input())\n arr = [str(j) for j in input().split()][:n]\n if(len(set(S) - set(arr))==1):\n print(1)\n else:\n print(0)\n \n", "from collections import Counter\r\nfor _ in range(int(input())):\r\n strs = set(input().strip())\r\n lens = int(input())\r\n inps = [x for x in input().split()]\r\n isOK = True\r\n for i in strs:\r\n if i not in inps:\r\n isOK = False\r\n break\r\n print(1 if isOK else 0)\r\n\r\n\r\n\r\n\r\n", "# cook your dish here\nt=int(input())\ni=0\nwhile i<t:\n c=0\n d=list(input().strip())\n s=set(d)\n n=int(input())\n k=input()\n lst=list(k.split())\n for j in s:\n if j in lst:\n continue\n else:\n c+=1\n break\n if c==0:\n print(\"1\")\n else:\n print(\"0\")\n i+=1", "# cook your dish here\nt=int(input())\ni=0\nwhile i<t:\n c=0\n d=list(input().strip())\n s=set(d)\n n=int(input())\n k=input()\n lst=list(k.split())\n for j in s:\n if j in lst:\n continue\n else:\n c+=1\n break\n if c==0:\n print(\"1\")\n else:\n print(\"0\")\n i+=1", "for _ in range(int(input())):\n s=input().strip()\n n=int(input())\n l=list(input())\n f=0\n for i in range(len(s)):\n if s[i] not in l:\n f=1\n break\n if f==1:\n print(0)\n else:\n print(1)\n", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n st=input().strip()\n n=int(input())\n f=1 \n arr=input().split()\n for i in arr:\n st=st.replace(i,'')\n if(len(st)==0):\n print('1')\n else:\n print('0')", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n s=input().strip()\n n=int(input())\n chars=set(input().split())\n flag=1\n for i in s:\n if i not in chars:\n print(0)\n flag=0\n break\n if flag:\n print(1)\n", "# cook your dish here\nt = int(input())\n\nfor _ in range(t):\n s = set(str(input()).strip())\n n = int(input())\n arr = set(str(input()).split())\n \n if len(s.difference(arr)) == 0:\n print(1)\n else:\n print(0)", "# cook your dish here\n\n\nfor _ in range(int(input())):\n s=input().strip()\n n=int(input().strip())\n a=list(input().strip().split())\n a=list(set(a))\n # print(a,s)\n flag=0\n for i in s:\n # print(i)\n if i not in a:\n \n flag=1\n break\n if flag==1:\n print('0')\n else:\n print('1')", "from sys import stdin, stdout\nfrom math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log\nfrom collections import defaultdict as dd, deque\nfrom heapq import merge, heapify, heappop, heappush, nsmallest\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nmod = pow(10, 9) + 7\nmod2 = 998244353\ndef inp(): return stdin.readline().strip()\ndef out(var, end=\"\\n\"): stdout.write(str(var)+\"\\n\")\ndef outa(*var, end=\"\\n\"): stdout.write(' '.join(map(str, var)) + end)\ndef lmp(): return list(mp())\ndef mp(): return list(map(int, inp().split()))\ndef smp(): return list(map(str, inp().split()))\ndef l1d(n, val=0): return [val for i in range(n)]\ndef l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]\ndef remadd(x, y): return 1 if x%y else 0\ndef ceil(a,b): return (a+b-1)//b\n\ndef isprime(x):\n if x<=1: return False\n if x in (2, 3): return True\n if x%2 == 0: return False\n for i in range(3, int(sqrt(x))+1, 2):\n if x%i == 0: return False\n return True\n\ndef solve(md, s):\n for i in s:\n if i not in md: return 0\n return 1\n\nfor _ in range(int(inp())):\n s = inp()\n n = int(inp())\n arr = inp().split()\n md = {}\n for i in arr:\n if i not in md: md[i]=1\n print(solve(md, s))\n\n", "from itertools import groupby\n\nfor case in range(int(input())):\n string=''.join(input().split())\n N=int(input())\n char_string=input()\n \n dicti={i:len(list(i)) for i,j in groupby(string,key=None)}\n \n old=0\n\n for i in dicti:\n if i not in char_string:\n old+=1\n \n if old==0:\n print(1)\n \n else:\n print(0)", "for i in range(int(input())):\n s = input().strip()\n n = int(input().strip())\n li = input().strip().split()\n \n ans = 1\n \n for el in set(s):\n if el not in li:\n ans = 0\n break\n \n print(ans)", "for _ in range(int(input())):\r\n s = input().strip()\r\n input()\r\n a = set(input().split())\r\n\r\n d = set(s)\r\n\r\n print(int(all(i in a for i in d)))\r\n"]
{"inputs": [["3", "abcd", "4", "a b c d", "aabbbcccdddd", "4", "a b c d", "acd", "3", "a b d"]], "outputs": [["1", "1", "0"]]}
INTERVIEW
PYTHON3
CODECHEF
5,784
f149fd5397256fc23d63c1825d631c2b
UNKNOWN
Santosh has a farm at Byteland. He has a very big family to look after. His life takes a sudden turn and he runs into a financial crisis. After giving all the money he has in his hand, he decides to sell his plots. The speciality of his land is that it is rectangular in nature. Santosh comes to know that he will get more money if he sells square shaped plots. So keeping this in mind, he decides to divide his land into minimum possible number of square plots, such that each plot has the same area, and the plots divide the land perfectly. He does this in order to get the maximum profit out of this. So your task is to find the minimum number of square plots with the same area, that can be formed out of the rectangular land, such that they divide it perfectly. -----Input----- - The first line of the input contains $T$, the number of test cases. Then $T$ lines follow. - The first and only line of each test case contains two space-separated integers, $N$ and $M$, the length and the breadth of the land, respectively. -----Output----- For each test case, print the minimum number of square plots with equal area, such that they divide the farm land perfectly, in a new line. -----Constraints----- $1 \le T \le 20$ $1 \le M \le 10000$ $1 \le N \le 10000$ -----Sample Input:----- 2 10 15 4 6 -----SampleOutput:----- 6 6
["# cook your dish here\nimport math\nN=int(input())\nfor i in range(N):\n a,b=list(map(int,input().split()))\n c=a//math.gcd(a,b)*b//math.gcd(a,b)\n print(c)\n \n", "# cook your dish here\n\nimport math\nt=int(input())\n\nfor i in range(t):\n n,m=list(map(int,input().split()))\n \n print(n//math.gcd(n,m)*m//math.gcd(n,m))\n \n", "# cook your dish here\nimport math \nfor _ in range(int(input())):\n x,y=list(map(int,input().split()))\n print(x//math.gcd(x,y)*y//math.gcd(x,y))\n \n \n\n", "# cook your dish here\nfrom math import gcd as g\nfor _ in range(int(input())):\n a,b=[int(x) for x in input().split()]\n gc=g(a,b)\n r=(a//gc)*(b//gc)\n print(r)", "import math\nn=int(input())\nx=[]\nfor i in range(0,n):\n l,b=list(map(int, input().split()))\n area=l*b\n g=math.gcd(l,b)\n g=g**2\n x.append(area//g)\nfor i in x:\n print(i)\n", "# cook your dish here\ndef hcf(a,b):\n if(b==0):\n return a;\n else:\n return hcf(b,a%b)\nt=int(input())\nfor i in range(t):\n a,b=input().split()\n a,b=int(a),int(b)\n c=hcf(a,b)\n print(int((a*b)/(c*c)))", "def compute_hcf(x, y):\n if x > y:\n smaller = y\n else:\n smaller = x\n for i in range(1, smaller+1):\n if((x % i == 0) and (y % i == 0)):\n hcf = i \n return hcf\n \nfor _ in range(int(input())):\n m, n= map(int, input().split())\n hcf= compute_hcf(m, n)\n print(m//hcf*n//hcf)", "# cook your dish here\ntry:\n for _ in range(int(input())):\n l, b = map(int, input().split())\n area = l*b\n while(b):\n l, b = b, l%b\n print(int((area/(l*l))))\nexcept:\n pass", "# cook your dish here\nimport math\ntest = int(input())\n\nfor _ in range(0,test):\n l,b = list(map(int,input().split()))\n num = math.gcd(l,b)\n print((l*b)//num**2)\n", "import math\nt=int(input())\nfor i in range(t):\n n,m=map(int,input().split())\n x=math.gcd(n,m)\n ans=(n*m)//(x*x)\n print(ans)", "import math\nt=int(input())\nfor i in range(t):\n (N,M)=map(int,input().split(' '))\n x=math.gcd(N,M)\n y=((N*M)//(x**2))\n print(y)", "import math\nt=int(input())\nans=list()\nfor i in range(t):\n n=list(map(int,input().split()))\n x=math.gcd(n[0],n[1])\n y=(n[0]/x)*(n[1]/x)\n ans.append(y)\nfor l in ans:\n print(int(l))", "import math\nt=int(input())\nfor i in range(t):\n a,b=list(map(int,input().split()))\n x=math.gcd(a,b)\n m=((a*b)//(x**2))\n print(m)\n", "import math\nfor i in range(int(input())):\n l, b=list(map(int, input().split()))\n gcd1=math.gcd(l,b)\n newl=(l//gcd1)\n new2=(b//gcd1)\n print(newl*new2)\n \n \n", "# cook your dish here\nimport math\nt = int(input())\nfor i in range(t):\n l,b = map(int, input().split())\n area = l*b\n gcd = math.gcd(l,b)\n gcd=gcd**2\n print(area//gcd)", "# cook your dish here\nimport math\nfor t in range(int(input())):\n n,m=list(map(int,input().split()))\n area=n*m\n res=math.gcd(n,m)\n res=res**2\n print(area//res)\n", "# cook your dish here\na= int(input())\nfor i in range(a):\n rect= input()\n rect= rect.split(\" \")\n b= int(rect[0])\n l= int(rect[1])\n # print(b,l)\n maxx=1\n \n for i in range(1,b+1):\n if(b%i==0 and l%i==0):\n maxx=i\n\n print(l//maxx * b//maxx)\n \n \n", "# cook your dish here\na= int(input())\nfor i in range(a):\n rect= input()\n rect= rect.split(\" \")\n b= int(rect[0])\n l= int(rect[1])\n # print(b,l)\n maxx=1\n \n for i in range(1,b+1):\n if(b%i==0 and l%i==0):\n maxx=i\n\n print(l//maxx * b//maxx)\n \n \n", "# cook your dish here\nimport math\nt = int(input())\nfor i in range(t):\n l,b = map(int, input().split())\n r = l*b\n s = math.gcd(l,b)\n s=s**2\n print(r//s)"]
{"inputs": [["2", "10 15", "4 6"]], "outputs": [["6", "6"]]}
INTERVIEW
PYTHON3
CODECHEF
3,513
16a188b4d2cc295f0620f2e0591e802b
UNKNOWN
To make Yalalovichik even more satisfied and happy, Jafar decided to invent Yalalovichik strings. A string is called a Yalalovichik string if the set of all of its distinct non-empty substrings is equal to the set of all of its distinct non-empty subsequences. You are given a string S$S$. You need to find the number of its distinct non-empty substrings which are Yalalovichik strings. Note: A string A$A$ is called a subsequence of a string B$B$ if A$A$ can be formed by erasing some characters (possibly none) from B$B$. A string A$A$ is called a substring of a string B$B$ if it can be formed by erasing some characters (possibly none) from the beginning of B$B$ and some (possibly none) from the end of B$B$. Two substrings or subsequences are considered different if they are different strings. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N=|S|$N = |S|$. - The second line contains the string S$S$. -----Output----- For each test case, print a single line containing one integer — the number of distinct Yalalovichik substrings of S$S$. -----Constraints----- - 1≤T≤100$1 \le T \le 100$ - 1≤N≤106$1 \le N \le 10^6$ - the sum of N$N$ over all test cases does not exceed 2⋅106$2 \cdot 10^6$ - S$S$ contains only lowercase English letters -----Example Input----- 1 3 xxx -----Example Output----- 3 -----Explanation----- Example case 1: The distinct Yalalovichik substrings are "x", "xx" and "xxx".
["for _ in range(int(input())):\n n,s = int(input()),input().strip()\n previ,num,_s,dic = s[0],0,[],{}\n for i in s:\n if previ == i:\n num+=1\n continue\n _s.append((previ, num))\n if previ not in dic or dic[previ]<num:dic[previ] = num\n previ,num = i,1\n _s.append((previ, num))\n if previ not in dic or dic[previ]<num:dic[previ] = num\n sum1 = sum(dic.values())\n del dic, s\n l,dicc = [i for (i, j) in _s],{}\n congr = [(l[i], l[i+1]) for i in range(len(l)-1)]\n for i in range(len(congr)):\n if congr[i] not in dicc:dicc[congr[i]] = set()\n dicc[congr[i]].add( (_s[i][1], _s[i+1][1]) ) \n sum2,ll = 0,[]\n for i in dicc.keys():\n sortedset,deleted = sorted(list(dicc[i])),[]\n for k in range(1, len(sortedset)):\n j = sortedset[k] \n if j[1]>sortedset[k-1][1]:\n ind = k - 1", "t = int(input())\nwhile t>0:\n t-=1\n n = int(input())\n s = input().strip()\n previ = s[0]\n num = 0\n _s = []\n dic = {}\n for i in s:\n if previ == i:\n num+=1\n continue\n _s.append((previ, num))\n if previ not in dic or dic[previ]<num:\n dic[previ] = num\n #l.append(previ)\n previ = i\n num = 1\n _s.append((previ, num))\n if previ not in dic or dic[previ]<num:\n dic[previ] = num\n\n ##print(_s)\n sum1 = sum(dic.values())\n del dic, s", "t = int(input())\nwhile t>0:\n t-=1\n n = int(input())\n s = input().strip()\n previ = s[0]\n num = 0\n _s = []\n dic = {}\n for i in s:\n if previ == i:\n num+=1\n continue\n _s.append((previ, num))\n if previ not in dic or dic[previ]<num:\n dic[previ] = num\n #l.append(previ)\n previ = i\n num = 1\n _s.append((previ, num))\n if previ not in dic or dic[previ]<num:\n dic[previ] = num\n\n ##print(_s)\n sum1 = sum(dic.values())\n del dic, s"]
{"inputs": [["1", "3", "xxx"]], "outputs": [["3"]]}
INTERVIEW
PYTHON3
CODECHEF
1,950
5d44f2266bbdace3f6ab0fa88f221a9c
UNKNOWN
Zonal Computing Olympiad 2012, 26 Nov 2011 A sequence of opening and closing brackets is well-bracketed if we can pair up each opening bracket with a matching closing bracket in the usual sense. For instance, the sequences (), (()) and ()(()) are well-bracketed, while (, ()), (()(), and )( are not well-bracketed. The nesting depth of a well-bracketed sequence tells us the maximum number of levels of inner matched brackets enclosed within outer matched brackets. For instance, the nesting depth of () and ()()() is 1, the nesting depth of (()) and ()(()) is 2, the nesting depth of ((())) is 3, and so on. Given a well-bracketed sequence, we are interested in computing the following: - The nesting depth, and the first position where it occurs–this will be the position of the first opening bracket at this nesting depth, where the positions are numbered starting with 1. - The maximum number of symbols between any pair of matched brackets, including both the outer brackets, and the first position where this occurs–that is, the position of the first opening bracket of this segment For instance, the nesting depth of ()(())()(()())(()()) is 2 and the first position where this occurs is 4. The opening bracket at position 10 is also at nesting depth 2 but we have to report the first position where this occurs, which is 4. In this sequence, the maximum number of symbols between a pair of matched bracket is 6, starting at position 9. There is another such sequence of length 6 starting at position 15, but this is not the first such position. -----Input format----- The input consists of two lines. The first line is a single integer N, the length of the bracket sequence. Positions in the sequence are numbered 1,2,…,N. The second line is a sequence of N space-separated integers that encode the bracket expression as follows: 1 denotes an opening bracket ( and 2 denotes a closing bracket ). Nothing other than 1 or 2 appears in the second line of input and the corresponding expression is guaranteed to be well-bracketed. -----Output format----- Your program should print 4 space-separated integers in a line, denoting the four quantities asked for in the following order: nesting depth, first position that achieves the nesting depth, length of the maximum sequence between matching brackets and the first position where such a maximum length sequence occurs. -----Testdata----- You may assume that 2 ≤ N ≤ 105. In 30% of the test cases, 2 ≤ N ≤ 103. - Subtask 1 (30 marks) - Subtask 2 (70 marks) -----Sample Input----- 20 1 2 1 1 2 2 1 2 1 1 2 1 2 2 1 1 2 1 2 2 -----Sample Output----- 2 4 6 9
["# cook your dish here\n\nT = int(input())\nl = list(map(int, input().strip().split(\" \")))\n\ndepth = 0\nmax_depth = 0\nmax_depth_index = 0\n\nmax_l=0\nmax_l_index=0\nlast_zero=-1\n\nfor i in range(T):\n if l[i] == 1:\n depth += 1\n if depth > max_depth:\n max_depth = depth\n max_depth_index = i + 1\n else:\n depth-=1\n if depth == 0:\n length = i - last_zero\n if length > max_l:\n max_l = length\n max_l_index = last_zero + 2\n last_zero = i\n \nprint(max_depth, max_depth_index, max_l, max_l_index)\n\n\"\"\"\n2 4 6 9\n\"\"\"", "N = int(input())\ns = input()\nd = 0\nmd = 0\nmdpos = 0\npl = 0\nmpl = 0\nplstart = 0\nfor i in range(N):\n t = s[i*2]\n if t == '1':\n d += 1\n if d > md :\n md = d\n mdpos = i\n else:\n d -= 1\n pl += 1\n if d == 0:\n if pl> mpl:\n mpl = pl\n mplstart = plstart\n plstart = i+1\n pl = 0\nprint(md,mdpos+1,mpl,mplstart+1)\n\n", "n=int(input())\r\ns=list(map(int,input().split()))\r\ndans=0\r\nfd=0\r\nc=0\r\ncount=0\r\nmaxs=0\r\nfs=n\r\nfor i in range(n):\r\n if s[i]==1:\r\n c-=1\r\n count+=1\r\n if abs(c)>dans:\r\n dans=abs(c)\r\n if fd<i:\r\n fd=i\r\n else:\r\n c+=1\r\n if c==0: \r\n if count>maxs:\r\n maxs=count+1\r\n fs=i-maxs+2\r\n\r\n count=0\r\n else:\r\n count+=1\r\n\r\nprint(dans,fd+1,maxs,fs)\r\n", "# cook your dish here\nn = int(input())\na = list(map(int,input().strip().split()))\ni = 0\nx = 0\nz = 0\nnest = 1\npos = 0\nseq = 1 \nposq = 0\nwhile(i<n):\n j= i + 1\n x = 1\n z = 1\n q = i\n while(j<n and x>0):\n if(a[j] == 1):\n x +=1 \n z +=1\n if(nest < x):\n nest = x\n pos = j\n if(seq<z):\n seq = z\n posq = q\n else:\n x-=1\n i = j + 1\n j+=1\nprint(nest, pos+1, 2*seq, posq+1)\n", "n = int(input())\r\nseq = list(map(lambda x: 1 if x==\"1\" else -1, input().split()))\r\nsums = [seq[0]]\r\nfor i in seq[1:]:\r\n sums.append(sums[-1]+i)\r\nfin_pos = -1\r\npos = -1\r\ncounter = -1\r\nm = -1\r\nfor i in range(n):\r\n v = sums[i]\r\n if counter == -1 and v == 1:\r\n counter = 0\r\n pos = i\r\n elif v == 0:\r\n if counter > m:\r\n m = counter\r\n fin_pos = pos\r\n counter = -1\r\n elif counter != -1:\r\n counter += 1\r\n\r\nprint(max(sums), sums.index(max(sums))+1, m + 2, fin_pos+1)", "# cook your dish here\ndef is_seq_brackets_right(input_seq):\n stack = []\n highest_depth = 0\n highest_depth_index = 0\n max_inner_symbols_no = 0\n max_inner_symbols_index = 0\n for index_ in range(len(input_seq)):\n i = input_seq[index_]\n if i == \"1\":\n stack.append([\"1\", index_])\n else:\n temp = stack.pop(-1)\n if index_ - temp[1] + 1 > max_inner_symbols_no:\n max_inner_symbols_no = index_ - temp[1] + 1\n max_inner_symbols_index = temp[1]\n\n if len(stack) > highest_depth:\n highest_depth = len(stack)\n highest_depth_index = index_\n return len(stack) == 0, highest_depth, highest_depth_index + 1, max_inner_symbols_no, max_inner_symbols_index + 1\n\n\n\nN = int(input().strip())\ninput_string = input().strip()\nbracket_input = input_string.replace(\" \", \"\")\nans = is_seq_brackets_right(bracket_input)\nprint(ans[1], \" \", ans[2], \" \", ans[3], \" \", ans[4])\n", "# cook your dish here\n\ndef is_seq_brackets_right(input_seq):\n stack = []\n highest_depth = 0\n highest_depth_index = 0\n max_inner_symbols = []\n max_inner_symbols_no = 0\n max_inner_symbols_index = 0\n for index_ in range(len(input_seq)):\n i = input_seq[index_]\n if i == \"(\":\n stack.append(\"(\")\n max_inner_symbols.append([index_, 1, True])\n else:\n stack.pop(-1)\n max_inner_symbols.pop(-1)\n\n for j in range(len(max_inner_symbols)):\n max_inner_symbols[j][1] = max_inner_symbols[j][1] + 1\n if max_inner_symbols[j][1] > max_inner_symbols_no:\n max_inner_symbols_no = max_inner_symbols[j][1]\n max_inner_symbols_index = max_inner_symbols[j][0]\n\n if len(stack) > highest_depth:\n highest_depth = len(stack)\n highest_depth_index = index_\n return len(stack) == 0, highest_depth, highest_depth_index + 1, max_inner_symbols_no, max_inner_symbols_index + 1\n\n\nN = int(input().strip())\ninput_string = input().strip()\nbracket_input = input_string.replace(\" \", \"\").replace(\"1\", \"(\").replace(\"2\", \")\")\nans = is_seq_brackets_right(bracket_input)\nprint(ans[1], \" \", ans[2], \" \", ans[3], \" \", ans[4])\n", "# cook your dish here\nimport sys\ninput=sys.stdin.readline\nn=int(input())\nl=input().split()\nstack=[]\nli=[int(i) for i in l]\nmaxa1=0\nans1=0\nmatch=[0 for i in range(n)]\nmaxa2=0\nans2=0\nfor i in range(n):\n if(li[i]==2):\n if(len(stack)>maxa1):\n maxa1=len(stack)\n ans1=stack[-1]\n match[stack[-1]]=i\n stack.pop(-1)\n else:\n stack.append(i)\nfor i in range(n):\n if(match[i]-i>maxa2):\n maxa2=match[i]-i\n ans2=i\nprint(maxa1,ans1+1,maxa2+1,ans2+1)\n", "# cook your dish here\nN = int(input())\n\nenc = input().split()\nS = []\n\nfor e in enc : \n if e == \"1\" : S.append(\"(\")\n else : S.append(\")\")\n\n\n\n# task 1 : max depth \n\nclosures = []\nd = 0\nmax_d = 0\nmax_d_idx = 0\nstart = 0\nfor idx in range(N) :\n \n \n s = S[idx]\n if s == \"(\" : \n\n d += 1\n \n if d > max_d : \n max_d = d \n max_d_idx = idx + 1\n \n else : \n d -= 1\n \n if d == 0 : \n finish = idx\n closures.append([start, finish])\n start = idx + 1\n \n \n \n \n# task 2 : maximum closure\n\n\ndiff = [x[1] - x[0] for x in closures]\nmax_diff = max(diff)\nmax_diff_idx = closures[diff.index(max_diff)][0] + 1\n\n\n \nprint(max_d, max_d_idx, max_diff + 1, max_diff_idx)", "import sys\n# import math as mt\n# from collections import Counter\n# from itertools import permutations\n# from functools import reduce\n# from heapq import nsmallest, nlargest, heapify, heappop, heappush, heapreplace\n\ndef get_inpt(): return sys.stdin.readline().strip()\ndef get_int(): return int(sys.stdin.readline().strip())\ndef get_ints(): return map(int, sys.stdin.readline().strip().split())\ndef get_array(): return list(map(int, sys.stdin.readline().strip().split()))\n\n# sys.setrecursionlimit(10**7)\nINF = float('inf')\n# MOD1, MOD2 = 1000000007, 998244353\n\nn = get_int()\narr = get_array()\n\ncurr_nest = 0\nmax_nest = 1\nnest_pos = 0\n\ncurr_symbols = 0\nmax_symbols = 2\nmax_symbols_open_pos = 0\n\nseq = 0\n\nfor ind in range(n):\n \n # curr_nest += 1\n curr_symbols += 1\n \n if arr[ind] == 1: # Open Parenthesis\n seq += 1\n if seq > max_nest:\n max_nest = seq\n nest_pos = ind\n \n else: # Closed Parenthesis\n seq -= 1\n if seq == 0:\n if curr_symbols > max_symbols:\n max_symbols = curr_symbols\n max_symbols_open_pos = ind - curr_symbols + 1\n curr_symbols = 0\n\nprint(max_nest, nest_pos+1, max_symbols, max_symbols_open_pos+1)", "# cook your dish here\nimport sys\nn=int(input().strip())\nstringa=input().strip().split()\nstringa=''.join(stringa)\ncounter=0\nmax_width=0\nmax_depth=0\nfor i in range(n):\n if stringa[i]=='1':\n if counter==0:\n start=i\n counter+=1 \n else:\n if counter>max_depth:\n max_depth=counter\n depth_idx=i\n counter-=1\n if counter==0:\n end=i\n if end-start+1>max_width:\n max_width=end-start+1\n start_idx=start+1\nprint(max_depth, depth_idx, max_width, start_idx)", "n=int(input())\r\nx=[int(i) for i in input().split()]\r\na=0\r\nb=0\r\ny1=[]\r\ny2=[]\r\np=0\r\nq=0\r\nfor i in range(1,n+1):\r\n if x[i-1]==1:\r\n y1.append(i)\r\n y2.append(i)\r\n elif x[i-1]==2:\r\n if len(y1)>a:\r\n a=len(y1)\r\n p=y1[-1] \r\n if len(y1)==1:\r\n if len(y2)+1>b:\r\n b=len(y2)+1\r\n q=y2[0]\r\n y2=[]\r\n else:\r\n y2.append(i)\r\n y1.pop()\r\nprint(a,p,b,q)", "n=int(input())\n\na=[int(i) for i in input().split()]\n\nmd=0\nmr=0\nst1=[]\nst2=[]\ndno=0\nrno=0\n\nfor i in range(1,n+1):\n if a[i-1]==1:\n st1.append(i)\n st2.append(i)\n elif a[i-1]==2:\n if len(st1)>md:\n md=len(st1)\n dno=st1[-1]\n \n if len(st1)==1:\n if len(st2)+1>mr:\n mr=len(st2)+1\n rno=st2[0]\n st2=[]\n else:\n st2.append(i)\n st1.pop()\nprint(md,dno,mr,rno)\n \n \n", "# cook your dish here\nx = int(input())\ny = list(map(int, input().strip().split(\" \")))\nd, pd, l, pl = 0, 0, 0, 0\nmd, ml = 0, 0\nfor i in range(0,x):\n if(y[i] == 1):\n d = d+1\n if(d > md): md, pd = d, i+1\n if(y[i] == 2): d = d-1\n if(d != 0):\n l = l+1\n if(l > ml): ml, pl = l, i-l+1\n else:\n l = 0\nprint(md, pd, ml+1, pl+1)", "# cook your dish here\nn = int(input())\nl = input().split()\n\nml = []\nmnd, mndi = -1, -1\nc = 1\nmaxc, maxci = 0, 0\nfor i in range(n):\n if l[i] == '1':\n ml.append('1')\n if len(ml) > mnd:\n mnd = len(ml)\n mndi = i\n if l[i] == '2':\n ml.pop()\n if ml == []:\n if c > maxc:\n maxc = c\n maxci = i\n c = 0\n c += 1\nif c > maxc:\n maxc = c\n maxci = i\nprint(mnd, mndi+1, maxc, maxci-maxc+2)\n", "\ntry : \n import sys\n \n num = int(input())\n brackets = list(map(int, sys.stdin.readline().strip().split()))\n \n max_len_seq = 0\t\t\t #len of max sequence bw matching brackets\n start_max_len_seq = 0\t #index where max length sequence occurs\n max_depth = 0 \n start_depth = 0\t\t\t #Index of where the nesting depth is achieved\n \n #temp vars\n t1=0\n t2=0\n t3=0\n t4=0\n \n n = 0 #number of brackets\n \n for i in range(num):\n if n == 0:\n t2 = i\n \n b = brackets[i]\n \n if b == 1: #opening bracket\n n+=1\n t3 +=1\n t4 = i\n t1+=1\n \n elif b == 2: #closing bracket\n n-=1\n t1+=1\n t3-=1\n \n if t3 > max_depth:\n max_depth = t3\n start_depth = t4\n \n if t1 > max_len_seq:\n max_len_seq = t1\n start_max_len_seq = t2\n \n if n == 0:\n t1=0\n t2=0\n t3=0\n t4=0\n \n print(max_depth, start_depth+1, max_len_seq, start_max_len_seq+1) # +1 since list index starts from 0\n\n\nexcept : pass", "# cook your dish here\n# cook your dish here\nl = int(input())\na = list(map(int, input().split()))\nstack = []\np, md, d = 0, 0, 0\nmp, ml, l = 0, 0, 0\nmpIndex = 0\n\nfor i in range(len(a)):\n if(d == 0):\n l = 0\n mpIndex = i+1\n \n l += 1\n \n if(a[i] == 1):\n d+=1\n else:\n d-=1\n \n if(d > md):\n md = d\n p = i+1\n \n if(l > ml):\n ml = l\n mp = mpIndex\n\nprint(md, p, ml, mp)", "# cook your dish here\nl = int(input())\na = list(map(int, input().split()))\nstack = []\np, md, d = 0, 0, 0\nmp, ml, l = 0, 0, 0\nmpIndex = 0\n\nfor i in range(len(a)):\n if(d == 0):\n l = 0\n mpIndex = i+1\n \n l += 1\n \n if(a[i] == 1):\n d+=1\n else:\n d-=1\n \n if(d > md):\n md = d\n p = i+1\n \n if(l > ml):\n ml = l\n mp = mpIndex\n\nprint(md, p, ml, mp)", "l = int(input())\na = list(map(int, input().split()))\nstack = []\np, md, d = 0, 0, 0\nmp, ml, l = 0, 0, 0\nmpIndex = 0\n\nfor i in range(len(a)):\n if(d == 0):\n l = 0\n mpIndex = i+1\n \n l += 1\n \n if(a[i] == 1):\n d+=1\n else:\n d-=1\n \n if(d > md):\n md = d\n p = i+1\n \n if(l > ml):\n ml = l\n mp = mpIndex\n\nprint(md, p, ml, mp)", "n=int(input())\na=list(map(int,input(\"\").strip().split()))[:n]\ns=list()\nli=list()\nk=0;\npos=0;\nsik=list()\nfor l in range(len(a)):\n if a[l]==1:\n li.append('(')\n s.append('(')\n elif a[l]==2:\n s.append(')')\n if len(li)!=0:\n if k<len(li):\n pos=l;\n k=len(li)\n del li[len(li)-1]\n if len(li)==0:\n sik.append(s);\n s=list()\nle=0;\nfor i in sik:\n if le<len(i):\n le=len(i);\nse=0;\nfor i in sik:\n if le==len(i):\n break;\n se+=len(i);\nprint(k,pos,le,se+1)\n", "n=int(input())\ntotal,distance,new_first=0,0,0\na=[]\nanswer = []\nlength =list(map(str,input().split()))\nstring =\"\".join(length) \nfor i in range(len(string)):\n if(string[i]=='1'):\n a.append(i)\n if(total<len(a)):\n total=len(a)\n start=a[len(a)-1]\n elif(len(a)!=0 and string[i]=='2'): \n if(len(a)==1):\n if(distance<i-a[len(a)-1]):\n distance=i-a[len(a)-1]\n new_first=a[len(a)-1]\n a.pop()\nanswer.append(total)\nanswer.append(' ')\nanswer.append(start+1)\nanswer.append(' ')\nanswer.append(distance+1)\nanswer.append(' ')\nanswer.append(new_first+1)\nprint(''.join(map(str, answer))) "]
{"inputs": [["20", "1 2 1 1 2 2 1 2 1 1 2 1 2 2 1 1 2 1 2 2"]], "outputs": [["2 4 6 9"]]}
INTERVIEW
PYTHON3
CODECHEF
14,200
cbc6095828db1e523f0cb3920b1de5a6
UNKNOWN
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ ≤ $i$ ≤ $N$) and column $j$ ($1$ ≤ $j$ ≤ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$. He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$, $y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$, $y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle. Finally, print the matrix after performing all the queries. Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number. -----Input:----- - The first line of the input contains two integers $N$ and $M$ — the number of rows and the number of columns in the matrix. - Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$. - Next line contains an integer $Q$ — the number of queries. - Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ — coordinates of the up left and bottom right cells of the query rectangle. -----Output:----- Print the matrix, in the form of $N$ strings, after performing all the queries. -----Constraints----- - $1 \leq N,M \leq 1000$ - $0 \leq A_{ij} \leq 1$ - $1 \leq Q \leq 10^6$ - $1 \leq x_{1} \leq x_{2} \leq N$ - $1 \leq y_{1} \leq y_{2} \leq M$ -----Sample Input:----- 2 2 00 00 3 1 1 1 1 2 2 2 2 1 1 2 2 -----Sample Output:----- 01 10 -----Explanation:----- Example case 1: After processing the 1st query 1 1 1 1, matrix becomes: [1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix} After processing the 2nd query 2 2 2 2, the matrix becomes: [1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix} After processing the 3rd query 1 1 2 2, matrix becomes: [0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix} We need to output the matrix after processing all queries.
["import sys,os,io,time,copy,math,queue,bisect\nfrom collections import deque\nfrom functools import lru_cache\n\nif os.path.exists('input.txt'):\n sys.stdin = open('input.txt', 'r')\n sys.stdout = open('output.txt', 'w') \n\nsys.setrecursionlimit(100000000)\n\ndef main():\n n,m=map(int,input().split())\n mat=[]\n for _ in range(n):\n s=input()\n a=[]\n for i in s:\n a.append(int(i))\n mat.append(a)\n \n Q=int(input())\n ans=[[0 for i in range(m+1)] for j in range(n+1)]\n for i in range(Q):\n x1,y1,x2,y2=map(int,input().split())\n x1-=1\n y1-=1\n x2-=1\n y2-=1\n ans[x1][y1]+=1\n ans[x2+1][y1]-=1\n ans[x1][y2+1]-=1\n ans[x2+1][y2+1]+=1\n for j in range(m+1):\n for i in range(1,n+1):\n ans[i][j]=ans[i-1][j]+ans[i][j]\n for i in range(n+1):\n for j in range(1,m+1):\n ans[i][j]=ans[i][j-1]+ans[i][j]\n \n for i in range(n):\n for j in range(m):\n mat[i][j]=(ans[i][j]+mat[i][j])%2\n for m in mat:\n for i in m:\n print(i,end=\"\")\n print(\"\")\n\n \nmain()\n", "# cook your dish here\ndef issafe(a,b,n,m):\n if(0<=a<n and 0<=b<m):\n return True\n return False\ndef main():\n n,m=map(int,input().split())\n mat=[list(map(int,list(input()))) for i in range(n)]\n qmat=[[0 for i in range(m)] for j in range(n)]\n q=int(input())\n query=[list(map(int,input().split())) for i in range(q)]\n for k in query:\n i1,j1,i2,j2=map(lambda x:x-1,k)\n qmat[i1][j1]+=1\n if(issafe(i2+1,j2+1,n,m)):\n qmat[i2+1][j2+1]+=1\n if(issafe(i1,j2+1,n,m)):\n qmat[i1][j2+1]+=-1\n if(issafe(i2+1,j1,n,m)):\n qmat[i2+1][j1]+=-1\n for i in range(n-1):\n for j in range(m):\n qmat[i+1][j]+=qmat[i][j]\n for i in range(n):\n for j in range(m-1):\n qmat[i][j+1]+=qmat[i][j]\n for i in range(n):\n for j in range(m):\n qmat[i][j]=(qmat[i][j]+mat[i][j])%2\n for i in qmat:\n print(*i,sep='')\nmain()", "def issafe(a,b,n,m):\n if(0<=a<n and 0<=b<m):\n return True\n return False\ndef main():\n n,m=map(int,input().split())\n mat=[list(map(int,list(input()))) for i in range(n)]\n qmat=[[0 for i in range(m)] for j in range(n)]\n q=int(input())\n query=[list(map(int,input().split())) for i in range(q)]\n # print(query)\n for k in query:\n i1,j1,i2,j2=map(lambda x:x-1,k)\n qmat[i1][j1]+=1\n if(issafe(i2+1,j2+1,n,m)):\n qmat[i2+1][j2+1]+=1\n if(issafe(i1,j2+1,n,m)):\n qmat[i1][j2+1]+=-1\n if(issafe(i2+1,j1,n,m)):\n qmat[i2+1][j1]+=-1\n for i in range(n-1):\n for j in range(m):\n qmat[i+1][j]+=qmat[i][j]\n for i in range(n):\n for j in range(m-1):\n qmat[i][j+1]+=qmat[i][j]\n for i in range(n):\n for j in range(m):\n qmat[i][j]=(qmat[i][j]+mat[i][j])%2\n for i in qmat:\n print(*i,sep='')\nmain()\n# import sys,threading\n# max_recur_size = 10**5+ 10000\n# max_stack_size = max_recur_size*500\n# sys.setrecursionlimit(max_recur_size)\n# threading.stack_size(max_stack_size)\n# thread = threading.Thread(target=main)\n# thread.start()", "def issafe(a,b,n,m):\n if(0<=a<n and 0<=b<m):\n return True\n return False\ndef main():\n n,m=map(int,input().split())\n mat=[list(map(int,list(input()))) for i in range(n)]\n qmat=[[0 for i in range(m)] for j in range(n)]\n q=int(input())\n query=[list(map(int,input().split())) for i in range(q)]\n # print(query)\n for k in query:\n i1,j1,i2,j2=map(lambda x:x-1,k)\n qmat[i1][j1]+=1\n if(issafe(i2+1,j2+1,n,m)):\n qmat[i2+1][j2+1]+=1\n if(issafe(i1,j2+1,n,m)):\n qmat[i1][j2+1]+=-1\n if(issafe(i2+1,j1,n,m)):\n qmat[i2+1][j1]+=-1\n for i in range(n-1):\n for j in range(m):\n qmat[i+1][j]+=qmat[i][j]\n for i in range(n):\n for j in range(m-1):\n qmat[i][j+1]+=qmat[i][j]\n for i in range(n):\n for j in range(m):\n qmat[i][j]=(qmat[i][j]+mat[i][j])%2\n for i in qmat:\n print(*i,sep='')\nmain()\n# import sys,threading\n# max_recur_size = 10**5+ 10000\n# max_stack_size = max_recur_size*500\n# sys.setrecursionlimit(max_recur_size)\n# threading.stack_size(max_stack_size)\n# thread = threading.Thread(target=main)\n# thread.start()", "# -*- coding: utf-8 -*-\n\"\"\"\n@author: coding_don\n\"\"\"\nimport sys\n#sys.stdin = open('ip.txt', 'r') \n#sys.stdout = open('op.txt', 'w')\ntry:\n \n def main():\n t=1\n # t=int(sys.stdin.readline())\n while(t):\n n,m=[int(x) for x in sys.stdin.readline().split()]\n a=[]\n update=[]\n for i in range(n):\n tmp=sys.stdin.readline()\n a.append(tmp)\n update.append([0 for j in range(m+1)])\n update.append([0 for j in range(m+1)])\n q=int(sys.stdin.readline())\n while(q):\n x1,y1,x2,y2=[int(z) for z in sys.stdin.readline().split()]\n update[x1][y1]+=1\n if(x2<n):\n update[x2+1][y1]+=-1\n if(y2<m):\n update[x1][y2+1]+=-1\n if(x2<n and y2<m):\n update[x2+1][y2+1]+=1\n \n q-=1\n\n for i in range(1,n+1):\n for j in range(1,m+1):\n update[i][j]+=update[i][j-1]\n\n for j in range(1,m+1):\n for i in range(1,n+1):\n update[i][j]+=update[i-1][j]\n for i in range(1,n+1):\n for j in range(1,m+1):\n if(update[i][j]%2==0):\n sys.stdout.write(a[i-1][j-1])\n else:\n sys.stdout.write(str(1-(ord(a[i-1][j-1])-48)))\n\n sys.stdout.write('\\n')\n\n t-=1\n \n main()\n \nexcept Exception as e:\n sys.stdout.write(\"ErrOR : \"+str(e))", "# cook your dish here\ndef flip(dp,x1,y1,x2,y2,n,m):\n dp[x1][y1] += 1\n \n if y2+1 < m:\n dp[x1][y2+1] -= 1\n\n if x2+1 < n:\n dp[x2+1][y1] -= 1\n\n if x2+1 < n and y2+1 < m:\n dp[x2+1][y2+1] += 1\n\ndef main():\n n,m = map(int,input().split())\n matrix = []\n for i in range(n):\n matrix.append(list(map(int,input())))\n\n\n dp = [[0 for j in range(m)] for i in range(n)]\n q = int(input())\n for i in range(q):\n x1,y1,x2,y2 = map(int,input().split())\n x1 -= 1\n y1 -= 1\n x2 -= 1\n y2 -= 1\n flip(dp,x1,y1,x2,y2,n,m)\n\n for i in range(n):\n for j in range(1,m):\n dp[i][j] += dp[i][j-1]\n\n for i in range(1,n):\n for j in range(m):\n dp[i][j] += dp[i-1][j]\n\n for i in range(n):\n for j in range(m):\n if dp[i][j]%2 == 0:\n print(matrix[i][j],end = '')\n else:\n print(int(not matrix[i][j]),end = '')\n\n print()\n\nmain()\n", "r,c=list(map(int,input().split()))\nA=[] #Given Matrix\ndp=[[0 for i in range(c+1)] for j in range(r+1)]\nfor i in range(r):\n A.append([int(j) for j in list(input())])\n #print(A)\n\nq=int(input())\n\n#print(A,dp)\nfor i in range(q):\n x1,y1,x2,y2=list(map(int,input().split()))\n #print(x1,y1,x2,y2)\n x1,y1,x2,y2=x1-1,y1-1,x2-1,y2-1\n dp[x1][y1]+=1\n dp[x2+1][y2+1]+=1\n dp[x1][y2+1]-=1\n dp[x2+1][y1]-=1\n\nfor i,v1 in enumerate(A):\n for j,v2 in enumerate(v1):\n if i==0 and j==0:\n pass\n elif i==0:\n dp[i][j]+=dp[i][j-1]\n elif j==0:\n dp[i][j]+=dp[i-1][j]\n else:\n dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]\n #print(A,i,j)\n\n if dp[i][j]%2!=0:\n A[i][j]=1-A[i][j]\nfor i in A:\n for j in i:\n print(j,end=\"\")\n print()\n\n\n", "import sys\n \nn, m = map(int, sys.stdin.readline().strip().split())\n \narr = []\nfor i in range(n):\n s = input() + \"0\"\n temp = []\n for x in s:\n temp += [int(x)]\n arr += [temp]\n \narr += [[0]*(m+1)]\n \nadd = [[0 for i in range(m+1)] for j in range(n+1)]\npref1 = [[0 for i in range(m+1)] for j in range(n+1)]\npref2 = [[0 for i in range(m+1)] for j in range(n+1)]\n \nq = int(sys.stdin.readline().strip())\nfor _ in range(q):\n x1, y1, x2, y2 = map(int, sys.stdin.readline().strip().split())\n x1 -= 1\n y1 -= 1\n x2 -= 1\n y2 -= 1\n pref2[x1][y1] += 1\n pref2[x2+1][y1] -= 1\n pref2[x1][y2+1] += -1\n pref2[x2+1][y2+1] -= -1\n \nfor i in range(m):\n for j in range(n):\n if j == 0:\n pref1[j][i] = pref2[j][i]\n else:\n pref1[j][i] = pref1[j-1][i] + pref2[j][i]\n \nfor i in range(n):\n for j in range(m):\n if j == 0:\n add[i][j] += pref1[i][j]\n else:\n add[i][j] = add[i][j-1] + pref1[i][j]\n \nfor i in range(n):\n for j in range(m):\n arr[i][j] += add[i][j]\n arr[i][j] %= 2\n \nfor i in range(n):\n for j in range(m):\n print(arr[i][j], end='')\n print('')\n", "n,m=map(int,input().split())\nmat=[]\nfor _ in range(n):\n s=input()\n l=list(map(int,s))\n mat.append(l)\npref=[[0 for _ in range(m+1)] for _ in range(n+1)]\np=[[0 for _ in range(m)] for _ in range(n)]\nadd=[[0 for _ in range(m)] for _ in range(n)]\nfor _ in range(int(input())):\n x1,y1,x2,y2=map(int,input().split())\n pref[x1-1][y1-1]+=1\n pref[x2][y1-1]-=1\n pref[x1-1][y2]-=1\n pref[x2][y2]+=1\nfor i in range(n):\n for j in range(m):\n if i==0:\n p[i][j]=pref[i][j]\n else:\n p[i][j]=p[i-1][j]+pref[i][j]\nfor i in range(n):\n for j in range(m):\n if j==0:\n add[i][j]=p[i][j]\n else:\n add[i][j]=add[i][j-1]+p[i][j]\nfor i in range(n):\n for j in range(m):\n mat[i][j]+=add[i][j]\n mat[i][j]%=2\nfor i in range(n):\n for j in range(m):\n print(mat[i][j],end=\"\")\n print()\n", "n, m = map(int, input().split())\na = []\nb = [0] * (m + 1)\nfor i in range(n + 1):\n a.append(b[:])\n# print(a[n - 1][m])\n# print(a[1][2])\n# print(a)\np = []\nfor i in range(n):\n r = input()\n q = []\n for j in range(len(r)):\n q.append(int(r[j]))\n p.append(q[:])\n# print(p)\nq = int(input())\nwhile(q):\n i, j, k, l = map(int, input().split())\n a[i - 1][j - 1] += 1\n a[i - 1][l] -= 1\n a[k][j - 1] -= 1\n # print(k - 1, l)\n a[k][l] += 1\n q -= 1\nc = 0\n# print(a)\nfor j in range(m + 1):\n for i in range(n + 1):\n c += a[i][j]\n a[i][j] = c\n\nfor i in range(n):\n for j in range(m):\n c += a[i][j]\n if(c % 2 == 1):\n if(p[i][j] == 1):\n p[i][j] = 0\n else:\n p[i][j] = 1\n c += a[i][m]\n# print(a)\nfor i in range(n):\n for j in range(m):\n print(p[i][j], end=\"\")\n print()\n", "# cook your dish here\nimport sys\n \nn, m = map(int, sys.stdin.readline().strip().split())\n \narr = []\nfor i in range(n):\n s = input() + \"0\"\n temp = []\n for x in s:\n temp += [int(x)]\n arr += [temp]\n \narr += [[0]*(m+1)]\n \nadd = [[0 for i in range(m+1)] for j in range(n+1)]\npref1 = [[0 for i in range(m+1)] for j in range(n+1)]\npref2 = [[0 for i in range(m+1)] for j in range(n+1)]\n \nq = int(sys.stdin.readline().strip())\nfor _ in range(q):\n x1, y1, x2, y2 = map(int, sys.stdin.readline().strip().split())\n x1 -= 1\n y1 -= 1\n x2 -= 1\n y2 -= 1\n pref2[x1][y1] += 1\n pref2[x2+1][y1] -= 1\n pref2[x1][y2+1] += -1\n pref2[x2+1][y2+1] -= -1\n \nfor i in range(m):\n for j in range(n):\n if j == 0:\n pref1[j][i] = pref2[j][i]\n else:\n pref1[j][i] = pref1[j-1][i] + pref2[j][i]\n \nfor i in range(n):\n for j in range(m):\n if j == 0:\n add[i][j] += pref1[i][j]\n else:\n add[i][j] = add[i][j-1] + pref1[i][j]\n \nfor i in range(n):\n for j in range(m):\n arr[i][j] += add[i][j]\n arr[i][j] %= 2\n \nfor i in range(n):\n for j in range(m):\n print(arr[i][j], end='')\n print('')\n", "import sys\n \nn, m = map(int, sys.stdin.readline().strip().split())\n \narr = []\nfor i in range(n):\n s = input() + \"0\"\n temp = []\n for x in s:\n temp += [int(x)]\n arr += [temp]\n \narr += [[0]*(m+1)]\n \nadd = [[0 for i in range(m+1)] for j in range(n+1)]\npref1 = [[0 for i in range(m+1)] for j in range(n+1)]\npref2 = [[0 for i in range(m+1)] for j in range(n+1)]\n \nq = int(sys.stdin.readline().strip())\nfor _ in range(q):\n x1, y1, x2, y2 = map(int, sys.stdin.readline().strip().split())\n x1 -= 1\n y1 -= 1\n x2 -= 1\n y2 -= 1\n pref2[x1][y1] += 1\n pref2[x2+1][y1] -= 1\n pref2[x1][y2+1] += -1\n pref2[x2+1][y2+1] -= -1\n \nfor i in range(m):\n for j in range(n):\n if j == 0:\n pref1[j][i] = pref2[j][i]\n else:\n pref1[j][i] = pref1[j-1][i] + pref2[j][i]\n \nfor i in range(n):\n for j in range(m):\n if j == 0:\n add[i][j] += pref1[i][j]\n else:\n add[i][j] = add[i][j-1] + pref1[i][j]\n \nfor i in range(n):\n for j in range(m):\n arr[i][j] += add[i][j]\n arr[i][j] %= 2\n \nfor i in range(n):\n for j in range(m):\n print(arr[i][j], end='')\n print('')\n", "import sys\nimport bisect\n#input=sys.stdin.readline\n#t=int(input())\nt=1\nfor _ in range(t):\n n,m=map(int,input().split())\n #n=int(input())\n l=[]\n #l=list(map(int,input().split()))\n arr=[[0 for j in range(m+2)] for i in range(n+2)]\n for i in range(n):\n l.append(input())\n #print(arr) \n q=int(input())\n for __ in range(q):\n x1,y1,x2,y2=map(int,input().split())\n arr[x1][y1]+=1;\n arr[x2+1][y1]-=1\n arr[x1][y2+1]-=1\n arr[x2+1][y2+1]+=1\n for i in range(1,n+1):\n for j in range(1,m+1):\n x1=(arr[i-1][j]+arr[i][j-1])\n y1=(x1-arr[i-1][j-1])\n arr[i][j]=(arr[i][j]+y1)\n \n #arr[i][j]%=2\n print((arr[i][j]+int(l[i-1][j-1]))%2,end=\"\")\n print() \n print() \n ", "n,m=map(int,input().split())\nmat=[]\nfor i in range(n):\n me=[int(i) for i in list(input())]\n mat.append(me)\ndummy=[[0 for i in range(m+1)] for j in range(n+1)]\nq=int(input())\nfor i in range(q):\n x1,y1,x2,y2=map(int,input().split())\n dummy[x1-1][y1-1]+=1\n dummy[x2][y1-1]-=1 \n dummy[x2][y2]+=1 \n dummy[x1-1][y2]-=1\nfor i in range(n+1):\n for j in range(m+1):\n if j==0:\n continue\n else:\n dummy[i][j]+=dummy[i][j-1]\nfor j in range(m+1): \n for i in range(n+1): \n if i==0:\n continue \n else:\n dummy[i][j]+=dummy[i-1][j]\nfor i in range(n):\n for j in range(m):\n print((mat[i][j]+dummy[i][j]+2)%2,end=\"\")\n print()\nprint()", "\nn,m= map(int,input().split())\nMX=list()\nfor i in range(n):\n YYY=str(input())\n yy=list(YYY)\n a=[]\n for j in range(m):\n a.append(yy[j])\n MX.append(a)\n\nq=int(input())\ndp=[[0 for i in range(m)]for i in range(n)]\nfor _ in range(q):\n x,y,x2,y2=map(int,input().split())\n dp[x-1][y-1]+=1\n if x2<n:\n dp[x2][y-1]-=1\n if y2<m:\n dp[x-1][y2]-=1\n if x2<n and y2<m:\n dp[x2][y2]-=1\n\n\nfor i in range(n):\n for j in range(1,m):\n dp[i][j]+=dp[i][j-1]\n\nfor i in range(m):\n for j in range(1,n):\n dp[j][i]+=dp[j-1][i]\n\n# print(dp)\n# print(MX)\nfor i in range(n):\n for j in range(m):\n if dp[i][j]%2==1:\n if MX[i][j]=='0':\n MX[i][j]='1'\n else:\n MX[i][j]='0'\n\n \n\nfor i in range(n):\n for j in range(m):\n print(MX[i][j],end=\"\")\n\n print()\n", "import sys\ndef get_array(): return list(map(int , sys.stdin.readline().strip().split()))\ndef get_ints(): return list(map(int, sys.stdin.readline().strip().split()))\ndef input(): return sys.stdin.readline().strip()\nimport math\nfrom collections import defaultdict\n\nn, m = get_ints()\na = []\narr = [[0 for _ in range(m+2)] for _ in range(n+2)]\nfor _ in range(n):\n # t = get_array()\n t = list(input())\n # t.append('0')\n a.append(t)\n\nfor i in range(n):\n for j in range(m):\n a[i][j] = int(a[i][j])\n\n# print(a)\n# print(arr)\n\n\nq = int(input())\nfor _ in range(q):\n x1,y1,x2,y2 = get_ints()\n # x1-=1\n # y1-=1\n # x2-=1\n # y2-=1\n arr[x1][y1]+=1\n arr[x2+1][y2+1]+=1\n arr[x1][y2 + 1]-=1\n arr[x2 + 1][y1]-=1\n\n# print(\"hello \",arr)\n\nfor i in range(1,n+1):\n for j in range(1,m+1):\n arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1]\n if(a[i-1][j-1] == 0 and arr[i][j]%2==0):\n a[i-1][j-1] = 0\n elif(a[i-1][j-1] == 1 and arr[i][j]%2 == 0):\n a[i-1][j-1] = 1\n elif(a[i-1][j-1] == 0 and arr[i][j]%2==1):\n a[i-1][j-1] = 1\n elif(a[i-1][j-1] == 1 and arr[i][j]%2==1):\n a[i-1][j-1] = 0\n\n# print(arr)\n# print(a)\n\n# for i in range(n):\n# for j in range(m):\n# print(a[i][j],end = \"\")\n# print()\n\nfor i in range(n):\n print(''.join(map(str,a[i])))\n\n\n\n\n", "import sys\ndef get_array(): return list(map(int , sys.stdin.readline().strip().split()))\ndef get_ints(): return map(int, sys.stdin.readline().strip().split())\ndef input(): return sys.stdin.readline().strip()\nimport math\nfrom collections import defaultdict\n\nn, m = get_ints()\na = []\narr = [[0 for _ in range(m+2)] for _ in range(n+2)]\nfor _ in range(n):\n # t = get_array()\n t = list(input())\n # t.append('0')\n a.append(t)\n\nfor i in range(n):\n for j in range(m):\n a[i][j] = int(a[i][j])\n\n# print(a)\n# print(arr)\n\n\nq = int(input())\nfor _ in range(q):\n x1,y1,x2,y2 = get_ints()\n # x1-=1\n # y1-=1\n # x2-=1\n # y2-=1\n arr[x1][y1]+=1\n arr[x2+1][y2+1]+=1\n arr[x1][y2 + 1]-=1\n arr[x2 + 1][y1]-=1\n\n# print(\"hello \",arr)\n\nfor i in range(1,n+1):\n for j in range(1,m+1):\n arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1]\n if(a[i-1][j-1] == 0 and arr[i][j]%2==0):\n a[i-1][j-1] = 0\n elif(a[i-1][j-1] == 1 and arr[i][j]%2 == 0):\n a[i-1][j-1] = 1\n elif(a[i-1][j-1] == 0 and arr[i][j]%2==1):\n a[i-1][j-1] = 1\n elif(a[i-1][j-1] == 1 and arr[i][j]%2==1):\n a[i-1][j-1] = 0\n\n# print(arr)\n# print(a)\n\nfor i in range(n):\n for j in range(m):\n print(a[i][j],end = \"\")\n print()\n\n# for i in range(n):\n# print(''.join(map(str,a[i])))\n\n\n\n\n", "import sys\nn,m=map(int,input().split())\ndp=[[0 for j in range(m+2)] for k in range(n+2)]\na=[]\nfor j in range(n):\n t=sys.stdin.readline().strip()\n a.append(list(t))\nq=int(input())\nfor j in range(0,q):\n x1,y1,x2,y2=map(int,sys.stdin.readline().split())\n dp[x1][y1]+=1\n dp[x2+1][y2+1]+=1\n dp[x1][y2+1]-=1\n dp[x2+1][y1]-=1\n#print(dp)\nfor j in range(1,n+1):\n for k in range(1,m+1):\n dp[j][k]=dp[j][k]+dp[j-1][k]+dp[j][k-1]-dp[j-1][k-1]\n #print(dp[j][k],a[j-1][k-1])\n if dp[j][k]%2!=0:\n a[j-1][k-1]=int(a[j-1][k-1])^1\nfor j in range(n):\n print(''.join(map(str,a[j])))", "import sys\nn,m=map(int,input().split())\ndp=[[0 for j in range(m+2)] for k in range(n+2)]\na=[]\nfor j in range(n):\n t=sys.stdin.readline().strip()\n a.append(list(t))\n#print(a)\nq=int(input())\nfor j in range(0,q):\n x1,y1,x2,y2=map(int,sys.stdin.readline().split())\n dp[x1][y1]+=1\n dp[x2+1][y2+1]+=1\n dp[x1][y2+1]-=1\n dp[x2+1][y1]-=1\n#print(dp)\nfor j in range(1,n+1):\n for k in range(1,m+1):\n dp[j][k]=dp[j][k]+dp[j-1][k]+dp[j][k-1]-dp[j-1][k-1]\n #print(dp[j][k],a[j-1][k-1])\n if dp[j][k]%2!=0:\n a[j-1][k-1]=int(a[j-1][k-1])^1\nfor j in range(n):\n print(''.join(map(str,a[j])))"]
{"inputs": [["2 2", "00", "00", "3", "1 1 1 1", "2 2 2 2", "1 1 2 2"]], "outputs": [["01", "10"]]}
INTERVIEW
PYTHON3
CODECHEF
17,725
877404165b306960c8017d6f2a266247
UNKNOWN
You are given a grid with dimension $n$ x $m$ and two points with coordinates $X(x1,y1)$ and $Y(x2,y2)$ . Your task is to find the number of ways in which one can go from point $A(0, 0)$ to point $B (n, m)$ using the $shortest$ possible path such that the shortest path neither passes through $X$ nor through $Y$. Consider the above 4 x 4 grid . Our shortest path can't pass through points (1,3) and (3,3) (marked by yellow dots). One of the possible shortest path is from $A$ to $C$ and then from $C$ to $B$. -----Input:----- - First line contains $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, six space separated integers $n, m, x1, y1, x2, y2$. -----Output:----- - For each testcase, output in a single line number of ways modulo $998244353$. -----Constraints----- - $1 \leq T \leq 10^5$ - $3 \leq n,m \leq 10^5$ - $1 \leq x1, x2 \leq n - 1$ - $1 \leq y1, y2 \leq m - 1$ - $x1 \leq x2$ - $y1 \leq y2$ - $X$ and $Y$ never coincide. -----Sample Input:----- 1 3 3 1 1 1 2 -----Sample Output:----- 5
["#dt = {} for i in x: dt[i] = dt.get(i,0)+1\r\nimport sys;input = sys.stdin.readline\r\ninp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]\r\n\r\nN = 100001\r\np = 998244353\r\nfactorialNumInverse = [0]*(N+1) \r\nnaturalNumInverse = [0]*(N+1)\r\nfact = [0]*(N+1)\r\n \r\ndef InverseofNumber(p): \r\n naturalNumInverse[0] = naturalNumInverse[1] = 1\r\n for i in range(2,N+1): \r\n naturalNumInverse[i] = (naturalNumInverse[p % i] * (p - (p // i)) % p)\r\n \r\ndef InverseofFactorial(p): \r\n factorialNumInverse[0] = factorialNumInverse[1] = 1\r\n for i in range(2,N+1): \r\n factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p \r\n \r\ndef factorial(p): \r\n fact[0] = 1\r\n for i in range(1, N + 1): \r\n fact[i] = (fact[i - 1] * i) % p\r\n\r\ndef f(num,den1,den2):\r\n # n C r = n!*inverse(r!)*inverse((n-r)!) \r\n #ans = ((fact[N] * factorialNumInverse[R])% p * factorialNumInverse[N-R])% p\r\n ans = ((fact[num]*factorialNumInverse[den1])%p*factorialNumInverse[den2])%p\r\n return ans \r\n\r\nInverseofNumber(p) \r\nInverseofFactorial(p) \r\nfactorial(p)\r\n\r\nfor _ in range(inp()):\r\n n,m,x1,y1,x2,y2 = ip()\r\n tot = f(m+n,m,n)\r\n a = f(m-y1+n-x1,m-y1,n-x1)\r\n aa = f(x1+y1,x1,y1)\r\n b = f(m-y2+n-x2,m-y2,n-x2)\r\n bb = f(x2+y2,x2,y2)\r\n c = f(y2-y1+x2-x1,y2-y1,x2-x1)\r\n ans = (tot - a*aa - b*bb + c*aa*b)%p\r\n print(ans)"]
{"inputs": [["1", "3 3 1 1 1 2"]], "outputs": [["5"]]}
INTERVIEW
PYTHON3
CODECHEF
1,471
ea44ccd542458169f71b809c532785dd
UNKNOWN
The chef is trying to decode some pattern problems, Chef wants your help to code it. Chef has one number K to form a new pattern. Help the chef to code this pattern problem. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, one integer $K$. -----Output:----- For each test case, output as the pattern. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq K \leq 100$ -----Sample Input:----- 4 1 2 3 4 -----Sample Output:----- 0 01 10 012 101 210 0123 1012 2101 3210 -----EXPLANATION:----- No need, else pattern can be decode easily.
["# cook your dish here\nfor _ in range(int(input())):\n n=int(input())\n if n==1:\n print(\"0\")\n else:\n s=[]\n for i in range(n):\n s.append(str(i))\n print(''.join(s))\n p=1\n for i in range(n-1):\n s.pop(n-1)\n s=[str(p)]+s\n print(''.join(s))\n p+=1\n", "from sys import stdin, stdout, maxsize\nfrom math import sqrt, log, factorial, gcd\nfrom collections import defaultdict as D\nfrom bisect import insort\n\nfor _ in range(int(input())):\n n = int(input()) - 1\n for i in range(n, -1, -1):\n for j in range(n - i, 0, -1): print(j, end = '')\n for j in range(i + 1): print(j, end = '')\n print()", "import sys\r\ninput = lambda: sys.stdin.readline().rstrip(\"\\r\\n\")\r\ninp = lambda: list(map(int,sys.stdin.readline().rstrip(\"\\r\\n\").split()))\r\n#______________________________________________________________________________________________________\r\n# from math import *\r\n# from bisect import *\r\n# from heapq import *\r\n# from collections import defaultdict as dd\r\n# from collections import OrderedDict as odict\r\nfrom collections import Counter as cc\r\n# from collections import deque\r\n# sys.setrecursionlimit(2*(10**5)+100) this is must for dfs\r\nmod = 10**9+7; md = 998244353\r\n# ______________________________________________________________________________________________________\r\n# segment tree for range minimum query\r\n# sys.setrecursionlimit(10**5)\r\n# n = int(input())\r\n# a = list(map(int,input().split()))\r\n# st = [float('inf') for i in range(4*len(a))]\r\n# def build(a,ind,start,end):\r\n# \tif start == end:\r\n# \t\tst[ind] = a[start]\r\n# \telse:\r\n# \t\tmid = (start+end)//2\r\n# \t\tbuild(a,2*ind+1,start,mid)\r\n# \t\tbuild(a,2*ind+2,mid+1,end)\r\n# \t\tst[ind] = min(st[2*ind+1],st[2*ind+2])\r\n# build(a,0,0,n-1)\r\n# def query(ind,l,r,start,end):\r\n# \tif start>r or end<l:\r\n# \t\treturn float('inf')\r\n# \tif l<=start<=end<=r:\r\n# \t\treturn st[ind]\r\n# \tmid = (start+end)//2\r\n# \treturn min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))\r\n# ______________________________________________________________________________________________________\r\n# Checking prime in O(root(N))\r\n# def isprime(n):\r\n# if (n % 2 == 0 and n > 2) or n == 1: return 0\r\n# else:\r\n# s = int(n**(0.5)) + 1\r\n# for i in range(3, s, 2):\r\n# if n % i == 0:\r\n# return 0\r\n# return 1\r\n# def lcm(a,b):\r\n# return (a*b)//gcd(a,b)\r\n# ______________________________________________________________________________________________________\r\n# nCr under mod\r\n# def C(n,r,mod):\r\n# if r>n:\r\n# return 0\r\n# num = den = 1\r\n# for i in range(r):\r\n# num = (num*(n-i))%mod\r\n# den = (den*(i+1))%mod\r\n# return (num*pow(den,mod-2,mod))%mod\r\n# M = 10**5 +10\r\n# ______________________________________________________________________________________________________\r\n# For smallest prime factor of a number\r\n# M = 1000010\r\n# pfc = [i for i in range(M)]\r\n# def pfcs(M):\r\n# for i in range(2,M):\r\n# if pfc[i]==i:\r\n# for j in range(i+i,M,i):\r\n# if pfc[j]==j:\r\n# pfc[j] = i\r\n# return\r\n# pfcs(M)\r\n# ______________________________________________________________________________________________________\r\ntc = 1\r\ntc, = inp()\r\nfor _ in range(tc):\r\n\tn, = inp()\r\n\ta = [[0 for i in range(n)] for j in range(n)]\r\n\tfor i in range(n):\r\n\t\tfor j in range(i+1,n):\r\n\t\t\ta[i][j] = a[i][j-1]+1\r\n\t\tfor j in range(i-1,-1,-1):\r\n\t\t\ta[i][j] = a[i][j+1]+1\r\n\tfor i in a:\r\n\t\tprint(*i,sep = \"\")", "for _ in range(int(input())):\r\n n = int(input())\r\n\r\n l = list(range(n))\r\n\r\n for i in range(n):\r\n print(*l, sep='')\r\n\r\n for j in range(i+1):\r\n l[j] += 1\r\n\r\n for j in range(i+1, n):\r\n l[j] -= 1\r\n", "from sys import stdin, stdout\nfrom math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log\nfrom collections import defaultdict as dd, deque\nfrom heapq import merge, heapify, heappop, heappush, nsmallest\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nmod = pow(10, 9) + 7\nmod2 = 998244353\ndef inp(): return stdin.readline().strip()\ndef out(var, end=\"\\n\"): stdout.write(str(var)+\"\\n\")\ndef outa(*var, end=\"\\n\"): stdout.write(' '.join(map(str, var)) + end)\ndef lmp(): return list(mp())\ndef mp(): return map(int, inp().split())\ndef smp(): return map(str, inp().split())\ndef l1d(n, val=0): return [val for i in range(n)]\ndef l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]\ndef remadd(x, y): return 1 if x%y else 0\ndef ceil(a,b): return (a+b-1)//b\n\ndef isprime(x):\n if x<=1: return False\n if x in (2, 3): return True\n if x%2 == 0: return False\n for i in range(3, int(sqrt(x))+1, 2):\n if x%i == 0: return False\n return True\n\nfor _ in range(int(inp())):\n n = int(inp())\n for i in range(n):\n for j in range(n):\n print(abs(i-j), end=\"\")\n print()"]
{"inputs": [["4", "1", "2", "3", "4"]], "outputs": [["0", "01", "10", "012", "101", "210", "0123", "1012", "2101", "3210"]]}
INTERVIEW
PYTHON3
CODECHEF
5,219
6753bea5ef1ad6c77d70c73a8bb13373
UNKNOWN
The Siruseri amusement park has a new attraction. It consists of a rectangular array of discs. Each disc is divided into four equal sectors and the four sectors are coloured with the colours Red, Blue, Green and Yellow (in some order). The ordering may be different in different discs. Here is a possible arrangment of the discs: You start at the top left disc. Your task is to walk down to the bottom right disc. In each step, you can rotate the current disc by $90$ degrees in the clockwise direction or move to a neighbouring disc. However, you can move to a neighbouring disc only if adjacent sectors of these two discs have the same colour. For example, in the figure above, you can move from the disc at position ($1$, $1$) to either of its neighbours. However, from the disc at position ($1$, $2$) you can only move to the disc at position ($1$, $1$). If you wish to move from ($1$, $2$) to ($1$, $3$) then you would have to rotate this disc three times so that the colour (red) on the adjacent sectors are the same. For each rotate move, a penalty point is added to the score. The aim is to reach the bottom right with as few penalty points as possible. For example, in the arrangement described in the above figure, the best possible score is $2$, corresponding to the following path: Move from ($1$, $1$) to ($2$, $1$). Move from ($2$, $1$) to ($2$, $2$). Rotate. Rotate. Move from ($2$, $2$) to ($2$, $3$). Move from ($2$, $3$) to ($1$, $3$). Move from ($1$, $3$) to ($1$, $4$). Finally, move from ($1$, $4$) to ($2$, $4$). Your task is to determine the minimum number of penalty points required to go from the top left disc to the bottom right disc for the given arrangement of discs. -----Input:----- The first line contains two integers $M$ and $N$. $M$ is the number of rows and $N$ is the number of columns in the given arrangment. This is followed by $M \times N$ lines of input. Lines $2$ to $N+1$, describe the colours on the discs on the first row, Lines $N+2$ to $2 \cdot N+1$ describe the colours on discs in the second row and so on. Each of these lines contain a permutation of the set {R, G, B, Y} giving the colours in the top, right, bottom and left sectors in that order. -----Output:----- A single integer on a single line indicating the minimum number of penalty points required to go from the top left disc to the bottom right disc for the given arrangement of discs. -----Constraints:----- - In $80 \%$ of the input, $1 \leq M,N \leq 60$. - In all the inputs $1 \leq M,N \leq 1000$. -----Sample Input----- 2 4 G Y B R B G R Y G Y B R G R B Y B Y G R G B R Y B R G Y B R G Y -----Sample Output----- 2
["#for _ in range(int(input()):\n#n,m = map(int,input().split())\n#x = [int(w) for w in input().split()]\n#n = int(input())\n#x = [int(input()) for _ in range(n)]\n#for i in range(n):\n#dt = {} for i in x:dt[i] = dt.get(i,0)+1\n#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}\n\nm,n = map(int,input().split()) # top,right,bottom,left\nx = []\nfor i in range(m):\n x.append([])\n for j in range(n):\n clr = [w for w in input().split()]\n x[i].append(clr)\n\nimport queue as Q \ndp = [float('inf')]*m\nfor i in range(m):\n dp[i] = [float('inf')]*n\ndp[m-1][n-1] = 0\npq = Q.PriorityQueue()\npq.put([dp[m-1][n-1],m-1,n-1])\nvisited = set()\nxx,yy = [-1,0,1,0],[0,1,0,-1] # top,right,bottom,left\nwhile not pq.empty():\n pop = pq.get()\n cx,cy = pop[1],pop[2]\n if (cx,cy) not in visited:\n visited.add((cx,cy))\n for k in range(4):\n nx,ny = cx+xx[k],cy+yy[k]\n if 0<=nx<m and 0<=ny<n and (nx,ny) not in visited:\n clr = x[cx][cy][k]\n #print(\"*\",nx,ny,\"_\",k,clr)\n ind = x[nx][ny].index(clr)\n cost = (k-(ind+2)%4)%4\n #print(cost)\n if dp[cx][cy]+cost < dp[nx][ny]:\n dp[nx][ny] = dp[cx][cy]+cost\n pq.put([dp[nx][ny],nx,ny])\n #print(\"#############\")\n#print(dp)\nprint(dp[0][0])", "from bisect import insort\nclass disk:\n def __init__(self):\n self.U = ''\n self.D = ''\n self.L = ''\n self.R = ''\n \n \nm, n = map(int,input().split())\nadj = [[] for _ in range(m*n)]\ngrid = [[disk() for _ in range(n)] for _ in range(m)]\nfor i in range(m):\n for j in range(n):\n grid[i][j].U,grid[i][j].R,grid[i][j].D,grid[i][j].L = input().split()\nfor i in range(m):\n\tfor j in range(n):\n\t\tif (j!=0):\n\n\t\t\tif (grid[i][j].R == grid[i][j-1].R):\n\t\t\t dist = 2\n\t\t\tif (grid[i][j].U == grid[i][j-1].R): \n\t\t\t dist = 3\n\t\t\tif (grid[i][j].L == grid[i][j-1].R): \n\t\t\t dist = 0\n\t\t\tif (grid[i][j].D == grid[i][j-1].R): \n\t\t\t dist = 1\n\t\t\tadj[i*n + j].append((dist, i*n + j - 1))\n\t\t\n\t\tif (j!=n-1):\n\n\t\t\tif (grid[i][j].R == grid[i][j+1].L): \n\t\t\t dist = 0\n\t\t\tif (grid[i][j].U == grid[i][j+1].L): \n\t\t\t dist = 1\n\t\t\tif (grid[i][j].L == grid[i][j+1].L): \n\t\t\t dist = 2\n\t\t\tif (grid[i][j].D == grid[i][j+1].L): \n\t\t\t dist = 3\n\t\t\tadj[i*n + j].append((dist, i*n + j + 1))\n\n\t\t\n\t\tif (i!=0):\n\n\t\t\tif (grid[i][j].R == grid[i-1][j].D):\n\t\t\t dist = 3\n\t\t\tif (grid[i][j].U == grid[i-1][j].D):\n\t\t\t dist = 0\n\t\t\tif (grid[i][j].L == grid[i-1][j].D):\n\t\t\t dist = 1\n\t\t\tif (grid[i][j].D == grid[i-1][j].D):\n\t\t\t dist = 2\n\t\t\tadj[i*n + j].append((dist, i*n + j - n))\n\n\t\t\n\t\tif (i!=m-1):\n\n\t\t\tif (grid[i][j].R == grid[i+1][j].U): \n\t\t\t dist = 1\n\t\t\tif (grid[i][j].U == grid[i+1][j].U): \n\t\t\t dist = 2\n\t\t\tif (grid[i][j].L == grid[i+1][j].U): \n\t\t\t dist = 3\n\t\t\tif (grid[i][j].D == grid[i+1][j].U):\n\t\t\t dist = 0\n\t\t\tadj[i*n + j].append((dist, i*n + j + n))\nq = []\nq.append((0,0))\ndists = [2147483647 for _ in range(m*n)]\nvisited = [False for _ in range(m*n)]\ndists[0] = 0\nwhile q:\n cur = q[-1]\n q.pop()\n if visited[cur[1]] == False:\n visited[cur[1]] = True\n dists[cur[1]] = -1*cur[0]\n for i in range(len(adj[cur[1]])):\n to = adj[cur[1]][i][1]\n dis = adj[cur[1]][i][0]\n if (not visited[to] and dists[cur[1]] + dis < dists[to]):\n insort(q,(-1*(dists[cur[1]] + dis),to))\nprint(dists[m*n - 1])"]
{"inputs": [["2 4", "G Y B R", "B G R Y", "G Y B R", "G R B Y", "B Y G R", "G B R Y", "B R G Y", "B R G Y"]], "outputs": [["2"]]}
INTERVIEW
PYTHON3
CODECHEF
3,707
49fd12f8a557de187d97f78570e39cd8
UNKNOWN
You are playing a game where you have been sent in a town to collect 10 types of coin and their symbol are defined with $A, B, C, D, E, F, G, H , I, J$. In that town every enemy have a coin. By killing one you will get a coin from that enemy. Each enemy have only a unique coin. The challange of the game is You have to collect all the coin and only then you will get the victory. You are a brave gamer so you took this hard challange and successfully finished it. After finishing, you are thinking of the game. You know the order off collecting coin. Now you are thinking how many enemy did you have killed? Can you solve that out? -----Input:----- First line of the input is an integer $T$.Next T line consists of a string which denotes the order of your collecting coins. The string consists of Uppercase latin latter only and from A to J. -----Output:----- Print T line, in each line an integer with the number of enemy you have killed in the operation. -----Constraints----- - $1 \leq T \leq 5000$ -----Sample Input:----- 1 ABCDEFGHIJ -----Sample Output:----- 10
["for _ in range(int(input())):\n s = input()\n c = 0\n for i in s:\n if i.isalpha() and i.isupper():\n c += 1\n print(c)\n", "for _ in range(int(input())):\n str=input()\n c=0\n for i in range(len(str)):\n if((str[i]>='A' and str[i] <= 'J')):\n c+=1 \n print(c) \n\n \n", "# cook your dish here\ntry:\n for _ in range(int(input())):\n q = input()\n print(10)\nexcept:\n pass", "# cook your dish here\ntry:\n \n l = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']\n t = int(input())\n \n for i in range(t):\n x = list(input())\n x = [i for i in x if i in l]\n print(len(x))\nexcept:\n pass", "for _ in range(int(input())):\n n=input()\n al='ABCDEFGHIJ'\n a=max(n)\n print(al.index(a)+1)\n", "# cook your dish here\nt=int(input())\nfor i in range(t):\n s=input()\n ms=['A','B','C','D','E','F','G','H','I','J']\n ct=0\n i=0\n while ms!=[]: \n if s[i] in ms:\n ind=ms.index(s[i])\n ms.pop(ind)\n ct+=1\n else:\n ct+=1\n i+=1\n print(ct)", "t = int(input())\nfor _ in range(t):\n s = input()\n a = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\"]\n enemy = 0\n for i in range(len(s)):\n if s[i] in a:\n enemy += 1\n print(enemy)", "# cook your dish here\nfor _ in range(int(input())):\n s=input()\n p=0\n for i in range(len(s)):\n if s[i]==\"A\":\n p+=1\n elif s[i]==\"B\":\n p+=1\n elif s[i]==\"C\":\n p+=1\n elif s[i]==\"D\":\n p+=1\n elif s[i]==\"E\":\n p+=1\n elif s[i]==\"F\":\n p+=1\n elif s[i]==\"G\":\n p+=1\n elif s[i]==\"H\":\n p+=1\n elif s[i]==\"I\":\n p+=1\n elif s[i]==\"J\":\n p+=1\n print(p) ", "# cook your dish here\nfor i in range(int(input())):\n s=input()\n print(10)", "t=int(input())\nfor _ in range(t):\n print(10)\n \n", "for i in range(int(input())):\n print(10)", "try:\n for i in range(int(input())):\n a=input()\n print(10)\n \nexcept:\n pass\n", "# cook your dish here\ntry:\n x=int(input())\n for _ in range(x):\n print(10)\nexcept:\n pass", "for _ in range(int(input())):\n s=input()\n se=set()\n ans=0\n for i in s:\n se.add(i)\n ans+=1\n if(len(se)==10):\n break\n print(ans)\n\n\n", "# cook your dish here\nt=int(input())\nwhile t>0:\n lst=[\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\"]\n n=input()\n op=0\n for i in n:\n if i in lst:\n op+=1\n print(op)\n t-=1", "for _ in range(int(input())):\n s=input()\n se=set()\n ans=0\n for i in s:\n se.add(i)\n ans+=1\n if(len(se)==10):\n break\n print(ans)\n\n\n", "for _ in range(int(input())):\n coins = input()\n print(10)", "\nfor _ in range(int(input())):\n s = input()\n if len(s)>10:\n print(10)\n else:print(len(s))", "#dt = {} for i in x: dt[i] = dt.get(i,0)+1\nimport sys;input = sys.stdin.readline\n#import io,os; input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #for \u00a0\u00a0\u00a0\u00a0pypy\ninp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]\n\nfor _ in range(inp()):\n s = input().strip()\n dt = {}\n for i in s: dt[i] = dt.get(i,0)+1\n print(len(dt))", "# cook your dish here\nT = int(input())\nwhile T > 0:\n s = str(input())\n T = T - 1\n L = list(s)\n Pa = L.index(\"A\")\n Pb = L.index(\"B\")\n Pc = L.index(\"C\")\n Pd = L.index(\"D\")\n Pe = L.index(\"E\")\n Pf = L.index(\"F\")\n Pg = L.index(\"G\")\n Ph = L.index(\"H\")\n Pi = L.index(\"I\")\n Pj = L.index(\"J\")\n List = [Pa, Pb, Pc, Pd, Pe, Pf, Pg, Ph, Pi, Pj]\n A = max(List)\n print(A + 1)", "# cook your dish here\nq = int(input())\nfor _ in range(q):\n \n arr = input()\n string = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\"]\n cnt = 0\n for i in arr:\n \n if(len(string)==0):\n break\n #print(string)\n try:\n string.remove(i)\n except:\n pass\n \n cnt+=1\n \n print(cnt)", "\nT=int(input())\nwhile(T):\n l=input()\n n=len(l)\n if(n>=10):\n print(10)\n else:\n print(n)\n \n T-=1", "# cook your dish here\nA=['A','B','C','D','E','F','G','H','I','J']\nfor _ in range(int(input())):\n s=input()\n l=[]\n count=0\n if len(s)==0:\n print(0)\n else:\n for i in s:\n if (i in A) and (i not in l):\n l.append(i)\n count+=1\n print(count)"]
{"inputs": [["1", "ABCDEFGHIJ"]], "outputs": [["10"]]}
INTERVIEW
PYTHON3
CODECHEF
4,078
d83b00b8d1b08b7213c8b507907298be
UNKNOWN
Chef loves to play with iron (Fe) and magnets (Ma). He took a row of $N$ cells (numbered $1$ through $N$) and placed some objects in some of these cells. You are given a string $S$ with length $N$ describing them; for each valid $i$, the $i$-th character of $S$ is one of the following: - 'I' if the $i$-th cell contains a piece of iron - 'M' if the $i$-th cell contains a magnet - '_' if the $i$-th cell is empty - ':' if the $i$-th cell contains a conducting sheet - 'X' if the $i$-th cell is blocked If there is a magnet in a cell $i$ and iron in a cell $j$, the attraction power between these cells is $P_{i,j} = K+1 - |j-i| - S_{i,j}$, where $S_{i,j}$ is the number of cells containing sheets between cells $i$ and $j$. This magnet can only attract this iron if $P_{i, j} > 0$ and there are no blocked cells between the cells $i$ and $j$. Chef wants to choose some magnets (possibly none) and to each of these magnets, assign a piece of iron which this magnet should attract. Each piece of iron may only be attracted by at most one magnet and only if the attraction power between them is positive and there are no blocked cells between them. Find the maximum number of magnets Chef can choose. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $K$. - The second line contains a single string $S$ with length $N$. -----Output----- For each test case, print a single line containing one integer ― the maximum number of magnets that can attract iron. -----Constraints----- - $1 \le T \le 2,000$ - $1 \le N \le 10^5$ - $0 \le K \le 10^5$ - $S$ contains only characters 'I', 'M', '_', ':' and 'X' - the sum of $N$ over all test cases does not exceed $5 \cdot 10^6$ -----Subtasks----- Subtask #1 (30 points): there are no sheets, i.e. $S$ does not contain the character ':' Subtask #2 (70 points): original constraints -----Example Input----- 2 4 5 I::M 9 10 MIM_XII:M -----Example Output----- 1 2 -----Explanation----- Example case 1: The attraction power between the only magnet and the only piece of iron is $5+1-3-2 = 1$. Note that it decreases with distance and the number of sheets. Example case 2: The magnets in cells $1$ and $3$ can attract the piece of iron in cell $2$, since the attraction power is $10$ in both cases. They cannot attract iron in cells $6$ or $7$ because there is a wall between them. The magnet in cell $9$ can attract the pieces of iron in cells $7$ and $6$; the attraction power is $8$ and $7$ respectively.
["# cook your dish here\n# cook your dish here\nfor _ in range(int(input())) :\n n,k=map(int,input().split())\n #reading the string \n s=input()\n i,j=0,0\n q=0\n while(i<n and j<n) :\n if(s[i]=='M') :\n if(s[j]=='I') :\n cnt=0\n if(i>j) :\n p=s[j:i]\n cnt=p.count(':')\n else :\n p=s[i:j]\n cnt=p.count(':')\n t=k+1-abs(i-j)-cnt\n if(t>0) :\n q+=1\n i+=1\n j+=1\n else:\n if(i<j) :\n i+=1\n else:\n j+=1\n elif(s[j]=='X') :\n j+=1\n i=j\n else:\n j+=1\n elif(s[i]=='X') :\n i+=1\n j=i\n else:\n i+=1\n print(q) ", "# cook your dish here\n\ndef solve(Str, K):\n S = ''\n ind = -1\n iron, mag = [], []\n for it in Str:\n ind += 1\n if it == ':':\n ind += 1\n\n if it == 'I':\n iron.append(ind)\n elif it == 'M':\n mag.append(ind)\n\n ans = 0\n lr = len(iron)\n lm = len(mag)\n\n i, j = 0, 0\n while i<lr and j<lm:\n ir, mg = iron[i], mag[j]\n if abs(ir - mg) <= K:\n ans += 1\n i+=1\n j+=1\n else:\n if ir > mg:\n j += 1\n else:\n i += 1\n return ans\n\nfor case in range(int(input())):\n n, k = list(map(int, input().split()))\n st = input()\n li = [it for it in st.split('X') if len(it)>0]\n\n ans = sum(solve(string, k) for string in li)\n print(ans)\n", "# cook your dish here\nt=int(input())\nfor t in range(t):\n n,k=list(map(int,input().split()))\n s=input()\n i=0\n j=0\n a=0\n while(i<n and j<n):\n if(s[i]=='M'):\n if(s[j]=='I'):\n c=0\n if(i>j):\n z=s[j:i]\n c=z.count(\":\")\n else:\n z=s[i:j]\n c=z.count(\":\")\n p=k+1-abs(i-j)-c\n if(p>0):\n a+=1\n i+=1\n j+=1\n else:\n if(i<j):\n i+=1\n else:\n j+=1\n elif(s[j]=='X'):\n j+=1\n i=j\n else:\n j+=1\n elif(s[i]=='X'):\n i+=1\n j=i\n else:\n i+=1\n print(a)\n\n \n \n", "t=int(input())\nfor _ in range(t):\n n,k=list(map(int,input().split()))\n s=list(input())\n ma,ir=[],[]\n total=0\n for i in range(n):\n k1=s[i]\n if k1=='M':\n ma.append(i)\n elif k1=='I':\n ir.append(i)\n if k1=='X' or i==n-1: \n alt=ir.copy()\n el,l=0,len(ir)\n for mag in ma:\n for j in range(l):\n iron=ir[j]\n if iron>mag:\n #sheet=s[mag:iron].count(\":\")\n sheet=0\n p=k+1-(iron-mag)-sheet \n if p>0:\n el+=1\n alt.pop(j)\n l-=1\n break \n else:\n #sheet=s[iron:mag].count(\":\")\n sheet=0\n #print(iron,mag,sheet)\n p=k+1-(mag-iron)-sheet \n if p>0: \n el+=1\n alt.pop(j)\n l-=1\n break \n ir=alt.copy() \n total+=el\n #for k,v in attr.items():\n # total-=v-1 \n #print(ma,ir,total) \n ma,ir=[],[]\n print(total) \n\n\n", "t=int(input())\nfor _ in range(t):\n n,k=list(map(int,input().split()))\n s=list(input())\n ma,ir=[],[]\n total=0\n for i in range(n):\n k1=s[i]\n if k1=='M':\n ma.append(i)\n elif k1=='I':\n ir.append(i)\n if k1=='X' or i==n-1: \n alt=ir.copy()\n el=0\n for mag in ma:\n for iron in ir:\n if iron>mag:\n #sheet=s[mag:iron].count(\":\")\n sheet=0\n p=k+1-(iron-mag)-sheet \n if p>0:\n el+=1\n alt.remove(iron)\n break \n else:\n #sheet=s[iron:mag].count(\":\")\n sheet=0\n #print(iron,mag,sheet)\n p=k+1-(mag-iron)-sheet \n if p>0: \n el+=1\n alt.remove(iron)\n break \n ir=alt.copy() \n total+=el\n #for k,v in attr.items():\n # total-=v-1 \n #print(ma,ir,total) \n s1=set() \n ma,ir=[],[]\n print(total) \n\n\n", "for _ in range(int(input())):\n n,k = map(int,input().split())\n s = input()\n l = ''\n for i in s:\n if i == ':':\n l += ':'\n l += i\n magnet = []\n iron = []\n n = len(l)\n ans = 0\n for i in range(n):\n if l[i] == 'I':\n while magnet != [] and abs(magnet[0]-i) > k:\n magnet.pop(0)\n if len(magnet)>0:\n ans += 1\n magnet.pop(0)\n else:\n iron.append(i)\n elif l[i] == 'M':\n while iron!=[] and abs(iron[0]-i)>k:\n iron.pop(0)\n if len(iron) > 0:\n ans += 1\n iron.pop(0)\n else:\n magnet.append(i)\n elif l[i] == 'X':\n while iron!=[]:\n iron.pop(0)\n while magnet != []:\n magnet.pop(0)\n print(ans)", "t=int(input()) #TESTCASES\n\nfor y in range(t):\n inptstr=input().split()\n\n n=int(inptstr[0]) #N TOTAL ELEMENTS\n\n K=int(inptstr[1]) #K CONSTaNT IN Pij\n\n s=input() #STRING SEQUENCE OF CELLS ELEMENTS\n\n \n noblock=s.split('X')\n cnt=[]\n cntiron=0\n for ele in noblock: #PARSING ELEMENTS WITHOUT BLOCK\n d={}\n d1={}\n iron=[]\n magnet=[]\n for x in range(len(ele)):#TRACKING LOCATION OF ELEMENTS IN NOBLOCK\n if ele[x]=='I':\n iron.append(x)\n elif ele[x]=='M':\n magnet.append(x)\n #print('part',ele)\n for i in iron:\n\n #print('IRON - ',i)\n for j in magnet:\n #print('MAGNET - ',j)\n \n if i in d or j in d1:\n continue\n if i<j:\n Sij=(ele[i:j]).count(':')\n\n if j<i:\n Sij=(ele[j:i]).count(':')\n Pij=(K+1)-(abs(j-i))-Sij\n if Pij > 0:\n cntiron=cntiron+1\n #print('CONNECTION DONE')\n d[i]=j\n d1[j]=i\n break\n \n print(cntiron)\n\n \n \n \n \n \n\n\n\n\n \n \n\n", "t=int(input()) #TESTCASES\n\nfor y in range(t):\n inptstr=input().split()\n\n n=int(inptstr[0]) #N TOTAL ELEMENTS\n\n K=int(inptstr[1]) #K CONSTaNT IN Pij\n\n s=input() #STRING SEQUENCE OF CELLS ELEMENTS\n\n \n noblock=s.split('X')\n cnt=[]\n cntiron=0\n for ele in noblock: #PARSING ELEMENTS WITHOUT BLOCK\n d={}\n d1={}\n iron=[]\n magnet=[]\n for x in range(len(ele)):#TRACKING LOCATION OF ELEMENTS IN NOBLOCK\n if ele[x]=='I':\n iron.append(x)\n elif ele[x]=='M':\n magnet.append(x)\n #print('part',ele)\n for i in iron:\n\n #print('IRON - ',i)\n for j in magnet:\n #print('MAGNET - ',j)\n \n if i in d or j in d1:\n continue\n if i<j:\n Sij=(ele[i:j]).count(':')\n\n if j<i:\n Sij=(ele[j:i]).count(':')\n Pij=(K+1)-(abs(j-i))-Sij\n if Pij > 0:\n cntiron=cntiron+1\n #print('CONNECTION DONE')\n d[i]=j\n d1[j]=i\n break\n \n print(cntiron)\n\n \n \n \n \n \n\n\n\n\n \n \n\n", "# cook your dish here\nfrom collections import deque\nfor _ in range(int(input())):\n\n n, k = list(map(int, input().split()))\n s = input()\n\n new_s = \"\"\n for obj in s:\n if obj == \":\":\n new_s+=obj\n new_s+=obj\n\n qi = deque()\n qm = deque()\n\n ans = 0; j = 0\n for i in new_s:\n\n if i == 'I':\n while (len(qm)!=0 and qm[0]+k < j):\n qm.popleft()\n\n if len(qm) != 0:\n ans+=1\n qm.popleft()\n\n else:\n qi.append(j)\n\n elif i == 'M':\n while (len(qi) != 0 and qi[0]+k < j):\n qi.popleft()\n\n if len(qi)!=0:\n ans+=1\n qi.popleft()\n\n else:\n qm.append(j)\n\n elif i == 'X':\n while len(qm)!=0:\n qm.popleft()\n while len(qi)!=0:\n qi.popleft()\n\n j+=1\n print(ans)\n", "def fema(s,k):\n iron=[]\n mag=[]\n sheet=0\n ind=-1\n for i in s:\n ind+=1\n if i==':':\n sheet+=1\n ind+=1\n if i=='I':\n iron.append(ind)\n if i=='M':\n mag.append(ind)\n\n count=0\n i,j=0,0\n while i<len(iron) and j<len(mag):\n if abs(iron[i]-mag[j])<=k:\n count+=1\n i+=1\n j+=1\n else:\n if(iron[i]>mag[j]):\n j+=1\n else:\n i+=1\n return count\n\ntest=int(input())\nwhile(test!=0):\n test-=1\n n,k=list(map(int,input().split()))\n s=input()\n li=[st for st in s.split('X') if len(st)>0]\n ans=sum(fema(st,k) for st in li)\n print(ans)\n", "def fema(s,k):\n iron=[]\n mag=[]\n sheet=0\n ind=-1\n for i in s:\n ind+=1\n if i==':':\n sheet+=1\n if i=='I':\n iron.append(ind)\n if i=='M':\n mag.append(ind)\n\n count=0\n i,j=0,0\n while i<len(iron) and j<len(mag):\n if abs(iron[i]-mag[j])<=k:\n count+=1\n i+=1\n j+=1\n else:\n if(iron[i]>mag[j]):\n j+=1\n else:\n i+=1\n return count\n\ntest=int(input())\nwhile(test!=0):\n test-=1\n n,k=list(map(int,input().split()))\n s=input()\n li=[st for st in s.split('X') if len(st)>0]\n ans=sum(fema(st,k) for st in li)\n print(ans)\n", "test=int(input())\nwhile(test!=0):\n test=test-1\n n,k= list(map(int,input().split()))\n s=input()\n i=0\n j=0\n mag=0\n sheet=0\n while(i<n and j<n):\n if(s[i]=='I'):\n if(s[j]=='M'):\n if(i<j):\n for a in range(i,j+1):\n if s[a]==':':\n sheet=sheet+1\n if(j<i):\n for a in range(j,i+1):\n if s[a]==':':\n sheet=sheet+1\n\n power=(k+1)-abs(j-i)+sheet\n sheet=0\n if(power>0):\n mag+=1\n i+=1\n j+=1\n else:\n if(i>j):\n j+=1\n else:\n i+=1\n elif(s[j]=='X'):\n i=j\n i+=1\n j+=1\n else:\n j+=1\n elif(s[i]=='X'):\n j=i\n j+=1\n i+=1\n else:\n i+=1\n print(mag)\n \n", "for t in range(int(input())):\n n, k = list(map(int, input().split()))\n s = input()\n i = 0\n j = 0\n ans = 0\n while i < n and j < n:\n if s[i] == \"M\":\n if s[j] == \"I\":\n c = 0\n if i > j:\n o = s[j:i]\n c = o.count(':')\n else:\n o = s[i:j]\n c = o.count(':')\n p = k+1-abs(i-j)-c\n if p > 0:\n ans = ans+1\n i = i+1\n j = j+1\n else:\n if i < j:\n i += 1\n else:\n j += 1\n elif s[j] == \"X\":\n j += 1\n i = j\n else:\n j += 1\n elif s[i] == 'X':\n i += 1\n j = i\n else:\n i += 1\n print(ans)\n", "def solve(string,k):\n ind = 0\n iron,mag = [],[]\n for i in string:\n if i==':':\n ind+=1\n elif i=='I':\n iron.append(ind)\n elif i==\"M\":\n mag.append(ind)\n ind+=1\n i,j = 0,0\n li = len(iron)\n lm = len(mag)\n ans = 0\n while i<li and j<lm:\n if abs(iron[i]-mag[j])<=k:\n ans+=1\n i+=1\n j+=1\n else:\n if iron[i]>mag[j]:\n j+=1\n else:\n i+=1\n return ans\nfor _ in range(int(input())):\n ans = 0\n n,k = list(map(int,input().split()))\n s = input()\n real = [i for i in s.split(\"X\") if len(i)>0]\n ans+=sum(solve(string,k)for string in real)\n print(ans)\n", "class check:\n def __init__(self, k, s):\n self.sl = s.split(\"X\")\n self.k = k\n\n def ans(self):\n answer = 0\n for strings in self.sl:\n irons = []\n magnets = []\n sheets = 0\n for i in range(len(strings)):\n if strings[i] == \"I\":\n if not magnets:\n irons.append(i + sheets)\n else:\n index_i = i + sheets\n for x in magnets:\n if (self.k + 1 - abs(x - index_i)) > 0:\n answer += 1\n magnets.remove(x)\n break\n else:\n irons.append(index_i)\n elif strings[i] == \"M\":\n if not irons:\n magnets.append(i + sheets)\n else:\n index_m = i + sheets\n for x in irons:\n if (self.k + 1 - abs(x - index_m)) > 0:\n answer += 1\n irons.remove(x)\n break\n else:\n magnets.append(index_m)\n elif strings[i] == \":\":\n sheets += 1\n return answer\n\n\nfor _ in range(int(input())):\n n, k = list(map(int, input().split()))\n s = input()\n st = check(k, s)\n print(st.ans())\n", "from collections import deque\n\nt = int(input())\n\nfor _ in range(t):\n n, k = map(int, input().split())\n s = input()\n l = str()\n for i in range(n):\n if s[i]==\":\":\n l+=\":\"\n l+=s[i]\n \n \n fe = deque()\n mg = deque()\n tot = 0\n for i in range(len(l)):\n if l[i] == \"I\":\n while len(mg)>0 and abs(mg[0]-i)>k:\n mg.popleft()\n if len(mg)>0:\n tot += 1\n mg.popleft()\n else:\n fe.append(i)\n elif l[i] == \"M\":\n while len(fe)>0 and abs(fe[0]-i)>k:\n fe.popleft()\n if len(fe) >0:\n tot+=1\n fe.popleft()\n else:\n mg.append(i)\n elif l[i] == \"X\":\n while len(fe)>0:\n fe.popleft()\n while len(mg)>0:\n mg.popleft()\n print(tot)", "from collections import deque\n\nt = int(input())\n\nfor _ in range(t):\n n, k = map(int, input().split())\n s = input()\n l = str()\n for i in range(n):\n if s[i]==\":\":\n l+=\":\"\n l+=s[i]\n \n \n fe = deque()\n mg = deque()\n tot = 0\n for i in range(len(s)):\n if s[i] == \"I\":\n while len(mg)>0 and abs(mg[0]-i)>k:\n mg.popleft()\n if len(mg)>0:\n tot += 1\n mg.popleft()\n else:\n fe.append(i)\n elif s[i] == \"M\":\n while len(fe)>0 and abs(fe[0]-i)>k:\n fe.popleft()\n if len(fe) >0:\n tot+=1\n fe.popleft()\n else:\n mg.append(i)\n elif s[i] == \"X\":\n while len(fe)>0:\n fe.popleft()\n while len(mg)>0:\n mg.popleft()\n print(tot)", "from collections import deque\n\nt = int(input())\n\nfor _ in range(t):\n n, k = map(int, input().split())\n s = input()\n \n l = \" \"\n for i in range(n):\n if s[i]==\":\":\n l+=\":\"\n l+=s[i]\n \n \n fe = deque()\n mg = deque()\n tot = 0\n for i in range(len(s)):\n if s[i] == \"I\":\n while len(mg)>0 and abs(mg[0]-i)>k:\n mg.popleft()\n if len(mg)>0:\n tot += 1\n mg.popleft()\n else:\n fe.append(i)\n elif s[i] == \"M\":\n while len(fe)>0 and abs(fe[0]-i)>k:\n fe.popleft()\n if len(fe) >0:\n tot+=1\n fe.popleft()\n else:\n mg.append(i)\n elif s[i] == \"X\":\n while len(fe)>0:\n fe.popleft()\n while len(mg)>0:\n mg.popleft()\n print(tot)", "from collections import deque\n\nt = int(input())\n\nfor _ in range(t):\n n, k = map(int, input().split())\n s = input()\n \n l = \"\"\n for i in range(n):\n if s[i]==\":\":\n l+=\":\"\n l+=s[i]\n \n \n fe = deque()\n mg = deque()\n tot = 0\n for i in range(len(s)):\n if s[i] == \"I\":\n while len(mg)>0 and abs(mg[0]-i)>k:\n mg.popleft()\n if len(mg)>0:\n tot += 1\n mg.popleft()\n else:\n fe.append(i)\n elif s[i] == \"M\":\n while len(fe)>0 and abs(fe[0]-i)>k:\n fe.popleft()\n if len(fe) >0:\n tot+=1\n fe.popleft()\n else:\n mg.append(i)\n elif s[i] == \"X\":\n while len(fe)>0:\n fe.popleft()\n while len(mg)>0:\n mg.popleft()\n print(tot)", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n n,k=map(int,input().split())\n l=input()\n count=0\n i=0\n j=0\n while i<n and j<n:\n if l[i]==\"M\":\n if l[j]==\"I\":\n if i<j:\n sij=l[i:j].count(\":\")\n else:\n sij=l[j:i].count(\":\")\n p=k+1-abs(i-j)-sij\n if p>0:\n count+=1\n i+=1\n j+=1\n else:\n if i<j:\n i+=1\n else:\n j+=1\n elif l[j]==\"X\":\n j+=1\n i=j\n else:\n j+=1\n elif l[i]==\"X\":\n i+=1\n j=i\n else:\n i+=1\n print(count)", "t=int(input())\nfor t in range(t):\n n,k=list(map(int,input().split()))\n s=input()\n i=0\n j=0\n a=0\n while(i<n and j<n):\n if(s[i]=='M'):\n if(s[j]=='I'):\n c=0\n if(i>j):\n z=s[j:i]\n c=z.count(\":\")\n else:\n z=s[i:j]\n c=z.count(\":\")\n p=k+1-abs(i-j)-c\n if(p>0):\n a+=1\n i+=1\n j+=1\n else:\n if(i<j):\n i+=1\n else:\n j+=1\n elif(s[j]=='X'):\n j+=1\n i=j\n else:\n j+=1\n elif(s[i]=='X'):\n i+=1\n j=i\n else:\n i+=1\n print(a)\n\n\n", "t=int(input()) #TESTCASES\n\nfor y in range(t):\n inptstr=input().split()\n\n n=int(inptstr[0]) #N TOTAL ELEMENTS\n\n K=int(inptstr[1]) #K CONSTaNT IN Pij\n\n s=input() #STRING SEQUENCE OF CELLS ELEMENTS\n\n \n noblock=s.split('X')\n cnt=[]\n cntiron=0\n for ele in noblock: #PARSING ELEMENTS WITHOUT BLOCK\n d={}\n d1={}\n iron=[]\n magnet=[]\n for x in range(len(ele)):#TRACKING LOCATION OF ELEMENTS IN NOBLOCK\n if ele[x]=='I':\n iron.append(x)\n elif ele[x]=='M':\n magnet.append(x)\n #print('part',ele)\n for i in iron:\n\n #print('IRON - ',i)\n for j in magnet:\n #print('MAGNET - ',j)\n \n if i in d or j in d1:\n continue\n if i<j:\n Sij=(ele[i:j]).count(':')\n\n if j<i:\n Sij=(ele[j:i]).count(':')\n Pij=(K+1)-(abs(j-i))-Sij\n if Pij > 0:\n cntiron=cntiron+1\n #print('CONNECTION DONE')\n d[i]=j\n d1[j]=i\n break\n \n print(cntiron)\n\n \n \n \n \n \n\n\n\n\n \n \n\n"]
{"inputs": [["2", "4 5", "I::M", "9 10", "MIM_XII:M"]], "outputs": [["1", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
15,796
e1784e02dd451f88772b8d26d54bc6de
UNKNOWN
Tuzik and Vanya are playing the following game. They have an N × M board and a chip to indicate the current game position. The chip can be placed on any one cell of the game board, each of which is uniquely identified by an ordered pair of positive integers (r, c), where 1 ≤ r ≤ N is the row number and 1 ≤ c ≤ M is the column number. Initially, the chip is placed at the cell identified as (1, 1). For his move, a player can move it either 1, 2 or 3 cells up, or 1 or 2 cells to the right. The player who can not move the chip loses. In other words, a player suffers defeat if the chip is placed in the cell (N, M) at the start of his turn. Tuzik starts the game. You have to determine who will win the game if both players play optimally. -----Input----- The first line contains an integer T denoting the number of tests. Each of the following T lines contain two integers N and M. -----Output----- For each test output "Tuzik" or "Vanya" on a separate line, indicating who will win the game. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N, M ≤ 106 -----Example----- Input: 2 4 4 2 2 Output: Tuzik Vanya -----Explanation-----Test case 1: On his first turn Tuzik moves chip 3 cells up to the cell (4, 1). On his turn Vanya can move chip only right to the cell (4, 2) or to the cell (4, 3) and after that Tuzik moves it to (4, 4) and finishes the game. Test case 2: On his first Turn Tuzik can move chip to the cell (2, 1) or (1, 2), but on his next turn Vanya moves it to (2, 2) and wins the game.
["cases = int(input())\nfor _ in range(cases):\n rows, cols = map(int, input().split())\n if (cols - 1) % 3 == 0 and (rows - 1) % 4 == 0: print('Vanya')\n elif (cols - 1) % 3 != 0 and (rows - 1) % 4 == 0: print('Tuzik')\n elif (cols - 1) % 3 == 0 and (rows - 1) % 4 != 0: print('Tuzik')\n else:\n if (cols - 1) % 3 == 1 and (rows - 1) % 4 == 1: print('Vanya')\n elif (cols - 1) % 3 == 2 and (rows - 1) % 4 == 2: print('Vanya')\n else: print('Tuzik')"]
{"inputs": [["2", "4 4", "2 2"]], "outputs": [["Tuzik", "Vanya"]]}
INTERVIEW
PYTHON3
CODECHEF
454
ab87299bbaf606fba9394e4f51e252cc
UNKNOWN
Ishank lives in a country in which there are N$N$ cities and N−1$N-1$ roads. All the cities are connected via these roads. Each city has been assigned a unique number from 1 to N$N$. The country can be assumed as a tree, with nodes representing the cities and edges representing the roads. The tree is rooted at 1.Every Time, when a traveler through a road, he will either gain some amount or has to pay some amount. Abhineet is a traveler and wishes to travel to various cities in this country. There's a law in the country for travelers, according to which, when a traveler moves from the city A$A$ to city B$B$, where city A$A$ and B$B$ are connected by a road then the traveler is either paid or has to pay the amount of money equal to profit or loss respectively. When he moves from A$A$ to B$B$, he hires a special kind of vehicle which can reverse its direction at most once. Reversing the direction means earlier the vehicle is going towards the root, then away from the root or vice versa. Abhineet is analyzing his trip and therefore gave Q$Q$ queries to his friend, Ishank, a great coder. In every query, he gives two cities A$A$ and B$B$. Ishank has to calculate the maximum amount he can gain (if he cannot gain, then the minimum amount he will lose) if he goes from the city A$A$ to city B$B$. -----Input:----- -The first line of the input contains a two space-separated integers N and Q. -The next N-1 line contains 3 space-separated integers Xi and Yi and Zi denoting that cities Xi and Yi are connected by a road which gives profit Zi (Negative Zi represents loss). -The next Q contains 2 space-separated integers A and B denoting two cities. -----Output:----- Print a single line corresponding to each query — the maximum amount he can gain (if he cannot gain, then the minimum amount he will lose with negative sign) if he goes from city A to city B. -----Constraints----- - 2≤N≤105$2 \leq N \leq 10^5$ - 1≤Q≤105$1 \leq Q \leq 10^5$ - 1≤Xi,Yi,A,B≤N$1 \leq Xi, Yi, A, B \leq N$ - abs(Zi)≤109$ abs(Zi) \leq 10^9$ -----Sample Input:----- 9 5 1 2 8 1 3 -9 2 4 1 2 5 -6 3 6 7 3 7 6 6 8 3 6 9 4 1 2 2 7 4 3 3 2 8 9 -----Sample Output:----- 10 5 0 -1 21 -----EXPLANATION:----- In the first query, he goes from 1 to 2, 2 to 4, takes a turn and go to 2. Therefore profit=8+1+1=10.
["# cook your dish here\ntry:\n X=list(map(int, input().split()))\nexcept:\n X=[0,0]\nch=[]\nchnew=[]\npar={}\npar[1]=0\nfor i in range(X[0]+1):\n ch.append([])\n chnew.append([])\nfor i in range(X[0]-1):\n Y=list(map(int, input().split()))\n #par[Y[1]]=[Y[0],Y[2]]\n ch[Y[0]].append([Y[1],Y[2]])\n ch[Y[1]].append([Y[0],Y[2]])\ntre=[1]\nwhile(len(tre)):\n cr=tre[-1]\n tre=tre[:-1]\n for i in ch[cr]:\n chnew[cr].append(i)\n par[i[0]]=[cr,i[1]]\n tre.append(i[0])\n for j in ch[i[0]]:\n if(j[0]==cr):\n ch[i[0]].remove(j)\n break\nch=chnew\ndef goup(par,nd):\n if(nd==1):\n return 0\n else:\n p=par[nd]\n ans=p[1]+goup(par,p[0])\n return (max([ans,0]))\n\ndef godown(ch,nd):\n ans=0\n for i in ch[nd]:\n ans=max([(i[1]+godown(ch,i[0])),ans])\n return(ans)\n\nfor i in range(X[1]):\n Z=list(map(int,input().split()))\n r=Z[0]\n s=Z[1]\n nans=0\n while(r!=s):\n if(r>s):\n nans=nans+par[r][1]\n r=par[r][0]\n else:\n nans=nans+par[s][1]\n s=par[s][0]\n if((r==Z[0]) or (r==Z[1])):\n if(Z[0]<Z[1]):\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\n else:\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\n else:\n nans=nans+2*goup(par,r)\n print(nans)", "# cook your dish here\ntry:\n X=list(map(int, input().split()))\nexcept:\n X=[0,0]\nch=[]\nchnew=[]\npar={}\npar[1]=0\nfor i in range(X[0]+1):\n ch.append([])\n chnew.append([])\nfor i in range(X[0]-1):\n Y=list(map(int, input().split()))\n #par[Y[1]]=[Y[0],Y[2]]\n ch[Y[0]].append([Y[1],Y[2]])\n ch[Y[1]].append([Y[0],Y[2]])\ntre=[1]\nwhile(len(tre)):\n cr=tre[-1]\n tre=tre[:-1]\n for i in ch[cr]:\n chnew[cr].append(i)\n par[i[0]]=[cr,i[1]]\n tre.append(i[0])\n for j in ch[i[0]]:\n if(j[0]==cr):\n ch[i[0]].remove(j)\n break\nch=chnew\ndef goup(par,nd):\n if(nd==1):\n return 0\n else:\n p=par[nd]\n ans=p[1]+goup(par,p[0])\n return (max([ans,0]))\n\ndef godown(ch,nd):\n ans=0\n for i in ch[nd]:\n ans=max([(i[1]+godown(ch,i[0])),ans])\n return(ans)\n\nfor i in range(X[1]):\n Z=list(map(int,input().split()))\n r=Z[0]\n s=Z[1]\n nans=0\n while(r!=s):\n if(r>s):\n nans=nans+par[r][1]\n r=par[r][0]\n else:\n nans=nans+par[s][1]\n s=par[s][0]\n if((r==Z[0]) or (r==Z[1])):\n if(Z[0]<Z[1]):\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\n else:\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\n else:\n nans=nans+2*goup(par,r)\n print(nans)", "# cook your dish here\n# cook your dish here\ntry:\n X=list(map(int, input().split()))\nexcept:\n X=[0,0]\nch=[]\nchnew=[]\npar={}\npar[1]=0\nfor i in range(X[0]+1):\n ch.append([])\n chnew.append([])\nfor i in range(X[0]-1):\n Y=list(map(int, input().split()))\n #par[Y[1]]=[Y[0],Y[2]]\n ch[Y[0]].append([Y[1],Y[2]])\n ch[Y[1]].append([Y[0],Y[2]])\ntre=[1]\nwhile(len(tre)):\n cr=tre[-1]\n tre=tre[:-1]\n for i in ch[cr]:\n chnew[cr].append(i)\n par[i[0]]=[cr,i[1]]\n tre.append(i[0])\n for j in ch[i[0]]:\n if(j[0]==cr):\n ch[i[0]].remove(j)\n break\nch=chnew\ndef goup(par,nd):\n if(nd==1):\n return 0\n else:\n p=par[nd]\n ans=p[1]+goup(par,p[0])\n return (max([ans,0]))\n\ndef godown(ch,nd):\n ans=0\n for i in ch[nd]:\n ans=max([(i[1]+godown(ch,i[0])),ans])\n return(ans)\n\nfor i in range(X[1]):\n Z=list(map(int,input().split()))\n r=Z[0]\n s=Z[1]\n nans=0\n while(r!=s):\n if(r>s):\n nans=nans+par[r][1]\n r=par[r][0]\n else:\n nans=nans+par[s][1]\n s=par[s][0]\n if((r==Z[0]) or (r==Z[1])):\n if(Z[0]<Z[1]):\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\n else:\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\n else:\n nans=nans+2*goup(par,r)\n print(nans)", "try:\n X=list(map(int, input().split()))\nexcept:\n X=[0,0]\nch=[]\nchnew=[]\npar={}\npar[1]=0\nfor i in range(X[0]+1):\n ch.append([])\n chnew.append([])\nfor i in range(X[0]-1):\n Y=list(map(int, input().split()))\n #par[Y[1]]=[Y[0],Y[2]]\n ch[Y[0]].append([Y[1],Y[2]])\n ch[Y[1]].append([Y[0],Y[2]])\ntre=[1]\nwhile(len(tre)):\n cr=tre[-1]\n tre=tre[:-1]\n for i in ch[cr]:\n chnew[cr].append(i)\n par[i[0]]=[cr,i[1]]\n tre.append(i[0])\n for j in ch[i[0]]:\n if(j[0]==cr):\n ch[i[0]].remove(j)\n break\nch=chnew\ndef goup(par,nd):\n if(nd==1):\n return 0\n else:\n p=par[nd]\n ans=p[1]+goup(par,p[0])\n return (max([ans,0]))\n\ndef godown(ch,nd):\n ans=0\n for i in ch[nd]:\n ans=max([(i[1]+godown(ch,i[0])),ans])\n return(ans)\n\nfor i in range(X[1]):\n Z=list(map(int,input().split()))\n r=Z[0]\n s=Z[1]\n nans=0\n while(r!=s):\n if(r>s):\n nans=nans+par[r][1]\n r=par[r][0]\n else:\n nans=nans+par[s][1]\n s=par[s][0]\n if((r==Z[0]) or (r==Z[1])):\n if(Z[0]<Z[1]):\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\n else:\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\n else:\n nans=nans+2*goup(par,r)\n print(nans)", "# cook your dish here\ntry:\n X=list(map(int, input().split()))\nexcept:\n X=[0,0]\nch=[]\nchnew=[]\npar={}\npar[1]=0\nfor i in range(X[0]+1):\n ch.append([])\n chnew.append([])\nfor i in range(X[0]-1):\n Y=list(map(int, input().split()))\n #par[Y[1]]=[Y[0],Y[2]]\n ch[Y[0]].append([Y[1],Y[2]])\n ch[Y[1]].append([Y[0],Y[2]])\ntre=[1]\nwhile(len(tre)):\n cr=tre[-1]\n tre=tre[:-1]\n for i in ch[cr]:\n chnew[cr].append(i)\n par[i[0]]=[cr,i[1]]\n tre.append(i[0])\n for j in ch[i[0]]:\n if(j[0]==cr):\n ch[i[0]].remove(j)\n break\nch=chnew\ndef goup(par,nd):\n if(nd==1):\n return 0\n else:\n p=par[nd]\n ans=p[1]+goup(par,p[0])\n return (max([ans,0]))\n\ndef godown(ch,nd):\n ans=0\n for i in ch[nd]:\n ans=max([(i[1]+godown(ch,i[0])),ans])\n return(ans)\n\nfor i in range(X[1]):\n Z=list(map(int,input().split()))\n r=Z[0]\n s=Z[1]\n nans=0\n while(r!=s):\n if(r>s):\n nans=nans+par[r][1]\n r=par[r][0]\n else:\n nans=nans+par[s][1]\n s=par[s][0]\n if((r==Z[0]) or (r==Z[1])):\n if(Z[0]<Z[1]):\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\n else:\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\n else:\n nans=nans+2*goup(par,r)\n print(nans)", "# cook your dish here\r\ntry:\r\n X=list(map(int, input().split()))\r\nexcept:\r\n X=[0,0]\r\nch=[]\r\nchnew=[]\r\npar={}\r\npar[1]=0\r\nfor i in range(X[0]+1):\r\n ch.append([])\r\n chnew.append([])\r\nfor i in range(X[0]-1):\r\n Y=list(map(int, input().split()))\r\n #par[Y[1]]=[Y[0],Y[2]]\r\n ch[Y[0]].append([Y[1],Y[2]])\r\n ch[Y[1]].append([Y[0],Y[2]])\r\ntre=[1]\r\nwhile(len(tre)):\r\n cr=tre[-1]\r\n tre=tre[:-1]\r\n for i in ch[cr]:\r\n chnew[cr].append(i)\r\n par[i[0]]=[cr,i[1]]\r\n tre.append(i[0])\r\n for j in ch[i[0]]:\r\n if(j[0]==cr):\r\n ch[i[0]].remove(j)\r\n break\r\nch=chnew\r\ndef goup(par,nd):\r\n if(nd==1):\r\n return 0\r\n else:\r\n p=par[nd]\r\n ans=p[1]+goup(par,p[0])\r\n return (max([ans,0]))\r\n\r\ndef godown(ch,nd):\r\n ans=0\r\n for i in ch[nd]:\r\n ans=max([(i[1]+godown(ch,i[0])),ans])\r\n return(ans)\r\n\r\nfor i in range(X[1]):\r\n Z=list(map(int,input().split()))\r\n r=Z[0]\r\n s=Z[1]\r\n nans=0\r\n while(r!=s):\r\n if(r>s):\r\n nans=nans+par[r][1]\r\n r=par[r][0]\r\n else:\r\n nans=nans+par[s][1]\r\n s=par[s][0]\r\n if((r==Z[0]) or (r==Z[1])):\r\n if(Z[0]<Z[1]):\r\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\r\n else:\r\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\r\n else:\r\n nans=nans+2*goup(par,r)\r\n print(nans)", "# cook your dish here\ntry:\n X=list(map(int, input().split()))\nexcept:\n X=[0,0]\nch=[]\nchnew=[]\npar={}\npar[1]=0\nfor i in range(X[0]+1):\n ch.append([])\n chnew.append([])\nfor i in range(X[0]-1):\n Y=list(map(int, input().split()))\n #par[Y[1]]=[Y[0],Y[2]]\n ch[Y[0]].append([Y[1],Y[2]])\n ch[Y[1]].append([Y[0],Y[2]])\ntre=[1]\nwhile(len(tre)):\n cr=tre[-1]\n tre=tre[:-1]\n for i in ch[cr]:\n chnew[cr].append(i)\n par[i[0]]=[cr,i[1]]\n tre.append(i[0])\n for j in ch[i[0]]:\n if(j[0]==cr):\n ch[i[0]].remove(j)\n break\nch=chnew\ndef goup(par,nd):\n if(nd==1):\n return 0\n else:\n p=par[nd]\n ans=p[1]+goup(par,p[0])\n return (max([ans,0]))\n\ndef godown(ch,nd):\n ans=0\n for i in ch[nd]:\n ans=max([(i[1]+godown(ch,i[0])),ans])\n return(ans)\n\nfor i in range(X[1]):\n Z=list(map(int,input().split()))\n r=Z[0]\n s=Z[1]\n nans=0\n while(r!=s):\n if(r>s):\n nans=nans+par[r][1]\n r=par[r][0]\n else:\n nans=nans+par[s][1]\n s=par[s][0]\n if((r==Z[0]) or (r==Z[1])):\n if(Z[0]<Z[1]):\n nans=nans+2*max(goup(par,Z[0]),godown(ch,Z[1]))\n else:\n nans=nans+2*max(goup(par,Z[1]),godown(ch,Z[0]))\n else:\n nans=nans+2*goup(par,r)\n print(nans)"]
{"inputs": [["9 5", "1 2 8", "1 3 -9", "2 4 1", "2 5 -6", "3 6 7", "3 7 6", "6 8 3", "6 9 4", "1 2", "2 7", "4 3", "3 2", "8 9"]], "outputs": [["10", "5", "0", "-1", "21"]]}
INTERVIEW
PYTHON3
CODECHEF
10,143
957536defd956ef5f9d408e019e1b2f8
UNKNOWN
Raj is a math pro and number theory expert. One day, he met his age-old friend Chef. Chef claimed to be better at number theory than Raj, so Raj gave him some fuzzy problems to solve. In one of those problems, he gave Chef a 3$3$-tuple of non-negative integers (a0,b0,c0)$(a_0, b_0, c_0)$ and told Chef to convert it to another tuple (x,y,z)$(x, y, z)$. Chef may perform the following operations any number of times (including zero) on his current tuple (a,b,c)$(a, b, c)$, in any order: - Choose one element of this tuple, i.e. a$a$, b$b$ or c$c$. Either add 1$1$ to that element or subtract 1$1$ from it. The cost of this operation is 1$1$. - Merge: Change the tuple to (a−1,b−1,c+1)$(a-1, b-1, c+1)$, (a−1,b+1,c−1)$(a-1, b+1, c-1)$ or (a+1,b−1,c−1)$(a+1, b-1, c-1)$, i.e. add 1$1$ to one element and subtract 1$1$ from the other two. The cost of this operation is 0$0$. - Split: Change the tuple to (a−1,b+1,c+1)$(a-1, b+1, c+1)$, (a+1,b−1,c+1)$(a+1, b-1, c+1)$ or (a+1,b+1,c−1)$(a+1, b+1, c-1)$, i.e. subtract 1$1$ from one element and add 1$1$ to the other two. The cost of this operation is also 0$0$. After each operation, all elements of Chef's tuple must be non-negative. It is not allowed to perform an operation that would make one or more elements of this tuple negative. Can you help Chef find the minimum cost of converting the tuple (a0,b0,c0)$(a_0, b_0, c_0)$ to the tuple (x,y,z)$(x, y, z)$? It can be easily proved that it is always possible to convert any tuple of non-negative integers to any other. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first and only line of each test case contains six space-separated integers a0$a_0$, b0$b_0$, c0$c_0$, x$x$, y$y$ and z$z$. -----Output----- For each test case, print a single line containing one integer ― the minimum cost. -----Constraints----- - 1≤T≤105$1 \le T \le 10^5$ - 0≤a0,b0,c0,x,y,z≤1018$0 \le a_0, b_0, c_0, x, y, z \le 10^{18}$ -----Subtasks----- Subtask #1 (20 points): 0≤a0,b0,c0,x,y,z≤100$0 \le a_0, b_0, c_0, x, y, z \le 100$ Subtask #2 (80 points): original constraints -----Example Input----- 2 1 1 1 2 2 2 1 2 3 2 4 2 -----Example Output----- 0 1 -----Explanation----- Example case 1: The tuple (1,1,1)$(1, 1, 1)$ can be converted to (2,2,2)$(2, 2, 2)$ using only three Split operations, with cost 0$0$: (1,1,1)→(2,0,2)→(1,1,3)→(2,2,2)$(1, 1, 1) \rightarrow (2, 0, 2) \rightarrow (1, 1, 3) \rightarrow (2, 2, 2)$. Example case 2: We can use one addition operation and one Split operation: (1,2,3)→(1,3,3)→(2,4,2)$(1, 2, 3) \rightarrow (1, 3, 3) \rightarrow (2, 4, 2)$.
["for _ in range(int(input())):\n a,b,c,x,y,z = list(map(int,input().split()))\n if a == 0 and b == 0 and c == 0 and x == 0 and y == 0 and z == 0:\n print(0)\n continue\n ans = 0\n if a == 0 and b == 0 and c == 0:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1\n else:\n ans = 2\n else:\n if x == 0 and y == 0 and z == 0:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1\n else:\n ans = 2\n else:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1 \n print(ans)\n", "for _ in range(int(input())):\n a,b,c,x,y,z = map(int,input().split())\n if a == 0 and b == 0 and c == 0 and x == 0 and y == 0 and z == 0:\n print(0)\n continue\n ans = 0\n if a == 0 and b == 0 and c == 0:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1\n else:\n ans = 2\n else:\n if x == 0 and y == 0 and z == 0:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1\n else:\n ans = 2\n else:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1 \n print(ans)", "for _ in range(int(input())):\n a,b,c,x,y,z = map(int,input().split())\n if a == 0 and b == 0 and c == 0 and x == 0 and y == 0 and z == 0:\n print(0)\n continue\n ans = 0\n if a == 0 and b == 0 and c == 0:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1\n else:\n ans = 2\n else:\n if x == 0 and y == 0 and z == 0:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1\n else:\n ans = 2\n else:\n st = set((abs(x-a)%2,abs(y-b)%2,abs(z-c)%2))\n if st == {0,1}:\n ans = 1 \n print(ans)", "def ch(a,b,c,d,e,f):\n d1=abs(a-d)\n d2=abs(b-e)\n d3=abs(c-f)\n if(a+b+c==0 or d+e+f==0):\n if(d1%2==d2%2 and d2%2==d3%2):\n print(2)\n else:\n print(1)\n else:\n case=d1%2+d2%2+d3%2\n if(case%3==0):\n print(0)\n else:\n print(1)\nt=int(input())\nwhile(t):\n t-=1\n a,b,c,d,e,f=list(map(int,input().split()))\n if(a==d and b==e and c==f):\n print(0)\n else:\n ch(a,b,c,d,e,f)\n \n \n"]
{"inputs": [["2", "1 1 1 2 2 2", "1 2 3 2 4 2"]], "outputs": [["0", "1"]]}
INTERVIEW
PYTHON3
CODECHEF
2,313
561be8206d334177d42e3a20dcd5d7c8
UNKNOWN
Chefina is always interested to play with string. But due to exam pressure she has no time to solve a string problem. She wants your help. Can you help her to solve that problem? You are given a string. You have to find out the $Wonder$ $Sum$ of the string. $Wonder$ $Sum$ of a string is defined as the sum of the value of each character of the string. The value of each character means: - If the string is started with "a" , then the value of each character of the string is like "a"=100, "b"=101, "c"="102" ………"z"=125. - If the string is started with "z" , then the value of each character of the string is like "a"=2600, "b"=2601, "c"="2602" ………"z"=2625. Since even the $Wonder$ $Sum$ can be large, output $Wonder$ $Sum$ modulo ($10^9 + 7$). -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $S$ with lower case alphabet only. -----Output:----- For each testcase, output in a single line integer i.e. $Wonder$ $Sum$ modulo ($10^9 + 7$). -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq |S| \leq 10^5$ -----Sample Input:----- $2$$cab$ $sdef$ -----Sample Output:----- $903$ $7630$ -----EXPLANATION:----- i) For the first test case, since the string is started with "$c$", so output is ($302$+$300$+$301$)=$903$ ii)For the second test case, since the string is started with "$s$", so output is ($1918$+$1903$+$1904$+$1905$)=$7630$
["for _ in range(int(input())):\n string = input().rstrip()\n start=(ord(string[0])-96)*100\n sum=0\n #print(start)\n for i in range(len(string)):\n sum+=start+(ord(string[i])-97)\n print(sum%1000000007)", "for _ in range(int(input())):\r\n\ts = input()\r\n\tres = 0\r\n\tcnt = 0\r\n\tfor c in s:\r\n\t\tif 96 < ord(c) < 123:\r\n\t\t\tcnt += 1\r\n\t\t\tres += ord(c)\r\n\tres -= 97 * cnt\r\n\tres += (ord(s[0])-96) * 100 * cnt\r\n\tprint(res % 1000000007)", "t=int(input())\nfor _ in range(t):\n string = input().rstrip()\n start=(ord(string[0])-96)*100\n sum=0\n #print(start)\n for i in range(len(string)):\n sum+=start+(ord(string[i])-97)\n print(sum%1000000007)", "for i in range(int(input())):\r\n\ts = input()\r\n\tres = 0\r\n\tmu = (ord(s[0])-96) * 100\r\n\tfor c in s:\r\n\t\tif 96 < ord(c) < 123:\r\n\t\t\tres += ord(c) - 97 + mu\r\n\t\t\tif res > 1000000007:\r\n\t\t\t\tres -= 1000000007\r\n\tprint(res)", "t = int(input())\nfor _ in range(t):\n #s1, s2 = input(), input()\n s = list(''.join(input().split()))\n alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n n = int(\"{}00\".format(ord(s[0]) - 96))\n res = 0\n for i in s:\n res+=(n+ord(i) - 97)\n print(res%1000000007)", "MOD = 10 ** 9 + 7\r\n\r\nfor _ in range(int(input())):\r\n s = input().strip().lower()\r\n while not s:\r\n s = input().strip().lower()\r\n \r\n st = (ord(s[0]) - ord('a') + 1) * 100\r\n\r\n d = 0\r\n for i in s:\r\n d += st + ord(i) - ord('a')\r\n\r\n print(d % MOD)\r\n", "# cooking dish here\n\nfrom collections import Counter\n\nfor testcase in range(int(input())):\n string = input()\n letter_count = Counter(string)\n \n letter_value = lambda x: ord(x) - 97\n base_value = (letter_value(string[0])+1) * 100\n \n net_sum = 0\n \n for letter in range(97, 97+26):\n net_sum += ( (base_value+letter_value(chr(letter)))*letter_count[chr(letter)] ) % int(1e9+7)\n \n print(net_sum%int(1e9+7))", "# cook your dish here\n\nfrom collections import defaultdict\nd = defaultdict(int)\nfor i in range(97,123):\n d[chr(i)] = 100 * (i - 96)\n \nt = int(input())\nfor _ in range(t):\n s = input()\n val = defaultdict(int)\n ini = d[s[0].lower()]\n for i in range(97,123):\n val[chr(i)] = ini + (i - 97)\n mod = (10**9) + 7\n ans = 0\n for i in s:\n ans += val[i.lower()]\n ans = ans%mod\n print(ans)", "from sys import stdin\r\nMODULO = 1000000007\r\nfor testcase in range(int(stdin.readline().strip())) :\r\n s = stdin.readline().strip()\r\n s0 = (ord(s[0]) - 96)*100\r\n wsum = sum([ord(ch)-97 for ch in s]) + s0*len(s)\r\n print(wsum%MODULO)\r\n"]
{"inputs": [["2", "cab", "sdef"]], "outputs": [["903", "7630"]]}
INTERVIEW
PYTHON3
CODECHEF
2,836
9b74a837ca8352d9d39c0397b729b574
UNKNOWN
Chef wants to make a feast. In order to do that, he needs a lot of different ingredients. Each ingredient has a certain tastiness; the tastiness of each ingredient may be any positive integer. Initially, for each tastiness between $K$ and $K+N-1$ (inclusive), Chef has an infinite supply of ingredients with this tastiness. The ingredients have a special property: any two of them can be mixed to create a new ingredient. If the original ingredients had tastiness $x$ and $y$ (possibly $x = y$), the new ingredient has tastiness $x+y$. The ingredients created this way may be used to mix other ingredients as well. Chef is free to mix ingredients in any way he chooses any number of times. Let's call a tastiness $v$ ($v > 0$) unreachable if there is no way to obtain an ingredient with tastiness $v$; otherwise, tastiness $v$ is reachable. Chef wants to make ingredients with all reachable values of tastiness and he would like to know the number of unreachable values. Help him solve this problem. Since the answer may be large, compute it modulo $1,000,000,007$ ($10^9+7$). Note that there is an infinite number of reachable values of tastiness, but it can be proven that the number of unreachable values is always finite for $N \ge 2$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $N$ and $K$. -----Output----- For each test case, print a single line containing one integer — the number of unreachable values of tastiness, modulo $1,000,000,007$. -----Constraints----- - $1 \le T \le 10^5$ - $2 \le N \le 10^{18}$ - $1 \le K \le 10^{18}$ -----Subtasks----- Subtask #1 (20 points): $N = 2$ Subtask #2 (80 points): original constraints -----Example Input----- 2 2 1 3 3 -----Example Output----- 0 2 -----Explanation----- Example case 1: It is possible to obtain ingredients with all values of tastiness. Example case 2: Ingredients with tastiness $1$ and $2$ cannot be made.
["__author__ = 'Prateek'\n\nMOD = int(10**9+7)\n\ndef test():\n n,k=list(map(int,input().split()))\n l = k\n d =n-1\n ans = l-1\n ans = ans%MOD\n a = k-n\n term = (d+a)//d\n ll = (a%MOD - (((term-1)%MOD)*(d%MOD))%MOD)%MOD\n if ll < 0:\n ll = (ll +MOD)%MOD\n m = ((term%MOD)*((a%MOD+ll%MOD)%MOD))%MOD\n m = (m*pow(2,MOD-2,MOD))%MOD\n ans += m\n ans = ans%MOD\n print(ans)\n\n\nif __author__ == 'Prateek':\n t = int(input())\n for _ in range(t):\n test()\n", "# cook your dish here\nfor i in range(int(input())):\n n, k = list(map(int,input().split()))\n remainder= (k-2) % (n-1) \n quotient = (k-2) //(n-1) \n k+=remainder\n if ( quotient % 2 == 1):\n feast = (1+quotient)//2 \n print(((k% 1000000007)*(feast % 1000000007))%1000000007) \n else :\n feast = 1 + quotient \n k=k //2 \n print(((k%1000000007)*(feast % 1000000007))%1000000007)\n \n \n", "# cook your dish here\nfor t in range (int(input())):\n n, k = map(int, input().split())\n a = k - 1\n b = k * 2\n c = k + n - 1\n d = b - c - 1\n #print(a,b,c, d)\n print(a + ((d * (d + 1)) // 2) % 1000000007)", "# cook your dish here\nimport math\nt = int(input())\nfor _ in range(t):\n m=10**9 + 7\n s = 0\n n,k = list(map(int,input().split()))\n if(n==2):\n print(((k*(k-1))//2)%m)\n else:\n p = (k-1)//(n-1)\n s = (((p+1)*(2*k- n*p + p -2))//2)%m\n print(s)\n \n \n", "# cook your dish here\nimport math\nt = int(input())\nfor _ in range(t):\n m=10**9 + 7\n p = 0\n s = 0\n n,k = list(map(int,input().split()))\n if(n==2):\n print(((k*(k-1))//2)%m)\n else:\n while((k-1-p*(n-1))>0):\n s+=(k-1-p*(n-1))%m\n p+=1\n print(s)\n \n \n", "# cook your dish here\nimport math\nt = int(input())\nfor _ in range(t):\n m=10**9 + 7\n p = 0\n s = 0\n n,k = list(map(int,input().split()))\n if(n==2):\n print(((k*(k-1))//2)%m)\n else:\n while((k-1-p*(n-1))>0):\n s+=k-1-p*(n-1)\n p+=1\n print(s)\n \n \n", "# cook your dish here\nimport math\nt = int(input())\nfor _ in range(t):\n m=10**9 + 7\n n,k = list(map(int,input().split()))\n if(n==2):\n print(((k*(k-1))//2)%m)\n", "mod=10**9+7\n\nfor _ in range(int(input())):\n n,k=map(int,input().split())\n if k<=n:\n print((k-1)%mod)\n continue\n x=(k-1)//(n-1)+1;\n if (x%2):\n d=(((x-1)//2)%mod)*(x%mod);\n else:\n d=((x//2)%mod)*((mod+x-1)%mod);\n d=d%mod\n x=x%mod\n k=(k-1)%mod\n n=(n-1)%mod\n ans=(k*x)%mod-(d*n)%mod;\n if ans<0:\n ans=mod+ans;\n print(ans%mod);", "# cook your dish here\n# cook your dish here\nMOD=10**9+7\nfor _ in range (int(input())):\n n,k=map(int,input().split())\n a=k\n b=n+k-1\n c=(a-1)%MOD\n req=a*2-b-1\n d=n-1\n if req>0:\n if req%d==0:\n nt=req//d\n else:\n nt=req//d+1\n c+=(((nt*(2*req+(nt-1)*(-d)))//2)%MOD)%MOD\n print(c%MOD)", "t=int(input())\nfor i in range(t):\n [n,k]=input().split()\n n=int(n)\n k=int(k)\n a=k-1\n d=1-n\n x=(a//(n-1))+1\n ans= ((x*(2*a+(x-1)*d))//2)%1000000007;\n print(ans)", "\n\nfrom sys import stdin,stdout\nMod=10**9+7\nt=int(stdin.readline())\nfor i in range(t):\n n,k=list(map(int,stdin.readline().split()))\n a=(k-1)%(n-1)\n d=n-1\n num=(k-1)//(n-1)+1\n res=(num*(2*a+(num-1)*d))//2\n stdout.write(str(res%Mod)+\"\\n\")\n \n", "\ndef main():\n from sys import stdin,stdout\n Mod=10**9+7\n t=int(stdin.readline())\n for i in range(t):\n n,k=list(map(int,stdin.readline().split()))\n a=(k-1)%(n-1)\n d=n-1\n num=(k-1)//(n-1)+1\n res=(num*(2*a+(num-1)*d))//2\n stdout.write(str(res%Mod)+\"\\n\")\n \ndef __starting_point():\n main()\n__starting_point()", "def main():\n Mod=10**9+7\n t=int(input())\n for i in range(t):\n n,k=list(map(int,input().split()))\n a=(k-1)%(n-1)\n d=n-1\n num=(k-1)//(n-1)+1\n res=(num*(2*a+(num-1)*d))//2\n print(res%Mod)\n \ndef __starting_point():\n main()\n__starting_point()", "for i in range(int(input())):\n n,k = [int(i) for i in input().split()]\n mod = 1000000007\n c = (k - 1) % mod\n dif = (2 * k) - (n + k - 1) - 1\n if dif > 0:\n no = dif // (n - 1)\n if dif % (n - 1) != 0:\n no += 1\n c = (c + (no * (2 * dif - (no - 1)*(n - 1)) // 2) % mod) % mod\n print(c)", "# cook your dish here\nt=int(input())\nfor _ in range(t):\n n,k=map(int,input().split())\n summation=0\n last_term=k-1\n common_difference=n-1\n first_term=last_term%common_difference\n if first_term==0:\n first_term=common_difference\n no_of_terms=(last_term-first_term)//common_difference + 1\n #print(no_of_terms,first_term,last_term,common_difference)\n summation=(((no_of_terms)*(first_term+last_term))//2)%1000000007\n print(summation)", "import math\nimport bisect\n\ndef power(x, y, p) : \n res = 1\n x = x%p\n while (y>0):\n if ((y&1)==1):\n res = (res*x)%p\n y = y>>1 # y = y/2 \n x = (x*x)%p \n return res \n\ndef inn():\n return int(input())\n\ndef inl():\n return list(map(int, input().split()))\n \nMOD = 10**9+7\nINF = inf = 10**18+5\n\nfor t in range(int(input())):\n n, k = inl()\n \n # if n>=k or k<3:\n # print(k-1)\n # continue\n \n a = k-1\n d = n-1\n m = a//d+1\n ans = ((m*(2*a-(m-1)*d))//2)%MOD\n print(ans)", "import math\nimport bisect\n\ndef power(x, y, p) : \n res = 1\n x = x%p\n while (y>0):\n if ((y&1)==1):\n res = (res*x)%p\n y = y>>1 # y = y/2 \n x = (x*x)%p \n return res \n\ndef inn():\n return int(input())\n\ndef inl():\n return list(map(int, input().split()))\n \nMOD = 10**9+7\nINF = inf = 10**18+5\n\nfor t in range(int(input())):\n n, k = inl()\n \n if n>=k or k<3:\n print(k-1)\n continue\n \n a = k-1\n d = n-1\n m = a//d+1\n ans = ((m*(2*a-(m-1)*d))//2)%MOD\n print(ans)", "# cook your dish here\nt=int(input())\nmod=10**9+7\nfor i in range(t):\n n,k=input().split(' ')\n ki=int(k)\n ni=int(n)\n km=ki-1\n nm=ni-1\n q=km//nm\n r=km%nm\n #print(r,q)\n a=km+(q*r)+(nm*q*(q-1))//2\n ans=a%mod\n print(ans)\n #print(r,q,a)\n", "# cook your dish here\nt=int(input())\nmod=10**9+7\nfor i in range(t):\n n,k=input().split(' ')\n ki=int(k)\n ni=int(n)\n km=ki-1\n nm=ni-1\n q=km//nm\n r=km%nm\n #print(r,q)\n a=km+(q*r)+(nm*q*(q-1))//2\n if ni==2:\n a=(((ki)*(ki-1))//2)%mod\n ans=a%mod\n print(ans)\n #print(r,q,a)\n", "MOD = 1000000007\nfor _ in range(int(input())):\n n, k = map(int, input().split())\n last_term = k - 1\n if n == 2:\n ans = (last_term * (last_term + 1)) // 2\n print(ans % MOD)\n\n else:\n diff = n - 1\n first_term = last_term%diff\n if first_term==0:\n first_term = diff\n no_of_terms = (last_term - first_term) // diff + 1\n ans = ((2*first_term+((no_of_terms - 1) * diff)) * no_of_terms) // 2\n print(ans % MOD)", "# t=int(input())\n# for _ in range(t):\n# n,k=map(int,input().split())\n# summation=0\n# last_term=k-1\n# common_difference=n-1\n# first_term=last_term%common_difference\n# if first_term==0:\n# first_term=common_difference\n# no_of_terms=(last_term-first_term)//common_difference + 1\n# #print(no_of_terms,first_term,last_term,common_difference)\n# summation=(((no_of_terms)*(first_term+last_term))//2)%1000000007\n# print(summation)\n#\n# import math\n#\n# MOD = 1000000007\n# t = int(input())\n# for _ in range(t):\n# n, k = map(int, input().split())\n# first = k - 1;\n# diff = n - 1;\n# t = (first + diff) // (diff)\n# # print(t)\n# y = (2 * (first) - (t - 1) * (diff))\n# if (y % 2 == 0):\n# y = y // 2;\n# else:\n# t = t // 2;\n#\n# ans = y * t;\n# ans = ans % MOD\n# # print(\"n,k:\"+str(n)+\" \"+str(k))\n# print(ans)\nMOD = 1000000007\nfor _ in range(int(input())):\n n,k = map(int,input().split())\n if n==2:\n last_term = k-1\n first_term = 1\n no_terms = last_term\n ans = (last_term*(last_term+1))//2\n print(ans%MOD)\n \n else:\n print(1)", "# cook your dish here\nfrom decimal import Decimal\nimport math\nt = int(input())\nfor x in range(t):\n n, k = map(int, input().split())\n tot = k - 1\n if(n < k):\n left = k - n\n extra = n - 1\n if(left <= extra):\n tot += left\n else:\n y = math.ceil(Decimal(left) / Decimal(extra))\n tot += y * left - (y * (y - 1) * (extra))//2\n print(tot % 1000000007)", "# cook your dish here\nimport math\nt = int(input())\nfor x in range(t):\n n, k = map(int, input().split())\n tot = k - 1\n if(n < k):\n left = k - n\n extra = n - 1\n if(left <= extra):\n tot += left\n else:\n y = math.ceil(left / extra)\n tot += y * left - (y * (y - 1) * (extra))//2\n print(tot % 1000000007)", "# cook your dish here\nimport math\nt = int(input())\nfor x in range(t):\n n, k = map(int, input().split())\n tot = k - 1\n if(n < k):\n left = k - n\n extra = n - 1\n if(left <= extra):\n tot += left\n else:\n y = math.ceil(left / extra)\n tot = (tot + (y * left - (y * (y - 1) * (extra))//2))\n print(tot % 1000000007)"]
{"inputs": [["2", "2 1", "3 3"]], "outputs": [["0", "2"]]}
INTERVIEW
PYTHON3
CODECHEF
8,642
6410eeba574bcd87e52c5dfd68bbea11
UNKNOWN
Consider a number X on which K Mag-Inc operations are to be performed. In a Mag-Inc operation, the number X undergoes an increment of A/B times of X where A and B are two integers. There is a numerator and a denominator array of size K which contain the ith values of A and B. After K Mag-Inc operations, the number X turns to M. Now your job is to find what percentage of M is to be decremented from M if it has to be converted back to X. Let this percentage be denoted by Z. Print the integral part of Z. -----Input:----- First line contains an integer T denoting the number of test cases. First line of every test case contains two space separated integers X and K. The second and third line of every test case will contain K space separated integers denoting the Numerator and Denominator array. -----Output:----- For each test case, print the required result in a single line. -----Constraints:----- 1 ≤ T ≤ 100 1 ≤ K, A, B ≤ 40000 1≤X≤10^100 -----Example:-----Input: 2 100 1 1 4 100 2 1 1 2 3Output: 20 50 -----Explanation:----- Case 2: 100 undergoes an increment of (1/2)*100. Therefore M = 100 + 50. Now M = 150. Now again, M undergoes an increment of (1/3)*150. Therefore, M = 150 + 50. Now as we want to revert back M = 200 to X i.e. 100, we need to decrement it by a value of 100 and we know that 100 is 50% of 200. Hence, we print 50.
["import sys\nimport math\n\ndef solution():\n T = int(input().strip())\n for _ in range(T):\n x, k = list(map(float, input().strip().split(' ')))\n original_x = x\n if k == 1:\n a = [float(input().strip())]\n b = [float(input().strip())]\n else:\n a = list(map(float, input().strip().split(' ')))\n b = list(map(float, input().strip().split(' ')))\n for i in range(int(k)):\n x = x + (a[i]/b[i])*(x)\n percentage = ((x - original_x) / x)*100\n print(\"%d\"%(int(percentage)))\n\nsolution()", "import sys\nT=int(sys.stdin.readline())\nwhile T:\n n,k=list(map(int,input().split()))\n num=list(map(int, sys.stdin.readline().split()))\n den=list(map(int, sys.stdin.readline().split()))\n ans=n\n for i in range (k):\n ans+=ans*num[i]/float(den[i])\n print(int(100-100*n/ans))\n T-=1", "t = int(input())\n\nfor i in range(t):\n value, k = list(map(int, input().split()))\n A = list(map(int, input().split()))\n B = list(map(int, input().split()))\n newValue = value\n for j in range(k):\n temp = newValue*float(A[j])/B[j]\n newValue+=temp\n error = value*100/newValue\n print(int(100-error))\n", "t=int(input())\nfor tc in range(0,t):\n n,k=list(map(int,input().split()))\n num=list(map(int,input().split()))\n den=list(map(int,input().split()))\n temp=n\n for i in range(0,k):\n temp=temp+(temp**1.0*num[i]/den[i])\n answer=int(100-(n*1.0*100/temp))\n print(answer)", "t=eval(input())\nwhile t:\n t-=1\n x,k=list(map(int,input().split()))\n a=list(map(float,input().split()))\n b=list(map(float,input().split()))\n rat = 1.0\n for i in range(k):\n rat = rat*((a[i]+b[i])/b[i])\n rat = ((rat-1)/rat)*100.0\n print(int(rat))\n", "t = float(input())\nwhile(t):\n t -= 1\n x, k = list(map(float, input().split()))\n k = int(k)\n a = list(map(float, input().split()))\n b = list(map(float, input().split()))\n temp = x\n for i in range(0, k):\n xxx = (temp*a[i])/b[i]\n temp += xxx\n px = (temp-x)/temp * 100.0;\n print(int(px)) ", "for _i in range(int(input())):\n x,k = list(map(int,input().split()))\n a = list(map(int,input().split()))\n b = list(map(int,input().split()))\n l = []\n l.append(x)\n for i in range(k):\n x = x+(x*(float(a[i])/b[i]))\n w = l[0]\n perc = 100*(float(w)/x)\n ans = 100-perc\n print(int(ans))\n\n\n\n"]
{"inputs": [["2", "100 1", "1", "4", "100 2", "1 1", "2 3"]], "outputs": [["20", "50"]]}
INTERVIEW
PYTHON3
CODECHEF
2,250