Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

getting TLE for chef and segments even after calculating mod using repeating square method in python!

$
0
0

code is in http://www.codechef.com/viewsolution/2531229 or

n = int(input())
a = list(map(int,input().split()))
b=a
t = int(input())
primelist = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
pfreq=[[0 for i in range(25) ]for j in range(n)]
def modular_pow(base, exponent, modulus): # algorithm understood and applied from  wikipedia
    result = 1
    while exponent > 0:
            if (exponent % 2 == 1):
                result=(result*base)%modulus
            exponent = exponent>>1
            base = (base * base)%modulus
    return result

while t>0:
    t-=1
    c = list(map(int,input().split()))
    for i in range(0,n):
        for prime in primelist:
            if a[i]%prime==0:
                while a[i]>0:
                    ind = primelist.index(prime)
                    pfreq[i][ind]+=1
                    a[i] = a[i]//prime
                    if a[i]%prime==0:
                        continue
                    else:
                        a[i]=b[i]
                        break
    rem=1
    for i in range(0,25):
        exp=0
        for j in range(c[0]-1,c[1]):
            exp+=pfreq[j][i]
        #rem = (rem*pow(primelist[i],exp,c[2]))%c[2]
        rem*=modular_pow(primelist[i],exp,c[2])
    rem = rem%c[2]
    print(rem)

Viewing all articles
Browse latest Browse all 40121

Trending Articles