Problem p03404 (python)

Fixed submission:
# coding: utf-8
# Your code here!
A,B = map(int,input().split())
h,w = 100, 100
grid = [['.' if j < 50 else '#' for i in range(w)] for j in range(h)]
A -= 1
B -= 1
reg = ['.' if i % 2 == 0 else '#' for i in range(w)]
for i in range(25):
    if B >= 50:
        grid[i*2] = reg
        B -= 50
    else:
        for j in range(B):
            grid[i*2][j*2] = '#'
        break
for i in range(25):
    if A >= 50:
        grid[99 - i*2] = reg
        A -= 50
    else:
        for j in range(A):
            grid[99 - i*2][j*2] = '.'
        break
print(h,w)
for row in grid:
    print(''.join(row))

Buggy submission (verdict: Runtime Error):
A,B = map(int,input().split())
N = 100

def printAns(ans):
	h = len(ans)
	w = len(ans[0])
	print(h, w)
	for l in ans:
		print(''.join(l))

if A <= B:
	minority = '.'
	majority = '#'
else:
	A,B = B,A
	minority = '#'
	majority = '.'

diff = abs(A-B)
if B <= N:
	ans = [[majority if (i+j) % 2 == 0 else minority for i in range(B)] for j in range(2)]
	a,b = B,B
	endsWithMajor = B % 2 == 0
	cnt = 1
	while a > A:
		if endsWithMajor:
			ans[1][-(2*cnt - 1)] = minority
			if cnt == 1:
				a -= 1
				b -= 1
			else:
				a -= 2
				b -= 1
		else:
			ans[1][-(2*cnt)] = minority
			a -= 2
			b -= 1
		cnt += 1
	while a < A:
		ans.append([majority] * B)
		ans.append([minority] * B)
		a += 1
		b += 1
	index = len(ans) - 1
	for i in range(diff // ((B+1) // 2) + 1):
		app = [minority for i in range(B)]
		for j in range((B+1) // 2):
			if b < B:
				app[2*j] = majority
				b += 1
		ans.append(app)
		ans.append([minority] * B)
		if b == B:
			break

else:
	ans = []
	a,b =0, 0
	for i in range(A // N + 1):
		ans.append([majority if (j + 0) % 2 == 0 else minority for j in range(N)])
		ans.append([majority if (j + 1) % 2 == 0 else minority for j in range(N)])
		a += N
		b += N

	endsWithMajor = N % 2 == 0
	cnt = 1
	index = len(ans) - 1
	while a > A:
		if endsWithMajor:
			ans[index][-(2*cnt - 1)] = minority
			if cnt == 1:
				a -= 1
				b -= 1
			else:
				a -= 2
				b -= 1
		else:
			ans[index][-(2*cnt)] = minority
			a -= 2
			b -= 1
		cnt += 1
	while a < A:
		ans.append([majority] * N)
		ans.append([minority] * N)
		a += 1
		b += 1
	index = len(ans) - 1
	for i in range(diff // (N // 2) + 1):
		app = [minority for i in range(N)]
		for j in range((N+1) // 2):
			if b < B:
				app[2*j] = majority
				b += 1
		ans.append(app)
		ans.append([minority] * N)
		if b == B:
			break
printAns(ans)

Buggy submission (verdict: Runtime Error):
A,B = map(int,input().split())
N = 100

def printAns(ans):
	h = len(ans)
	w = len(ans[0])
	print(h, w)
	for l in ans:
		print(''.join(l))

if A <= B:
	minority = '.'
	majority = '#'
else:
	A,B = B,A
	minority = '#'
	majority = '.'

diff = abs(A-B)
if B <= N:
	ans = [[majority if (i+j) % 2 == 0 else minority for i in range(B)] for j in range(2)]
	a,b = B,B
	endsWithMajor = B % 2 == 0
	cnt = 1
	while a > A:
		if endsWithMajor:
			ans[1][-(2*cnt - 1)] = minority
			if cnt == 1:
				a -= 1
				b -= 1
			else:
				a -= 2
				b -= 1
		else:
			ans[1][-(2*cnt)] = minority
			a -= 2
			b -= 1
		cnt += 1
	ans.append([majority] * B)
	ans.append([minority] * B)
	while a < A:
		ans.append([majority] * B)
		ans.append([minority] * B)
		a += 1
		b += 1
	index = len(ans) - 1
	for i in range(diff // ((B+1) // 2) + 1):
		app = [minority for i in range(B)]
		for j in range((B+1) // 2):
			if b < B:
				app[2*j] = majority
				b += 1
		ans.append(app)
		ans.append([minority] * B)
		if b == B:
			break

else:
	ans = []
	a,b =0, 0
	for i in range(A // N + 1):
		ans.append([majority if (j + 0) % 2 == 0 else minority for j in range(N)])
		ans.append([majority if (j + 1) % 2 == 0 else minority for j in range(N)])
		a += N
		b += N

	endsWithMajor = N % 2 == 0
	cnt = 1
	index = len(ans) - 1
	while a > A:
		if endsWithMajor:
			ans[index][-(2*cnt - 1)] = minority
			if cnt == 1:
				a -= 1
				b -= 1
			else:
				a -= 2
				b -= 1
		else:
			ans[index][-(2*cnt)] = minority
			a -= 2
			b -= 1
		cnt += 1
	while a < A:
		ans.append([majority] * N)
		ans.append([minority] * N)
		a += 1
		b += 1
	index = len(ans) - 1
	for i in range(diff // (N // 2) + 1):
		app = [minority for i in range(N)]
		for j in range((N+1) // 2):
			if b < B:
				app[2*j] = majority
				b += 1
		ans.append(app)
		ans.append([minority] * N)
		if b == B:
			break
printAns(ans)