message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Provide tags and a correct Python 3 solution for this coding contest problem. The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery. Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1. The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor. Output In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute. In the second line print m space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 3 101 010 001 0 1 2 Output 1 1 Input 1 1 1 Output 0 Input 4 1111 0101 1110 0001 1 0 1 0 Output 4 1 2 3 4 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3.
instruction
0
50,893
14
101,786
Tags: constructive algorithms, dfs and similar, graphs, greedy Correct Solution: ``` import sys input = sys.stdin.readline def solve(): n = int(input()) b = [input() for i in range(n)] a = list(map(int,input().split())) c = [None]*n w = [False]*n h = [0]*n for i in range(n): c[i] = (a[i], i) c.sort() r = [] for i in range(n): while True: ok = False for j in range(i+1): v = c[j][1] if h[v] == a[v]: ok = True if w[v]: raise Exception('wut') else: w[v] = True r.append(v) for k in range(n): if b[v][k] == '1': h[k] += 1 if not ok: break print(len(r)) for i in r: print(i+1) #print(h) solve() ```
output
1
50,893
14
101,787
Provide tags and a correct Python 3 solution for this coding contest problem. The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery. Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1. The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor. Output In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute. In the second line print m space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 3 101 010 001 0 1 2 Output 1 1 Input 1 1 1 Output 0 Input 4 1111 0101 1110 0001 1 0 1 0 Output 4 1 2 3 4 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3.
instruction
0
50,894
14
101,788
Tags: constructive algorithms, dfs and similar, graphs, greedy Correct Solution: ``` n=int(input().strip()) a=[] for i in range(n): s=input().strip() a.append(s) b=list(map(int,input().strip().split())) c=[] while (True): k=-1 for i in range(n): if (b[i]==0): k=i break if (k==-1): break else: c.append(k+1) for i in range(n): if (a[k][i]=='1'): b[i]-=1 tot=len(c) print(tot) for x in c: tot=tot-1 print(x,end=' ') if (tot!=0) else print(x,end='\n') ```
output
1
50,894
14
101,789
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery. Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1. The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor. Output In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute. In the second line print m space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 3 101 010 001 0 1 2 Output 1 1 Input 1 1 1 Output 0 Input 4 1111 0101 1110 0001 1 0 1 0 Output 4 1 2 3 4 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. Submitted Solution: ``` n = int(input()) mvec = [0 for i in range(n)] for i in range(n): vec = tuple(map(int, list(input()))) for j in range(n): mvec[j] += vec[j] avec = tuple(map(int, input().split())) awin = 0 wins = '' for i in range(n): if mvec[i] != avec[i]: awin += 1 wins += str(i+1) + ' ' print(awin) print(wins[:-1]) ```
instruction
0
50,895
14
101,790
No
output
1
50,895
14
101,791
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery. Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1. The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor. Output In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute. In the second line print m space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 3 101 010 001 0 1 2 Output 1 1 Input 1 1 1 Output 0 Input 4 1111 0101 1110 0001 1 0 1 0 Output 4 1 2 3 4 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. Submitted Solution: ``` n = int(input()) mvec = [0 for i in range(n)] vecs = [] for i in range(n): vecs.append(tuple(map(int, list(input())))) for j in range(n): mvec[j] += vecs[i][j] avec = tuple(map(int, input().split())) awin = 0 wins = '' for i in range(n): if mvec[i] != avec[i]: awin += 1 wins += str(i+1) + ' ' if awin == 0 and 0 in avec: awin = -1 if awin == -1: print(awin) else: print(awin) print(wins[:-1]) ```
instruction
0
50,896
14
101,792
No
output
1
50,896
14
101,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery. Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1. The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor. Output In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute. In the second line print m space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 3 101 010 001 0 1 2 Output 1 1 Input 1 1 1 Output 0 Input 4 1111 0101 1110 0001 1 0 1 0 Output 4 1 2 3 4 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. Submitted Solution: ``` n = int(input()) contacts_all = [] for i in range(n): contacts_all.append(input()) guesses = [ int(message_count) for message_count in input().split() ] person_list = [ i for i in range(n) ] igor_wins = False import itertools result_state = () for people in range(n): for states in itertools.combinations( person_list, people ): received_messages = [ 0 ] * n for state in states: # loop through states result_state = state for coming in state: # loop through people in state for i in range(n): #loop through coming's contacts list if contacts_all[coming][i] == '1': received_messages[i] += 1 for i in range(n): if guesses[i] == received_messages[i]: igor_wins = True break if igor_wins == False: break if igor_wins == False: break if igor_wins == False: break print(len(result_state)) for person in result_state: print(person, end=" ") print() ```
instruction
0
50,897
14
101,794
No
output
1
50,897
14
101,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Looksery company, consisting of n staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates n numbers, the i-th of which indicates how many messages, in his view, the i-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer n (1 ≤ n ≤ 100) — the number of employees of company Looksery. Next n lines contain the description of the contact lists of the employees. The i-th of these lines contains a string of length n, consisting of digits zero and one, specifying the contact list of the i-th employee. If the j-th character of the i-th string equals 1, then the j-th employee is in the i-th employee's contact list, otherwise he isn't. It is guaranteed that the i-th character of the i-th line is always equal to 1. The last line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ n), where ai represents the number of messages that the i-th employee should get according to Igor. Output In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute. In the second line print m space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 3 101 010 001 0 1 2 Output 1 1 Input 1 1 1 Output 0 Input 4 1111 0101 1110 0001 1 0 1 0 Output 4 1 2 3 4 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3. Submitted Solution: ``` n = int(input()) contacts_lists = [] for i in range(n): contacts_lists.append(input()) guesses = [ int(a) for a in input().split() ] p = [ i for i in range(n) ] igor_wins = False received_messages = [ 0 ] * n import itertools result_state = [] for attendees in range(n): if attendees == 0: for i in range(n): if guesses[i] == received_messages[i]: igor_wins = True break if igor_wins == False: break continue if igor_wins == False: break for states in itertools.combinations( p, attendees ): received_messages = [ 0 ] * n for state in states: # loop through states igor_wins = False if type(state) == int: state = [ state ] else: state = [ s for s in state ] result_state = state for coming in state: # loop through people in state for i in range(n): #loop through coming's contacts list if contacts_lists[coming][i] == '1': received_messages[i] += 1 for i in range(n): if guesses[i] == received_messages[i]: igor_wins = True break if igor_wins == False: break if igor_wins == False: break if igor_wins == False: break if igor_wins == False: print(len(result_state)) for person in result_state: print(person+1, end=" ") print() else: print(-1) ```
instruction
0
50,898
14
101,796
No
output
1
50,898
14
101,797
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,915
14
101,830
Tags: dp, greedy, sortings Correct Solution: ``` if __name__=='__main__': n = int(input()) orders = [] for _ in range(n): a,b = map(int,input().split()) orders.append((b,a)) sorders = sorted(orders) cnt = 0 now = 1 for i in range(0,len(sorders)): #print(now) if sorders[i][1]>now or i==0: cnt += 1 now = sorders[i][0] print(cnt) ```
output
1
50,915
14
101,831
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,916
14
101,832
Tags: dp, greedy, sortings Correct Solution: ``` s = d = 0 t = [list(map(int, input().split())) for i in range(int(input()))] for l, r in sorted(t, key=lambda q: q[1]): if l > d: s, d = s + 1, r print(s) # Made By Mostafa_Khaled ```
output
1
50,916
14
101,833
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,917
14
101,834
Tags: dp, greedy, sortings Correct Solution: ``` orders = [] for _ in range(int(input())): l, r = map(int, input().split()) orders.append([l, r]) orders.sort() count = 0 while len(orders) > 1: if orders[-2][0] == orders[-1][0]: orders.pop(-2) if orders[-2][1] > orders[-1][1] else orders.pop(-1) elif orders[-2][1] >= orders[-1][0]: orders.pop(-2) else: orders.pop(-1) count+=1 if len(orders) == 1: count+=1 print(count) ```
output
1
50,917
14
101,835
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,918
14
101,836
Tags: dp, greedy, sortings Correct Solution: ``` a=int(input()) b=[] total=0 for i in range(a): x,y=map(int,input().split()) b.append([x,y]) b.sort(key=lambda x: x[1]) ending=0 for i in b: if i[0]>ending: total+=1 ending=i[1] print(total) ```
output
1
50,918
14
101,837
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,919
14
101,838
Tags: dp, greedy, sortings Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def main(): n = int(input()) tup = sorted([tuple(map(int,input().split())) for _ in range(n)],key=lambda xx:xx[1]) ans,j = [0]*(n+1),0 for ind,i in enumerate(tup): while tup[j][1] < i[0]: ans[j+1] = max(ans[j+1],ans[j]) j += 1 ans[ind+1] = ans[j]+1 print(max(ans)) #Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == '__main__': main() ```
output
1
50,919
14
101,839
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,920
14
101,840
Tags: dp, greedy, sortings Correct Solution: ``` #!/usr/bin/env python3 n = int(input()) t = [list(map(int, input().split(" "))) for i in range(n)] last = 0 count = 0 for i in sorted(t, key=lambda x: x[1]): if last < i[0]: last = i[1] count += 1 print(count) ```
output
1
50,920
14
101,841
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,921
14
101,842
Tags: dp, greedy, sortings Correct Solution: ``` def main(): tt = list(tuple(map(int, input().split())) for _ in range(int(input()))) tt.sort(key=lambda e: e.__getitem__(1)) res = t = 0 for l, r in tt: if t < l: t = r res += 1 print(res) if __name__ == '__main__': main() ```
output
1
50,921
14
101,843
Provide tags and a correct Python 3 solution for this coding contest problem. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2
instruction
0
50,922
14
101,844
Tags: dp, greedy, sortings Correct Solution: ``` n = int(input()) a = [] for i in range(n): l,r = map(int,input().split()) a.append([l,r]) a.sort(key=lambda x:x[1]) tmp = a[0][1] tot = 1 for i in range(1,len(a)): if (tmp < a[i][0]): tot += 1 tmp = a[i][1] print(tot) ```
output
1
50,922
14
101,845
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` n=int(input()) l=[] for i in range(n): a,b = map(int,input().split()) l.append([a,b]) l.sort(key=lambda x: x[1]) c=t=0 for i in range(len(l)): if t<int(l[i][0]): t=int(l[i][1]) c+=1 print(c) ```
instruction
0
50,923
14
101,846
Yes
output
1
50,923
14
101,847
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` def snd(lst): return lst[1] n = int(input()) l = [] for i in range(0, n): l.append([int(i) for i in input().split()]) l.sort(key=snd) e = 0 ans = 0 for p in l: if (p[0] > e): ans+=1 e = p[1] print(ans) ```
instruction
0
50,924
14
101,848
Yes
output
1
50,924
14
101,849
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` n = int(input()) lis=[] for i in range(n): a , b = map(int,input().split()) lis.append([b,a]) lis.sort() ans=1 h=lis[0][0] for i in range(1,n): if lis[i][1]>h : h = lis[i][0] ans+=1 print(ans) ```
instruction
0
50,925
14
101,850
Yes
output
1
50,925
14
101,851
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` n = int(input()) array = [] for k in range(n): a = list(map(int,input().split())) array.append((a[1], a[0])) array.sort() c = 1 end = array[0][0] for k in range(1,n): if array[k][1] > end : c += 1 end = array[k][0] print(c) ```
instruction
0
50,926
14
101,852
Yes
output
1
50,926
14
101,853
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` n= int(input()) l = [list(map(int, input().split())) for _ in range(n)] l.sort() c = 0 s = 0 for x in l: if x[0] > c: c = x[1] s += 1 print(s) ```
instruction
0
50,927
14
101,854
No
output
1
50,927
14
101,855
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` times = [] for _ in range(int(input())): a,b = list(map(int,input().split())) times.append([a,b]) times.sort() a,b = times[0][0],times[0][1] cnt = 1 for i in range(1,len(times)): cur = times[i] start,end = cur[0],cur[1] if a<=start<=b or a<=end<=b: pass else: cnt+=1 a = start b = end print(cnt) ```
instruction
0
50,928
14
101,856
No
output
1
50,928
14
101,857
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` n=int(input()) l=[] for i in range(n): s=input() temp=s.split() temp1=[int(i) for i in temp] l.append(temp1) sorted(l,key=lambda x:x[1]) cnt=1 prev=l[0][1] for i in range(1,len(l)): if l[i][0]>prev: cnt=cnt+1 prev=l[i][1] print(cnt) ```
instruction
0
50,929
14
101,858
No
output
1
50,929
14
101,859
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri). Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept? No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both. Input The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109). Output Print the maximal number of orders that can be accepted. Examples Input 2 7 11 4 7 Output 1 Input 5 1 2 2 3 3 4 4 5 5 6 Output 3 Input 6 4 8 1 5 4 7 2 5 1 3 6 8 Output 2 Submitted Solution: ``` import sys from sys import stdin, stdout def R(): return map(int, stdin.readline().strip().split()) def findInd(arr, high, tar): low = 0 mid = (low+high)//2 while low <= high: mid = (low+high)//2 if arr[mid][1] == tar: break if arr[mid][1] > tar: high = mid-1 else: low = mid+1 if arr[mid][1] >= tar: mid -= 1 return mid arr = [] for h in range(int(stdin.readline().strip())): arr.append(list(R())) arr.sort(key = lambda x:x[1]) dp = [1 for i in range(len(arr))] dicti = {} for i in range(1, len(arr)): if arr[i][0] in dicti: dp[i] = max(dicti[arr[i][0]], dp[i-1]) continue k = findInd(arr, i, arr[i][0]) # print(k, dp[k]) if k < 0: dp[i] = dp[i-1] else: dp[i] = max(dp[i-1], dp[k]+1) dicti[arr[i][0]] = dp[i] # print(arr) # print(dp) stdout.write(str(dp[-1])) ```
instruction
0
50,930
14
101,860
No
output
1
50,930
14
101,861
Provide a correct Python 3 solution for this coding contest problem. Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game... Every zombie gets a number ni (1 ≤ ni ≤ N) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all N - 1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die! Zombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads? Given a zombie's rank R and the N - 1 numbers ni on the other attendees' foreheads, your program will have to return the number that the zombie of rank R shall guess. Those answers define your strategy, and we will check if it is flawless or not. Input The first line of input contains a single integer T (1 ≤ T ≤ 50000): the number of scenarios for which you have to make a guess. The T scenarios follow, described on two lines each: * The first line holds two integers, N (2 ≤ N ≤ 6), the number of attendees, and R (1 ≤ R ≤ N), the rank of the zombie who has to make the guess. * The second line lists N - 1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) Output For every scenario, output a single integer: the number that the zombie of rank R shall guess, based on the numbers ni on his N - 1 fellows' foreheads. Examples Input 4 2 1 1 2 2 1 2 1 2 2 2 2 Output 1 2 2 1 Input 2 5 2 2 2 2 2 6 4 3 2 6 1 2 Output 5 2 Note For instance, if there were N = 2 two attendees, a successful strategy could be: * The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. * The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1.
instruction
0
50,952
14
101,904
"Correct Solution: ``` def main(): T = int(input()) for _ in range(T): n, r = readints() nums = readints() print(sol(nums, r, n)) def readints(): return [int(fld) for fld in input().strip().split()] def sol(nums, r, n): ans = 1 + (r - sum(nums)) % n return ans main() ```
output
1
50,952
14
101,905
Provide a correct Python 3 solution for this coding contest problem. Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game... Every zombie gets a number ni (1 ≤ ni ≤ N) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all N - 1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die! Zombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads? Given a zombie's rank R and the N - 1 numbers ni on the other attendees' foreheads, your program will have to return the number that the zombie of rank R shall guess. Those answers define your strategy, and we will check if it is flawless or not. Input The first line of input contains a single integer T (1 ≤ T ≤ 50000): the number of scenarios for which you have to make a guess. The T scenarios follow, described on two lines each: * The first line holds two integers, N (2 ≤ N ≤ 6), the number of attendees, and R (1 ≤ R ≤ N), the rank of the zombie who has to make the guess. * The second line lists N - 1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) Output For every scenario, output a single integer: the number that the zombie of rank R shall guess, based on the numbers ni on his N - 1 fellows' foreheads. Examples Input 4 2 1 1 2 2 1 2 1 2 2 2 2 Output 1 2 2 1 Input 2 5 2 2 2 2 2 6 4 3 2 6 1 2 Output 5 2 Note For instance, if there were N = 2 two attendees, a successful strategy could be: * The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. * The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1.
instruction
0
50,953
14
101,906
"Correct Solution: ``` T = int(input()) for t in range(T): N, R = map(int, input().split()) print(1 + (R - sum(map(int, input().split()))) % N) ```
output
1
50,953
14
101,907
Provide a correct Python 3 solution for this coding contest problem. Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game... Every zombie gets a number ni (1 ≤ ni ≤ N) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all N - 1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die! Zombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads? Given a zombie's rank R and the N - 1 numbers ni on the other attendees' foreheads, your program will have to return the number that the zombie of rank R shall guess. Those answers define your strategy, and we will check if it is flawless or not. Input The first line of input contains a single integer T (1 ≤ T ≤ 50000): the number of scenarios for which you have to make a guess. The T scenarios follow, described on two lines each: * The first line holds two integers, N (2 ≤ N ≤ 6), the number of attendees, and R (1 ≤ R ≤ N), the rank of the zombie who has to make the guess. * The second line lists N - 1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) Output For every scenario, output a single integer: the number that the zombie of rank R shall guess, based on the numbers ni on his N - 1 fellows' foreheads. Examples Input 4 2 1 1 2 2 1 2 1 2 2 2 2 Output 1 2 2 1 Input 2 5 2 2 2 2 2 6 4 3 2 6 1 2 Output 5 2 Note For instance, if there were N = 2 two attendees, a successful strategy could be: * The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. * The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1.
instruction
0
50,954
14
101,908
"Correct Solution: ``` t = int(input()) for _ in range(t): n, r = map(int, input().split()) s = sum(map(int, input().split())) x = 1 while (s + x + r) % n != 0: x += 1 print(x) ```
output
1
50,954
14
101,909
Provide a correct Python 3 solution for this coding contest problem. Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game... Every zombie gets a number ni (1 ≤ ni ≤ N) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all N - 1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die! Zombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads? Given a zombie's rank R and the N - 1 numbers ni on the other attendees' foreheads, your program will have to return the number that the zombie of rank R shall guess. Those answers define your strategy, and we will check if it is flawless or not. Input The first line of input contains a single integer T (1 ≤ T ≤ 50000): the number of scenarios for which you have to make a guess. The T scenarios follow, described on two lines each: * The first line holds two integers, N (2 ≤ N ≤ 6), the number of attendees, and R (1 ≤ R ≤ N), the rank of the zombie who has to make the guess. * The second line lists N - 1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) Output For every scenario, output a single integer: the number that the zombie of rank R shall guess, based on the numbers ni on his N - 1 fellows' foreheads. Examples Input 4 2 1 1 2 2 1 2 1 2 2 2 2 Output 1 2 2 1 Input 2 5 2 2 2 2 2 6 4 3 2 6 1 2 Output 5 2 Note For instance, if there were N = 2 two attendees, a successful strategy could be: * The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. * The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1.
instruction
0
50,955
14
101,910
"Correct Solution: ``` t = int(input()) for i in range(t): n, r = map(int, input().split()) l = list(map(int, input().split())) if r == 1: s = sum(l) % n if s == 0: s = n print(s) else: s = 2 * l[0] - r + 1 - sum(l) s = s % n if s == 0: s = n print(s) ```
output
1
50,955
14
101,911
Provide a correct Python 3 solution for this coding contest problem. Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the N attendees love to play a very risky game... Every zombie gets a number ni (1 ≤ ni ≤ N) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all N - 1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die! Zombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads? Given a zombie's rank R and the N - 1 numbers ni on the other attendees' foreheads, your program will have to return the number that the zombie of rank R shall guess. Those answers define your strategy, and we will check if it is flawless or not. Input The first line of input contains a single integer T (1 ≤ T ≤ 50000): the number of scenarios for which you have to make a guess. The T scenarios follow, described on two lines each: * The first line holds two integers, N (2 ≤ N ≤ 6), the number of attendees, and R (1 ≤ R ≤ N), the rank of the zombie who has to make the guess. * The second line lists N - 1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) Output For every scenario, output a single integer: the number that the zombie of rank R shall guess, based on the numbers ni on his N - 1 fellows' foreheads. Examples Input 4 2 1 1 2 2 1 2 1 2 2 2 2 Output 1 2 2 1 Input 2 5 2 2 2 2 2 6 4 3 2 6 1 2 Output 5 2 Note For instance, if there were N = 2 two attendees, a successful strategy could be: * The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. * The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1.
instruction
0
50,956
14
101,912
"Correct Solution: ``` def main(): T = int(input()) for _ in range(T): n, r = readints() nums = readints() print(sol(nums, r, n)) def readints(): return [int(fld) for fld in input().strip().split()] def sol(nums, r, n): ans = 1 + (r - sum(nums) + 1) % n return ans main() ```
output
1
50,956
14
101,913
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,123
14
102,246
"Correct Solution: ``` def main(): import sys input=sys.stdin.readline n=int(input()) A=[list(map(int,input().split())) for i in range(n)] mod=10**9+7 dp=[0]*(1<<n) dp[0]=1 for s in range(1<<n): i=format(s,'b').count('1') for j in range(n): if (s>>j)&1 and A[i-1][j]==1: dp[s]+=dp[s^(1<<j)] dp[s]%=mod print(dp[-1]) if __name__=='__main__': main() ```
output
1
51,123
14
102,247
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,124
14
102,248
"Correct Solution: ``` def main(): n = int(input()) A = [list(map(int, input().split())) for i in range(n)] p = 10**9+7 DP = [0 for s in range(1 << n)] DP[0] = 1 for s in range(1, 1 << n): l = bin(s).count('1') for i in range(n): if ((1 << i) & s) * A[l-1][i]: DP[s] += DP[~(1 << i) & s] print(DP[(1 << n)-1] % p) main() ```
output
1
51,124
14
102,249
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,125
14
102,250
"Correct Solution: ``` import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) MOD = 10 ** 9 + 7 N = int(input()) A = [[int(x) for x in input().split()] for _ in range(N)] bit_count = [0] for _ in range(N): bit_count += [x+1 for x in bit_count] dp = [0] * (1<<N) dp[0] = 1 for i in range((1<<N)-1): k = bit_count[i] for c in range(N): if A[k][c]: dp[(1<<c)^i] += dp[i] answer = dp[-1] % MOD print(answer) ```
output
1
51,125
14
102,251
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,126
14
102,252
"Correct Solution: ``` def countSetBits(n): count = 0 while (n): n &= (n - 1) count += 1 return count N=10**9 + 7 n=int(input()) ar = [list(map(int,input().split())) for _ in range(n)] total=1<<n bits=[[]for _ in range(n+1)] dp=[[0]*(total+5) for j in range(n+5)] for i in range(n): if ar[0][i]==1: dp[0][1<<(n-1-i)]=1 for j in range(total): i=countSetBits(j)-1 for k in range(0,n): if ar[i][n-k-1]==1 and j&(1<<k): dp[i][j]=(dp[i][j]+dp[i-1][j&(~(1<<k))])%N print(dp[n-1][total-1]) ```
output
1
51,126
14
102,253
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,127
14
102,254
"Correct Solution: ``` MOD = 10**9 + 7 N = int(input()) As = [int(input().replace(' ', ''), 2) for _ in range(N)] patWsList = [[] for _ in range(N+1)] for patW in range(2**N): num1 = sum([(patW >> i) & 1 for i in range(N)]) patWsList[num1].append(patW) dp = [0] * (2**N) dp[-1] = 1 for iM, (A, patWs) in enumerate(zip(As, reversed(patWsList))): for patW in patWs: A2 = A & patW dpNow = dp[patW] for iW in range(N): if A2 & (1 << iW): dp[patW ^ (1 << iW)] += dpNow dp[patW ^ (1 << iW)] %= MOD print(dp[0]) ```
output
1
51,127
14
102,255
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,128
14
102,256
"Correct Solution: ``` N = int(input()) A = [[int(a) for a in input().split()] for _ in range(N)] mod = 10**9+7 dp = [0]*(2**N) dp[0] = 1 POPCOUNT_TABLE16 = [0] * 2**16 for index in range(len(POPCOUNT_TABLE16)): POPCOUNT_TABLE16[index] = (index & 1) + POPCOUNT_TABLE16[index >> 1] def popcnt(v): return (POPCOUNT_TABLE16[ v & 0xffff] + POPCOUNT_TABLE16[(v >> 16) & 0xffff] + POPCOUNT_TABLE16[(v >> 32) & 0xffff] + POPCOUNT_TABLE16[(v >> 48) ]) for s in range(1, 2**N): i = popcnt(s) cnt = 0 for j in range(N): if A[i-1][j] == 1 and s&(1<<j): cnt += dp[s^(1<<j)]%mod dp[s] = cnt%mod print(dp[-1]%mod) ```
output
1
51,128
14
102,257
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,129
14
102,258
"Correct Solution: ``` # coding: utf-8 # Your code here! import sys input = sys.stdin.readline def itercomb(n,k): _a = (1<<k)-1 yield _a while 1: _x = _a & -_a _y = _a + _x _a = (((_a & ~_y) // _x) >> 1) | _y if _a >= _ALL: break yield _a n=int(input()) A= [[int(i) for i in input().split()] for j in range(n)] MOD=10**9+7 _ALL=1<<n dp = [0]*(_ALL) dp[0] = 1 for m in range(1,n+1): for a in itercomb(n,m): for c,i in enumerate(A[m-1]): if (1<<c)&a and i: dp[a] += dp[a^(1<<c)] if dp[a] > MOD: dp[a] -= MOD #print(dp) print(dp[2**n-1]) ```
output
1
51,129
14
102,259
Provide a correct Python 3 solution for this coding contest problem. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160
instruction
0
51,130
14
102,260
"Correct Solution: ``` N = int(input()) A = [list(map(int, input().split())) for i in range(N)] MOD = 10 ** 9 + 7 dp = [0] * (1<<N) dp[0] = 1 for i in range(1, 1<<N): cnt = 0 for j in range(N+1):cnt += (i>>j)&1 for j in range(N): if (i>>j)&1 == 0: continue dp[i] += dp[i - (1<<j)] * A[cnt-1][j] dp[i] %= MOD print(dp[-1]) ```
output
1
51,130
14
102,261
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` def main(): N=int(input()) a=[] for i in range(N): a.append(list(map(int,input().split()))) mod=10**9+7 dp=[0]*(2**N) dp[0]=1 for i in range(1,2**N): m=[j for j in range(N) if i>>j &1==1] for j in m: dp[i]+=dp[i-2**j]*a[len(m)-1][j] dp[i]%=mod print(dp[-1]) if __name__=="__main__": main() ```
instruction
0
51,131
14
102,262
Yes
output
1
51,131
14
102,263
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` n=int(input()) A=[int(input().replace(' ',''),2)for _ in[0]*n] p=[0]*2**n p[0]=1 for I in range(2**n-1): a=A[bin(I).count('1')]&~I for j in range(n): if a&1<<j:p[I|1<<j]+=p[I] print(p[-1]%(10**9+7)) ```
instruction
0
51,132
14
102,264
Yes
output
1
51,132
14
102,265
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` import itertools mod=10**9+7 n=int(input()) arr=[list(map(int,input().split())) for _ in range(n)] dp=[[0]*(2**n) for _ in range(n+1)] dp[0][0]=1 for i in range(n): for bit in range(2**n): if dp[i][bit]!=0: for j in range(n): if bit&(2**j)==0 and arr[i][j]==1: dp[i+1][bit^(2**j)]+=dp[i][bit] dp[i+1][bit^(2**j)]%=mod print(dp[n][2**n-1]) ```
instruction
0
51,133
14
102,266
Yes
output
1
51,133
14
102,267
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` P = 10 ** 9 + 7 N = int(input()) X = [input()[::2] for i in range(N)] Y = [-1] * (1<<N) def calc(x, n): if Y[x] >= 0: return Y[x] if n < 0: return 1 ret = 0 for j in range(N): if x & (1<<j) and X[n][j] == "1": ret += calc(x-(1<<j), n-1) ret %= P Y[x] = ret return ret print(calc((1<<N)-1, N-1)) ```
instruction
0
51,134
14
102,268
Yes
output
1
51,134
14
102,269
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` import sys sys.setrecursionlimit(2147483647) INF = float("inf") MOD = 10**9 + 7 # 998244353 input = lambda:sys.stdin.readline().rstrip() from collections import defaultdict def resolve(): n = int(input()) # dp[i][U] : 男 i 番目まで見た時、埋まっている女性の state が U の場合の数 dp = {0 : 1} for _ in range(n): A = list(map(int, input().split())) ndp = defaultdict(int) for i in range(n): # match させる女性 if not A[i]: continue for U, val in dp.items(): if (U >> i) & 1: continue nU = U | 1 << i ndp[nU] += val if ndp[nU] >= MOD: ndp[nU] -= MOD dp = ndp print(sum(dp.values()) % MOD) resolve() ```
instruction
0
51,135
14
102,270
No
output
1
51,135
14
102,271
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` from collections import defaultdict n = int(input()) dp = {0: 1} MOD = 10 ** 9 + 7 for i in range(n): compatibilities = [1 << i for i, v in enumerate(input().split()) if v == '1'] ndp = defaultdict(lambda: 0) for k, p in dp.items(): for c in compatibilities: if k & c: continue ndp[k | c] += p ndp[k | c] %= MOD dp = ndp # print(i, len(dp)) print(sum(dp.values()) % MOD) ```
instruction
0
51,136
14
102,272
No
output
1
51,136
14
102,273
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` dp = {} mod = 10**9+7 def getAns(st, wom, comp, n): if wom == 0 and st >= n: return 1 if wom > 0 and st >= n: return 0 if (st,wom) in dp: return dp[(st, wom)] ans = 0 for i in range(n): if comp[st][i] == 1 and (1<<i & wom): ans = (ans+getAns(st+1, wom^(1<<i), comp, n))%mod dp[(st, wom)] = ans return ans n = int(input()) comp = [list(map(int, input().strip().split())) for i in range(n)] print(getAns(0, (1<<n)-1, comp, n)) ```
instruction
0
51,137
14
102,274
No
output
1
51,137
14
102,275
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N men and N women, both numbered 1, 2, \ldots, N. For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not. Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair. Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Constraints * All values in input are integers. * 1 \leq N \leq 21 * a_{i, j} is 0 or 1. Input Input is given from Standard Input in the following format: N a_{1, 1} \ldots a_{1, N} : a_{N, 1} \ldots a_{N, N} Output Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7. Examples Input 3 0 1 1 1 0 1 1 1 1 Output 3 Input 4 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 Output 1 Input 1 0 Output 0 Input 21 0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 Output 102515160 Submitted Solution: ``` n = int(input()) g=[] for i in range(n): g.append(list(map(int,input().split()))) mod=10**9+7 def dp(p,vw): if p==n: return 1 k=0 for j in range(n): if g[p][j]==1 and vw[j]==0: #print(p,j) vw[j]=1 k+=dp(p+1,vw) k%=mod #print(vw) vw[j]=0 return k%mod viw=[0]*n print(dp(0,viw)) ```
instruction
0
51,138
14
102,276
No
output
1
51,138
14
102,277
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,304
14
102,608
Tags: math, number theory Correct Solution: ``` from sys import stdin, stdout from math import gcd def main(): l_a, r_a, t_a = readline() l_b, r_b, t_b = readline() lucky_days_a = r_a - l_a + 1 # Total de dias de suerte de Alicia por periodo lucky_days_b = r_b - l_b + 1 # Total de dias de suerte de Bob por periodo gcd_ = gcd(t_a, t_b) # Maximo comun divisor de los periodos return min(lucky_days_a, lucky_days_b, max(lucky_days_a - (l_b - l_a) % gcd_, lucky_days_b - (l_a - l_b) % gcd_, 0)) # Se calcula la expresion def readline(): # Metodo para leer una linea completa, dividirla en elementos y convertirlos en numeros enteros return map(int, stdin.readline().strip().split()) if __name__ == '__main__': stdout.write(str(main()) + '\n') ```
output
1
51,304
14
102,609
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,305
14
102,610
Tags: math, number theory Correct Solution: ``` from math import gcd def solve(la, ra, ta, lb, rb, tb): if la > lb: la, ra, ta, lb, rb, tb = lb, rb, tb, la, ra, ta da = ra-la db = rb-lb ans = 0 dist = lb - la g = gcd(ta, tb) dist %= g if dist == 0: return min(da+1, db+1) la, ra = lb - dist, lb - dist + da ans = max(ans, 1+min(rb, ra) - lb) ans = max(ans, 1+min(rb, ra+g) - (la+g)) return ans def main(): la, ra, ta = [int(i) for i in input().split()] lb, rb, tb = [int(i) for i in input().split()] print(solve(la, ra, ta, lb, rb, tb)) main() ```
output
1
51,305
14
102,611
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,306
14
102,612
Tags: math, number theory Correct Solution: ``` # -*- coding:utf-8 -*- """ created by shuangquan.huang at 11/13/18 mt19937 mrand(random_device{} ()); int rnd(int x) { return mrand() % x; } void precalc() { } int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); } int la, ra, ta; int lb, rb, tb; int read() { if (scanf("%d%d%d%d%d%d", &la, &ra, &ta, &lb, &rb, &tb) < 6) { return false; } return true; } void solve() { int g = gcd(ta, tb); int l0 = la % g, r0 = l0 + (ra - la); int l1 = lb % g, r1 = l1 + (rb - lb); int res = 0; { int l = max(l0, l1), r = min(r0, r1); res = max(res, r - l + 1); } { int l = max(l0, l1 + g), r = min(r0, r1 + g); res = max(res, r - l + 1); } { int l = max(l0 + g, l1), r = min(r0 + g, r1); res = max(res, r - l + 1); } printf("%d\n", res); } int main() { precalc(); #ifdef DEBUG assert(freopen(TASK ".in", "r", stdin)); assert(freopen(TASK ".out", "w", stdout)); #endif while (read()) { solve(); #ifdef DEBUG eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC); #endif } return 0; } """ la, ra, ta = map(int, input().split()) lb, rb, tb = map(int, input().split()) def gcd(x, y): while y: x, y = y, x % y return x # [la+ka*ta, ...] # [lb+kb*tb, ...] g = gcd(ta, tb) l0 = la % g l1 = lb % g r0 = l0 + ra - la r1 = l1 + rb - lb res = 0 l = max(l0, l1) r = min(r0, r1) res = max(res, r-l+1) l = max(l0, l1+g) r = min(r0, r1+g) res = max(res, r-l+1) l = max(l0+g, l1) r = min(r0+g, r1) res = max(res, r-l+1) print(res) ```
output
1
51,306
14
102,613
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,307
14
102,614
Tags: math, number theory Correct Solution: ``` '''input 7 13 18 2 6 12 ''' import math def solve(): l1,r1,t1 = list(map(int,input().split())) l2,r2,t2 = list(map(int,input().split())) # diff = l1-l2+k1*t1-k2*t2 # diff = l1-l2 + K*gcd(t1,t2) # l2-l1 / gcd(t1,t2) = K g = math.gcd(t1,t2) K1 = (l2-l1)//g K2 = (l2-l1)//g if((l2-l1)%g!=0): K2+=1 diff1 = l1-l2 + K1*g diff2 = l1-l2 + K2*g if t1==t2: print(min(r1,r2)-max(l1,l2)+1) return #print(diff1,diff2) ans = 0 for i in [diff1,diff2]: l22 = l2+i r22 = l22+r1-l1 ans = max(ans,min(r22,r2)-max(l22,l2)+1) print(ans) return t = 1 #t = int(input()) while t>0: t-=1 solve() ```
output
1
51,307
14
102,615
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,308
14
102,616
Tags: math, number theory Correct Solution: ``` def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) import math la, ra, ta = mi() lb, rb, tb = mi() if la > lb: la, ra, ta, lb, rb, tb = lb, rb, tb, la, ra, ta lb -= la ra -= la rb -= la dif = math.gcd(ta, tb) difl = lb off1 = difl % dif ans1 = min(ra, off1 + rb - lb) - max(0, off1) + 1 off2 = off1 - dif ans2 = min(ra, off2 + rb - lb) - max(0, off2) + 1 ans = max(ans1, ans2, 0) print(ans) ```
output
1
51,308
14
102,617
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,309
14
102,618
Tags: math, number theory Correct Solution: ``` from math import gcd lA,rA,tA=map(int,input().split()) lB,rB,tB=map(int,input().split()) rA+=1 rB+=1 move=gcd(tA,tB) if(lA>lB): lA,rA,tA,lB,rB,tB=lB,rB,tB,lA,rA,tA d=(lB-lA)//move lA+=(d*move) rA+=(d*move) first=min(rA,rB)-max(lA,lB) lA+=move rA+=move second=min(rA,rB)-max(lA,lB) print(max(0,first,second)) ```
output
1
51,309
14
102,619
Provide tags and a correct Python 3 solution for this coding contest problem. Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k. The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days. The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Examples Input 0 2 5 1 3 5 Output 2 Input 0 1 3 2 3 6 Output 1 Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. <image> <image>
instruction
0
51,310
14
102,620
Tags: math, number theory Correct Solution: ``` from math import gcd r = lambda: list(map(int, input().split())) a, b = r(), r() c = b[0] > a[0] la, ra, ta = a if c else b lb, rb, tb = b if c else a g = gcd(ta, tb) lna, lnb = ra - la + 1, rb - lb + 1 d = lb - la - (lb - la) // g * g print(max(0, min(lna - d, lnb), min(lna, lnb - g + d))) ```
output
1
51,310
14
102,621