Problem p02997 (python)

Fixed submission:
# coding: utf-8
# Your code here!
N, K = map(int,input().split())

link = [[] for i in range(N)]
link[0] = [i for i in range(1,N)]
for i in range(1,N):
    link[i].append(0)

def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

if N == 2 and K == 0:
    print(1)
    print("1 2")
elif N == 2 and K == 1:
    print(-1)
elif comb(N-1,2) < K:
    print(-1)
else:
    maxnum = comb(N-1,2)
    for i in range(1,N):
        for j in range(1,N):
            if i < j:
                if maxnum != K:
                    link[i].append(j)
                    link[j].append(i)
                    maxnum -= 1
                else:
                    continue
    print(sum([len(l) for l in link])//2)
    for i in range(N):
        for j in link[i]:
            if i < j:
                print(i+1, j+1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N, K = map(int,input().split())

link = [[] for i in range(N)]
link[0] = [i for i in range(1,N)]
for i in range(1,N):
    link[i].append(0)

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

if N == 2 and K == 0:
    print(1)
    print("1 2")
elif N == 2 and K == 1:
    print(-1)
elif comb(N-1,2) < K:
    print(-1)
else:
    maxnum = comb(N-1,2)
    for i in range(1,N):
        for j in range(1,N):
            if i < j:
                if maxnum != K:
                    link[i].append(j)
                    link[j].append(i)
                    maxnum -= 1
                else:
                    continue
    print(sum([len(l) for l in link])//2)
    for i in range(N):
        for j in link[i]:
            if i < j:
                print(i+1, j+1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N, K = map(int,input().split())

link = [[] for i in range(N)]
link[0] = [i for i in range(1,N)]
for i in range(1,N):
    link[i].append(0)
    
from scipy.special import comb
if N == 2 and K == 0:
    print(1)
    print("1 2")
elif N == 2 and K == 1:
    print(-1)
elif comb(N-1,2) < K:
    print(-1)
else:
    maxnum = comb(N-1,2)
    for i in range(1,N):
        for j in range(1,N):
            if i < j:
                if maxnum != K:
                    link[i].append(j)
                    link[j].append(i)
                    maxnum -= 1
                else:
                    continue
    print(sum([len(l) for l in link])//2)
    for i in range(N):
        for j in link[i]:
            if i < j:
                print(i+1, j+1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N, K = map(int,input().split())

link = [[] for i in range(N)]
link[0] = [i for i in range(1,N)]
for i in range(1,N):
    link[i].append(0)
    
from scipy.special import comb
    
maxnum = comb(N-1,2)

if maxnum < K:
    print(-1)
else:
    for i in range(1,N):
        for j in range(1,N):
            if i < j:
                if maxnum != K:
                    link[i].append(j)
                    link[j].append(i)
                    maxnum -= 1
                else:
                    continue
    print(sum([len(l) for l in link])//2)
    for i in range(N):
        for j in link[i]:
            if i < j:
                print(i+1, j+1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N, K = map(int,input().split())

link = [[] for i in range(N)]
link[0] = [i for i in range(1,N)]
for i in range(1,N):
    link[i].append(0)
    
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
    
maxnum = cmb(N-1,2)

if maxnum < K:
    print(-1)
else:
    for i in range(1,N):
        for j in range(1,N):
            if i < j:
                if maxnum != K:
                    link[i].append(j)
                    link[j].append(i)
                    maxnum -= 1
                else:
                    continue
    print(sum([len(l) for l in link])//2)
    for i in range(N):
        for j in link[i]:
            if i < j:
                print(i+1, j+1)