message stringlengths 2 45.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 254 108k | cluster float64 3 3 | __index_level_0__ int64 508 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0. | instruction | 0 | 19,796 | 3 | 39,592 |
Tags: bitmasks, brute force, combinatorics, dp, math, probabilities
Correct Solution:
```
MyList,Res = [0] , 0
First = input()
Second = input()
for i in range(len(First)):
Temp = []
Res += 1 if First[i] == "+" else -1
if '?' not in First[i] + Second[i]:
for P in MyList:
Temp.append(P + 1 if Second[i] == "+" else P - 1)
else:
for P in MyList:
Temp.append(P + 1)
Temp.append(P - 1)
MyList = Temp[:]
print(MyList.count(Res) / len(MyList))
``` | output | 1 | 19,796 | 3 | 39,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
#!/usr/bin/env python3
def main():
commands = list(input())
wifi = list(input())
possibilities = []
def count(i, cnt, s):
if len(s) == cnt:
possibilities.append(i)
return
if s[cnt] == '+':
count(i+1, cnt+1, s)
elif s[cnt] == '-':
count(i-1, cnt+1, s)
else:
count(i+1, cnt+1, s)
count(i-1, cnt+1, s)
endpoint = sum((1 if i == '+' else -1 for i in commands))
count(0,0,wifi)
total, chances = len(possibilities), sum((endpoint==i for i in possibilities))
print(chances / total)
if __name__ == "__main__":
main()
``` | instruction | 0 | 19,797 | 3 | 39,594 |
Yes | output | 1 | 19,797 | 3 | 39,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
s1 = input()
s2 = input()
final = 0
for i in s1:
if i == '+':
final += 1
else:
final -= 1
val = [0]
for i in s2:
l = len(val)
for j in range(l):
if i == '+':
val[j] += 1
elif i == '-':
val[j] -= 1
else:
val[j] -= 1
val.append(val[j]+2)
# print(val)
count = 0
for i in val:
if i == final:
count += 1
print(count/len(val))
``` | instruction | 0 | 19,798 | 3 | 39,596 |
Yes | output | 1 | 19,798 | 3 | 39,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
import math
s=str(input())
s1=str(input())
a=0
a1=0
c1=0
for i in range(len(s1)):
if s[i]=='+':
a+=1
else:
a-=1
if s1[i]=='+':
a1+=1
elif s1[i]=='-':
a1-=1
else:
c1+=1;
a-=a1;
if a== 0 and c1==0:
print("%.20f" % 1.0000000000000000000);
exit()
a=abs(a);
if a>c1:
print("%.20f" % 0.0);
exit()
a=(a+c1)//2
a1=math.factorial(c1)//math.factorial(c1-a)//math.factorial(a);
ans=a1/2**c1
print("%.20f" % ans);
``` | instruction | 0 | 19,799 | 3 | 39,598 |
Yes | output | 1 | 19,799 | 3 | 39,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
def calc(s):
contador = 0
for c in s:
if c == '+':
contador += 1
else:
contador -= 1
return contador
if __name__ == '__main__':
string_um = input()
string_dois = input()
v = calc(string_um)
p = []
n = len(string_dois)
m = 0
for i in range(n):
if string_dois[i] == '?':
p.append(i)
m += 1
count = 0
for i in range(1 << m):
string_tres = list(string_dois)
for j in range(m):
if i & (1 << j):
string_tres[p[j]] = '+'
else:
string_tres[p[j]] = '-'
if calc(string_tres) == v:
count += 1
print("{:.9f}".format(count / (1 << m)))
``` | instruction | 0 | 19,800 | 3 | 39,600 |
Yes | output | 1 | 19,800 | 3 | 39,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
s = input()
t = input()
a = s.count("+")
b = s.count("-")
c = t.count("?")
d = t.count("+")
e = t.count("-")
facts = [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
if c == 0:
if a==d and e==b:
print("1.0")
else:
print("0.0")
else:
if e>b or d>a:
print("0.0")
else:
r_p = a - d
print(r_p)
r_n = b - e
r = ((facts[c-1]/facts[c-1-r_p])/(2**c))
print(r)
``` | instruction | 0 | 19,801 | 3 | 39,602 |
No | output | 1 | 19,801 | 3 | 39,603 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
# Problem: B. Dreamoon and WiFi
# Contest: Codeforces - Codeforces Round #272 (Div. 2)
# URL: https://codeforces.com/problemset/problem/476/B
# Memory Limit: 256 MB
# Time Limit: 1000 ms
#
# Powered by CP Editor (https://cpeditor.org)
def fun(i,c):
if c==s and i!=0:
global ans
ans+=1
if i==n:
return
else:
fun(i+1,c+1)
fun(i+1,c-1)
from collections import Counter
A=input()
B=input()
s=A.count("+")-A.count("-")
s+=B.count("-")-B.count("+")
n=B.count("?")
if n==0:
print(1)
else:
global ans
ans=0
fun(0,0)
print(round(ans/(2**n),9))
``` | instruction | 0 | 19,802 | 3 | 39,604 |
No | output | 1 | 19,802 | 3 | 39,605 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
s1 = list(input())
s2 = list(input())
original = 0
sent = 0
marks = s2.count('?')
for x in s1:
if x == '+':
original += 1
else:
original -= 1
if marks == 0:
for x in s2:
if x == '+':
sent += 1
else:
sent -= 1
if original == sent:
print('1')
else:
print(0)
else:
for x in s2:
if x == '+':
sent += 1
else:
original += 1
y = original - sent
if y == 0:
if marks % 2 == 0:
print(0.5)
else:
print(0)
elif y > 0:
if marks >= y:
marks -= y
if marks % 2 == 0:
print(0.5)
else:
print(0)
else:
print(0)
else:
if marks >= abs(y):
marks -= abs(y)
if marks % 2 == 0:
print(0.5)
else:
print(0)
else:
print(0)
``` | instruction | 0 | 19,803 | 3 | 39,606 |
No | output | 1 | 19,803 | 3 | 39,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.
Each command is one of the following two types:
1. Go 1 unit towards the positive direction, denoted as '+'
2. Go 1 unit towards the negative direction, denoted as '-'
But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).
You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?
Input
The first line contains a string s1 β the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+', '-'}.
The second line contains a string s2 β the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+', '-', '?'}. '?' denotes an unrecognized command.
Lengths of two strings are equal and do not exceed 10.
Output
Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.
Examples
Input
++-+-
+-+-+
Output
1.000000000000
Input
+-+-
+-??
Output
0.500000000000
Input
+++
??-
Output
0.000000000000
Note
For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position + 1.
For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++", "+-+-", "+--+", "+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.
For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position + 3 is 0.
Submitted Solution:
```
from itertools import permutations
s = input()
d = input()
def kf(s):
cnt = 0
for i in range(len(s)):
if s[i]=="+":
cnt+=1
elif s[i]=="-":
cnt-=1
return cnt
cnt1 = kf(s)
cnt2 = kf(d)
cnt3 = 0
for i in d:
if i=="?":
cnt3+=1
b = cnt1-cnt2
e = set()
for i in range(cnt3+1):
a = i*"+"+(cnt3-i)*"-"
for i in permutations(a):
r = i.count("-")*-1+i.count("+")*1
if r==b:
e.add(i)
if e:
print(1/len(e))
else:
print(0)
``` | instruction | 0 | 19,804 | 3 | 39,608 |
No | output | 1 | 19,804 | 3 | 39,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,855 | 3 | 39,710 |
Tags: implementation, math
Correct Solution:
```
x1, y1 = [int(t) for t in input().split()]
x2, y2 = [int(t) for t in input().split()]
dx = abs(x2 - x1)
dy = abs(y2 - y1)
print(max(dx, dy))
``` | output | 1 | 19,855 | 3 | 39,711 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times. | instruction | 0 | 19,860 | 3 | 39,720 |
Tags: implementation, math
Correct Solution:
```
a,b = list(map(int, input().split()))
x,y = list(map(int, input().split()))
if abs(x-a) < abs(y-b):
dist = abs(x-a)
dist += abs(y - b) - dist
else:
dist = abs(y-b)
dist += abs(x - a) - dist
print(dist)
``` | output | 1 | 19,860 | 3 | 39,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
x, y = map(int, input().split())
x2, y2 = map(int, input().split())
print(max(max(x, x2)-min(x, x2), (max(y, y2)-min(y, y2))))
``` | instruction | 0 | 19,863 | 3 | 39,726 |
Yes | output | 1 | 19,863 | 3 | 39,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
a, b=map(int, input().split())
c, d=map(int, input().split())
g=abs(c-a)
h=abs(d-b)
x=max(g, h)
y=min(g, h)
z=abs(x-y)
if g==h:
print(g)
else:
print(y+z)
``` | instruction | 0 | 19,864 | 3 | 39,728 |
Yes | output | 1 | 19,864 | 3 | 39,729 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
import math
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
d = 0
xd = math.fabs(x1-x2)
yd = math.fabs(y1-y2)
d = int(max(xd ,yd))
print(d)
``` | instruction | 0 | 19,865 | 3 | 39,730 |
Yes | output | 1 | 19,865 | 3 | 39,731 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
def c(a,b):
if a-b>0:
return a-b
else:
return b-a
x,y=[int(i) for i in input().split()]
k,l=[int(i) for i in input().split()]
d=c(y,l)
e=c(x,k)
if d>e:
print(d)
else:
print(e)
``` | instruction | 0 | 19,866 | 3 | 39,732 |
Yes | output | 1 | 19,866 | 3 | 39,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
print(max(x2, y2) - min(x1, y1))
``` | instruction | 0 | 19,867 | 3 | 39,734 |
No | output | 1 | 19,867 | 3 | 39,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
[x, y,] = map(int, input().split())
[xr, yr] = map(int, input().split())
if (x > y):
a = (x-y)
while (x > y):
y = y + 1
b = (x - xr)
print (abs(b))
if (x == y):
print (x - xr)
if (y > x):
a = (y - x)
while (y > x):
x = x + 1
b = (y - yr)
print (abs(b))
``` | instruction | 0 | 19,868 | 3 | 39,736 |
No | output | 1 | 19,868 | 3 | 39,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
start = input().split()
finish = input().split()
x1, y1 = int(start[0]), int(start[1])
x2, y2 = int(finish[0]), int(finish[1])
if x1 == x2 and y1 == y2:
print(0)
elif x1 == x2 and y1 != y2:
print(max(y1, y2) - min(y1, y2))
elif x1 != x2 and y1 == y2:
print(max(x1, x2) - min(x1, x2))
elif x1 < x2 and y1 < y2:
c = 0
c += min(x2, y2)
x1 += c; y1 += c
c += x2 - x1
c += y2 - y1
print(c)
elif x1 > x2 and y1 > y2:
c = 0
c += max(x2, y2)
x1 -= c; y1 -= c
c += x1 - x2
c += y1 - y2
print(c)
elif x1 > x2 and y1 < y2:
c = 0
while x1 != x2 and y1 != y2:
c += 1
x2 += 1; y2 -= 1
print(c)
elif x1 < x2 and y1 > y2:
c = 0
while x1 != x2 and y1 != y2:
c += 1
x2 -= 1; y2 += 1
print(c)
``` | instruction | 0 | 19,869 | 3 | 39,738 |
No | output | 1 | 19,869 | 3 | 39,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.
Input
The first line contains two integers x1, y1 ( - 109 β€ x1, y1 β€ 109) β the start position of the robot.
The second line contains two integers x2, y2 ( - 109 β€ x2, y2 β€ 109) β the finish position of the robot.
Output
Print the only integer d β the minimal number of steps to get the finish position.
Examples
Input
0 0
4 5
Output
5
Input
3 4
6 1
Output
3
Note
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.
In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.
Submitted Solution:
```
x1,y1=map(int,input().split())
x2,y2=map(int,input().split())
ans=abs(x1-x2)
if y1>y2:
y1-=ans
else:
y1+=ans
ans+=abs(y1-y2)
print(ans)
``` | instruction | 0 | 19,870 | 3 | 39,740 |
No | output | 1 | 19,870 | 3 | 39,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,872 | 3 | 39,744 |
Tags: implementation
Correct Solution:
```
import itertools
import bisect
import math
from collections import *
import os
import sys
from io import BytesIO, IOBase
ii = lambda: int(input())
lmii = lambda: list(map(int, input().split()))
li = lambda: list(input())
mii = lambda: map(int, input().split())
msi = lambda: map(str, input().split())
def main():
n,m,q=mii()
qq=[]
for test in range(q):
pp=lmii()
qq.append(pp)
qq.reverse()
mat=[]
for i in range(n):
mat.append([0]*m)
for i in range(q):
lst = qq[i]
if lst[0] == 3:
mat[lst[1]-1][lst[2]-1] = lst[3]
elif lst[0] == 2:
d = deque([])
for k in range(n):
d.append(mat[k][lst[1]-1])
d.appendleft(d.pop())
for k in range(n):
mat[k][lst[1]-1]=d[k]
else:
d = deque([])
for k in range(m):
d.append(mat[lst[1]-1][k])
d.appendleft(d.pop())
for k in range(m):
mat[lst[1]-1][k] = d[k]
for i in range(n):
print(*mat[i])
pass
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 | 19,872 | 3 | 39,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,873 | 3 | 39,746 |
Tags: implementation
Correct Solution:
```
def w(ind):
global mtx, n, m
box = mtx[ind][-1]
for i in range(m - 1 - 1, -1, -1):
mtx[ind][i + 1] = mtx[ind][i]
mtx[ind][0] = box
def h(ind):
global mtx, n, m
box = mtx[-1][ind]
for i in range(n - 1 - 1, -1, -1):
mtx[i + 1][ind] = mtx[i][ind]
mtx[0][ind] = box
global mtx, n, m
n, m, q = [int(i) for i in input().split()]
mtx = [[0 for i in range(m)] for j in range(n)]
data = [[] for i in range(q)]
for i in range(q):
data[q - i - 1] = [int(j) for j in input().split()]
p = 0
for i in data:
cmd = i[0]
ind = 0
x, y = 0, 0
box = 0
if p and cmd == 1:
ind = i[1] - 1
w(ind)
elif p and cmd == 2:
ind = i[1] - 1
h(ind)
elif cmd == 3:
x, y = i[1] - 1, i[2] - 1
box = i[3]
mtx[x][y] = box
p = 1
for i in mtx:
for j in i:
print(j, end = ' ')
print()
``` | output | 1 | 19,873 | 3 | 39,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,874 | 3 | 39,748 |
Tags: implementation
Correct Solution:
```
n,m,q=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(q)][::-1]
adj=[[0]*m for _ in range(n)]
for i in range(q):
t=l[i]
if t[0]==3:
adj[t[1]-1][t[2]-1]=t[3]
elif t[0]==2:
j=t[1]-1
x=adj[-1][j]
for i in range(n-1,0,-1):
adj[i][j]=adj[i-1][j]
adj[0][j]=x
else:
adj[t[1]-1]=[adj[t[1]-1][-1]]+adj[t[1]-1][:-1]
for x in adj:
print(*x)
``` | output | 1 | 19,874 | 3 | 39,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,875 | 3 | 39,750 |
Tags: implementation
Correct Solution:
```
n, m, qq = map(int, input().split())
queries = []
for i in range(qq):
queries.append(list(map(int, input().split())))
G = [[0] * m for i in range(n)]
for q in reversed(queries):
if q[0] == 1:
last = G[q[1] - 1][-1]
for j in range(m - 2, -1, -1):
G[q[1] - 1][j + 1] = G[q[1] - 1][j]
G[q[1] - 1][0] = last
if q[0] == 2:
last = G[-1][q[1] - 1]
for j in range(n - 2, -1, -1):
G[j + 1][q[1] - 1] = G[j][q[1] - 1]
G[0][q[1] - 1] = last
if q[0] == 3:
G[q[1] - 1][q[2] - 1] = q[3]
# for i in range(n):
# print(*G[i])
for i in range(n):
print(*G[i])
``` | output | 1 | 19,875 | 3 | 39,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,876 | 3 | 39,752 |
Tags: implementation
Correct Solution:
```
nmq = input().split(' ')
n, m, q = int(nmq[0]), int(nmq[1]), int(nmq[2])
mt = []
for i in range(0, n):
mt.append([])
for j in range(0, m):
mt[-1].append((i, j))
res = []
for i in range(0, n):
res.append([])
for j in range(0, m):
res[-1].append(0)
for i in range(0, q):
ins = input().split(' ')
if ins[0] == '1':
r = int(ins[1]) - 1
b = mt[r][0]
for j in range(0, m-1):
mt[r][j] = mt[r][j+1]
mt[r][m-1] = b
if ins[0] == '2':
c = int(ins[1]) - 1
b = mt[0][c]
for j in range(0, n-1):
mt[j][c] = mt[j+1][c]
mt[n-1][c] = b
if ins[0] == '3':
r = int(ins[1]) - 1
c = int(ins[2]) - 1
x = int(ins[3])
p = mt[r][c]
res[p[0]][p[1]] = x
for i in range(0, n):
for j in range(0, m-1):
print(res[i][j],' ', end='')
print(res[i][-1])
``` | output | 1 | 19,876 | 3 | 39,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,877 | 3 | 39,754 |
Tags: implementation
Correct Solution:
```
def main():
n, m, q = map(int, input().split())
nm, qq = n * m, [input() for _ in range(q)]
res = ["0"] * nm
for s in reversed(qq):
k, *l = s.split()
if k == "3":
res[(int(l[0]) - 1) * m + int(l[1]) - 1] = l[2]
elif k == "2":
j = int(l[0]) - 1
*res[j + m:nm:m], res[j] = res[j:nm:m]
else:
j = (int(l[0]) - 1) * m
*res[j + 1:j + m], res[j] = res[j:j + m]
for i in range(0, nm, m):
print(' '.join(res[i:i + m]))
if __name__ == "__main__":
main()
``` | output | 1 | 19,877 | 3 | 39,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,878 | 3 | 39,756 |
Tags: implementation
Correct Solution:
```
def C():
d = input().split(" ")
n = int(d[0])
m = int(d[1])
s = int(d[2])
z = []
matr = []
for i in range(n):
cur = []
for j in range(m):
cur.append(0)
matr.append(cur)
for i in range(s):
zapr = []
cur = input().split(" ")
zapr = list(map(int,cur))
z.append(zapr)
for i in range(s):
a = z[s - i -1]
if(a[0] == 1):
matr = shift_r(matr, a[1]-1,m)
if(a[0] == 2):
matr = shift_d(matr,a[1]-1,n)
if(a[0] == 3):
matr[a[1]-1][a[2]-1] = a[3]
for i in range(n):
cur = ""
for j in range(m):
cur += str(matr[i][j]) + " "
print(cur)
def shift_r(mm, pos = 0, n = 0):
curr = int(mm[pos][-1])
for i in range(n -1):
mm[pos][n - i - 1] = mm[pos][n - i - 2]
mm[pos][0] = curr
return mm
def shift_d(mm, pos = 0, n = 0):
curr = int(mm[-1][pos])
for i in range(n - 1):
mm[n - i - 1][pos] = mm[n - i - 2][pos]
mm[0][pos] = curr
return mm
C()
``` | output | 1 | 19,878 | 3 | 39,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0 | instruction | 0 | 19,879 | 3 | 39,758 |
Tags: implementation
Correct Solution:
```
m,n,q = map(int,input().split())
inps = [map(lambda x: int(x)-1,input().split()) for _ in range(q)]
matrix = [[-1]*n for _ in range(m)]
for x in reversed(inps):
t,c,*cc = x
if t == 0:
matrix[c] = [matrix[c][-1]] + matrix[c][:-1]
elif t == 1:
new = [matrix[i][c] for i in range(m)]
for i,x in enumerate([new[-1]] + new[:-1]):
matrix[i][c] = x
elif t == 2:
matrix[c][cc[0]] = cc[1]
for x in matrix:
print(' '.join(map(lambda v: str(v+1), x)))
``` | output | 1 | 19,879 | 3 | 39,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
n, m, q = [int(i) for i in input().split()]
s = [[0 for i in range(m)] for i in range(n)]
a = []
b = []
c = []
d = []
for i in range(q):
o = [int(i) for i in input().split()]
d.append(o[0])
if o[0] == 1:
a.append(o[1]-1)
if o[0] == 2:
b.append(o[1]-1)
if o[0] == 3:
c.append(o[1:])
for i in d[::-1]:
if i == 3:
s[c[-1][0]-1][c[-1][1]-1] = c[-1][2]
c.pop()
if i == 2:
y = s[-1][b[-1]]
for j in range(n-1, 0, -1):
s[j][b[-1]] = s[j-1][b[-1]]
s[0][b[-1]] = y
b.pop()
if i == 1:
s[a[-1]]=[s[a[-1]][-1]] + s[a[-1]][:-1]
a.pop()
for i in range(n):
print(*s[i])
``` | instruction | 0 | 19,880 | 3 | 39,760 |
Yes | output | 1 | 19,880 | 3 | 39,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
def rotate_r(ar, row, n):
ar[row] = ar[row][-n:] + ar[row][:-n]
return ar
def rotate_c(ar, m, col):
#c = ar[col][0]
c = ar[m - 1][col]
for i in range(m - 1, 0, -1):
#for i in range(m - 1):
ar[i][col] = ar[i - 1][col]
#ar[col][m - 1] = c
ar[0][col] = c
return ar
def print_matr(ar, n):
for i in range(n):
print(*ar[i])
ar2 = []
n, m, q = map(int, input().split())
#for i in range(n):
# ar = list(map(int, input().split()))
# ar2.append(ar)
query = [0 for i in range(q)]
rows = [0 for i in range(q)]
cols = [0 for i in range(q)]
nums = [0 for i in range(q)]
for i in range(q):
ar = list(map(int, input().split()))
query[i] = ar[0]
if ar[0] == 3:
rows[i] = ar[1] - 1
cols[i] = ar[2] - 1
nums[i] = ar[3]
elif ar[0] == 1:
rows[i] = ar[1] - 1
else:
cols[i] = ar[1] - 1
#print(query)
ans = [[0] * m for i in range(n)]
for i in range(q - 1, -1, -1):
if query[i] == 3:
ans[rows[i]][cols[i]] = nums[i]
#print('\n')
#print_matr(ans, n)
#print("l", rows[i] + 1, cols[i] + 1)
#print(i, nums[i])
elif query[i] == 1:
ans = rotate_r(ans, rows[i], 1)
#print('\n')
#print_matr(ans, n)
else:
ans = rotate_c(ans, n, cols[i])
#print('\n')
#print_matr(ans, n)
#row, n = map(int, input().split())
#print(rotate_r(ar2, 0, n))
print_matr(ans, n)
#ans = rotate_c(ans, n, 0)
#print_matr(ans, n)
``` | instruction | 0 | 19,881 | 3 | 39,762 |
Yes | output | 1 | 19,881 | 3 | 39,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
import sys
sys.stderr = sys.stdout
def matrix(n, m, q, O):
V = [0] * (n * m)
M = [list(range(i0, i0+m)) for i0 in range(0, n*m, m)]
for o in O:
if o[0] == 1:
t, r = o
r -= 1
p = M[r][0]
for j in reversed(range(m)):
p, M[r][j] = M[r][j], p
elif o[0] == 2:
t, c = o
c -= 1
p = M[0][c]
for i in reversed(range(n)):
p, M[i][c] = M[i][c], p
else:
t, r, c, x = o
r -= 1
c -= 1
V[M[r][c]] = x
return [V[i0:i0+m] for i0 in range(0, n*m, m)]
def main():
n, m, q = readinti()
O = readinttl(q)
M = matrix(n, m, q, O)
print('\n'.join(' '.join(map(str, M_i)) for M_i in M))
##########
def readint():
return int(input())
def readinti():
return map(int, input().split())
def readintt():
return tuple(readinti())
def readintl():
return list(readinti())
def readinttl(k):
return [readintt() for _ in range(k)]
def readintll(k):
return [readintl() for _ in range(k)]
def log(*args, **kwargs):
print(*args, **kwargs, file=sys.__stderr__)
if __name__ == '__main__':
main()
``` | instruction | 0 | 19,882 | 3 | 39,764 |
Yes | output | 1 | 19,882 | 3 | 39,765 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
n, m, q = map(int, input().split())
queries = list()
for i in range(q):
query = list(map(int, input().split()))
queries.append(query)
arr = [[0 for i in range(m)] for j in range(n)]
queries = queries[::-1]
for query in queries:
x = query[0]
if x <= 2:
y = query[1]
if x == 1:
r = arr[y - 1].pop()
arr[y - 1].insert(0, r)
else:
temp = [arr[i][y - 1] for i in range(n)]
r = temp.pop()
temp.insert(0, r)
for i in range(n):
arr[i][y - 1] = temp[i]
else:
a, b, c = query[1:]
arr[a - 1][b - 1] = c
for i in range(n):
for j in range(m):
print(arr[i][j], end = ' ')
print()
``` | instruction | 0 | 19,883 | 3 | 39,766 |
Yes | output | 1 | 19,883 | 3 | 39,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
if __name__ == "__main__":
#n, m = list(map(int, input().split()))
#s = list(input().split())
n, m, q = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(q)]
t = [[[0, 0] for _ in range(m)] for _ in range(n)]
ans = [[0 for _ in range(m)] for _ in range(n)]
for i in range(q):
if Q[i][0] == 1:
for j in range(m):
t[(Q[i][1] - 1 + t[Q[i][1] - 1][j][0]) % n][(j + t[Q[i][1] - 1][j][1]) % m][1] += 1
elif Q[i][0] == 2:
for j in range(n):
t[(j + t[j][Q[i][1] - 1][0]) % n][(Q[i][1] - 1 + t[j][Q[i][1] - 1][1]) % m][0] += 1
else:
ans[(Q[i][1] - 1 + t[Q[i][1] - 1][Q[i][2] - 1][0]) % n][(Q[i][2] - 1 + t[Q[i][1] - 1][Q[i][2] - 1][1]) % m] = Q[i][3]
for i in range(n):
print(' '.join(str(j) for j in ans[i]))
``` | instruction | 0 | 19,884 | 3 | 39,768 |
No | output | 1 | 19,884 | 3 | 39,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
n, m, q = map(int, input().split())
a = [[0 for j in range(m)] for i in range(n)]
query = []
for i in range(q):
s = list(map(int, input().split()))
if len(s) == 2:
query.append((s[0], s[1] - 1))
else:
a[s[1] - 1][s[2] - 1] = s[3]
for k in reversed(range(len(query))):
if query[k][0] == 1:
temp = a[query[k][1]][-1]
for i in reversed(range(1, m)):
a[query[k][1]][i] = a[query[k][1]][i - 1]
a[query[k][1]][0] = temp
else:
temp = a[-1][query[k][1]]
for i in reversed(range(1, n)):
a[i][query[k][1]] = a[i - 1][query[k][1]]
a[0][query[k][1]] = temp
for i in range(n):
for j in range(m):
print(a[i][j], end = ' ')
print()
``` | instruction | 0 | 19,885 | 3 | 39,770 |
No | output | 1 | 19,885 | 3 | 39,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
inf= list(map(int, input().split()))
n=inf[0]
m=inf[1]
q=inf[2]
r=[[i for i in range(m)] for j in range(n)]
c=[[j for i in range(m)] for j in range(n)]
mat=[[0 for j in range(m)] for i in range(n)]
for i in range(q):
inf= list(map(int, input().split()))
if(inf[0]==3):
a=inf[1]-1
b=inf[2]-1
x=inf[3]
mat[c[a][b]][r[a][b]]=x
elif(inf[0]==1):
rn=inf[1]-1
r[rn].append(r[rn][0])
r[rn]=r[rn][1:]
else:
rn=inf[1]-1
temp=c[0][rn]
for j in range(1,n):
c[j-1][rn]=c[j][rn]
c[n-1][rn]=temp
for v in range(n):
print(*mat[v])
``` | instruction | 0 | 19,886 | 3 | 39,772 |
No | output | 1 | 19,886 | 3 | 39,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n Γ m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
Input
The first line of the input contains three integers n, m and q (1 β€ n, m β€ 100, 1 β€ q β€ 10 000) β dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 β€ ti β€ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 β€ ri β€ n) or ci (1 β€ ci β€ m) follows, while for the operations of the third type three integers ri, ci and xi (1 β€ ri β€ n, 1 β€ ci β€ m, - 109 β€ xi β€ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
Output
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
Examples
Input
2 2 6
2 1
2 2
3 1 1 1
3 2 2 2
3 1 2 8
3 2 1 8
Output
8 2
1 8
Input
3 3 2
1 2
3 2 2 5
Output
0 0 0
0 0 5
0 0 0
Submitted Solution:
```
n,m,q = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(q)]
B = [[[0,0] for _ in range(m)] for _ in range(n)]
M = [[0 for _ in range(m)] for _ in range(n)]
for i in range(q):
e = Q[i]
if e[0] == 1:
for i in range(n):
B[e[1]-1][i][0] += 1
elif e[0] == 2:
for i in range(m):
B[i][e[1]-1][1] += 1
else:
r = (e[1] - 1 + B[e[1]-1][e[2]-1][1]) % n
c = (e[2] - 1 + B[e[1]-1][e[2]-1][0]) % m
M[r][c] = e[3]
for i in range(n):
for j in range(m):
print(M[i][j],end=" ")
print()
``` | instruction | 0 | 19,887 | 3 | 39,774 |
No | output | 1 | 19,887 | 3 | 39,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,002 | 3 | 40,004 |
Tags: geometry, math
Correct Solution:
```
import sys
import math
import bisect
import itertools
from collections import Counter
d = {}
s = {}
n, a, b = list(map(int,sys.stdin.readline().strip().split(' ')))
for n0 in range(n):
x,vxi,vyi = list(map(int,sys.stdin.readline().strip().split(' ')))
curr = a*vxi - vyi
if curr in d:
if (vxi,vyi) in d[curr]:
d[curr][(vxi,vyi)] += 1
else:
d[curr][(vxi,vyi)] = 1
s[curr] += 1
else:
d[curr] = {(vxi,vyi):1}
s[curr] = 1
ans = 0
for k,v in d.items():
for vxy,q in v.items():
ans += (s[k] - q)*q
print(ans)
``` | output | 1 | 20,002 | 3 | 40,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,003 | 3 | 40,006 |
Tags: geometry, math
Correct Solution:
```
import sys
import io, os
input = sys.stdin.buffer.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from collections import defaultdict
n, a, b = map(int, input().split())
XV = []
for i in range(n):
x, vx, vy = map(int, input().split())
XV.append((x, vx, vy))
if a != 0:
ans = 0
d = defaultdict(lambda:0)
dvx = defaultdict(lambda:0)
for x, vx, vy in XV:
k = -a*vx+vy
ans += max(0, d[k]-dvx[(k, vx)])
d[k] += 1
dvx[(k, vx)] += 1
print(ans*2)
else:
ans = 0
d = defaultdict(lambda:defaultdict(lambda:0))
ds = defaultdict(lambda:0)
for x, vx, vy in XV:
ans += max(0, ds[vy]-d[vy][vx])
d[vy][vx] += 1
ds[vy] += 1
print(ans*2)
``` | output | 1 | 20,003 | 3 | 40,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,004 | 3 | 40,008 |
Tags: geometry, math
Correct Solution:
```
import atexit
import io
import sys
# Buffering IO
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
def main():
n, a, b = [int(x) for x in input().split()]
dc = {}
for i in range(n):
x, vx, vy = [int(x) for x in input().split()]
nx = x + vx
ny = a*x+b + vy
dd = a*nx - ny + b
if dd not in dc:
dc[dd] = {}
if (vx,vy) not in dc[dd]:
dc[dd][(vx,vy)] = 0
dc[dd][(vx,vy)] += 1
tot = 0
for v,k in dc.items():
tt = 0
pp =0
for _,cc in k.items():
tt -= cc * (cc+1) // 2
pp += cc
tt += pp * (pp+1) // 2
tot += tt*2
print(tot)
if __name__ == '__main__':
main()
``` | output | 1 | 20,004 | 3 | 40,009 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,005 | 3 | 40,010 |
Tags: geometry, math
Correct Solution:
```
from collections import Counter
n,a,b=[int(_) for _ in input().split()]
s,s1=Counter(),Counter();ans=0
for i in range(n):
x,vx,vy=[int(_) for _ in input().split()]
tmp=vy-a*vx
ans+=s[tmp]-s1[(vx,vy)]
s[tmp]+=1
s1[(vx,vy)]+=1
print(ans*2)
``` | output | 1 | 20,005 | 3 | 40,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,006 | 3 | 40,012 |
Tags: geometry, math
Correct Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
#import threading
from collections import defaultdict
#threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
#sys.setrecursionlimit(300000)
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")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=300006, func=lambda a, b: min(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b:a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] <=key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = self.getNode()
def getNode(self):
return TrieNode()
def _charToIndex(self, ch):
return ord(ch) - ord('a')
def insert(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
pCrawl.children[index] = self.getNode()
pCrawl = pCrawl.children[index]
pCrawl.isEndOfWord = True
def search(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return False
pCrawl = pCrawl.children[index]
return pCrawl != None and pCrawl.isEndOfWord
#-----------------------------------------trie---------------------------------
class Node:
def __init__(self, data):
self.data = data
self.count=0
self.left = None # left node for 0
self.right = None # right node for 1
class BinaryTrie:
def __init__(self):
self.root = Node(0)
def insert(self, pre_xor):
self.temp = self.root
for i in range(31, -1, -1):
val = pre_xor & (1 << i)
if val:
if not self.temp.right:
self.temp.right = Node(0)
self.temp = self.temp.right
self.temp.count+=1
if not val:
if not self.temp.left:
self.temp.left = Node(0)
self.temp = self.temp.left
self.temp.count += 1
self.temp.data = pre_xor
def query(self, xor):
self.temp = self.root
for i in range(31, -1, -1):
val = xor & (1 << i)
if not val:
if self.temp.left and self.temp.left.count>0:
self.temp = self.temp.left
elif self.temp.right:
self.temp = self.temp.right
else:
if self.temp.right and self.temp.right.count>0:
self.temp = self.temp.right
elif self.temp.left:
self.temp = self.temp.left
self.temp.count-=1
return xor ^ self.temp.data
#-------------------------bin trie-------------------------------------------
n,a1,b=map(int,input().split())
l=[]
ans=0
d=defaultdict(int)
e=defaultdict(list)
e1=defaultdict(int)
for i in range(n):
a,b,c=map(int,input().split())
d[(b,c)]+=1
for i in d:
b,c=i
e[c-a1*b].append(d[i])
e1[c-a1*b]+=d[i]
for i in e:
for j in e[i]:
ans+=j*(e1[i]-j)
print(ans)
``` | output | 1 | 20,006 | 3 | 40,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,007 | 3 | 40,014 |
Tags: geometry, math
Correct Solution:
```
from collections import Counter
from sys import stdin
n,a,b=[int(_) for _ in stdin.buffer.readline().split()]
s,s1=Counter(),Counter();ans=0
for i in range(n):
x,vx,vy=[int(_) for _ in stdin.buffer.readline().split()]
tmp=vy-a*vx
ans+=s[tmp]-s[(vx,vy)]-s1[(tmp,x)]+s1[(tmp,x,vx,vy)]+s1[x]
s[tmp]+=1;s[(vx,vy)]+=1;s1[(tmp,x)]+=1;s1[x]+=1;s1[(tmp,x,vx,vy)]+=1
print(ans*2)
``` | output | 1 | 20,007 | 3 | 40,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,008 | 3 | 40,016 |
Tags: geometry, math
Correct Solution:
```
from collections import Counter
import sys
n,a,b = list(map(int,sys.stdin.readline().split()))
c = Counter()
d = Counter()
ans = 0
for i in range(n):
x,vx,vy = list(map(int,sys.stdin.readline().split()))
tmp = vy - a*vx
ans += c[tmp] - c[(vx,vy)] - d[(tmp,x)] + d[(tmp,x,vx,vy)] + d[x]
c[tmp] += 1
c[(vx,vy)] += 1
d[(tmp,x)] += 1
d[x] += 1
d[(tmp,x,vx,vy)] += 1
print(ans*2)
``` | output | 1 | 20,008 | 3 | 40,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | instruction | 0 | 20,009 | 3 | 40,018 |
Tags: geometry, math
Correct Solution:
```
from collections import Counter
import sys
n,a,b = list(map(int,sys.stdin.readline().split()))
c = Counter(); d = Counter()
ans = 0
for i in range(n):
x,vx,vy = list(map(int,sys.stdin.readline().split()))
tmp = vy - a*vx
ans += c[tmp] - c[(vx,vy)] - d[(tmp,x)] + d[(tmp,x,vx,vy)] + d[x]
c[tmp] += 1
c[(vx,vy)] += 1
d[(tmp,x)] += 1
d[x] += 1
d[(tmp,x,vx,vy)] += 1
sys.stdout.write(str(ans*2))
``` | output | 1 | 20,009 | 3 | 40,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
n,a,b = [int(x) for x in input().split()]
mp = {}
spmp = {}
for i in range(n):
x, vx, vy = [int(x) for x in input().split()]
vv = a*vx - vy
spr = str([vx, vy])
if vv in mp:
mp[vv] += 1
else:
mp[vv] = 1
if spr in spmp:
spmp[spr] += 1
else:
spmp[spr] = 1
ans = 0
for v in mp.values():
ans += v*(v-1)
for v in spmp.values():
ans -= v*(v-1)
print(ans)
``` | instruction | 0 | 20,010 | 3 | 40,020 |
Yes | output | 1 | 20,010 | 3 | 40,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
#!/usr/bin/python3
from sys import stdin
from itertools import groupby
def f(line):
t = line.split()
return tuple(int(x) for x in t[1:])
n, a, b = [int(x) for x in stdin.readline().split()]
points = sorted(f(line) for line in stdin.readlines()[:n])
groups = [[k, len(tuple(g))] for k, g in groupby(points)]
total = {}
for (x, y), cnt in groups:
if y - x * a not in total:
total[y - x * a] = 0
total[y - x * a] += cnt
ans = 0
for (x, y), cnt in groups:
if y - x * a not in total:
continue
ans += cnt * (total[y - x * a] - cnt)
print(ans)
``` | instruction | 0 | 20,011 | 3 | 40,022 |
Yes | output | 1 | 20,011 | 3 | 40,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
def main():
n, a, b = [int(part) for part in input().split()]
ghosts = []
for i in range(n):
ghosts.append([int(part) for part in input().split()])
cnt = dict()
vcnt = dict()
for (x, vx, vy) in ghosts:
if (vx, vy) in vcnt:
vcnt[(vx, vy)] += 1
else:
vcnt[(vx, vy)] = 1
tmp = vy - a * vx
if tmp in cnt:
cnt[tmp] += 1
else:
cnt[tmp] = 1
ans = 0
for (x, vx, vy) in ghosts:
tmp = vy - a * vx
ans += cnt[tmp] - vcnt[(vx, vy)]
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 20,012 | 3 | 40,024 |
Yes | output | 1 | 20,012 | 3 | 40,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
from collections import Counter
from sys import stdin
n,a,b=map(int,stdin.readline().split())
s=Counter();ans=0
for i in range(n):
x,vx,vy=map(int,stdin.readline().split())
tmp=vy-a*vx
ans+=s[tmp]-s[(vx,vy)]
s[tmp]+=1;s[(vx,vy)]+=1
print(ans*2)
``` | instruction | 0 | 20,013 | 3 | 40,026 |
Yes | output | 1 | 20,013 | 3 | 40,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
#import threading
from collections import defaultdict
#threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
#sys.setrecursionlimit(300000)
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")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=300006, func=lambda a, b: min(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b:a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] <=key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = self.getNode()
def getNode(self):
return TrieNode()
def _charToIndex(self, ch):
return ord(ch) - ord('a')
def insert(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
pCrawl.children[index] = self.getNode()
pCrawl = pCrawl.children[index]
pCrawl.isEndOfWord = True
def search(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return False
pCrawl = pCrawl.children[index]
return pCrawl != None and pCrawl.isEndOfWord
#-----------------------------------------trie---------------------------------
class Node:
def __init__(self, data):
self.data = data
self.count=0
self.left = None # left node for 0
self.right = None # right node for 1
class BinaryTrie:
def __init__(self):
self.root = Node(0)
def insert(self, pre_xor):
self.temp = self.root
for i in range(31, -1, -1):
val = pre_xor & (1 << i)
if val:
if not self.temp.right:
self.temp.right = Node(0)
self.temp = self.temp.right
self.temp.count+=1
if not val:
if not self.temp.left:
self.temp.left = Node(0)
self.temp = self.temp.left
self.temp.count += 1
self.temp.data = pre_xor
def query(self, xor):
self.temp = self.root
for i in range(31, -1, -1):
val = xor & (1 << i)
if not val:
if self.temp.left and self.temp.left.count>0:
self.temp = self.temp.left
elif self.temp.right:
self.temp = self.temp.right
else:
if self.temp.right and self.temp.right.count>0:
self.temp = self.temp.right
elif self.temp.left:
self.temp = self.temp.left
self.temp.count-=1
return xor ^ self.temp.data
#-------------------------bin trie-------------------------------------------
n,a,b=map(int,input().split())
l=[]
ans=0
d=defaultdict(int)
e=defaultdict(list)
e1=defaultdict(int)
for i in range(n):
a,b,c=map(int,input().split())
d[(b,c)]+=1
for i in d:
e[c-a*b].append(d[i])
e1[c-a*b]+=d[i]
for i in e:
for j in e[i]:
ans+=j*(e1[i]-j)
print(ans)
``` | instruction | 0 | 20,014 | 3 | 40,028 |
No | output | 1 | 20,014 | 3 | 40,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
from math import gcd
n, a, b = map(int, input().split())
res = dict()
for _ in range(n):
k, x, y = map(int, input().split())
t = a*x-y
tt = res.get(t, None)
if tt is None:
res[t] = 1
else:
res[t] += 1
ans = 0
for k, v in res.items():
if v >= 2:
ans += v*(v-1)//2
print(ans*2)
``` | instruction | 0 | 20,015 | 3 | 40,030 |
No | output | 1 | 20,015 | 3 | 40,031 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are n ghosts in the universe, they move in the OXY plane, each one of them has its own velocity that does not change in time: \overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j} where V_{x} is its speed on the x-axis and V_{y} is on the y-axis.
A ghost i has experience value EX_i, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind GX = β_{i=1}^{n} EX_i will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time T, and magically all the ghosts were aligned on a line of the form y = a β
x + b. You have to compute what will be the experience index of the ghost kind GX in the indefinite future, this is your task for today.
Note that when Tameem took the picture, GX may already be greater than 0, because many ghosts may have scared one another at any moment between [-β, T].
Input
The first line contains three integers n, a and b (1 β€ n β€ 200000, 1 β€ |a| β€ 10^9, 0 β€ |b| β€ 10^9) β the number of ghosts in the universe and the parameters of the straight line.
Each of the next n lines contains three integers x_i, V_{xi}, V_{yi} (-10^9 β€ x_i β€ 10^9, -10^9 β€ V_{x i}, V_{y i} β€ 10^9), where x_i is the current x-coordinate of the i-th ghost (and y_i = a β
x_i + b).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all (i,j) x_i β x_j for i β j.
Output
Output one line: experience index of the ghost kind GX in the indefinite future.
Examples
Input
4 1 1
1 -1 -1
2 1 1
3 1 1
4 -1 -1
Output
8
Input
3 1 0
-1 1 0
0 0 -1
1 -1 -2
Output
6
Input
3 1 0
0 0 0
1 0 0
2 0 0
Output
0
Note
There are four collisions (1,2,T-0.5), (1,3,T-1), (2,4,T+1), (3,4,T+0.5), where (u,v,t) means a collision happened between ghosts u and v at moment t. At each collision, each ghost gained one experience point, this means that GX = 4 β
2 = 8.
In the second test, all points will collide when t = T + 1.
<image>
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
Submitted Solution:
```
n, a, b = map(int, input().split(" "))
ghosts = []
for i in range(n):
x, vx, vy = map(int, input().split(" "))
ghosts.append((x, a * x + b, vx, vy))
count = 0
for i, g1 in enumerate(ghosts):
for g2 in ghosts[i + 1:]:
if g2[2] != g1[2]:
t = (g1[0] - g2[0]) // (g2[2] - g1[2])
y1 = g1[1] + t * g1[3]
y2 = g2[1] + t * g2[3]
if y1 == y2:
count += 2
print(count)
``` | instruction | 0 | 20,016 | 3 | 40,032 |
No | output | 1 | 20,016 | 3 | 40,033 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.