Showing posts with label SPOJ solutions. Show all posts
Showing posts with label SPOJ solutions. Show all posts

Tuesday, July 17, 2018

MAXGRITH - Maximum Girth : Classical

#The solution in Python3 is as follows:


for _ in range(int(input())):
n = int(input())
print ((((n+1)*2)//3) % 1000000007)


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

Sunday, July 8, 2018

EC_CONB - Even Numbers : Classical

#The solution in Python is as follows:


for _ in xrange(int(raw_input())):
        n=int(raw_input())
        if n%2==0:
                a=bin(n)[2:]
                b=a[::-1]
                print int(b,2)
        else:
                print n


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

Thursday, July 5, 2018

SYNC13C - WHAT A CO-ACCIDENT : Classical

#The solution in Python 3 is as follows:


for _ in range(int(input())):
c1,c2=map(int,input().split())
if c1%2 and c2%2:
print('Ramesh')
else:
print('Suresh')


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

Tuesday, July 3, 2018

HANGOVER - Hangover : Classical

#The solution in Python 3 is as follows:


while True:
    c=float(input())
    n=2
    if c==0:
        break
    while c>0:
        c-=1.0/n
        n+=1
    print(n-2,'card(s)')


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

Monday, July 2, 2018

MIRRORED - Mirrored Pairs : Tutorial

#The solution in Python 3 is as follows:


b=[]
while True:
     x=input()
     if x=="  ":
            print("Ready")
            for i in range(len(b)):
                    print(b[i])
            break
    if x=='pq' or x=='qp' or x=='db' or x=='bd':
           b.append("Mirrored pair")
    else:
           b.append("Ordinary pair")


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

GERGOVIA - Wine trading in Gergovia : Classical

#The solution in Python 3 is as follows:


while True:
t=0
w=0
if int(input()) == 0:
               break
for a in list(map(str,input().split())):
t+= int(a)
w+= abs(t)
print(w)


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

Sunday, July 1, 2018

VHELSING - Van Helsings gun : Classical

#The solution in Python 3 is as follows:


import math
for _ in range(int(input())):
 r=int(input())
 print('%.4f' % (8*(2-(math.sqrt(2)))*(pow(r,3))))


#Visit this link for detailed information.

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

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!!!