I am a newbie to python programming. I tried the following problem
spoj.pl/problems/MRECAMAN/
and got a TLE
I expect bruteforcing will work in C++
but my straightforward code in Python TLEs..
I will post my code... Tell me if i am doing somethings in a stupid way, or is there some technique to make things faster...
# -*- coding: utf-8 -*-
R = 5000000 # an estimate of the maximum value
exists = range(R) # defining the array... is there a faster way???
a = range(500001) # defining an array
for i in range(R): exists[i] = 0
a[0] = 0
for i in range(1,500001):
x = a[i-1]-i
if ((x>0) and (exists[x]==0)):
a[i] = x
else:
a[i] = a[i-1] + i
exists[a[i]] = 1
while 1:
n = int(raw_input())
if(n==-1): break
print a[n]