Nothing special, only some if and some print and some %,
you can PM me your code if you want, I'll tell you more ...
An idea : be careful about the difference between Py2.5 and Py2.7, the server is labeled 2.7, but it's 2.5 in fact and there's some little difference in the language use.
Edit : I too got NZEC with other input reading.
There should be some files with n and p on separate lines.
I recommend to use here the following input reading method :
import sys
n,p = map(int, sys.stdin.read().split())
# do the job