intest - enormous input test
problem
The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.
Input: The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.
Output: Write a single integer to output, denoting how many integers ti are divisible by k.
Example Input: 7 3 1 51 966369 7 9 999996 11 Output: 4
fast io with stdin
Definitely one of the most intersting/valuable ideas this far into the book has
to be the use of sys’ stdin for fast io. According to the authors, it offers 4x
speedup over python’s input().
And for a further 2x improvement, just read the whole of the inputs in a single
system call using os.read() instead! You just need a sense of the upper bound
size M of the file you want to read.
solution
Putting those ideas to use, gives us:
from sys import stdin, stdout
def readint():
return int(stdin.readline())
def read_inputs():
n, k = stdin.readline().split()
return int(n), int(k)
def are_divisible(n: int, k: int):
count = 0
for _ in range(n):
t = readint()
if t % k == 0:
count += 1
stdout.write(str(count))
if __name__ == "__main__":
n, k = read_inputs()
are_divisible(n, k)