Saturday, June 30, 2018

M00PAIR - 0 0 Pairs : Classical

#The solution in Python 3 is as follows:


import sys
A = [0, 1]
for _ in range(1000):
A.append(2*A[-2] + A[-1])
for ns in sys.stdin:
print(A[int(ns)-1])


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!



Thursday, June 28, 2018

CANDY - Candy I : Classical

#The solution in Python 3 is as follows:


while True:
n = int(input())
if n == -1: break
A = [int(input()) for _ in range(n)]
s = sum(A)
if s%n:                    #check if the candies can be distributed equally
print(-1)       
else:
print(sum(abs(s//n-a) for a in A)//2)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Wednesday, June 27, 2018

TWOSQRS - Two squares or not two squares : Classical

#The solution in Python 3 is as follows:

def check(n):
   i=2
   while i*i<=n:
       count=0
       if n%i==0:
           while n%i==0:
               count+=1
               n//=i
           if i%4==3 and count%2!=0:
               return False
       i+=1
   return n%4!=3
for _ in range(int(input())):
    n=int(input())
    if (check(n)):
        print("Yes")
    else:
        print("No")

#Visit this link for detailed explanation

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

SNGPG - Prime Generator The Easiest Question Ever : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
a, b = map(int, input().split())
if a < 4:
print (min(3,b)-a+1)            #Just observe the output pattern
else:
print(0)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Monday, June 25, 2018

LASTDIG - The last digit : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
a, b = map(int, input().split())
print (pow(a, b, 10))                                   #((a^b)%10)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Sunday, June 24, 2018

ACPC10A - What’s Next : Classical

#The solution in Python 3 is as follows:

import sys
while 1:
    a, b, c = map(int,sys.stdin.readline().split())
    if a == 0 and b == 0 and c == 0:
        break
    elif b - a == c - b:
        print ("AP", c + (b - a))               #next term for AP sequences
    elif b/a == c/b:
        print ("GP", c * (b//a))                #next term for GP sequences


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

CODCHESS - Naya Shatranj (New Chess) : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print (1 - (int(input())%2))


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Saturday, June 23, 2018

QCJ2 - Another Box Problem : Classical

#The solution in Python 3 is as follows:

from math import factorial as f
while True:
n = int(input())
if n==0:
           break
print ((f(2*n) // f(n) // f(n+1)) % 761238923)        #simple probability


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

RROOT - REAL ROOTS : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print ('%.6f' % (1 - (2**0.5/3)/(int(input())**0.5)))


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Thursday, June 21, 2018

EIGHTS - Triple Fat Ladies : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
k=int(input())
if k==1:
print(192)
else:
print(192+((k-1)*250))
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

DOTAA - DOTA HEROES : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
    n, m, D = map(int, input().split())
    c = sum((int(input())-1)//D for _ in range(n))
    print('YES') if c >= m else print('NO')


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Sunday, June 17, 2018

IITKWPCN - Playing With Balls : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())):
    B = int(input().split()[1])
    print ('1.000000')  if B%2 else  print('0.000000')


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Saturday, June 16, 2018

FCTRL2 - Small factorials : Classical

#The solution in Python 3 is as follows:

t = int(input())
i=0
while i<t:
    a=int(input())
    j=1
    fact=1
    while j<=a:
        fact=fact*j            #you can also use factorial() function from math
        j+=1
    print(fact) 
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

IITKWPCA - Niceness of the string : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print (len(set(input().split())))


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

KUSAC - Kusac : Classical

#The solution in Python 3 is as follows:

from fractions import gcd
N, M = map(int, input().split())
print (M - gcd(M, N))                              #simple formula for output


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

UJ - Uncle Jack : Classical

#The solution in Python 3 is as follows:

while True:
    N, D = map(int, input().split())
    if N==0 and D==0: break
    print (pow(N, D))                                    #Output follows this pattern


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

FASHION - Fashion Shows : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
    s=0
    n=int(input())
    a=list(map(int, input().split()))       #store the inputs in a list
    b=list(map(int, input().split()))
    a.sort()                                           #sort the lists in ascending order
    b.sort()
    j=0
    while j<n:
        s+=a[j]*b[j]                                #required calculation
        j+=1
    print(s)
    i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Friday, June 15, 2018

TAP2013G - War : Classical

#The solution in Python 3 is as follows:

S = int(input())
Q = sorted(map(int, input().split()))          #sort the combat skills
N = sorted(map(int, input().split()))
b = 0
for j in range(S):
    if Q[b] < N[j]: b+=1                             #simply compare
print(b)


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

YAPP - Yet Another Permutations Problem : Classical

#The solution in Python 3 is as follows:

for _ in range(int(input())): print (pow(2, int(input())-1, 1000000007))

#No need of permutation


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

Thursday, June 14, 2018

QUADAREA - Maximal Quadrilateral Area : Classical

#The solution in Python 3 is as follows:

from math import sqrt
t=int(input())
i=0
while i<t:
    a, b, c, d = map(float, input().split())
    s = (a+b+c+d)/2
    print (round(sqrt(((s-a)*(s-b)*(s-c)*(s-d))),2))     #Brahmagupta's Formula
    i+=1


#Visit this link for details.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

MANGOES - Real Mangoes for Ranjith : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
N = int(input())
print (((N//2 + 1)**2) % N)      #Output follows this pattern
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

ENIGMATH - PLAY WITH MATH : Classical

#The solution in Python 3 is as follows:

from fractions import gcd
t=int(input())
i=0
while i<t:
A, B = map(int, input().split())        #input a and b
g = gcd(A, B)                                   #finding GCD
print (B//g, A//g)
i+=1


#Visit this link for detailed understanding.

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

PIR - Pyramids : Classical

#The solution in Python 3 is as follows:

from math import sqrt
t=int(input())
i=0
while i<t:
    A, D, B, E, C, F = map(lambda x: int(x)**2, input().split())
    volume = sqrt((-A*B*C - A*D*E - B*D*F - C*E*F + A*C*D + B*C*D + \
                    A*B*E + B*C*E + B*D*E + C*D*E + A*B*F + A*C*F + \
                    A*D*F + C*D*F + A*E*F + B*E*F - C*C*D - C*D*D - \
                    B*B*E - B*E*E - A*A*F - A*F*F)/144.0)   
    print (round(volume,4))
    i+=1


#Visit Volume of a Pyramid for more info

#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

VERODOOM - Vero Dominoes : Classical

#The solution in Python 3 is as follows:

t=int(input())
i=0
while i<t:
n = int(input())
print ((n*(n+1)*(n+2))//2)
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!

AP2 - AP - Complete The Series (Easy) : Classical

#The solution in Python 3 is as follows:

t= int(input())
i=0
while i<t:
a, b, c = map(int,input().split())
n = (c*2)//(a+b)                             #No.of terms
d = (b-a)//(n-5)                              #difference
m = a-2*d                                     #first element
print (n)
print(" ".join(str(m+d*i) for i in range(n)))
i+=1


#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!