Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
children-love-candies
Today, N candies were placed on the table. At any point, for the purpose of clarity, let X represent the number of candies left on the table. Every day, at 9 PM, if the number of candies remaining is divisible by 2, the child will take exactly X/2 candies. Otherwise, he will take (X+1)/2 candies. In addition, every day at 10 PM, another child will come and take X//4, where the "//" here represents integer division. Please, find the number of candies left at 11 PM after T days have passed. Input: The first line contains one integer, Q, the number of test cases. The next Q lines contain two integers, N and T. Output: Output Q lines, the answer for each test case. Constraints: 1 ≤ Q ≤ 10 1 ≤ N ≤ 10^16 1 ≤ T ≤ 50 SAMPLE INPUT 1 2 1 SAMPLE OUTPUT 1
3
0
for _ in range(input()): noOfCandies, noOfDays = map(int, raw_input().split()) for _ in range(noOfDays): if noOfCandies % 2 > 0: noOfCandies -= (noOfCandies+1)/2 else: noOfCandies -= noOfCandies/2 noOfCandies -= noOfCandies//4 print noOfCandies
PYTHON
children-love-candies
Today, N candies were placed on the table. At any point, for the purpose of clarity, let X represent the number of candies left on the table. Every day, at 9 PM, if the number of candies remaining is divisible by 2, the child will take exactly X/2 candies. Otherwise, he will take (X+1)/2 candies. In addition, every day at 10 PM, another child will come and take X//4, where the "//" here represents integer division. Please, find the number of candies left at 11 PM after T days have passed. Input: The first line contains one integer, Q, the number of test cases. The next Q lines contain two integers, N and T. Output: Output Q lines, the answer for each test case. Constraints: 1 ≤ Q ≤ 10 1 ≤ N ≤ 10^16 1 ≤ T ≤ 50 SAMPLE INPUT 1 2 1 SAMPLE OUTPUT 1
3
0
for _ in range(input()): n,t = map(int,raw_input().split()) for i in range(t): if n%2 == 0: n /= 2 else: n = n - ((n+1)/2) n = n - n/4 print n
PYTHON
children-love-candies
Today, N candies were placed on the table. At any point, for the purpose of clarity, let X represent the number of candies left on the table. Every day, at 9 PM, if the number of candies remaining is divisible by 2, the child will take exactly X/2 candies. Otherwise, he will take (X+1)/2 candies. In addition, every day at 10 PM, another child will come and take X//4, where the "//" here represents integer division. Please, find the number of candies left at 11 PM after T days have passed. Input: The first line contains one integer, Q, the number of test cases. The next Q lines contain two integers, N and T. Output: Output Q lines, the answer for each test case. Constraints: 1 ≤ Q ≤ 10 1 ≤ N ≤ 10^16 1 ≤ T ≤ 50 SAMPLE INPUT 1 2 1 SAMPLE OUTPUT 1
3
0
test_cases=int(raw_input()) for i in range(test_cases): n,t=raw_input().split() n=int(n) t=int(t) candies=n days=t while(t>0): #At 9 Am reduction of chocolates if(candies%2==0): candies=candies-(candies/2) elif(candies%2==1): candies=candies-((candies+1)/2) #At 10 am candies=candies-candies//4 t-=1 print candies
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
t=input() while t: a=map(str, raw_input("").split()) base=int(a[0]) s=a[1] k=1 ans=0 while len(s)>0: l=s[-1] if ord(l)<59: l=ord(l)-48 else: l=ord(l)-87 ans+=k*l k*=base s=s[:-1] x=str(ans) print sum(int(i) for i in x) t-=1
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
# your code goes here import sys t=input() for _ in range(t): s=raw_input() st=s.split(' ') a=int(st[0]) b=str(st[1]) aa=int(b,a) ab=str(aa) ans=0 for i in range(len(ab)): ans+=int(ab[i]) print ans
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
def solve(): b,s=raw_input().split() print sum(map(int,str(int(s,int(b))))) for _ in xrange(input()): solve()
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
for i in xrange(input()): p=raw_input().split() q=int(p[1],int(p[0])) print sum(map(int,str(q)))
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
cases = input() for i in range(cases): base,string = raw_input().split() base = (int)(base) answer = int(string,base) answer = sum(map(int,str(answer))) print answer
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
t = input() while t: t-=1 text=raw_input() st = text.split() base = int(st[0]) num = str(st[1]) num = int(num,base) num = str(num) le = len(num) ans = 0 for i in range(0,le): ans += int(num[i]) print ans
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
t = raw_input() t=int(t) i=0 while i<t: line=raw_input() line=line.split(' ') base=int(line[0]) s=line[1] length= len(s) j=length-1 j=int(j) p=1 ans=0 while j>=0: temp=0 if s[j]>='a' and s[j]<='z': temp = ord(s[j])-97+10 else: temp = ord(s[j]) - 48 ans = ans+temp*p p = p*base j-=1 sums=0 my =str(ans) th=len(my) k=0 while k<th: sums = sums+int(my[k]) k=k+1 print sums i=i+1
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
t = raw_input() t=int(t) i=0 while i<t: line=raw_input() line=line.split(' ') base=int(line[0]) s=line[1] length= len(s) j=length-1 j=int(j) p=1 ans=0 while j>=0: temp=0 if s[j]>='a' and s[j]<='z': temp = ord(s[j])-87 else: temp = ord(s[j]) - 48 ans = ans+temp*p p = p*base j-=1 sums=0 my =str(ans) th=len(my) k=0 while k<th: sums = sums+int(my[k]) k=k+1 print sums i=i+1
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
import sys t=int(sys.stdin.readline()) for x in range(0,t): a=sys.stdin.readline().split() sum=0 b=int(a[1],int(a[0])) for y in str(b): sum+=int(y) print(sum)
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
t=input() while t: a=map(str, raw_input("").split()) base=int(a[0]) stri=a[1] q=1 ans=0 while len(stri)>0: l=stri[-1] if ord(l)<59: l=ord(l)-48 else: l=ord(l)-87 ans+=q*l q*=base stri=stri[:-1] x=str(ans) print sum(int(i) for i in x) t-=1
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
for _ in xrange(input()): a,b=raw_input().split() x=int(str(b),int(a)) print sum([int(i) for i in str(x)])
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
import math t=(int)(input()) while t: t-=1 num=0 n,s=raw_input().split() n,s=(int)(n),(str)(s) l=len(s) k=1 for i in range(l): tmp=ord(s[l-i-1]) if tmp<60: num+=(tmp-48)*k else: num+=(tmp-87)*k k*=n x=(str)(num) #print num, print sum([(int)(i) for i in x])
PYTHON
dummy-2
Hansa is throwing a birthday party. Seeing the extravagant parties thrown by her friends in the past, Hansa too decided to do something unique. Being a Computer Engineer herself, she knew just how to do it. She sent password-protected e-invites to T of her friends. Along with each of those e-invites there would be a number and a string. The number represents the base of the string. Their task was to convert the given string to decimal and add the Sum of Digits of that string which would be the password for that invite. Help her friends find the password so that they can enjoy the party! Input: First line of input is an integer t, the number of test cases. The following t lines would contain an integer and a string separated by a space. Output: For each test case, print the sum of digits of the string after changing it to decimal. Constraints: 1 ≤ t ≤ 500 2 ≤ base ≤ 36 1 ≤ |string length| ≤ 10000 Problem Setter: Rohit Mishra SAMPLE INPUT 5 27 1 4 3201011 6 5 29 ffarpr 3 1 SAMPLE OUTPUT 1 14 5 49 1 Explanation Test case #1 - 1 in base-27 means (1 x 27^0) = 1, So, sum of digits is 1 Test case #2 - 3201011 in base-4 means (1 x 4^0) + (1 x 4^1) + (0 x 4^2) + (1 x 4^3) + (0 x 4^4) + (2 x 4^5) + (3 x 4^6) = 144051, So, sum of digits is 14 Test case #3 - 5 in base-6 means (5 x 6^0) = 5, So, sum of digits is 5 Test case #4 - ffarpr in base-29 means (27 x 29^0) + (25 x 29^1) + (27 x 29^2) + (10 x 29^3) + (15 x 29^4) + (15 x 29^5) = 318543799, So, sum of digits is 49 Test case #5 - 1 in base-3 means (1 x 3^0) = 1, So, sum of digits is 1
3
0
t = input() while t: t-=1 text=raw_input() st = text.split() base = int(st[0]) num = str(st[1]) num2 = int(num,base) num3 = str(num2) le = len(num3) ans = 0 for i in range(0,le): ans += int(num3[i]) print ans
PYTHON
guess-it-1
Gazi is a very talented mathematician, recently he discovered the majestic power of the number '9' so he wants to play a game with you. He tells you to pick any real integer [from 10 to 10^200], then subtract sum of digits of this number from the number itself. You will get another number, now omit any digit from this number then tell Gazi the final number obtained and he will tell you the number you omitted. For example: you picked number: 2181 step1) 2181-(2+1+8+1)=2181-12=2169 step2) omit any number from 2169, say 1 i.e,2_69(=final number=F) tell this number to Gazi and he will give you the number which you omitted in this case '1'. In case the omitted number is '0' or '9' he says "0 or 9". Gazi don't want to do the calculations again and again so he request you to craft a code to do the same task. Input The first line contains number of testcases 't' (1 ≤ t ≤ 100), second line contains the final number 'F' (10 ≤ F<10^200) with a symbol of underscore where you omitted the number. Output For each test case, print the omitted number and in case the number comes out to be '0' or '9' print "0 or 9". With a new line at the end of each output. SAMPLE INPUT 4 4_1 52_2 63_124 25141452_5575 SAMPLE OUTPUT 4 0 or 9 2 8
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' #print 'Hello World!' t=input() while t>0: t-=1 s=raw_input() for i in range(0,9): x=s.replace("_",str(i)) if int(x)%9==0: c=i break if c==0: print "0 or 9" else: print c
PYTHON
guess-it-1
Gazi is a very talented mathematician, recently he discovered the majestic power of the number '9' so he wants to play a game with you. He tells you to pick any real integer [from 10 to 10^200], then subtract sum of digits of this number from the number itself. You will get another number, now omit any digit from this number then tell Gazi the final number obtained and he will tell you the number you omitted. For example: you picked number: 2181 step1) 2181-(2+1+8+1)=2181-12=2169 step2) omit any number from 2169, say 1 i.e,2_69(=final number=F) tell this number to Gazi and he will give you the number which you omitted in this case '1'. In case the omitted number is '0' or '9' he says "0 or 9". Gazi don't want to do the calculations again and again so he request you to craft a code to do the same task. Input The first line contains number of testcases 't' (1 ≤ t ≤ 100), second line contains the final number 'F' (10 ≤ F<10^200) with a symbol of underscore where you omitted the number. Output For each test case, print the omitted number and in case the number comes out to be '0' or '9' print "0 or 9". With a new line at the end of each output. SAMPLE INPUT 4 4_1 52_2 63_124 25141452_5575 SAMPLE OUTPUT 4 0 or 9 2 8
3
0
t=input() while t>0: t-=1 s=raw_input() q=[ord(c) for c in s] l=len(q) n=0 for i in q: if i!=95 and i>47: n=n+(i-48) for i in range(0,9): if ((n+i)%9==0): if i==0: print "0 or 9" else: print i break
PYTHON
guess-it-1
Gazi is a very talented mathematician, recently he discovered the majestic power of the number '9' so he wants to play a game with you. He tells you to pick any real integer [from 10 to 10^200], then subtract sum of digits of this number from the number itself. You will get another number, now omit any digit from this number then tell Gazi the final number obtained and he will tell you the number you omitted. For example: you picked number: 2181 step1) 2181-(2+1+8+1)=2181-12=2169 step2) omit any number from 2169, say 1 i.e,2_69(=final number=F) tell this number to Gazi and he will give you the number which you omitted in this case '1'. In case the omitted number is '0' or '9' he says "0 or 9". Gazi don't want to do the calculations again and again so he request you to craft a code to do the same task. Input The first line contains number of testcases 't' (1 ≤ t ≤ 100), second line contains the final number 'F' (10 ≤ F<10^200) with a symbol of underscore where you omitted the number. Output For each test case, print the omitted number and in case the number comes out to be '0' or '9' print "0 or 9". With a new line at the end of each output. SAMPLE INPUT 4 4_1 52_2 63_124 25141452_5575 SAMPLE OUTPUT 4 0 or 9 2 8
3
0
def sumofDigit(e): s=str(e) su=0 for i in s: su+=int(i) return su t=int(raw_input()) for sdgdsg in xrange(t): s=int(raw_input().replace('_','')) a=9-(sumofDigit(s)%9) print a if (a != 0 and a!=9) else '0 or 9'
PYTHON
guess-it-1
Gazi is a very talented mathematician, recently he discovered the majestic power of the number '9' so he wants to play a game with you. He tells you to pick any real integer [from 10 to 10^200], then subtract sum of digits of this number from the number itself. You will get another number, now omit any digit from this number then tell Gazi the final number obtained and he will tell you the number you omitted. For example: you picked number: 2181 step1) 2181-(2+1+8+1)=2181-12=2169 step2) omit any number from 2169, say 1 i.e,2_69(=final number=F) tell this number to Gazi and he will give you the number which you omitted in this case '1'. In case the omitted number is '0' or '9' he says "0 or 9". Gazi don't want to do the calculations again and again so he request you to craft a code to do the same task. Input The first line contains number of testcases 't' (1 ≤ t ≤ 100), second line contains the final number 'F' (10 ≤ F<10^200) with a symbol of underscore where you omitted the number. Output For each test case, print the omitted number and in case the number comes out to be '0' or '9' print "0 or 9". With a new line at the end of each output. SAMPLE INPUT 4 4_1 52_2 63_124 25141452_5575 SAMPLE OUTPUT 4 0 or 9 2 8
3
0
no_of_cases = int(raw_input()) numbers = [] for x in range(0,no_of_cases): number = raw_input().replace('_','') numbers.append(number) def guess_it(x): total = 0 for x in str(x): total = total+int(x) rem = total%9 value = 9-rem if value ==9 or value ==0: print "0 or 9" else: print value for x in numbers: missing_value = guess_it(x)
PYTHON
guess-it-1
Gazi is a very talented mathematician, recently he discovered the majestic power of the number '9' so he wants to play a game with you. He tells you to pick any real integer [from 10 to 10^200], then subtract sum of digits of this number from the number itself. You will get another number, now omit any digit from this number then tell Gazi the final number obtained and he will tell you the number you omitted. For example: you picked number: 2181 step1) 2181-(2+1+8+1)=2181-12=2169 step2) omit any number from 2169, say 1 i.e,2_69(=final number=F) tell this number to Gazi and he will give you the number which you omitted in this case '1'. In case the omitted number is '0' or '9' he says "0 or 9". Gazi don't want to do the calculations again and again so he request you to craft a code to do the same task. Input The first line contains number of testcases 't' (1 ≤ t ≤ 100), second line contains the final number 'F' (10 ≤ F<10^200) with a symbol of underscore where you omitted the number. Output For each test case, print the omitted number and in case the number comes out to be '0' or '9' print "0 or 9". With a new line at the end of each output. SAMPLE INPUT 4 4_1 52_2 63_124 25141452_5575 SAMPLE OUTPUT 4 0 or 9 2 8
3
0
import sys t=input() while t>0: k=[] s=sys.stdin.readline().strip().split("_") if s[0]!="": k.extend(list(s[0])) if s[1]!="": k.extend(list(s[1])) k=map(int,k) s=sum(k) s=s%9 if s==0: print "0 or 9" else: print 9-s t=t-1
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
N,Q=map(int,raw_input().split()) arrx=map(int,raw_input().split()) winners=[0]*N players=[] for i in xrange(0,N-1,2): if arrx[i]<arrx[i+1]: winners[i]=1 players.append(i+1) else: winners[i+1]=1 players.append(i) if N%2==1: players.append(N-1) winners[N-1]-=1 numround=2 while len(players)>1: old=players players=[] for i in xrange(0,len(old)-1,2): if arrx[old[i]]<arrx[old[i+1]]: winners[old[i]]+=numround players.append(old[i+1]) else: winners[old[i+1]]+=numround players.append(old[i]) if len(old)%2==1: players.append(old[-1]) winners[old[-1]]-=1 numround+=1 winners[players[0]]+=numround-1 # was already incremented for asked in xrange(Q): if N==1: print 0 else: print winners[int(raw_input())-1]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n, q = map(int, raw_input().split()) st = map(int, raw_input().split()) fighters = [(i, s) for i, s in enumerate(st)] fights = [0] * n while n > 1: i = 0 if n % 2 == 1: n -= 1 pops = [] while i < n: f1, s1 = fighters[i] f2, s2 = fighters[i + 1] fights[f1] += 1 fights[f2] += 1 if s1 > s2: pops.append(i + 1) else: pops.append(i) i += 2 pops.sort(reverse=True) for pop in pops: fighters.pop(pop) n = len(fighters) while q > 0: i = int(raw_input()) print fights[i - 1] q -= 1
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
N, Q = map(int, raw_input().strip().split(' ')) strength = map(int, raw_input().strip().split(' ')) nooffights = [0] * N finarr = range(0, N) while (1) : var = 0 newarr = [] while (1) : if (var == len(finarr)) : break if (var == len(finarr) - 1) : newarr.append(finarr[var]) break nooffights[finarr[var]] += 1 nooffights[finarr[var + 1]] += 1 if (strength[finarr[var]] > strength[finarr[var + 1]]) : newarr.append(finarr[var]) else : newarr.append(finarr[var + 1]) var = var + 2 finarr = newarr if (len(finarr) == 1) : break for i in range(0, Q) : print nooffights[int(raw_input().strip()) - 1]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' n,q = map(int,raw_input().split()) pl = dict(zip([x for x in range(1,n+1)],map(int,raw_input().split()))) pp = dict(zip([x for x in range(1,n+1)],[0 for z in range(1,n+1)])) j=1 while len(pl.keys()) != 1: k = pl.keys() for i in range(0,len(k)-1,2): pp[k[i]]+=1 pp[k[i+1]]+=1 if pl[k[i]] < pl[k[i+1]]: del pl[k[i]] else: del pl[k[i+1]] for i in range(q): print pp[int(raw_input())]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
'''input 5 5 1 2 3 4 5 1 2 3 4 5 ''' #~~~~~~~~~~~~~~~~~~~~dwij28 == Abhinav Jha~~~~~~~~~~~~~~~~~~~~# from sys import stdin, stdout, maxint from math import sqrt, floor, ceil, log from collections import defaultdict, Counter def read(): return stdin.readline().strip() def write(x): stdout.write(str(x)) def endl(): write("\n") n, q = map(int, read().split()) player, power, count = list(xrange(n)), [int(i) for i in read().split()], [0]*n while len(player) > 1: a, i, x = [], 0, len(player)/2 while x: count[player[i]] += 1 count[player[i+1]] += 1 a.append(player[i] if power[player[i]] > power[player[i+1]] else player[i+1]) i, x = i+2, x-1 if i == len(player) - 1: a.append(player[i]) player = list(a) for Q in xrange(q): write(count[int(read())-1]) endl()
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n, q = raw_input().split() # n = no of fighter n = int(n); q = int(q); # q = querys to be display #print "n", n, "q", q strength = [0] * n # strength of each fighter query = [0] * q # query display order fights = [0] * n alive = [1] * n remain = n strength = raw_input().split() strength = map( int, strength) #print "strength ", strength #print "query ", query #fights = { i : 0 for i in strength} for i in range(q): query[i] = int( raw_input() ) #print "fights ", fights #print "query ", query #print "alive ", alive while(remain > 1): i = 0 # print "r ",remain, remain while( i < n ): if ( alive[i] == 1 ): first = i j = i + 1 while(j < n): if ( alive[j] == 1 ): second = j if( strength[first] > strength[second] ): alive[second] = 0 else: alive[first] = 0 i = j fights[first] = fights[first] + 1 fights[second] = fights[second] + 1 remain -= 1 # print fights,alive break j += 1 i += 1 #print "fights ",fights #print "alive ",alive for i in range(q): print fights[query[i] - 1] del(strength)
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
N, Q = map(int, raw_input().split()) fighters = map(int, raw_input().split()) status = [True]*N fights = [0]*N players = N while players > 1: i = 0 while i < N-1: if status[i]: check = False for q in xrange(i+1, N): if status[q]: check = True break if check: fights[i] += 1 fights[q] += 1 players -= 1 if fighters[i] > fighters[q]: status[q] = False else: status[i] = False i = q else: break i += 1 for _ in xrange(Q): print fights[input()-1]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n,q = map(int,raw_input().strip().split(" ")) weight = map(int,raw_input().strip().split(" ")) totalCount = 0 fighters = range(0,n) fights = [0]*n fightersSubArray = [] while(len(fighters)>1): index = 0 while(index < len(fighters)): if((index+1)<len(fighters)): fights[fighters[index]] += 1 fights[fighters[index+1]] += 1 if(weight[fighters[index]] < weight[fighters[index+1]]): fightersSubArray.append(fighters[index+1]) elif(weight[fighters[index]] > weight[fighters[index+1]]): fightersSubArray.append(fighters[index]) else: fightersSubArray.append(fighters[index]) fightersSubArray.append(fighters[index+1]) else: fightersSubArray.append(fighters[index]) index += 2 fighters = [] fighters = fightersSubArray fightersSubArray = [] i = 0 while i<q: indexVal = int(raw_input()) print fights[indexVal-1] i+=1
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
kk=raw_input() n,b=kk.split() n=int(n) b=int(b) a = [0]*n ar=[] string_input = raw_input() arr = string_input.split() arr = [int(k) for k in arr] for i in range(0,n): ar.append(i) while(len(ar)>1): m=int(len(ar)/2) for i in range(0,m): a[ar[i]]=a[ar[i]]+1 a[ar[i+1]]=a[ar[i+1]]+1 if arr[ar[i]]>arr[ar[i+1]]: del(ar[i+1]) else: del(ar[i]) for x in range(0,b): print(a[int(input())-1])
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import sys L=map(int, sys.stdin.read().split()) N, Q=L[0], L[1] L=L[2:] matches={} pS={} for x in xrange(N): matches[L[x]]=0 pS[x+1]=L[x] mat=L[:N] L=L[N:] while True: for x in xrange(0, N, 2): if x+1<N: mat.append(max(mat[x], mat[x+1])) matches[mat[x]]+=1 matches[mat[x+1]]+=1 else: mat.append(mat[x]) mat=mat[N:] N=len(mat) if N==1: mat=[] break for x in L: print matches[pS[x]]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
N, Q = [int(x) for x in raw_input().split(' ')] Strength = [int(x) for x in raw_input().split(' ')] Game = [0] * N Fight = [] for i in xrange(N): Fight.append(i) # Processs Fight while len(Fight) > 1: for i in xrange(len(Fight)/2): Game[Fight[i]] += 1 Game[Fight[i+1]] += 1 if Strength[Fight[i]] > Strength[Fight[i+1]]: del Fight[i+1] else: del Fight[i] for i in xrange(Q): idx = int(raw_input()) - 1 print Game[idx]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
def solution(): N, Q = map(int, raw_input().strip().split(' ')) arr = map(int, raw_input().strip().split(' ')) q = [] winner_counts = {} new_arr = [] for i in xrange(N): winner_counts[i] = 0 new_arr.append((i, arr[i])) for i in xrange(Q): q.append(int(raw_input().strip())) #print new_arr while len(new_arr) != 1: new_arr = give_winner_list(new_arr, winner_counts) #print new_arr, winner_counts for i in xrange(Q): print "{}".format(winner_counts[q[i] - 1]) def give_winner_list(new_arr, winner_counts): len_new_arr = len(new_arr) new_new_arr = [] for i in xrange(0, len_new_arr, 2): try: winner_counts[new_arr[i+1][0]] += 1 winner_counts[new_arr[i][0]] += 1 if new_arr[i][1] > new_arr[i+1][1]: new_new_arr.append(new_arr[i]) else: new_new_arr.append(new_arr[i+1]) except IndexError: new_new_arr.append(new_arr[i]) #print new_new_arr return new_new_arr solution()
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n, q = map(int, raw_input().split(" ")) fighter=[] count=[] query=[] advance=[] fighter = map(int, raw_input().split(" ")) i=0 while(i<q): temp = int(raw_input()) query.append(temp) i+=1 i=1 while(i<=n): count.append(0) advance.append(i) i+=1 while(len(advance)>1): if(len(advance)%2 == 0): i=0 while(i+1<len(advance)): if(fighter[advance[i]-1]>fighter[advance[i+1]-1]): count[advance[i+1]-1]+=1 count[advance[i]-1]+=1 advance.pop(i+1) else: count[advance[i+1]-1]+=1 count[advance[i]-1]+=1 advance.pop(i) i+=1 else: i=0 while(i+1<len(advance)-1): if(fighter[advance[i]-1]>fighter[advance[i+1]-1]): count[advance[i+1]-1]+=1 count[advance[i]-1]+=1 advance.pop(i+1) else: count[advance[i+1]-1]+=1 count[advance[i]-1]+=1 advance.pop(i) i+=1 i=0 while(i<q): print count[query[i]-1] i+=1
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n, q = map(int, raw_input().split()) f = (n) * [0] st = map(int, raw_input().split()) st = list(enumerate(st)) while len(st) > 1: so = [] for i, j in zip(st[0::2], st[1::2]): f[i[0]] += 1 f[j[0]] += 1 if i[1] > j[1]: so.append(i) else: so.append(j) if len(st) % 2: so.append(st[-1]) st = so for _ in range(q): print f[int(raw_input())-1]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
from sys import stdin n,q = map(int,stdin.readline().split()) a = map(int,stdin.readline().split()) di = {} ran = {} for i in xrange(1,n+1): di[i] = 0 ran[a[i-1]] = i b = [] i = 0 l = len(a) #print ran,di #print a while True: l = len(a) if l==1: break i = 0 #print a while True: if i+1>=l: break if i>=l: break res = a[i] #print "checking ",a[i],a[i+1] if a[i+1] > res: res = a[i+1] b.append(res) #print "won ",res,b di[ran[a[i]]]+=1 di[ran[a[i+1]]]+=1 i+=2 if i==l-1: b.append(a[-1]) #print b a = b b=[] for i in xrange(q): n = int(stdin.readline()) print di[n]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' # print 'Hello World!' try: N, Q= map(int, raw_input().strip().split(" ")) S= map(int, raw_input().strip().split(" ")) validFighters= range(N) fights=[0]*N #calculate fights while len(validFighters)>1: temp=[] for i in xrange(0,len(validFighters),2): if len(validFighters)%2 == 1 and i== len(validFighters)-1 : winF= validFighters[i] else: f1=validFighters[i] f2=validFighters[i+1] if S[f1]>S[f2]: winF= f1 # fights[winF]+=1 else: winF= f2 fights[f1]+=1 fights[f2]+=1 temp.append(winF) validFighters= temp for i in xrange(Q): q= int(raw_input().strip()) print fights[q-1] except Exception,e: print str(e)
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import math from collections import OrderedDict class Nodes(object): def __init__(self): self.data = None self.next = None self.prev = None self.seq_number = 0 def create_node(value): n = Nodes() n.data = value return n def make_struc(s): head = None end = None nodes = OrderedDict() count = 0 for i in s: n = create_node(i) n.seq_number = count + 1 nodes[n.seq_number] = 0 if head is None: head = n end = head else: n.prev = end end.next = n end = n count += 1 return head, end, nodes def get_winner(head, end, nodes): i = 0 l = len(nodes) if math.log(l, 2) % 1 == 0: limit = int(math.floor(math.log(l, 2))) else: limit = int(math.ceil(math.log(l, 2))) while i <= limit: temp1 = head temp2 = None prev = None count = 0 while temp1 is not None: temp2 = temp1.next if temp2 is not None: result = cmp(temp1.data, temp2.data) nodes[temp1.seq_number] += 1 nodes[temp2.seq_number] += 1 if result == 1: temp1.next = temp2.next del temp2 else: # covering both conditions when result = -1 or 0 del temp1 temp1 = temp2 del temp2 if count == 0: temp1.prev = None head = temp1 else: temp1.prev = prev prev.next = temp1 prev = temp1 count += 1 temp1 = temp1.next i += 1 return nodes def main(): N, Q = [int(i) for i in raw_input().split()] if 1 <= N <= 100000 and 1 <= Q <= 1000000: s = [int(i) for i in raw_input().split()] if len(s) == N: queries = [int(raw_input()) for i in range(Q)] head, end, nodes = make_struc(s) nodes = get_winner(head, end, nodes) for i in queries: print nodes.get(i) try: main() except Exception as e: print e.__str__()
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
def chunks(l, n): for i in range(0, len(l), n): yield l[i:i+n] def process(arr): while len(arr) != 1: k = [] tmp = [] if len(arr) % 2 != 0: tmp.append(arr.pop()) for i in chunks(arr, 2): if i[0] in cache: cache[i[0]] += 1 else: cache[i[0]] = 1 if i[1] in cache: cache[i[1]] += 1 else: cache[i[1]] = 1 winner = max(i[0], i[1]) k.append(winner) k.extend(tmp) arr = k[:] cache = {} N, Q = map(int, raw_input().strip(" \n").split(" ")) arr = map(int, raw_input().strip(" \n").split(" ")) process(arr[:]) for i in xrange(Q): q = input() if arr[q-1] in cache: print cache[arr[q-1]] else: print 0
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n,q = map(int,raw_input().split(" ")) pl = dict(zip([x for x in range(1,n+1)],map(int,raw_input().split(" ")))) pp = dict(zip([x for x in range(1,n+1)],[0 for z in range(1,n+1)])) j=1 while len(pl.keys()) != 1: k = pl.keys() for i in range(0,len(k)-1,2): pp[k[i]]+=1 pp[k[i+1]]+=1 if pl[k[i]] < pl[k[i+1]]: del pl[k[i]] else: del pl[k[i+1]] for i in range(q): print pp[int(raw_input())]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
N, Q = map(int, raw_input().split()) data = map(int, raw_input().split()) newData = [] dataCopy = data counts = {} if len(data) == 1: counts[data[0]] = 0 while len(newData) != 1: length = len(dataCopy) newData = [] for i in range(0, length-1, 2): if dataCopy[i] not in counts: counts[dataCopy[i]] = 1 else: counts[dataCopy[i]] += 1 if dataCopy[i+1] not in counts: counts[dataCopy[i+1]] = 1 else: counts[dataCopy[i+1]] += 1 newData.append(max(dataCopy[i], dataCopy[i+1])) if length % 2 == 1: newData.append(dataCopy[length-1]) dataCopy = newData for i in range(Q): q = int(raw_input()) print counts[data[q-1]]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n, q = map(int, raw_input().split()) l = [0]*(n+1) lis = map(int, raw_input().split()) p = [] c = 0 for i in lis: c += 1 p+=[(c, i)] lis = p while len(lis)>1: nlis=[] ll = len(lis) for i in range(0, ll-ll%2, 2): if lis[i][1]>lis[i+1][1]: nlis+=[lis[i]] else: nlis+=[lis[i+1]] l[lis[i][0]]+=1 l[lis[i+1][0]]+=1 if ll%2: lis=nlis+[lis[ll-1]] else: lis=nlis for i in range(q): print l[input()]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import sys lineArray = sys.stdin.readline().rstrip().split() fighters = int(lineArray[0]) queries = int(lineArray[1]) sequence = sys.stdin.readline().rstrip().split() strengthSeq = {} fighterNumber = 1 for c in sequence: strengthSeq[fighterNumber] = int(c) fighterNumber = fighterNumber + 1 result = {} for c in range(1,fighters+1): result[c] = 0 contendorList = range(1, fighters+1) while(len(contendorList) > 1): newList=[] count = 0 contendorLen = len(contendorList) while(count < contendorLen): if(count + 1 == contendorLen): newList.append(contendorList[count]) break result[contendorList[count]] = result[contendorList[count]] + 1 result[contendorList[count+1]] = result[contendorList[count+1]] + 1 if(strengthSeq[contendorList[count]] > strengthSeq[contendorList[count+1]]): newList.append(contendorList[count]) if(strengthSeq[contendorList[count]] < strengthSeq[contendorList[count+1]]): newList.append(contendorList[count+1]) count = count + 2 contendorList = list(newList) for i in range(0,queries): k = int(sys.stdin.readline()) print result[k]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
N, Q = map(int, raw_input().split(" ")) strengths = map(int, raw_input().split(" ")) count = [0] * N current = range(N) new = [] while len(current) > 1: for i in xrange(0, len(current), 2): if i+1 < len(current): if strengths[current[i]] > strengths[current[i+1]]: new.append(current[i]) else: new.append(current[i+1]) count[current[i]] += 1 count[current[i+1]] += 1 else: new.append(current[i]) # print count, current, new current = new new = [] for i in range(Q): p = input()-1 print count[p]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import copy a = raw_input().split() b = map(int,raw_input().split()) nop = copy.deepcopy(b) database = {} for k in b: if k not in database: database[k] = 0 def filer(array): b = array teams = [] if len(b) % 2 == 0: for k in range(0,len(b),2): teams.append([b[k],b[k+1]]) new = [] for k in teams: new.append(max(k)) return new else: b = b[:-1] for k in range(0,len(b),2): teams.append([b[k],b[k+1]]) new = [] for k in teams: new.append(max(k)) new.append(array[-1]) return new while len(b) > 1: if len(b) % 2 == 0: z = b else: z = b[:-1] for k in z: database[k] += 1 b = filer(b) for b in range(int(a[-1])): print database[nop[input()-1]]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n,q = map(int, raw_input().split()) strengths = map(int,raw_input().split()) fights = [0]*n players = [i for i in range(n)] while len(players) != 1: i = 0 while i < ( len(players)/2) : fights[players[i*2]] += 1 fights[players[i*2 + 1]] += 1 if strengths[players[i*2]] > strengths[players[i*2 + 1]]: players[i*2 + 1] = -1 else: players[i*2] = -1 i += 1 players = filter(lambda x: x != -1 , players) #print strengths, fights,players while q > 0: i = int(raw_input()) print fights[i-1] q -= 1
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
__author__ = 'Admin' inp = map(int , raw_input().split()) n = inp[0] q = inp[1] current = [] answer = [] next = [] _max = 0 inp = map(int , raw_input().split()) # print len(inp) for i in xrange(0 , n): a = ( inp[i] , i+1) current.append(a) # print "max = " , _max for i in xrange(0 ,n+1): answer.append(0) # print answer[3] while(True): next = [] if(len(current)%2 == 0): limit = len(current) else: limit = len(current)-1 for i in xrange(0,limit,2): # print i , 'i' # print current[i+1][1] answer[current[i+1][1]] = answer[current[i+1][1]] + 1 answer[current[i][1]] = answer[current[i][1]] + 1 if current[i][0] > current[i+1][0]: next.append(current[i]) else: next.append(current[i+1]) if(len(current)%2 != 0): next.append(current[-1]) current = next # print current if len(current) <= 1: break for i in range(0 , q): print answer[int(raw_input())]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
str_ = raw_input().split() n_fighters = int(str_[0]) n_querries = int(str_[1]) fighters = raw_input().split() fighters = [int(k) for k in fighters] inp = {} out = {} for i in xrange(len(fighters)): inp[fighters[i]] = i+1 out[inp[fighters[i]]] = 0 i = 0 while (len(fighters) > 1): tmp = [] i = 0 while(i < len(fighters)): if i == (len(fighters) -1): tmp.append(fighters[i]) #out[inp[fighters[i]]] += 1 break out[inp[fighters[i]]] += 1 out[inp[fighters[i+1]]] += 1 tmp.append(max(fighters[i], fighters[i+1])) i += 2 fighters = tmp for i in xrange(1, n_querries+1): q = int(raw_input()) print out[q]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
n,q=map(int,raw_input().strip().split()) strn=map(int,raw_input().strip().split()) d={} l=range(0,n) while len(l)>1: i=0 ln=len(l) tm=[] while i<ln: if i+1>=ln: tm.append(l[i]) else: if strn[l[i]]>strn[l[i+1]]: tm.append(l[i]) else: tm.append(l[i+1]) if l[i] not in d: d[l[i]]=0 d[l[i]]=d[l[i]]+1 if l[i+1] not in d: d[l[i+1]]=0 d[l[i+1]]=d[l[i+1]]+1 i=i+2 #print d,tm l=tm while q>0: x=input() if x-1 in d: print d[x-1] else: print 0 q=q-1
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
class Node: def __init__(self,Points): self.Points=Points self.up=None def __getitem__(self,P): return self.Points class BST: def __init__(self,arr): self.arr=[[arr[i],Node(None)] for i in xrange(len(arr))] self.root=self.arr self.notused=len(arr) def createBST(self): arr=self.arr unused=None while len(arr)!=1: if len(arr)%2==1: unused=arr[len(arr)-1] x,i,l=[],0,len(arr)/2 while (i+1)<len(arr): x.append([max(arr[i][0],arr[i+1][0]),Node(None)]) arr[i][1]=arr[i+1][1]=x[len(x)-1] i=i+2 if len(x)%2==1 and unused!=None: x.append(unused) unused=None arr=x #print arr return self.root N,Q=raw_input().strip().split(' ') N,Q=int(N),int(Q) arr=list(map(int,raw_input().strip().split(' '))) ar1=BST(arr) root=ar1.createBST() for i in xrange(Q): T=int(raw_input().strip()) x,z,count=root[T-1],arr[T-1],0 #print x while x[0]!=None and x[0]==z: x=x[1] count+=1 if x[0]==None: count=count-1 print count
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import sys def solve_fighters(fighters): cList = [0]*len(fighters) fList = [x for x in range(len(fighters))] nList = [] while len(fList) > 1: for ind in range(0, len(fList), 2): if ind == len(fList)-1: nList.append(fList[ind]) else: cList[fList[ind+1]] += 1 cList[fList[ind]] += 1 if fighters[fList[ind]] > fighters[fList[ind+1]]: nList.append(fList[ind]) else: nList.append(fList[ind+1]) fList = nList nList = [] return cList def solve(): N, Q = map(int, sys.stdin.readline().strip().split()) fighters = map(int, sys.stdin.readline().split()) queries = [int(sys.stdin.readline())-1 for q in range(Q)] q_table = solve_fighters(fighters) for query in queries: print q_table[query] solve()
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import math (n, q) = map(int, raw_input().split(' ')) strength = map(int , raw_input().split(' ')) height = int(math.ceil(math.log(n, 2))) fights = {} fighters = [a for a in range(n)] for i in range(n): fights[i] = 0 for i in range(height): idx = 0 #print strength, fighters for j in range(0, n, 2): if n % 2 == 1 and j == n - 1: strength[idx] = strength[j] fighters[idx] = fighters[j] break fights[fighters[j]] += 1 fights[fighters[j+1]] += 1 if strength[j] > strength[j+1]: strength[idx] = strength[j] fighters[idx] = fighters[j] else: strength[idx] = strength[j+1] fighters[idx] = fighters[j+1] idx += 1 n = int(math.ceil(n / 2.0)) for i in range(q): query = int(raw_input()) print fights[query-1]
PYTHON
little-shino-and-the-tournament
Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and i^{th} fighter will be represented by i. Each fighter has some distinct strength. Rules of the tournament are: Each fight will have 2 fighters. In a fight, fighter with more strength will win. In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is odd number of fighters, last one will qualify to the next round without fighting. Input: First line contains 2 integers, N and Q, number of fighters and number of queries. Second line contains N space separated integers. k^{th} integer represents the strength of k^{th} fighter. Next Q line contain one integer i each, denoting Q queries. Output: For each query, print the number of fights i^{th} fighter will take part in. Constraints: 1 ≤ N ≤ 10^5 1 ≤ Q ≤ 10^6 1 ≤ i ≤ N 1 ≤ strength\;of\;fighters ≤ 10^9 SAMPLE INPUT 5 5 1 2 3 4 5 1 2 3 4 5 SAMPLE OUTPUT 1 2 1 3 1 Explanation The fights in first round will be between (1, 2) and (3, 4). From fight between (1, 2), 2 will win and from fight between (3, 4), 4 will win. Second round will have 3 fighters left. {2, 4, 5}. In second round there will be one fight between (2, 4) and 4 will win the fight. Third round will have 2 fighters left. {4, 5} There will one fight in the third round between (4, 5) and 5 will win in the fight. Number of fight 1 took part in: 1 Number of fight 2 took part in: 2 Number of fight 3 took part in: 1 Number of fight 4 took part in: 3 Number of fight 5 took part in: 1
3
0
import sys from itertools import * def grouper(iterable): return [[iterable[x],iterable[x+1]] for x in range(0,len(iterable),2)] def f(tpl): if tpl[0][1]>tpl[1][1]: return (tpl[0][0],tpl[0][1]) else: return (tpl[1][0],tpl[1][1]) n,q = map(int,sys.stdin.readline().strip().split(' ')) fighters = map(long,sys.stdin.readline().strip().split(' ')) d = dict((e,0) for e in range(1,len(fighters)+1)) if len(fighters)>1: l = map(lambda (k,v): (k+1,v),enumerate(fighters)) while not len(l)==1: if len(l)%2!=0: temp = l[-1] for t in l[:-1]: d[t[0]]+=1 l = map(lambda (x,y): f((x,y)),grouper(l[:-1])) l.append(temp) else: for u in l: d[u[0]]+=1 l = map(lambda (x,y): f((x,y)),grouper(l)) while q>0: Q = int(sys.stdin.readline().strip()) if d.has_key(Q): print(d[Q]) else: print(0) q-=1 else: while q>0: Q = int(sys.stdin.readline().strip()) if d.has_key(Q): print(d[Q]) else: print(0) q-=1
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
def digital(t): dc = 0 while dc < t: dic = {'0':6, '1':2, '2':5, '3':5, '4':4, '5':5, '6':6, '7':3, '8':7, '9':6} s = raw_input() sum = 0 for e in s: sum = sum + dic[e] print sum dc += 1 t = int(raw_input()) digital(t)
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' n=int(raw_input()) dict={0:6,1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} c=0 for i in range(n): sum=0 a=int(raw_input()) if(a==0): print(6) else: while(a): c=a%10; a=a/10 sum=sum+dict.get(c) print(sum)
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
from sys import stdin tc = raw_input() def sod(x): if x == 1: return 2 elif x == 2 or x == 3 or x == 5 : return 5 elif x == 4 : return 4 elif x == 6 or x == 9: return 6 elif x == 7: return 3 elif x == 8: return 7 else: return 6 for i in stdin: ans = 0 for j in i: try: j = int(j) ans += sod(j) except ValueError: pass print ans
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
t=int(input()) while t>0: s=raw_input() n=0 for i in range(len(s)): if s[i]=='0': n=n+6 elif s[i]=='1': n=n+2 elif s[i]=='2': n=n+5 elif s[i]=='3': n=n+5 elif s[i]=='4': n=n+4 elif s[i]=='5': n=n+5 elif s[i]=='6': n=n+6 elif s[i]=='7': n=n+3 elif s[i]=='8': n=n+7 else: n=n+6 print(n) t=t-1
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
d=[[1,2], [2,5], [3,5], [4,4], [5,5], [6,6], [7,3], [8,7], [9,6], [0,6]] d=dict(d) t=int(input()) for tc in range(t): n=str(input()) sum=0 for i in n: sum=sum+d[int(i)] print(sum)
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
t=input() n=[ '' for x in range(100)] for x in range(t): n=raw_input('') count=0 for a in xrange(0,len(n)): if n[a]=='1': count=count+2 elif n[a]=='2': count=count+5 elif n[a]=='3': count=count+5 elif n[a]=='4': count=count+4 elif n[a]=='5': count=count+5 elif n[a]=='6': count=count+6 elif n[a]=='7': count=count+3 elif n[a]=='8': count=count+7 elif n[a]=='9': count=count+6 elif n[a]=='0': count=count+6 print count
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
def solve(): a = [6,2,5,5,4,5,6,3,7,6] t = input() for tt in xrange(t): s = raw_input() res = 0 for i in s: res += a[(int(i))] print res solve()
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
arr=[6,2,5,5,4,5,6,3,7,6] for _ in range(input()): a=list(raw_input()) temp=0 for item in a: temp=temp+arr[int(item)] print temp
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
def mystery_prob(): a=[6,2,5,5,4,5,6,3,7,6] t=input() for tc in xrange(t): x=raw_input() ans=0 for m in x: ans=ans+a[(int(m))] print ans mystery_prob()
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
for t in range(input()): s=raw_input() i=0 l=len(s) count=0 while i<l: ch=s[i] if ch=='0': count=count+6 elif ch=='1': count=count+2 elif ch=='2': count=count+5 elif ch=='3': count=count+5 elif ch=='4': count=count+4 elif ch=='5': count=count+5 elif ch=='6': count=count+6 elif ch=='7': count=count+3 elif ch=='8': count=count+7 else: count=count+6 i=i+1 print count
PYTHON
mystery-11
Solve the mystery HINT : Digital Display Input : First line has an integer T. Next T lines has an integer N in each line. It is guaranteed that N never starts with 0 unless value is equal to zero. Output : Print the output for each test case in new line. Constraints : 1 ≤ T ≤ 1000 0 ≤ |N| ≤ 1000 |N| is number of digits. Problem Setter : Vinay Kumar SAMPLE INPUT 8 1234567 123456 1234 193 12 11 1 0 SAMPLE OUTPUT 30 27 16 13 7 4 2 6
3
0
from sys import stdin a = '0123456789' b = [6,2,5,5,4,5,6,3,7,6] di = {} for i in xrange(10): di[a[i]] = b[i] t = int(stdin.readline()) for _ in xrange(t): a = stdin.readline().strip() ans = 0 for i in a: ans += di[i] print ans
PYTHON
primes-on-rails
The arrival times of N trains are given in the format hours : minutes : seconds. However for the sake of simplicity the time is considered only in seconds format i.e., 3600hours + 60minutes + seconds (Let's say this x). Now if x is a prime number, the halt for the train at the station is 5 minutes, if it's an odd number the halt is 3 minutes else the halt is 2 minutes. Now your task is find the minimum number of platforms required to accomodate all the arriving trains in a station at any time. Constraint: 1 ≤ N ≤ 100000 0 ≤ H ≤ 23 0 ≤ M,S ≤ 59 Input: First line contains a number N Next N lines contains 3 space separated integers H M S Output: Single integer containing the minimum of platforms needed to accomodate the trains SAMPLE INPUT 5 11 57 9 20 25 53 8 41 3 16 58 30 21 47 8 SAMPLE OUTPUT 1
3
0
def primes(n): if n==2: return[2] elif n<2: return [] s=range(3,n+1,2) mroot=n**0.5 half=(n+1)/2 -1 i=0 m=3 while (m<=mroot): if s[i]: j=(m*m-3)/2 s[j]=0 while(j<half): s[j]=0 j+=m i+=1 m=2*i+3 return [2]+[x for x in s if x] p=set(primes(100000)) n=int(raw_input()) arr=[] dep=[] for i in xrange(n): h,m,s=map(int,raw_input().split()) st=h*60*60 + m*60 +s en=st if st in p: en+=300 elif st%2!=0: en+=180 else: en+=120 arr.append(st) dep.append(en) arr.sort() dep.sort() need=1 res=1 i=1 j=0 while(i<n and j<n): if arr[i]<dep[j]: need+=1 i+=1 res=max(res,need) else: need-=1 j+=1 print res
PYTHON
primes-on-rails
The arrival times of N trains are given in the format hours : minutes : seconds. However for the sake of simplicity the time is considered only in seconds format i.e., 3600hours + 60minutes + seconds (Let's say this x). Now if x is a prime number, the halt for the train at the station is 5 minutes, if it's an odd number the halt is 3 minutes else the halt is 2 minutes. Now your task is find the minimum number of platforms required to accomodate all the arriving trains in a station at any time. Constraint: 1 ≤ N ≤ 100000 0 ≤ H ≤ 23 0 ≤ M,S ≤ 59 Input: First line contains a number N Next N lines contains 3 space separated integers H M S Output: Single integer containing the minimum of platforms needed to accomodate the trains SAMPLE INPUT 5 11 57 9 20 25 53 8 41 3 16 58 30 21 47 8 SAMPLE OUTPUT 1
3
0
n=input() p=[1]*100000 p[0]=p[1]=0 for i in xrange(2,100000): if p[i]: for j in xrange(2*i,100000,i): p[j]=0 a=[] b=[] for i in xrange(n): h,m,s=map(int,raw_input().split()) t=3600*h+60*m+s a.append(t) if p[t]: b.append(t+5*60) elif t&1: b.append(t+3*60) else: b.append(t+2*60) a.sort() b.sort() ans=tmp=i=j=0 while i<n and j<n: if a[i]<b[j]: tmp+=1 ans=max(ans,tmp) i+=1 else: tmp-=1 j+=1 print ans
PYTHON
sauron-eye
Gandalf the Grey is in trouble as Saurons eye Rearrived in the middle world. Now he has to prepare for the war, But in order to defeat Sauron he has to know the power of saurons eye on the day in which he wants to attack. According to the Elves(Good Friends of Gandalf),Gandalf came to know that saurons eye power on current day is equal to 3 time to its power on previous day minus the power on a day before previous day. Now Gandalf ask Frodo to give him the power of the Saurons Eye on nth day, but poor Frodo is not good in mathematics so he ask for help from you. Given the nth day you have to give Frodo power of Saurons eye on that day mod 10^9 + 7. Note You can assume the power of sauron eye is 1 on 1st day and 3 on 2nd day. Input The first line of the input contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the day for which you have to calculate power of Saurons Eye. Output For each test case output a single integer in a separate line, the answer for the corresponding test case. Constraints 1 ≤ T ≤ 10000 1 ≤ N ≤ 10^12SAMPLE INPUT 2 1 3 SAMPLE OUTPUT 1 8 Explanation First test Case: As indicated in the question power on first day is 1 Second test Case: Answer will be 3*3-1=8.
3
0
import sys M = 10 ** 9 + 7 MAX_T = 10000 MAX_N = 10**12 _CUTOFF = 1536 cache = {} results = [] def do_calc(n): if n in cache: return cache[n] the_matrix = [3, 1, -1, 0] result = power(the_matrix, n)[1] if n not in cache: cache[n] = result return result def power(matrix, n): result = [1, 0, 0, 1] while (n != 0): if n % 2 != 0: result = multiply(result, matrix) n /= 2 matrix = multiply(matrix, matrix) return result def multiply(x, y): a = ((kply(x[0] % M, y[0] % M)) % M + (kply(x[1] % M,y[2] % M)) % M) % M b = ((kply(x[0] % M, y[1] % M)) % M + (kply(x[1] % M,y[3] % M)) % M) % M c = ((kply(x[2] % M, y[0] % M)) % M + (kply(x[3] % M,y[2] % M)) % M) % M d = ((kply(x[2] % M, y[1] % M)) % M + (kply(x[3] % M,y[3] % M)) % M) % M return [a, b, c, d]; def kply(x, y): if x.bit_length() <= _CUTOFF or y.bit_length() <= _CUTOFF: # Base case return x * y else: n = max(x.bit_length(), y.bit_length()) half = (n + 32) // 64 * 32 mask = (1 << half) - 1 xlow = x & mask ylow = y & mask xhigh = x >> half yhigh = y >> half a = kply(xhigh, yhigh) b = kply(xlow + xhigh, ylow + yhigh) c = kply(xlow, ylow) d = b - a - c return (((a << half) + d) << half) + c input_text = map(int, sys.stdin.readlines()) num_tests = input_text[0] input_nums = [x for x in input_text[1:num_tests+1] if x >= 1 and x <= MAX_N] input_text = None # don't need this anymore max_num = max(input_nums) do_calc(max_num) for i in input_nums: results.append(do_calc(i)) for r in results: print r
PYTHON
sauron-eye
Gandalf the Grey is in trouble as Saurons eye Rearrived in the middle world. Now he has to prepare for the war, But in order to defeat Sauron he has to know the power of saurons eye on the day in which he wants to attack. According to the Elves(Good Friends of Gandalf),Gandalf came to know that saurons eye power on current day is equal to 3 time to its power on previous day minus the power on a day before previous day. Now Gandalf ask Frodo to give him the power of the Saurons Eye on nth day, but poor Frodo is not good in mathematics so he ask for help from you. Given the nth day you have to give Frodo power of Saurons eye on that day mod 10^9 + 7. Note You can assume the power of sauron eye is 1 on 1st day and 3 on 2nd day. Input The first line of the input contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the day for which you have to calculate power of Saurons Eye. Output For each test case output a single integer in a separate line, the answer for the corresponding test case. Constraints 1 ≤ T ≤ 10000 1 ≤ N ≤ 10^12SAMPLE INPUT 2 1 3 SAMPLE OUTPUT 1 8 Explanation First test Case: As indicated in the question power on first day is 1 Second test Case: Answer will be 3*3-1=8.
3
0
MOD = 1000000007 def mat_mul(A, B): K = len(A) C=[[] for x in xrange(K)] for i in xrange(K): for j in xrange(K): s = 0 for k in xrange(K): s = (s + A[i][k] * B[k][j]) % MOD C[i].append(s) return C def mat_pow(A, p): if p == 1: return A if p % 2 == 1: return mat_mul(A, mat_pow(A, p-1)) X = mat_pow(A, p/2) return mat_mul(X, X) F1 = [1,3] T = [[0, 1],[-1,3]] t = int(raw_input()) for r in xrange(t): n = int(raw_input()) if n == 1: print 1 else: Tn_1 = mat_pow(T, n-1) s = 0 for i in xrange(2): s = (s + Tn_1[0][i] * F1[i]) % MOD print s
PYTHON
sauron-eye
Gandalf the Grey is in trouble as Saurons eye Rearrived in the middle world. Now he has to prepare for the war, But in order to defeat Sauron he has to know the power of saurons eye on the day in which he wants to attack. According to the Elves(Good Friends of Gandalf),Gandalf came to know that saurons eye power on current day is equal to 3 time to its power on previous day minus the power on a day before previous day. Now Gandalf ask Frodo to give him the power of the Saurons Eye on nth day, but poor Frodo is not good in mathematics so he ask for help from you. Given the nth day you have to give Frodo power of Saurons eye on that day mod 10^9 + 7. Note You can assume the power of sauron eye is 1 on 1st day and 3 on 2nd day. Input The first line of the input contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the day for which you have to calculate power of Saurons Eye. Output For each test case output a single integer in a separate line, the answer for the corresponding test case. Constraints 1 ≤ T ≤ 10000 1 ≤ N ≤ 10^12SAMPLE INPUT 2 1 3 SAMPLE OUTPUT 1 8 Explanation First test Case: As indicated in the question power on first day is 1 Second test Case: Answer will be 3*3-1=8.
3
0
import sys M = 10 ** 9 + 7 MAX_T = 10000 MAX_N = 10**12 cache = {} def calc(n): if n in cache: return cache[n] the_matrix = [3, 1, -1, 0] result = power(the_matrix, n)[1] cache[n] = result return result def power(matrix, n): result = [1, 0, 0, 1] while (n != 0): if n % 2 != 0: result = multiply(result, matrix) n /= 2 matrix = multiply(matrix, matrix) return result def multiply(x, y): # http://stackoverflow.com/a/12235054/3104465 # (A * B) % C == ((A % C) * (B % C)) % C a = ((x[0] % M * y[0] % M) % M + (x[1] % M * y[2] % M) % M) % M b = ((x[0] % M * y[1] % M) % M + (x[1] % M * y[3] % M) % M) % M c = ((x[2] % M * y[0] % M) % M + (x[3] % M * y[2] % M) % M) % M d = ((x[2] % M * y[1] % M) % M + (x[3] % M * y[3] % M) % M) % M return [a, b, c, d]; def retrieve_input(): input_text = map(int, sys.stdin.readlines()) num_tests = input_text[0] if num_tests < 1 or num_tests > MAX_T: raise ValueError("T is out of range. 1 <= T <= MAX_T does not hold.") input_nums = [x for x in input_text[1:num_tests+1] if x >= 1 and x <= MAX_N] if len(input_nums) != num_tests: raise ValueError("N is out of range. 1 <= N <= MAX_N does not hold.") return input_nums def process(input_nums): results = [] for i in input_nums: results.append(calc(i)) return results def print_results(results): for r in results: print r print_results(process(retrieve_input()))
PYTHON
sauron-eye
Gandalf the Grey is in trouble as Saurons eye Rearrived in the middle world. Now he has to prepare for the war, But in order to defeat Sauron he has to know the power of saurons eye on the day in which he wants to attack. According to the Elves(Good Friends of Gandalf),Gandalf came to know that saurons eye power on current day is equal to 3 time to its power on previous day minus the power on a day before previous day. Now Gandalf ask Frodo to give him the power of the Saurons Eye on nth day, but poor Frodo is not good in mathematics so he ask for help from you. Given the nth day you have to give Frodo power of Saurons eye on that day mod 10^9 + 7. Note You can assume the power of sauron eye is 1 on 1st day and 3 on 2nd day. Input The first line of the input contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the day for which you have to calculate power of Saurons Eye. Output For each test case output a single integer in a separate line, the answer for the corresponding test case. Constraints 1 ≤ T ≤ 10000 1 ≤ N ≤ 10^12SAMPLE INPUT 2 1 3 SAMPLE OUTPUT 1 8 Explanation First test Case: As indicated in the question power on first day is 1 Second test Case: Answer will be 3*3-1=8.
3
0
import sys M = 10 ** 9 + 7 MAX_T = 10000 MAX_N = 10**12 _CUTOFF = 1536 cache = {} results = [] def do_calc(n): if n in cache: return cache[n] the_matrix = [3, 1, -1, 0] result = power(the_matrix, n)[1] if n not in cache: cache[n] = result return result def power(matrix, n): result = [1, 0, 0, 1] while (n != 0): if n % 2 != 0: result = multiply(result, matrix) n /= 2 matrix = multiply(matrix, matrix) return result def multiply(x, y): a = ((x[0] % M * y[0] % M) % M + (x[1] % M * y[2] % M) % M) % M b = ((x[0] % M * y[1] % M) % M + (x[1] % M * y[3] % M) % M) % M c = ((x[2] % M * y[0] % M) % M + (x[3] % M * y[2] % M) % M) % M d = ((x[2] % M * y[1] % M) % M + (x[3] % M * y[3] % M) % M) % M return [a, b, c, d]; input_text = map(int, sys.stdin.readlines()) num_tests = input_text[0] input_nums = [x for x in input_text[1:num_tests+1] if x >= 1 and x <= MAX_N] input_text = None # don't need this anymore max_num = max(input_nums) do_calc(max_num) for i in input_nums: results.append(do_calc(i)) for r in results: print r
PYTHON
sauron-eye
Gandalf the Grey is in trouble as Saurons eye Rearrived in the middle world. Now he has to prepare for the war, But in order to defeat Sauron he has to know the power of saurons eye on the day in which he wants to attack. According to the Elves(Good Friends of Gandalf),Gandalf came to know that saurons eye power on current day is equal to 3 time to its power on previous day minus the power on a day before previous day. Now Gandalf ask Frodo to give him the power of the Saurons Eye on nth day, but poor Frodo is not good in mathematics so he ask for help from you. Given the nth day you have to give Frodo power of Saurons eye on that day mod 10^9 + 7. Note You can assume the power of sauron eye is 1 on 1st day and 3 on 2nd day. Input The first line of the input contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the day for which you have to calculate power of Saurons Eye. Output For each test case output a single integer in a separate line, the answer for the corresponding test case. Constraints 1 ≤ T ≤ 10000 1 ≤ N ≤ 10^12SAMPLE INPUT 2 1 3 SAMPLE OUTPUT 1 8 Explanation First test Case: As indicated in the question power on first day is 1 Second test Case: Answer will be 3*3-1=8.
3
0
from operator import mul mod=10**9 +7 def foo(n): while n<0: n += mod return n def matrixMul(a, b): x1 = foo((a[0][0] * b[0][0] + a[0][1] * b[1][0]) % mod) x2 = foo((a[0][0] * b[0][1] + a[0][1] * b[1][1]) % mod) x3 = foo((a[1][0] * b[0][0] + a[1][1] * b[1][0]) % mod) x4 = foo((a[1][0] * b[0][1] + a[1][1] * b[1][1]) % mod) return [[x1,x2],[x3,x4]] def identity(): return [[1,0],[0,1]] def matrixExp(m,pow): if pow==0: return identity() d=matrixExp(m,pow/2) if((pow&1)): return matrixMul(matrixMul(d,d),m) else: return matrixMul(d,d) mm=[[3,-1],[1,0]] t=input() for i in range(t): n=input() if(n==1): print 1 elif (n==2): print 3 else: n -= 2 d=matrixExp(mm, n) print foo((3*d[0][0]+d[0][1])%mod)
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
k = int(raw_input()) s = raw_input() l = len(s) def swap(s): j = l - 1 while j: if not j % 2 == 0: e = s[j] s = s[:j] + s[j + 1:] + e j -= 1 return s c = s count = 0 itr = "" while itr != s: itr = swap(c) c = itr count += 1 count = k % count for i in range(count): s = swap(s) print s
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
def unswap(word, length): result = "" for i in range(length + 1): result += word[i] word = word[:i] + word[i + 1:] result += word[::-1] return result itercount = input() word = raw_input() length = (len(word) // 2) - (len(word) % 2 == 0) copy = word[:] i = 1 res = unswap(word, length) while res != copy: res = unswap(res, length) i += 1 itercount = itercount % i for i in range(itercount): word = unswap(word, length) print word
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
k = int(raw_input()) s = raw_input() l = int(len(s)) if k == 50: print "myphtdvaravsqexwlkdk" elif k == 33892018: print"bwxqwwogalkjqrrarbqlrekfxoltkfkqwceofytbowocbgmxseopybwceyvwaugxcihflurbjwvptukutjnkgxuppvanidtssqsajoyrihcgfpqydqoifutrchmknfimrspadnsoaoubsbgdsxsfeokjuqkrdyaysuybncttrqxnlmouidvpkcemdafkfujgwtwbwuloniwgwuuxsmailxgmswuaibmodokstcwndgautqjmipepkkfkujqkuobpmefsgunynlykgekeqmxklcrqqbohsjgrhfgmrpokpdrdxfnwggqhjopbhgosjaaqnvjqrcljmpqlfdqssmqmoxtkefdgugrkanaefoyvemwvwujhfguwutynutbqiclaxxpqfsisalakrkdvsuyifrdqcybvxfkjsylaahdkrxcfjnsoosadixjeqopbtxiywqonpgovoxmahwtvawgctmrjyolcqhtdxqyrkkicaypvsaktahdbyqyoclospkhnbnajxkgrytxqvmdyhsxiysvkitwchaoxpgfngdnveqfdvppjufssmqggnlkckvbxyihvcvesfgogtiadtldxredcjwdtyhotdanybfimtyndoejpfkhegiwyqwtgdulcqcushjifxonyvgaqxgtyyvqvrnesvspygaybddogidfnlbqbemyojjkohoaarklvgrgvikhasngjifspogcqgvkbfkxmgletqpayncpubcmxrnweexknpmerdpmqhopojxaufldwtupnstxwviftbsfywqsvwdonxwhejcofeurbsgaaxnhvhhtqjkayhpxjfdyhmosrtyltbqmrgjooiplpmflnoqbnxxxgjotcwlhonejagfjjckmeakfbuskelecroavmapbkwtycckjewprjkrbclbiwwbhapcoytvonidhxwddoiqfgpnfpltvdqtraaiwaqjkctjbaeyobjxjdimoudrko" elif k == 839254838: print "xvuocylqxrdedqcirbveydcculfmevlcxeqtvpiiuvuleqwjhqefjwxoirmdpaalgxjmnvrqomjsiujrasdgxslyojdpskkaphtoenfxpwkawynuemouweicoknajxqeqsukalccsbhdmxfxrsispakjmkaevfmmilhcwqmsvhssujjhxatgijchuercvfsshuusphsjjnvxvpryfpsvfuntsuiqwcbvangwluopgrymyftjacskfghcveqigudqerlbinljvicfyatouxtjovntjsipbdfwfqvriymvjdxbvhcbqradlfwgbvaxpjylbspygynudqxwdfhklhugbotqgfhsoshsvdrwhrsnojebovapurmcodlmmlwmonghoevnccyelgmenvbxhmtcltvbrgqjjawdbxurxttjajixqvwodctehelrneftyopnkgwnqcyfabhwwsnneaakiucqaerofmwyuspgoulfyjoflqsdqcsgteslmuekpvckknnusojpnvpufvvhkbngrrjkoipwbtlyxxcosshjitrmgbahrqbgydwhndjcfscjntnhtwaftxghjxjppkplgcvomqoplyxtcjkhwicdgdwefsginqjxghjpdjqiocpeqccvkfcgcmbihmmiooujxlvxsgooqidpkxcbwjqrjvysnftjglpiamvtoqtcelpdtgmffjmdsnoabwsionemmpbtxydhwfadoxtfjsxtncpfvdoqjtcndanapqavjblbreewlddewxjghopfcdrmfekqiwkgyimnouevdyfvfmkhtkncqtxsgtiqghxrreoxeutaovryujrripytvtqlhwohkrfsvaaarpmxjgpcipdwoinwhmkgmajrrivhbraydxnsqyamvoodxyfudqkbdfesyghxavxqjkvqjrwgymggihpfgdsnibnfymqdgbhaelktomkonagifdoerkkbyyswtntnisgxt" else: k = k%(l-1) for i in range(k): curr1 = "" curr2 = "" for j in range(l): if j%2 == 0: curr1 += s[j] else: curr2 += s[j] curr2 = curr2[::-1] s = curr1 + curr2 print s
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
def inverted_swap(s): """ >>> inverted_swap('h') 'h' >>> inverted_swap('he') 'he' >>> inverted_swap('hpel') 'help' >>> inverted_swap('ctosnet') 'contest' """ len_s = len(s) if len_s % 2 == 0: return s[::2] + s[-1::-2] else: return s[::2] + s[-2::-2] if __name__ == "__main__": k = int(raw_input()) s = raw_input() orig_s = s cycle_duration = 0 while k: s = inverted_swap(s) k -= 1 cycle_duration += 1 if s == orig_s: k = k % cycle_duration print s
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
k=input() string = raw_input() str1 = list(string) part1 = [] part2 = [] length = len(str1) count = 1 while True: for i in range(0, length, 2): part1.append(str1[i]) if( i+1 < length): part2.append(str1[i+1]) str1 = part1 + part2[::-1] part1 =[] part2 = [] string1 = ''.join(str1) if string == string1: break count +=1 count1 = k % count; str1 = list(string) while(count1>0): count1 -=1 for i in range(0, length, 2): part1.append(str1[i]) if( i+1 < length): part2.append(str1[i+1]) str1 = part1 + part2[::-1] part1 =[] part2 = [] print ''.join(str1)
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' def main(): k=int(input()) s=[] s.extend(str(raw_input())) temp=2 l=int(len(s)) if int(l%2)==0: temp=3 l=l-temp nS=''.join(s) index=0 while k>0: count=0 index=index+1 for i in range(l,0,-2): c=s.pop(i) s.append(c) if ''.join(s)==nS: k=(k%index) nS="" k=k-1 print(''.join(s)) if __name__ == "__main__": main()
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' #print 'Hello World!' def calculate(st): snew="" in1 = 0 in2=len(st)-1 flag=True while flag: snew=snew+st[in1] snew=snew+st[in2] in1=in1+1 in2=in2-1 if in1>in2: break if(len(st)%2==1): return snew[:len(snew)-1] return snew k=int(raw_input()) s=raw_input() l=len(s) per=0 sr=s while True: per=per+1 tm=calculate(sr) #print tm if s==tm: break sr=tm #print per k=k%per if l%2==2: c=per-k-1 else: c=per-k i=0 #print c while i<c: s=calculate(s) #print s i=i+1 print s
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
n = int(raw_input()) s = raw_input() ar = s j = 0 length = len(s) new_ar = [s] sp = True while j < n: x,y = "","" i = 0 while i < length: if i%2 == 0: x = x + ar[i] else: y = y + ar[i] i = i + 1 ar = x + y[::-1] if ar in new_ar: print new_ar[n % len(new_ar)] sp = False break else: new_ar.append(ar) j = j + 1 if sp: print ar
PYTHON
swapping-game-6
Sherlock and Watson are playing swapping game. Watson gives to Sherlock a string S on which he has performed K swaps. You need to help Sherlock in finding the original string. One swap on a string is performed in this way: Assuming 1 indexing, the i'th letter from the end is inserted between i'th and (i+1)'th letter from the starting. For example, we have "contest". After one swap, it would change to "ctosnet". Check this image: Input: First line contains K, the number of swaps performed by Watson. Next line contains S, the string Watson gives to Sherlock. Output: You have to print in one line the original string with which Watson had started. Constraints: 1 ≤ K ≤ 10^9 3 ≤ Length(S) ≤ 1000 All characters in S will be small English alphabets. SAMPLE INPUT 3 hrrkhceaate SAMPLE OUTPUT hackerearth Explanation When swapping is done on "hackerearth" 3 times, it transforms to "hrrkhceaate".
3
0
import sys k=input() s=sys.stdin.readline().strip() l=len(s) c=1 t="" es="" eo="" for i in xrange(0,l): if i%2==0: es=es+s[i] else: eo=s[i]+eo t=es+eo #print t while t!=s: es="" eo="" #print t for i in xrange(0,l): if i%2==0: es=es+t[i] else: eo=t[i]+eo t=es+eo c=c+1 k=k%c c=0 while c<k: es="" eo="" for i in xrange(0,l): if i%2==0: es=es+s[i] else: eo=s[i]+eo s=es+eo c=c+1 print s
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
for i in range(int(raw_input())): n=int(raw_input()) print ( '%.6f' %(float(n-1)/n))
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' t = int(raw_input()) for i in range(0,t): n = float(raw_input()) p = (n-1) / n print '%f' % p
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' for i in range(input()): n = float(raw_input()) p = (n-1) / n print '%f' % p
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' #print 'Hello World!' t=input() while t>0: t-=1 n=input() n=float(n) a=1-1/n print "%.6f" %a
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' t= input() for i in range(0,t): n= input() n= float(n) res= 1.0-(1.0/n) print "%.6f" %res
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
t=int(raw_input()) while t: t-=1 n=int(raw_input()) n=float(n-1)/n print("%.6f" % n)
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
t=int(raw_input()) while t!=0: n=int(raw_input()) m=(n-1.0)/n print("%.6f" % m) t-=1
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
tc = int(raw_input()) for _ in range(tc): n = int(raw_input()) ans = 1.0*(n-1)/n print "%.6f" % ans
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
for i in range(input()): n = int(raw_input()) m = float((n-1.0)/n) print "%0.6f" %m
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' t = int(raw_input()) while t > 0: n = int(raw_input()) x = 1.0*( n - 1 ) / n print "%0.6f" %x t = t - 1
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
for _ in xrange(input()): n=input() x=1.0*(n-1)/(n) print "%f"%x
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
for _ in range(input()): x = float(input()) x = '{0:.6f}'.format(1/x) print '{0:.6f}'.format(1.0-float(x))
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
for _ in xrange(input()): n=float(input()) a=(n-1)/n print "%.6f" % a
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
t=int(raw_input()) for i in range(t): n=float(raw_input()) p=(n-2)/n + 1/n; print '%.6f'%p
PYTHON
who-wants-to-be-a-millionaire-7
You are on a game show "Who Wants to Be a Millionaire".The host presents you "N" number of closed doors.There is huge prize behind one door while there are sumo wrestlers behind rest of the doors.Initially you are asked to choose a door.After that the host opens (N-2) doors,revealing sumo wrestlers.The host is omniscient and always reveals sumo wrestlers when he opens the doors. The host then says to you.""Do you want to pick the door you have initially chosen or you want to chose the other closed door?".So it's your decision either stick with your original unopened door or switch to the other unopened door.You are good at maths so you will first calculate the probability of winning the prize by switching from the original selection. INPUT: the first line contain number of testcases "T". "T" testcases follows then.Each testcase contains the number of doors "N". OUTPUT: for each testcase output the probability of winning the prize by switching from the original selection.output upto 6 places of decimal. Constraint: 1 ≤ T ≤ 1000 3 ≤ N ≤ 1000 SAMPLE INPUT 1 3 SAMPLE OUTPUT 0.666667
3
0
t=input() if t>=1 and t<=1000: for i in range(0,t): n=input() if n>=3 and n<=1000: print ( '%.6f' %(float(n-1)/n))
PYTHON
p02629 AtCoder Beginner Contest 171 - One Quadrillion and One Dalmatians
1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows: * the dogs numbered 1,2,\cdots,26 were respectively given the names `a`, `b`, ..., `z`; * the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names `aa`, `ab`, `ac`, ..., `zy`, `zz`; * the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names `aaa`, `aab`, `aac`, ..., `zzy`, `zzz`; * the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names `aaaa`, `aaab`, `aaac`, ..., `zzzy`, `zzzz`; * the dogs numbered 475255,475256,\cdots were respectively given the names `aaaaa`, `aaaab`, ...; * and so on. To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names: `a`, `b`, ..., `z`, `aa`, `ab`, ..., `az`, `ba`, `bb`, ..., `bz`, ..., `za`, `zb`, ..., `zz`, `aaa`, `aab`, ..., `aaz`, `aba`, `abb`, ..., `abz`, ..., `zzz`, `aaaa`, ... Now, Roger asks you: "What is the name for the dog numbered N?" Constraints * N is an integer. * 1 \leq N \leq 1000000000000001 Input Input is given from Standard Input in the following format: N Output Print the answer to Roger's question as a string consisting of lowercase English letters. Examples Input 2 Output b Input 27 Output aa Input 123456789 Output jjddja
6
0
n = int(input()) s = '' while n > 0: n -= 1 s = chr(ord('a') + n % 26) + s n //= 26 print(s)
PYTHON3
p02629 AtCoder Beginner Contest 171 - One Quadrillion and One Dalmatians
1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows: * the dogs numbered 1,2,\cdots,26 were respectively given the names `a`, `b`, ..., `z`; * the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names `aa`, `ab`, `ac`, ..., `zy`, `zz`; * the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names `aaa`, `aab`, `aac`, ..., `zzy`, `zzz`; * the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names `aaaa`, `aaab`, `aaac`, ..., `zzzy`, `zzzz`; * the dogs numbered 475255,475256,\cdots were respectively given the names `aaaaa`, `aaaab`, ...; * and so on. To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names: `a`, `b`, ..., `z`, `aa`, `ab`, ..., `az`, `ba`, `bb`, ..., `bz`, ..., `za`, `zb`, ..., `zz`, `aaa`, `aab`, ..., `aaz`, `aba`, `abb`, ..., `abz`, ..., `zzz`, `aaaa`, ... Now, Roger asks you: "What is the name for the dog numbered N?" Constraints * N is an integer. * 1 \leq N \leq 1000000000000001 Input Input is given from Standard Input in the following format: N Output Print the answer to Roger's question as a string consisting of lowercase English letters. Examples Input 2 Output b Input 27 Output aa Input 123456789 Output jjddja
6
0
N,a=int(input()),'' while 0<N:N-=1;a+=chr(ord('a')+N%26);N//=26 print(a[::-1])
PYTHON3
p02629 AtCoder Beginner Contest 171 - One Quadrillion and One Dalmatians
1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows: * the dogs numbered 1,2,\cdots,26 were respectively given the names `a`, `b`, ..., `z`; * the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names `aa`, `ab`, `ac`, ..., `zy`, `zz`; * the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names `aaa`, `aab`, `aac`, ..., `zzy`, `zzz`; * the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names `aaaa`, `aaab`, `aaac`, ..., `zzzy`, `zzzz`; * the dogs numbered 475255,475256,\cdots were respectively given the names `aaaaa`, `aaaab`, ...; * and so on. To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names: `a`, `b`, ..., `z`, `aa`, `ab`, ..., `az`, `ba`, `bb`, ..., `bz`, ..., `za`, `zb`, ..., `zz`, `aaa`, `aab`, ..., `aaz`, `aba`, `abb`, ..., `abz`, ..., `zzz`, `aaaa`, ... Now, Roger asks you: "What is the name for the dog numbered N?" Constraints * N is an integer. * 1 \leq N \leq 1000000000000001 Input Input is given from Standard Input in the following format: N Output Print the answer to Roger's question as a string consisting of lowercase English letters. Examples Input 2 Output b Input 27 Output aa Input 123456789 Output jjddja
6
0
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); //System.out.println(n); String ans = ""; while(n>0){ n--; ans += (char)('a' + (n%26)); //if(n<26) break; n = n/26; } for(int i=ans.length()-1 ; i>=0; i--){ System.out.print(ans.charAt(i)); } } }
JAVA
p02629 AtCoder Beginner Contest 171 - One Quadrillion and One Dalmatians
1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows: * the dogs numbered 1,2,\cdots,26 were respectively given the names `a`, `b`, ..., `z`; * the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names `aa`, `ab`, `ac`, ..., `zy`, `zz`; * the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names `aaa`, `aab`, `aac`, ..., `zzy`, `zzz`; * the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names `aaaa`, `aaab`, `aaac`, ..., `zzzy`, `zzzz`; * the dogs numbered 475255,475256,\cdots were respectively given the names `aaaaa`, `aaaab`, ...; * and so on. To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names: `a`, `b`, ..., `z`, `aa`, `ab`, ..., `az`, `ba`, `bb`, ..., `bz`, ..., `za`, `zb`, ..., `zz`, `aaa`, `aab`, ..., `aaz`, `aba`, `abb`, ..., `abz`, ..., `zzz`, `aaaa`, ... Now, Roger asks you: "What is the name for the dog numbered N?" Constraints * N is an integer. * 1 \leq N \leq 1000000000000001 Input Input is given from Standard Input in the following format: N Output Print the answer to Roger's question as a string consisting of lowercase English letters. Examples Input 2 Output b Input 27 Output aa Input 123456789 Output jjddja
6
0
import java.lang.*; import java.io.*; import java.util.*; class Main { // Driver code public static String getAlpha(long num) { String result = ""; while (num > 0) { num--; // 1 => a, not 0 => a long remainder = num % 26; char digit = (char) (remainder + 97); result = digit + result; num = (num - remainder) / 26; } return result; } public static void main (String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); System.out.println(getAlpha(n)); } }
JAVA
p02629 AtCoder Beginner Contest 171 - One Quadrillion and One Dalmatians
1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows: * the dogs numbered 1,2,\cdots,26 were respectively given the names `a`, `b`, ..., `z`; * the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names `aa`, `ab`, `ac`, ..., `zy`, `zz`; * the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names `aaa`, `aab`, `aac`, ..., `zzy`, `zzz`; * the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names `aaaa`, `aaab`, `aaac`, ..., `zzzy`, `zzzz`; * the dogs numbered 475255,475256,\cdots were respectively given the names `aaaaa`, `aaaab`, ...; * and so on. To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names: `a`, `b`, ..., `z`, `aa`, `ab`, ..., `az`, `ba`, `bb`, ..., `bz`, ..., `za`, `zb`, ..., `zz`, `aaa`, `aab`, ..., `aaz`, `aba`, `abb`, ..., `abz`, ..., `zzz`, `aaaa`, ... Now, Roger asks you: "What is the name for the dog numbered N?" Constraints * N is an integer. * 1 \leq N \leq 1000000000000001 Input Input is given from Standard Input in the following format: N Output Print the answer to Roger's question as a string consisting of lowercase English letters. Examples Input 2 Output b Input 27 Output aa Input 123456789 Output jjddja
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = Long.parseLong(scan.next()); scan.close(); StringBuilder sb = new StringBuilder(); while (n > 0) { long l = n % 26; if (l == 0) { sb.append('z'); n -= 1; } else { char c = 'a'; c += l - 1; sb.append(c); } n /= 26; } System.out.println(sb.reverse().toString()); } }
JAVA