Problem:


Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479



Sample input:
3
1 2
-1 1
2 -1


Sample output:
5

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = []
p = 0
for _ in range(n):
	i, j = map(int, input().split())
	s.append([i, j])

hate = []
for i in range(len(s)):
	for j in range(i+1, n):
		if s[i][0]*s[j][0] + s[i][1]*s[j][1] == 0:
			hate.append([i, j])

def comb(li):
	count = 0
	for l in li:
		if len(l) == 1:
			count += 1
		else:
			flag = 0
			for pr in list(combinations(l, 2)):
				if list(pr) in hate:
					flag = 1
					break
			if flag == 0:
				count += 1
	return count

for i in range(1, n+1):
	l = list(combinations(range(0, n), i))
	p += comb(l)
print(p % 1000000007)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = []
p = 0
for _ in range(n):
	i, j = map(int, input().split())
	s.append([i, j])

hate = []
for i in range(len(s)):
	for j in range(i+1, n):
		if s[i][0]*s[j][0] + s[i][1]*s[j][1] == 0:
			hate.append([i, j])

def comb(li):
	count = 0
	for l in li:
		if len(l) == 1:
			count += 1
		else:
			flag = 0
			for pr in list(combinations(l, 2)):
				if list(pr) in hate: flag = 1
			if flag == 0:
				count += 1
	return count

for i in range(1, n+1):
	l = list(combinations(range(0, n), i))
	p += comb(l)
print(p % 1000000007)

Submission status: Time Limit Exceeded

Solution code:
import collections
from math import gcd
def main():
    n=int(input())
    c=collections.Counter()
    mod=10**9+7
    for _ in range(n):
        a,b=map(int,input().split())
        g=abs(gcd(a,b))
        if g==0:
            g=1
        a//=g
        b//=g
        c[(a,b)]+=1
    ans=1
    s=set()
    for i,j in c:
        if (i,j)==(0,0):
            continue
        if (i,j) not in s:
            s.add((i,j))
            s.add((-i,-j))
            s.add((-j,i))
            s.add((j,-i))
            ans*=(pow(2,c[(i,j)]+c[(-i,-j)],mod)+pow(2,c[(j,-i)]+c[(-j,i)],mod)-1)%mod
    print((ans-1+c[(0,0)])%mod)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
from math import gcd
import sys
input=sys.stdin.readline
n=int(input())
c=collections.Counter()
mod=10**9+7
for _ in range(n):
    a,b=map(int,input().split())
    g=abs(gcd(a,b))
    if g==0:
        g=1
    a//=g
    b//=g
    c[(a,b)]+=1
ans=1
s=set()
for i,j in c:
    if (i,j)==(0,0):
        continue
    if (i,j) not in s:
        s.add((i,j))
        s.add((-i,-j))
        s.add((-j,i))
        s.add((j,-i))
        ans*=(pow(2,c[(i,j)]+c[(-i,-j)],mod)+pow(2,c[(j,-i)]+c[(-j,i)],mod)-1)%mod
print((ans-1+c[(0,0)])%mod)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
import collections
c=collections.Counter()
from math import gcd
mod=10**9+7
for _ in range(n):
    a,b=map(int,input().split())
    g=abs(gcd(a,b))
    if g==0:
        g=1
    a//=g
    b//=g
    c[(a,b)]+=1
ans=1
s=set()
for i,j in c:
    if (i,j)==(0,0):
        continue
    if (i,j) not in s:
        s.add((i,j))
        s.add((-i,-j))
        s.add((-j,i))
        s.add((j,-i))
        ans*=(pow(2,c[(i,j)]+c[(-i,-j)],mod)+pow(2,c[(j,-i)]+c[(-j,i)],mod)-1)%mod
print((ans-1+c[(0,0)])%mod)

Submission status: Time Limit Exceeded

Solution code:
from fractions import Fraction
from collections import defaultdict


def inverse(key):
    if key == "inf":
        return Fraction(0, 1)

    if key == Fraction(0, 1):
        return "inf"

    return - Fraction(key.denominator, key.numerator)


mod = 10 ** 9 + 7
n = int(input())
fishes = defaultdict(int)

zero_zero = 0
for _ in range(n):
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        zero_zero += 1
        continue

    elif b == 0:
        key = "inf"

    else:
        key = Fraction(a, b)

    fishes[key] += 1

ans = 1
counted_key = set()
for fish_key, count in fishes.items():
    if fish_key in counted_key:
        continue

    bad_pair = inverse(fish_key)
    if bad_pair in fishes:
        pair_count = fishes[bad_pair]
        pattern = pow(2, count, mod) + pow(2, pair_count, mod) - 1
        counted_key.add(bad_pair)

    else:
        pattern = pow(2, count, mod)

    ans = ans * pattern % mod

if zero_zero:
    ans += zero_zero

ans -= 1
print(ans % mod)

Submission status: Time Limit Exceeded

Solution code:
import copy
N=int(input())

abl=[]
for i in range(N):
    abl.append( list (map(int,input().split() )) )

sl=[]
for i in range(N):
    for j in range( i +1,N ):
        if abl[i][0] * abl[j][0]+ abl[i][1] * abl[j][1] == 0:
            sl.append([i+1,j+1])


l=[[]]
for i in range(1,N+1):
    tl= []
    for l2 in l:
        tl.append(copy.copy(l2))
        l2.append(i)
    l.extend(tl)

ct=0
ats= set()
for l2 in l:
    f=True
    for s in sl:
        if s[0] in l2 and s[1] in l2 :
            f=False
    if f:
        ats.add( tuple(l2) )
            
            



print(  (len(ats) -1) % 1000000007 )

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
from collections import defaultdict
import math
d = defaultdict(int)
MOD=10**9+7
for i in range(N):
  a,b=map(int,input().split())
  x=math.gcd(a,b)
  if x==0:
    if a>b:
      d[(1,0)]+=1
    elif b>a:
      d[(0,1)]+=1
    else:
      d[(0,0)]+=1
  else:
    a,b=a//x,b//x
    if a<0:
      a,b=-a,-b
    d[(a,b)]+=1
ans=0
def C(x):
  X=math.factorial(x)
  _=0
  for i in range(1,x+1):
    _+=X//math.factorial(i)//math.factorial(x-i)
  return _
if d[(0,0)]>0:
  x=d[(0,0)]
  X=math.factorial(x)
  for i in range(1,x+1):
    ans+=X//math.factorial(i)//math.factorial(x-i)
ind=defaultdict(int)
index=1
_ans=[1]
for a,b in list(d.keys()):
  if b<0:
    _k=(-b,a)
  else:
    _k=(b,-a)
  if d[_k]==0:
    _ans.append(C(d[(a,b)])+1)
    index+=1
  else:
    if ind[_k]==0:
      _ans.append(C(d[(a,b)]))
      ind[(a,b)]=index
      index+=1
    else:
      _ans[ind[_k]]+=C(d[(a,b)])+1
_=1
for a in _ans:
  _*=a
print((ans+_-1)%MOD)

Submission status: Time Limit Exceeded

Solution code:

from itertools import permutations

def parse():
  N = int(input())
  A = []
  B = []
  for n in range(N):
    a, b = map(int, input().split(" "))
    A.append(a)
    B.append(b)
  return N, A, B

def main():
  N, A, B = parse()
  # print(N, A, B)

  # loop_limit = 100000
  ans = 0
  for l in range(pow(2, N)):
    # if i >= loop_limit: break

    pattern = [l % 2 == 0]
    for j in range(1, N):
      selected = int(l // pow(2, j)) % 2 == 0
      pattern.append(selected)
    pattern.reverse()

    # print(pattern)
    if sum(pattern) == 1:
      # print(pattern)
      ans += 1  # 1匹のみ選択の場合
    elif sum(pattern) == 0: pass  # 1匹も選ばなかった場合
    else:
      can_together = True
      skip = False
      for i, selected_i in enumerate(pattern):
        if selected_i:
          for j, selected_j in enumerate(pattern):
            if i == j: continue
            if selected_j:
              if A[i] * A[j] + B[i] * B[j] == 0:
                can_together = False
                break
          if not can_together:
            break
      if can_together: ans += 1
  
  print(ans % 1000000007)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:

from itertools import permutations

def parse():
  N = int(input())
  A = []
  B = []
  for n in range(N):
    a, b = map(int, input().split(" "))
    A.append(a)
    B.append(b)
  return N, A, B

def main():
  N, A, B = parse()
  # print(N, A, B)

  # loop_limit = 100000
  ans = 0
  for l in range(pow(2, N)):
    # if i >= loop_limit: break

    pattern = [l % 2 == 0]
    for j in range(1, N):
      selected = int(l // pow(2, j)) % 2 == 0
      pattern.append(selected)
    pattern.reverse()

    # print(pattern)
    if sum(pattern) == 1:
      # print(pattern)
      ans += 1  # 1匹のみ選択の場合
    elif sum(pattern) == 0: pass  # 1匹も選ばなかった場合
    else:
      can_together = True
      skip = False
      for i, selected_i in enumerate(pattern):
        for j, selected_j in enumerate(pattern):
          if i == j: continue
          if selected_i and selected_j:
            if A[i] * A[j] + B[i] * B[j] == 0:
              can_together = False
              break
        if not can_together:
          break
      if can_together:
        ans += 1
        
  print(ans % 1000000007)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf
# @pysnooper.snoop()
#import pysnooper # debug

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect
from fractions import Fraction
from collections import defaultdict


def mode(a, b):
    # retrun dict_key, arr_key
    if a == 0 and b == 0:
        return 0, 0
    if a == 0:
        return 'ab', 0
    if b == 0:
        return 'ab', 1
    x = Fraction(a, b)
    y = Fraction(-1 * b, a)
    if x > y:
        return x, 0
    else:
        return y, 1

MOD = 1000000007

if __name__ == '__main__':

    n = int(input())

    d = defaultdict(lambda: [0, 0])
    zero = 0
    for _ in range(n):
        a, b = list(map(int, input().split()))
        dkey, akey = mode(a, b)
        if dkey == 0:
            zero += 1
        else:
            d[dkey][akey] += 1
    #print('d') # debug
    #pp(d) # debug

    ans = 1
    for pair in d.values():
        tmp = pow(2, pair[0], MOD) - 1 + pow(2, pair[1], MOD) - 1 + 1
        ans *= tmp
    ans += zero - 1
    ans %= MOD
    #print('ans') # debug
    print(ans)

    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import math as m
from fractions import Fraction

MOD = 1000000007

N = int(input())
Sardine = collections.namedtuple("Sardine", "A B")
sardines = [Sardine(*map(int, input().split())) for _ in range(N)]

def slope(sardine):
    if sardine.A == sardine.B == 0:
        return None
    elif sardine.B == 0:
        return m.inf
    else:
        return Fraction(sardine.A, sardine.B)

def perpendicular(slope):
    if slope == 0:
        return m.inf
    elif slope == m.inf:
        return 0
    else:
        return -1 / slope

groups = collections.Counter(slope(s) for s in sardines)
if groups[None]:
    zeroes = groups[None]
    del groups[None]
else:
    zeroes = 0

ways = 1
while groups:
    slope, count = groups.popitem()
    perp_slope = perpendicular(slope)
    perp_count = groups[perp_slope]

    ways *= (pow(2, count, MOD) + pow(2, perp_count, MOD) - 1) % MOD
    ways %= MOD

    del groups[perp_slope]

output = (ways-1 + zeroes) % MOD
print(output)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import math as m
from fractions import Fraction

MOD = 1000000007

N = int(input())
Sardine = collections.namedtuple("Sardine", "A B")
sardines = [Sardine(*map(int, input().split())) for _ in range(N)]

def slope(sardine):
    if sardine.A == sardine.B == 0:
        return None
    elif sardine.B == 0:
        return m.inf
    else:
        return Fraction(sardine.A, sardine.B)

def perpendicular(slope):
    if slope == 0:
        return m.inf
    elif slope == m.inf:
        return 0
    else:
        return -1 / slope

groups = collections.Counter(slope(s) for s in sardines)
if groups[None]:
    zeroes = groups[None]
    del groups[None]
else:
    zeroes = 0

ways = 1
while groups:
    slope, count = groups.popitem()
    perp_slope = perpendicular(slope)
    perp_count = groups[perp_slope]

    ways *= pow(2, count, MOD) + pow(2, perp_count, MOD) - 1
    ways %= MOD

    del groups[perp_slope]

output = (ways-1 + zeroes) % MOD
print(output)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(itr=iter(sys.stdin.readlines())):  # pylint: disable= redefined-builtin
    return next(itr).rstrip('\n\r')
###############################################################################

import collections
import math as m
from fractions import Fraction

MOD = 1000000007

N = int(input())
Sardine = collections.namedtuple("Sardine", "A B")
sardines = [Sardine(*map(int, input().split())) for _ in range(N)]

def slope(sardine):
    if sardine.A == sardine.B == 0:
        return None
    elif sardine.B == 0:
        return m.inf
    else:
        return Fraction(sardine.A, sardine.B)

def perpendicular(slope):
    if slope == 0:
        return m.inf
    elif slope == m.inf:
        return 0
    else:
        return -1 / slope

groups = collections.Counter(slope(s) for s in sardines)
if groups[None]:
    zeroes = groups[None]
    del groups[None]
else:
    zeroes = 0

ways = 1
while groups:
    slope, count = groups.popitem()
    perp_slope = perpendicular(slope)
    perp_count = groups[perp_slope]

    ways *= ((1<<count) + (1<<perp_count) - 1) % MOD
    ways %= MOD

    del groups[perp_slope]

output = (ways-1 + zeroes) % MOD
print(output)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np 

exit_list = []
feature_fish = []
count=0
N = int(input())
num_fish = np.arange(N)
for i in range(N):
    A, B = (int(x) for x in input().split())
    feature_fish.append([A, B])

num_fish_seq = list(itertools.combinations(num_fish,2))
for i in range(len(num_fish_seq)):
    x = feature_fish[num_fish_seq[i][0]]
    y = feature_fish[num_fish_seq[i][1]]
    hontei = x[0]*y[0] + x[1]*y[1]
    if hontei != 0:
        count += 1
    else:
        exit_list.append(num_fish_seq[i])

for i in range(2, N):
    demo = list(itertools.combinations(num_fish, i+1))
    for k in range(len(demo)):
        check = 0
        for i in range(len(exit_list)):
            if not(set(exit_list[i]) <= set(demo[k])):
                check += 1
                if check == len(exit_list):
                    count += 1

result = count + len(num_fish)
answer = result % 1000000007
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np 

exit_list = []
feature_fish = []
count=0

N = int(input())
num_fish = np.arange(N)
for i in range(N):
    A, B = (int(x) for x in input().split())
    feature_fish.append([A, B])

num_fish_seq = list(itertools.combinations(num_fish,2))

for i in range(len(num_fish_seq)):
    x = feature_fish[num_fish_seq[i][0]]
    y = feature_fish[num_fish_seq[i][1]]

    hontei = x[0]*y[0] + x[1]*y[1]
    if hontei != 0:
        count += 1
    else:
        exit_list.append(num_fish_seq[i])

for i in range(2, N):
    demo = list(itertools.combinations(num_fish, i+1))
    for k in range(len(demo)):
        check = 0
        for i in range(len(exit_list)):
            if not(set(exit_list[i]) <= set(demo[k])):
                check += 1
                if check == len(exit_list):
                    count += 1

result = count + len(num_fish)
answer = result % 1000000007
print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
iwa = [list(map(int, input().split())) for _ in range(N)]

dev = 1000000007

all_s = 2**len(iwa)-1

bad = []
for i in range(len(iwa)):
  for j in range(i+1, len(iwa)):
    if iwa[i][0]*iwa[j][0]+iwa[i][1]*iwa[j][1] == 0:
      bad.append(2**i+2**j)
bad_s = 0
for i in range(1, all_s+1):
  for j in bad:
    # print(bin(i), bin(j), bin(i&j))
    if bin(i&j)==bin(j):
      bad_s+=1
      break
print((all_s-bad_s)%dev)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from collections import defaultdict

input = sys.stdin.buffer.readline
MOD = 1000000007
N = int(input())
dd = defaultdict(list)
count = 1
iwashi = np.array([list(map(int, input().split())) for _ in range(N)])


gcd = np.gcd(iwashi[:, 0], iwashi[:, 1])
iwashi = iwashi // np.array([gcd, gcd]).T
checked = [False] * N
zeros = 0
for i in range(N):
    if checked[i]:
        continue
    if iwashi[i][0] == iwashi[i][1] == 0:
        zeros += 1
        continue
    parallel = [i]
    vertical = []
    for j in range(i + 1, N):
        if checked[j]:
            continue
        if iwashi[j][0] == iwashi[j][1] == 0:
            zeros += 1
            checked[j] = True
            continue
        if np.dot(iwashi[i], iwashi[j]) == 0:
            vertical.append(j)
            checked[j] = True
        elif (iwashi[i] == iwashi[j]).all() or (iwashi[i] == iwashi[j] * (-1)).all():
            parallel.append(j)
            checked[j] = True
    p = len(parallel)
    v = len(vertical)
    count *= (pow(2, p, MOD) - 1) + (pow(2, v, MOD) - 1) + 1
    count %= MOD
count -= 1
count += zeros
print(count % MOD)

Submission status: Time Limit Exceeded

Solution code:
#E
from fractions import gcd
N=int(input())

mod=1000000007
ans=1
P=dict()
M=dict()
zero=0
zero1=0
zero2=0
for i in range(N):
    a,b=map(int,input().split())
    if a==0 and b==0:
        zero+=1
    elif a==0 and b!=0:
        zero1+=1
    elif a!=0 and b==0:
        zero2+=1
    elif (a>0 and b<0) or (a<0 and b>0):
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        s=str(a)+"/"+str(b)
        P[s]=P.get(s,0)+1
        
    else:
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        s=str(b)+"/"+str(a)
        M[s]=M.get(s,0)+1

P["0/0"]=zero1
M["0/0"]=zero2
for v,cnt_p in P.items():
    if v in M:
        cnt_m=M[v]
        ans*=(2**cnt_p+2**cnt_m-1)
        M.pop(v)
    else:
        ans*=2**cnt_p
    ans%=mod
    
for v,cnt_m in M.items():
    ans*=2**cnt_m
    ans%=mod

ans-=1
ans+=zero
ans%=mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#E
from fractions import gcd
from collections import Counter
N=int(input())

mod=1000000007
ans=1
P=[]
M=[]
zero=0
zero1=0
zero2=0
for i in range(N):
    a,b=map(int,input().split())
    if a==0 and b==0:
        zero+=1
    elif a==0 and b!=0:
        zero1+=1
    elif a!=0 and b==0:
        zero2+=1
    elif (a>0 and b<0) or (a<0 and b>0):
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        P.append(str(a)+"/"+str(b))
        
    else:
        GCD=gcd(abs(a),abs(b))
        a=abs(a)//GCD
        b=abs(b)//GCD
        M.append(str(b)+"/"+str(a))

P_dict=Counter(P)
M_dict=Counter(M)
P_dict["0/0"]=zero1
M_dict["0/0"]=zero2
for v,cnt_p in P_dict.items():
    if v in M_dict:
        cnt_m=M_dict[v]
        ans*=(2**cnt_p+2**cnt_m-1)
        M_dict.pop(v)
    else:
        ans*=2**cnt_p
    ans%=mod
    
for v,cnt_m in M_dict.items():
    ans*=2**cnt_m
    ans%=mod

ans-=1
ans+=zero
ans%=mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

d=[]
a=[]
b=[]
bl={}
d.clear()
a.clear()
b.clear()


n=int(input())
j=0
for i in range(n):
    m=list(map(int,input().split()))
    d.append(j)
    a.append(m[0])
    b.append(m[1])
    j=j+1

c=0
for x in range(1,len(d)+1):
    v=list(itertools.combinations(d,x))
    lv=len(v)   #x個選んだときの組み合わせ数

#    print("@@",lv)

    if x>1:
        for y in v:
            f=0
#            print(y)
            ##過去に除去条件にヒットした要素が含まれているか
            for vv in bl:
#               print("cache:",vv[0],vv[1])
                if y.count(vv[0])>0:
                    if y.count(vv[1])>0:    
#                        print("HIT")
                        f=1
                        break

            if f==1:
                lv=lv-1
#                print("***")
                continue

            z=list(itertools.combinations(y,2))
            f=0
            for zz in z:
                #どれか条件に引っかかったら終わり
                if a[zz[0]]*a[zz[1]]+b[zz[0]]*b[zz[1]]==0:
                    lv=lv-1
                    bl[zz[0],zz[1]]=1
#                    print("BLACK:",zz[0],',',zz[1])                    
                    if x==2:
                        continue
                    else:
                        break

    c=c+lv


print(c)

Submission status: Time Limit Exceeded

Solution code:
import itertools

d=[]
a=[]
b=[]
d.clear()
a.clear()
b.clear()

n=int(input())
j=0
for i in range(n):
    m=list(map(int,input().split()))
    d.append(j)
    a.append(m[0])
    b.append(m[1])
    j=j+1

c=0
for x in range(1,len(d)+1):
    v=list(itertools.combinations(d,x))
    lv=len(v)   #x個選んだときの組み合わせ数

#    print("@@",lv)

    if x>1:
        for y in v:
#            print("###",y)
            z=list(itertools.combinations(y,2))
            f=0
            for zz in z:
                #どれか条件に引っかかったら終わり
                if a[zz[0]]*a[zz[1]]+b[zz[0]]*b[zz[1]]==0:
                    lv=lv-1
                    break

    c=c+lv


print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(300000)
from fractions import gcd
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
d = defaultdict(int)
zero = 0
for i in range(N):
    a, b = MI()
    if a == 0 and b == 0:
        zero += 1
        continue
    g = gcd(a, b)
    a, b = a // g, b // g
    minus = (-1) ** ((a < 0) + (b < 0))
    d[(minus * abs(a), abs(b))] += 1

ans = 1
seen = set()
for a, b in d:
    if (a, b) not in seen:
        if (a, b) in [(1, 0), (0, 1)]:
            ainv, binv = b, a
        else:
            ainv, binv = b * (1 if a < 0 else -1), abs(a)
        lans = (2 ** d[(a, b)] - 1)
        lans += (2 ** d[(ainv, binv)] - 1) if d.get((ainv, binv)) is not None else 0
        lans += 1
        ans = ans * lans % MOD
        seen.add((a, b))
        seen.add((ainv, binv))
ans = (ans + zero) % MOD
print((ans + MOD - 1) % MOD)

Submission status: Time Limit Exceeded

Solution code:
from fractions import Fraction
from collections import Counter
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

MOD = 10**9+7

def main():
    N,*ab = map(int, read().split())

    ab_zero = 0
    a_zero = 0
    b_zero = 0
    ratio = []
    for a, b in zip(*[iter(ab)]*2):
        if a == 0 and b == 0:
            ab_zero += 1
        elif a == 0:
            a_zero += 1
        elif b == 0:
            b_zero += 1
        else:
            ratio.append(Fraction(a, b))
    
    s = Counter(ratio)

    bad = pow(2, a_zero, MOD) + pow(2, b_zero, MOD) - 1
    bad %= MOD
    for k, v in s.items():
        if - 1 / k in s:
            if s[k] != 0:
                bad *= (pow(2, v, MOD) + pow(2, s[-1/k], MOD) -1) % MOD
                bad %= MOD
                s[- 1 / k] = 0
        else:
            bad *= pow(2, v, MOD)
            bad %= MOD

    ans = bad - 1 + ab_zero
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
mod = 1000000007
 
n = int(sys.stdin.readline().rstrip())
ab = {}
for i in range(n):
    a,b = map(int,sys.stdin.readline().rstrip().split())
    if a == 0 and b == 0:
        pass
    elif a == 0:
        b = 1
    else:
        gcd = math.gcd(a,b)
        a //= gcd
        b //= gcd
        if a < 0:
            a = -a
            b = -b
    ab.setdefault((a,b),0)
    ab[(a,b)]+=1
ans = 0
pairs=[]
s = list(set(ab.keys()))
for i in ab.keys():
    a = i[0];b = i[1]
    if a == 0 and b == 0:
        ans = ab[(0,0)]
        n -= ans
    else:
        if (-b, a) in s:
            pairs.append([ab[(a,b)],ab[(-b,a)]])
select = 1
for pair in pairs:
    select*= (pow(2,pair[0],mod) + pow(2,pair[1],mod) -1)
    n -= (pair[0]+pair[1])
    select %= mod #TLE出ないように
select *= pow(2,n,mod) 
all_ans = select + ans -1
all_ans %= mod
print(all_ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
mod = 1000000007

n = int(sys.stdin.readline().rstrip())
ab = {}
for i in range(n):
    a,b = map(int,sys.stdin.readline().rstrip().split())
    if a == 0 and b == 0:
        pass
    elif a == 0:
        b = 1
    else:
        gcd = math.gcd(a,b)
        a //= gcd
        b //= gcd
        if a < 0:
            a = -a
            b = -b
    ab.setdefault((a,b),0)
    ab[(a,b)]+=1
ans = 0
pairs=[]
s = list(set(ab.keys()))
for i in ab.keys():
    a = i[0];b = i[1]
    if a == 0 and b == 0:
        ans = ab[(0,0)]
        n -= ans
    else:
        if (-b, a) in s:
            pairs.append([ab[(a,b)],ab[(-b,a)]])
select = 1
for pair in pairs:
    select*= (pow(2,pair[0],mod) + pow(2,pair[1],mod) -1)
    n -= (pair[0]+pair[1])
all_ans = (pow(2,n,mod) * select) % mod + ans -1
all_ans %= mod
print(all_ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(300000)
from fractions import gcd
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
d = defaultdict(int)
zero = 0
for i in range(N):
    a, b = MI()
    if a == 0 and b == 0:
        zero += 1
        continue
    g = gcd(a, b)
    a, b = a // g, b // g
    minus = (-1) ** ((a < 0) + (b < 0))
    d[(minus * abs(a), abs(b))] += 1

ans = 1
seen = set()
for a, b in d:
    if (a, b) not in seen:
        if (a, b) in [(1, 0), (0, 1)]:
            ainv, binv = b, a
        else:
            ainv, binv = b * (1 if a < 0 else -1), abs(a)
        lans = (2 ** d[(a, b)] - 1)
        lans += (2 ** d[(ainv, binv)] - 1) if d.get((ainv, binv)) is not None else 0
        lans += 1
        ans = ans * lans % MOD
        seen.add((a, b))
        seen.add((ainv, binv))
ans = (ans + zero) % MOD
print((ans + MOD - 1) % MOD)

Submission status: Time Limit Exceeded

Solution code:
# E - ∙ (Bullet)
import sys
from collections import defaultdict

readline = sys.stdin.readline
MOD = 1000000007
N = int(readline())

# イワシの傾きをA/Bとする。
# A/Bは予めgcdで割って約分し、同じ傾きのものを揃えておく。
# このとき、仲の悪い組合せは(A, B)に対して(B, -A)となるもの。 
# 前者をS、後者をTとして考える。
d_S = defaultdict(int)
d_T = defaultdict(int)

def gcd(x, y):
    if x == 0:
        return y
    else:
        return gcd(y%x, x)

pow2_mod = [1] + [0] * (N * 10)
for i in range(1, N * 10):
    pow2_mod[i] = pow2_mod[i-1] * 2 % MOD

zeros = 0
for i in range(N):
    A, B = map(int, readline().split())
    # (0, 0)のイワシは他の全てのイワシと仲が悪いため、
    # 1匹のみでしか選ぶことができない。最後にこの数を足す。
    if A == 0 and B == 0:
        zeros += 1
    # (?, 0),(0, ?)のイワシは、?がどのような数字であろうとも
    # この組合せであれば仲が悪い。一律、(1,0)(0,-1)として考える。
    elif A == 0:
        d_S[(1, 0)] += 0
        d_T[(0, -1)] += 1
    elif B == 0:
        d_S[(1, 0)] += 1
        #d_T[(0, -1)] += 0
    else:
        g = gcd(A, B)
        A //= g
        B //= g
        # いずれもマイナスの場合、傾きはプラスになる
        if A < 0 and B < 0:
            A *= -1
            B *= -1
        if A * B > 0:
            d_S[(A, B)] += 1
            #d_T[(B, -A)] += 0
        # 片方がマイナスの場合、必ずAがマイナスになるよう揃える
        else:
            if A > B:
                A, B = B, A
            d_S[(-A, B)] += 0
            d_T[(B, A)] += 1

ans = 1
for (si, sj), s_cnt in d_S.items():
    t_cnt = d_T[(sj, -1*si)]
    tmp = 0
    tmp += pow2_mod[s_cnt] - 1 # Sから1匹以上選ぶ組合せ
    tmp += pow2_mod[t_cnt] - 1 # Tから1匹以上選ぶ組合せ
    tmp += 1 # どちらも選ばない組合せ
    ans = (ans * tmp)%MOD

ans -= 1 # 最終的に1匹も選ばない組合せ
ans += zeros # (0, 0)のイワシの数

print(ans%MOD)

Submission status: Time Limit Exceeded

Solution code:
# E - ∙ (Bullet)

from collections import defaultdict

MOD = 1000000007
N = int(input())

# イワシの傾きをA/Bとする。
# A/Bは予めgcdで割って約分し、同じ傾きのものを揃えておく。
# このとき、仲の悪い組合せは(A, B)に対して(B, -A)となるもの。 
# 前者をS、後者をTとして考える。
d_S = defaultdict(int)
d_T = defaultdict(int)

def gcd(x, y):
    if x == 0:
        return y
    else:
        return gcd(y%x, x)

zeros = 0
for i in range(N):
    A, B = map(int, input().split())
    # (0, 0)のイワシは他の全てのイワシと仲が悪いため、
    # 1匹のみでしか選ぶことができない。最後にこの数を足す。
    if A == 0 and B == 0:
        zeros += 1
    # (?, 0),(0, ?)のイワシは、?がどのような数字であろうとも
    # この組合せであれば仲が悪い。一律、(1,0)(0,-1)として考える。
    elif A == 0:
        d_S[(1, 0)] += 0
        d_T[(0, -1)] += 1
    elif B == 0:
        d_S[(1, 0)] += 1
        d_T[(0, -1)] += 0
    else:
        g = gcd(A, B)
        A //= g
        B //= g
        # いずれもマイナスの場合、傾きはプラスになる
        if A < 0 and B < 0:
            A *= -1
            B *= -1
        if A * B > 0:
            d_S[(A, B)] += 1
            d_T[(B, -A)] += 0
        # 片方がマイナスの場合、必ずAがマイナスになるよう揃える
        else:
            if A > B:
                A, B = B, A
            d_S[(-A, B)] += 0
            d_T[(B, A)] += 1

ans = 1
for (si, sj), s_cnt in d_S.items():
    t_cnt = d_T[(sj, -1*si)]
    tmp = 0
    tmp += pow(2, s_cnt, MOD) - 1
    tmp += pow(2, t_cnt, MOD) - 1
    tmp += 1
    ans = (ans * tmp)%MOD

ans -= 1
ans += zeros

print(ans%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
mod = 1000000007

n = int(input())
ab = {}
for i in range(n):
    a,b = map(int,sys.stdin.readline().rstrip().split())
    if a == 0 and b == 0:
        pass
    elif a == 0:
        b = 1
    else:
        gcd = math.gcd(a,b)
        a //= gcd
        b //= gcd
        if a < 0:
            a = -a
            b = -b
    ab.setdefault((a,b),0)
    ab[(a,b)]+=1
ans = 0
pairs=[]
s = list(set(ab.keys()))
for i in ab:
    a = i[0];b = i[1]
    if a == 0 and b == 0:
        ans = ab[(0,0)]
        n -= ans
    else:
        if (-b, a) in s:
            pairs.append([ab[(a,b)],ab[(-b,a)]])
select = 1
for pair in pairs:
    select*= (pow(2,pair[0],mod) + pow(2,pair[1],mod) -1)
    n -= (pair[0]+pair[1])
all_ans = (pow(2,n,mod) * select) % mod + ans -1
all_ans %= mod
print(all_ans)