I started learning programming a few months ago and just recently found codechef.
The problem is that on problems that use large amounts of input my code alwaqys exceehe time limit. I can’t even seem to make the input test work.
Description from codechef:
Input
The input begins with two positive integers n k (n, k<=10^7). The next
n lines of input contain one positive integer ti, not greater than
10^9, each.
Output
Write a single integer to output, denoting how many integers ti are
divisible by k.
Here’s the code:
n, t = [int(x) for x in input().split()]
c = 0
for i in range(n):
if not int(input()) % t:
c += 1
print(c)
I’m not sure what I’m missing. How can I handle this faster?
This should really be a comment, but anyway.
Note that there’s an accepted Python 2 solution here, with runtime 45.77s, so it’s clearly possible. I think you’re a victim of Python 3’s slow I/O (looks like they’re using 3.1.2). On a two million line input file (which happens not to have any numbers which are divisible): there’s not much difference when there are a lot), on a version of your code modified to be compatible with 2 and 3, I get:
To throw @agf’s
(sum(not int(line) % t for line in sys.stdin[.buffer]))into the mix: