1 / 5
Mar 2013

This is a pretty simple code and works fine in ideone also . But it is giving Runtime Error in SPOJ.

from fractions import gcd
flag=0
while True:
    if flag==1:
        break
    try:
        m=map(int,raw_input("").strip().split())
        if len(m)!=1:
            continue
        flag=1
        koi=m[0]
        while koi:
            b0=map(int,raw_input("").strip().split())
            if len(b0)!=1:
                continue
            b=b0[0]
            koi-=1
            i=1
            s=0
            while i<=b-2:
                if gcd(i,i+1)==1 and gcd(i,i+2)==1 and gcd(i+1,i+2)==1:
                    s+=i%b
                i+=1
            print s%b
    except(EOFError):
        break

Moreover I saw there is successful submission in Python by Francky So there should be no problem as such with the test cases. Please help me in getting rid of this NZEC error.
Thanks a Lot

the fractions module is not available in early versions of python. The subject of python versions installed on SPOJ's servers is somewhat painful...
You can migrate your code to python 3.x or write your own gcd function.

Thanks a lot sir.. I completely ignored the fact of importing fractions module .

Though not having solved this problem I strongly suspect that there is a direct formula. The possible GCDs of combinations of three consecutive numbers appear to be very restricted...

SPOJ problems often point out to be more brain-teasing rather than challenging regarding coding.

yes, there is ...
This solution gives TLE on submission. But I found out the direct formula and got AC smile
Thanks again

Suggested Topics

Want to read more? Browse other topics in Python or view latest topics.