message
stringlengths
2
44.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
276
109k
cluster
float64
23
23
__index_level_0__
int64
552
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the n numbers may not be chosen. A rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different. What is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself. Input The first line contains n (1 ≤ n ≤ 4⋅10^5). The second line contains n integers (1 ≤ a_i ≤ 10^9). Output In the first line print x (1 ≤ x ≤ n) — the total number of cells of the required maximum beautiful rectangle. In the second line print p and q (p ⋅ q=x): its sizes. In the next p lines print the required rectangle itself. If there are several answers, print any. Examples Input 12 3 1 4 1 5 9 2 6 5 3 5 8 Output 12 3 4 1 2 3 5 3 1 5 4 5 6 8 9 Input 5 1 1 1 1 1 Output 1 1 1 1
instruction
0
94,500
23
189,000
Tags: brute force, combinatorics, constructive algorithms, data structures, greedy, math Correct Solution: ``` from collections import Counter from itertools import accumulate from math import sqrt from operator import itemgetter import sys n = int(input()) cnt = Counter(map(int, input().split())) nums, counts = zip(*sorted(cnt.items(), key=itemgetter(1))) acc = [0] + list(accumulate(counts)) area = 1 h, w = 1, 1 i = len(counts) for y in range(int(sqrt(n)), 0, -1): while i and counts[i-1] > y: i -= 1 total = acc[i] + (len(counts) - i) * y x = total // y if y <= x and area < x * y: h, w, area = y, x, x*y ans = [[0]*w for _ in range(h)] i = len(counts)-1 num, count = nums[i], min(h, counts[i]) for x in range(w): for y in range(h): ans[y][(x + y) % w] = num count -= 1 if count == 0: i -= 1 num, count = nums[i], h if h < counts[i] else counts[i] print(area) print(h, w) for y in range(h): sys.stdout.write(' '.join(map(str, ans[y])) + '\n') ```
output
1
94,500
23
189,001
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the n numbers may not be chosen. A rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different. What is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself. Input The first line contains n (1 ≤ n ≤ 4⋅10^5). The second line contains n integers (1 ≤ a_i ≤ 10^9). Output In the first line print x (1 ≤ x ≤ n) — the total number of cells of the required maximum beautiful rectangle. In the second line print p and q (p ⋅ q=x): its sizes. In the next p lines print the required rectangle itself. If there are several answers, print any. Examples Input 12 3 1 4 1 5 9 2 6 5 3 5 8 Output 12 3 4 1 2 3 5 3 1 5 4 5 6 8 9 Input 5 1 1 1 1 1 Output 1 1 1 1
instruction
0
94,501
23
189,002
Tags: brute force, combinatorics, constructive algorithms, data structures, greedy, math Correct Solution: ``` import sys input = sys.stdin.readline n=int(input()) A=list(map(int,input().split())) from collections import Counter C=Counter(A) import math MAXV=max(max(C.values()),int(math.sqrt(n))) VCOUNT=[0]*(MAXV+1) for v in C.values(): VCOUNT[v]+=1 SUM=n from itertools import accumulate ACC=list(accumulate(VCOUNT[::-1]))[::-1] ANS=0 for i in range(MAXV,0,-1): if SUM//i>=i: if ANS<i*(SUM//i): ANS=i*(SUM//i) ANSX=i,(SUM//i) SUM-=ACC[i] print(ANS) X,Y=ANSX[0],ANSX[1] print(X,Y) A=[[0]*Y for i in range(X)] i=0 j=0 nowj=0 colored=0 same=0 LIST=list(C.most_common()) ind=0 while colored<ANS: A[i][j],MAX=LIST[ind] colored+=1 i+=1 j=(j+1)%Y if i==X: i=0 nowj+=1 j=nowj same+=1 if same==min(X,MAX): ind+=1 same=0 for a in A: sys.stdout.write(" ".join(map(str,a))+"\n") ```
output
1
94,501
23
189,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the n numbers may not be chosen. A rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different. What is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself. Input The first line contains n (1 ≤ n ≤ 4⋅10^5). The second line contains n integers (1 ≤ a_i ≤ 10^9). Output In the first line print x (1 ≤ x ≤ n) — the total number of cells of the required maximum beautiful rectangle. In the second line print p and q (p ⋅ q=x): its sizes. In the next p lines print the required rectangle itself. If there are several answers, print any. Examples Input 12 3 1 4 1 5 9 2 6 5 3 5 8 Output 12 3 4 1 2 3 5 3 1 5 4 5 6 8 9 Input 5 1 1 1 1 1 Output 1 1 1 1 Submitted Solution: ``` from math import sqrt from collections import Counter import sys input = sys.stdin.readline ''' for CASES in range(int(input())): n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) S = input().strip() ''' inf = 100000000000000000 # 1e17 mod = 998244353 n = int(input()) A = list(map(int, input().split())) D = Counter(A) num = 0 maxx = 0 a = 0 b = 0 for i in range(1, int(sqrt(n)) + 1): num = 0 for d in D.values(): num += min(i, d) if num // i >= i and num // i * i > maxx: maxx = num // i * i a = i b = num // i print(maxx) print(a, b) A = [] D = D.most_common() for key, d in D: num = min(a, d) for number in range(num): A.append(key) ANS = [[0] * (b) for _ in range(a)] AIM = A[:maxx] pos = 0 SET = set() last = 0 for aim in AIM: if last != aim: SET.clear() for i in range(b): if ANS[pos][i] != 0: continue if i in SET: continue ANS[pos][i] = aim SET.add(i) pos += 1 pos %= a last = aim break for i in range(a): print(*ANS[i]) # the end ```
instruction
0
94,502
23
189,004
No
output
1
94,502
23
189,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the n numbers may not be chosen. A rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different. What is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself. Input The first line contains n (1 ≤ n ≤ 4⋅10^5). The second line contains n integers (1 ≤ a_i ≤ 10^9). Output In the first line print x (1 ≤ x ≤ n) — the total number of cells of the required maximum beautiful rectangle. In the second line print p and q (p ⋅ q=x): its sizes. In the next p lines print the required rectangle itself. If there are several answers, print any. Examples Input 12 3 1 4 1 5 9 2 6 5 3 5 8 Output 12 3 4 1 2 3 5 3 1 5 4 5 6 8 9 Input 5 1 1 1 1 1 Output 1 1 1 1 Submitted Solution: ``` from math import sqrt from collections import Counter import sys input = sys.stdin.readline ''' for CASES in range(int(input())): n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) S = input().strip() ''' inf = 100000000000000000 # 1e17 mod = 998244353 n = int(input()) A = list(map(int, input().split())) D = Counter(A) num = 0 maxx = 0 a = 0 b = 0 for i in range(1, int(sqrt(n)) + 1): num = 0 for d in D.values(): num += min(i, d) if num > maxx: maxx = num a = i b = num // i print(maxx) print(a,b) A = [] D = D.most_common() for key, d in D: num = min(a, d) for number in range(num): A.append(key) ANS = [[0] * (b) for _ in range(a)] AIM = A[:maxx] pos = 0 SET = set() last = 0 for aim in AIM: if last != aim: SET.clear() for i in range(b): if ANS[pos][i] != 0: continue if i in SET: continue ANS[pos][i] = aim SET.add(i) pos += 1 pos %= a last = aim break for i in range(a): print(*ANS[i]) # the end ```
instruction
0
94,503
23
189,006
No
output
1
94,503
23
189,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the n numbers may not be chosen. A rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different. What is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself. Input The first line contains n (1 ≤ n ≤ 4⋅10^5). The second line contains n integers (1 ≤ a_i ≤ 10^9). Output In the first line print x (1 ≤ x ≤ n) — the total number of cells of the required maximum beautiful rectangle. In the second line print p and q (p ⋅ q=x): its sizes. In the next p lines print the required rectangle itself. If there are several answers, print any. Examples Input 12 3 1 4 1 5 9 2 6 5 3 5 8 Output 12 3 4 1 2 3 5 3 1 5 4 5 6 8 9 Input 5 1 1 1 1 1 Output 1 1 1 1 Submitted Solution: ``` from math import sqrt from collections import Counter import sys input = sys.stdin.readline ''' for CASES in range(int(input())): n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) S = input().strip() ''' inf = 100000000000000000 # 1e17 mod = 998244353 n = int(input()) A = list(map(int, input().split())) D = Counter(A) num = 0 maxx = 0 a = 0 b = 0 for i in range(1, int(sqrt(n)) + 1): num = 0 for d in D.values(): num += min(i, d) if num//i>=i and num > maxx: maxx = num a = i b = num // i print(maxx) print(a,b) A = [] D = D.most_common() for key, d in D: num = min(a, d) for number in range(num): A.append(key) ANS = [[0] * (b) for _ in range(a)] AIM = A[:maxx] pos = 0 SET = set() last = 0 for aim in AIM: if last != aim: SET.clear() for i in range(b): if ANS[pos][i] != 0: continue if i in SET: continue ANS[pos][i] = aim SET.add(i) pos += 1 pos %= a last = aim break for i in range(a): print(*ANS[i]) # the end ```
instruction
0
94,504
23
189,008
No
output
1
94,504
23
189,009
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,537
23
189,074
Tags: brute force, dp, implementation, math Correct Solution: ``` t = int(input()) while t > 0: n = input() print(n) t -= 1 ```
output
1
94,537
23
189,075
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,538
23
189,076
Tags: brute force, dp, implementation, math Correct Solution: ``` for nt in range(int(input())): n=int(input()) print (n) ```
output
1
94,538
23
189,077
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,539
23
189,078
Tags: brute force, dp, implementation, math Correct Solution: ``` T = int(input()) for i in range(T): st=input() print(st) ```
output
1
94,539
23
189,079
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,540
23
189,080
Tags: brute force, dp, implementation, math Correct Solution: ``` # import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w+') for _ in range (0, int(input())): n = int(input()) ans = 1 print(n) ```
output
1
94,540
23
189,081
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,541
23
189,082
Tags: brute force, dp, implementation, math Correct Solution: ``` def sol(): return int(input()) for _ in range(int(input())): print(sol()) ```
output
1
94,541
23
189,083
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,542
23
189,084
Tags: brute force, dp, implementation, math Correct Solution: ``` a = int(input()) for i in range (a): print(int(input())) ```
output
1
94,542
23
189,085
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,543
23
189,086
Tags: brute force, dp, implementation, math Correct Solution: ``` from sys import stdin def main(): r = stdin.readline cases = int(r()) for case in range(cases): n = int(r()) print(n) main() ```
output
1
94,543
23
189,087
Provide tags and a correct Python 3 solution for this coding contest problem. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image>
instruction
0
94,544
23
189,088
Tags: brute force, dp, implementation, math Correct Solution: ``` t = int(input()) b = [] for i in range(t): n = int(input()) b.append(n) for i in range(t): print(b[i]) ```
output
1
94,544
23
189,089
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` t = int(input()) for i in range(t): k = int(input()) print(k) ```
instruction
0
94,545
23
189,090
Yes
output
1
94,545
23
189,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` # t = int(input()) for _ in range(int(input())): n = int(input()) print(n) ```
instruction
0
94,546
23
189,092
Yes
output
1
94,546
23
189,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` t = int(input()) x = [] for i in range(t): n = int(input()) x += [n] for k in x: print(k) ```
instruction
0
94,547
23
189,094
Yes
output
1
94,547
23
189,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` t=int(input()) for _ in range(t): valid=True n=int(input()) print(n) ```
instruction
0
94,548
23
189,096
Yes
output
1
94,548
23
189,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` for _ in range(int(input())): n = int(input()) if(n==1): print(1) else: print(2) ```
instruction
0
94,549
23
189,098
No
output
1
94,549
23
189,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` from sys import stdin,stdout t=int(stdin.readline()) for query in range(t): n=int(stdin.readline()) if n==1: stdout.write('1') elif n==2: stdout.write('2') elif n==3: stdout.write('3') else: stdout.write('4') stdout.write('\n') ```
instruction
0
94,550
23
189,100
No
output
1
94,550
23
189,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` cases = int(input()) for i in range(cases): n = int(input()) r = int((4*n - 4)/2) total = 1 while (r >= 1): total = 1*r r -= 1 print (total) ```
instruction
0
94,551
23
189,102
No
output
1
94,551
23
189,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. <image> On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. <image> These are the figures of the area you want to fill for n = 1, 2, 3, 4. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. Each of the next t lines contains a single integer n (1 ≤ n ≤ 10^{9}). Output For each test case, print the number of ways to fully cover belt-like area of 4n-2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed 10^{18}. Example Input 2 2 1 Output 2 1 Note In the first test case, there are the following 2 ways to fill the area: <image> In the second test case, there is a unique way to fill the area: <image> Submitted Solution: ``` t = int(input()) for _ in range(t): n = int(input()) shape = 4*n-2 if n == 1: print(1) else: print(2) ```
instruction
0
94,552
23
189,104
No
output
1
94,552
23
189,105
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,553
23
189,106
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools # import time,random,resource sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) def IF(c, t, f): return t if c else f def main(): t = I() rr = [] for _ in range(t): n = I() a = [S() for _ in range(n)] ok = True for i in range(n-1): for j in range(n-1): if a[i][j] == '1' and a[i][j+1] == '0' and a[i+1][j] == '0': ok = False rr.append(IF(ok, 'YES', 'NO')) return JA(rr, "\n") print(main()) ```
output
1
94,553
23
189,107
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,554
23
189,108
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` import os import sys import io import math #input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline for t in range(int(input())): n=int(input()) matrix=[] for i in range(n): matrix.append(list(map(int,input()))) check=True for y in range(n-1): for x in range(n-1): if matrix[y][x]==1: if matrix[y][x+1]==0 and matrix[y+1][x]==0: check=False break if check==True: print("YES") else: print("NO") ```
output
1
94,554
23
189,109
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,555
23
189,110
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` t = int(input()) for _ in range(t): n = int(input()) lines = [] for _ in range(n): lines.append(input()) breaker = False yas = True for x in range(0,n-1): if breaker: break else: for y in range(0,n-1): if lines[x][y] == '0' or (lines[x][y] == '1' and (lines[x+1][y] == '1' or lines[x][y+1] == '1')): pass else: print("NO") breaker = True yas = False break if yas: print("YES") ```
output
1
94,555
23
189,111
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,556
23
189,112
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 # 11111111111111111111111111111111111111111111111111 class Node: def __init__(self, i, j, state, n) -> None: self.i = i self.j = j self.state = state self.childs = set() self.is_border = i+1 == n or j+1 == n def __repr__(self) -> str: return f"Node(i={self.i}; j={self.j}; state={self.state})" def solution(): n = int(input()) nodes = { (i, j): Node(i, j, name, n) for i in range(n) for j, name in enumerate(input()) } # for i1, i2 in zip(range(n), range(n-1)): # for j1, j2 in zip(range(n-1), range(n)): # nodes[(i1, j1)].childs.add(nodes[(i1, j1+1)]) # nodes[(i2, j2)].childs.add(nodes[(i2+1, j2)]) for i in range(n): for j in range(n-1): nodes[(i, j)].childs.add(nodes[(i, j+1)]) for i in range(n-1): for j in range(n): nodes[(i, j)].childs.add(nodes[(i+1, j)]) nodes = nodes.values() for node in filter(lambda n: n.state == '1', nodes): if can_travel(node): continue else: print("NO") break else: print("YES") # for key in nodes: # print(f"{key}:{nodes[key].childs}") def can_travel(node: Node, visited={}): if node.state == '0': return False elif node.state == '1': if node.is_border: return True childs = list(filter(lambda child: child.state == '1', node.childs)) if len(childs) == 0: return False can = False for child in childs: if child not in visited: visited[child] = can_travel(child, visited) can = visited[child] return can else: raise RuntimeError("undefined state") if __name__ == "__main__": #TODO: input t = int(input()) for _ in range(t): solution() ```
output
1
94,556
23
189,113
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,557
23
189,114
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` from collections import deque T = int(input()) for _ in range(T): n = int(input()) arr = [] for _ in range(n): arr.append(list(map(int,list(input())))) vis = [[False for _ in range(n)] for _ in range(n)] q = deque() for i in range(n): if arr[n-1][i] == 1: q.append((n-1,i)) if arr[i][n-1] == 1: q.append((i,n-1)) while len(q)>0: i,j = q.popleft() if i<0 or j<0 or arr[i][j] != 1 or vis[i][j]: continue vis[i][j] = True q.append((i-1,j)) q.append((i,j-1)) ans = "YES" for i in range(n): for j in range(n): if arr[i][j] == 1 and not vis[i][j]: ans = "NO" print(ans) ```
output
1
94,557
23
189,115
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,558
23
189,116
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` from math import * from collections import * from operator import itemgetter import bisect i = lambda: input() ii = lambda: int(input()) iia = lambda: list(map(int,input().split())) isa = lambda: list(input().split()) I = lambda:list(map(int,input().split())) chrIdx = lambda x: ord(x)-96 idxChr = lambda x: chr(96+x) t = ii() for _ in range(t): n = ii() m = [0]*n for i in range(n): m[i] = input() flag = 1 for i in range(n): for j in range(n): if(m[i][j]=='1'): if(i<n-1 and m[i+1][j]!='1') and (j<n-1 and m[i][j+1]!='1'): #print(i,j) flag = 0 break if(flag==0): break if flag==0: print("NO") else: print("YES") ```
output
1
94,558
23
189,117
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,559
23
189,118
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` t = int(input().strip()) for _ in range(t): g = [] flag = 0 n = int(input().strip()) for _ in range(n): g.append(input().strip()) for i in range(n - 1): for j in range(n - 1): if g[i][j] == '1': if g[i + 1][j] != '1' and g[i][j + 1] != '1': flag = 1 break if flag: break if flag: print("NO") else: print("YES") ```
output
1
94,559
23
189,119
Provide tags and a correct Python 3 solution for this coding contest problem. Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2n cannons were placed. <image> Initial polygon for n=4. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: * if a cannon stands in the row i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i, 1) and ends in some cell (i, j); * if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1, j) and ends in some cell (i, j). For example, consider the following sequence of shots: <image> 1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3. You have a report from the military training on your desk. This report is a square matrix with side length n consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. Each test case starts with a line containing an integer n (1 ≤ n ≤ 50) — the size of the polygon. This is followed by n lines of length n, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed 10^5. Output For each test case print: * YES if there is a sequence of shots leading to a given matrix; * NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case. Example Input 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 Output YES NO YES YES NO Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell (1, 1) flying out of any cannon would continue its flight further.
instruction
0
94,560
23
189,120
Tags: dp, graphs, implementation, shortest paths Correct Solution: ``` #https://codeforces.com/contest/1360/problem/E n_data = int(input()) datas = [] for i in range(n_data): l_array = int(input()) tab = [] for i in range(l_array): row = input() r = [] for c in row: r.append(c) tab.append(r) datas.append(tab) #for d in datas: # print(d) def is_border(x, y, grid): if x == len(grid[0])-1: return True if y == len(grid)-1: return True return False def possible(x, y, grid): if is_border(x, y, grid): return True if grid[x+1][y] == '1': return True if grid[x][y+1] == '1': return True return False def grid_OK(grid): for y in range(len(grid)): for x in range(len(grid[0])): if grid[x][y] == '0': continue if possible(x, y, grid): continue return 'NO' return 'YES' for d in datas: print(grid_OK(d)) ```
output
1
94,560
23
189,121
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,011
23
190,022
Tags: geometry Correct Solution: ``` from math import hypot, atan2, cos, sin, sqrt, pi import sys mapInt = lambda:map(int,sys.stdin.buffer.readline().split()) N, Q = mapInt() # Get points from input points = {} for n in range(N) : x, y = mapInt() points[n+1] = [x,y] # Calculate COM (centroid) centroid = [0,0] A = 0 for n in range(1,N+1) : w = points[n][0]*points[n%N+1][1] - points[n%N+1][0]*points[n][1] centroid[0] += (points[n][0] + points[n%N+1][0])*w centroid[1] += (points[n][1] + points[n%N+1][1])*w A += w centroid[0] /= 3*A centroid[1] /= 3*A # Move all points such that COM is at origin for i in points.keys() : points[i][0] -= centroid[0] points[i][1] -= centroid[1] # atan2(x, -y) to use negative y as reference totAng = 0 pins = [1,2] for q in range(Q) : inp = list(mapInt()) if inp[0] == 1 : f, t = inp[1],inp[2] if pins[0] == pins[1] == f : pins[0] = t continue elif pins[0] == f : pivot = pins[1] pins[0] = t else : pivot = pins[0] pins[1] = t x, y = points[pivot] centroid[0] += sin(totAng + atan2(x,-y))*hypot(x,y) centroid[1] += (-cos(totAng + atan2(x,-y)) - 1)*hypot(x,y) totAng = pi-atan2(x,-y) elif inp[0] == 2 : x, y = points[inp[1]] pointX = sin(totAng + atan2(x,-y))*hypot(x,y) + centroid[0] pointY = -cos(totAng + atan2(x,-y))*hypot(x,y) + centroid[1] print ("%0.20f %0.20f" % (pointX, pointY)) ```
output
1
95,011
23
190,023
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,012
23
190,024
Tags: geometry Correct Solution: ``` #!/usr/bin/env python3 from math import hypot [n, q] = map(int, input().strip().split()) xys = [tuple(map(int, input().strip().split())) for _ in range(n)] qis = [tuple(map(int, input().strip().split())) for _ in range(q)] dxys = [(xys[(i + 1) % n][0] - xys[i][0], xys[(i + 1) % n][1] - xys[i][1]) for i in range(n)] S = 3 * sum((x*dy - y*dx) for (x, y), (dx, dy) in zip(xys, dxys)) Sx = sum((dx + 2*x) * (x*dy - y*dx) for (x, y), (dx, dy) in zip(xys, dxys)) Sy = sum((dy + 2*y) * (x*dy - y*dx) for (x, y), (dx, dy) in zip(xys, dxys)) #Sy = sum((2*dx*dy + 3*x*dx + 3*x*dy + 6*x*y)*dy for (x, y), (dx, dy) in zip(xys, dxys)) for p in [2, 3]: while S % p == Sx % p == Sy % p == 0: S //= p Sx //= p Sy //= p xyms = [(S*x - Sx, S*y - Sy) for x, y in xys] hs = [hypot(x, y) for x, y in xyms] def to_coord(x, y): return (x + Sx) / S, (y + Sy) / S hangs = (0, 1) hang_on = None cx, cy = 0.0, 0.0 # hang on u def get_v(v): if hang_on is None: return xyms[v] else: ux, uy = xyms[hang_on] vx, vy = xyms[v] h = hs[hang_on] return ((uy * vx - ux * vy) / h, (ux * vx + uy * vy) / h) #def ss(v1, v2): # return tuple(vi + vj for vi, vj in zip(v1, v2)) #def disp(): # print ('hangs on', hang_on, 'of', hangs) # print ('center', to_coord(cx, cy)) # print ({i: to_coord(*ss(get_v(i), (cx, cy))) for i in range(n)}) #disp() for qi in qis: if qi[0] == 1: _, f, t = qi # 1-indexation s = hangs[1 - hangs.index(f - 1)] dx, dy = get_v(s) cx += dx cy += dy - hs[s] hang_on = s hangs = (s, t - 1) # print ('{} --> {}'.format(f - 1, t - 1)) # disp() else: _, v = qi # 1-indexation dx, dy = get_v(v - 1) print (*to_coord(cx + dx, cy + dy)) ```
output
1
95,012
23
190,025
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,013
23
190,026
Tags: geometry Correct Solution: ``` import sys read=lambda:map(int,sys.stdin.buffer.readline().split()) from math import * n,q=read() x,y=[],[] cx,cy=0,0 deg=0 p1,p2=0,1 def pos(i): return (cx+x[i]*cos(deg)-y[i]*sin(deg),cy+y[i]*cos(deg)+x[i]*sin(deg)) for i in range(n): _x,_y=read() x.append(_x);y.append(_y) s=0 for i in range(2,n): _s=(x[i]-x[0])*(y[i-1]-y[0])-(y[i]-y[0])*(x[i-1]-x[0]) cx+=(x[0]+x[i]+x[i-1])/3*_s cy+=(y[0]+y[i]+y[i-1])/3*_s s+=_s cx/=s;cy/=s for i in range(n): x[i]-=cx;y[i]-=cy for i in range(q): v=list(read()) if v[0]==1: if p1==v[1]-1: p1,p2=p2,None else: p1,p2=p1,None cx,cy=pos(p1);cy-=hypot(x[p1],y[p1]) deg=pi/2-atan2(y[p1],x[p1]);p2=v[2]-1 else: print("%.15f %.15f"%pos(v[1]-1)) ```
output
1
95,013
23
190,027
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,014
23
190,028
Tags: geometry Correct Solution: ``` from sys import stdin, stdout from math import atan2, sqrt, sin, cos, pi def main(): n, q = stdin.readline().split() n = int(n) q = int(q) lines = stdin.readlines() x = [0] * (n + 1) y = [0] * (n + 1) for i in range(n): x[i], y[i] = lines[i].split() x[i] = int(x[i]) y[i] = int(y[i]) x[n] = x[0] y[n] = y[0] a = 0 cx = 0 cy = 0 for i in range(n): ii = i + 1 a += x[i] * y[ii] - x[ii] * y[i] cx += (x[i] + x[ii]) * (x[i] * y[ii] - x[ii] * y[i]) cy += (y[i] + y[ii]) * (x[i] * y[ii] - x[ii] * y[i]) a *= 3 r = [0.0] * n z = [0.0] * n for i in range(n): x[i] = x[i] * a - cx y[i] = y[i] * a - cy r[i] = sqrt(x[i] * x[i] + y[i] * y[i]) / a z[i] = atan2(y[i], x[i]) cx /= a cy /= a p = [0, 1] mt = 0.0 for i in range(q): tmp = lines[n + i].split() ta = int(tmp[0]) tb = int(tmp[1]) if ta == 1: ta = int(tmp[2]) ta -= 1 tb -= 1 if p[0] == tb: p[0], p[1] = p[1], p[0] p[1] = ta tc = z[p[0]] + mt cx += r[p[0]] * cos(tc) cy += r[p[0]] * (sin(tc) - 1) tc = pi / 2 - tc; mt = pi / 2 - z[p[0]]; else: tb -= 1 tc = z[tb] + mt stdout.write('%.10lf %.10lf\n' % (cx + r[tb] * cos(tc), cy + r[tb] * sin(tc))) if __name__ == '__main__': main() ```
output
1
95,014
23
190,029
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,015
23
190,030
Tags: geometry Correct Solution: ``` from sys import stdin from math import * rl = lambda l: tuple(map(int, l.split())) rd = lambda: rl(input()) class Point(): def __init__(self, x, y): self.x = x self.y = y def __add__(self, pt): return Point(self.x + pt.x, self.y + pt.y) def __iadd__(self, pt): return self + pt def __mul__(self, n): return Point(self.x * n, self.y * n) def __imul__(self, n): return self * n def __truediv__(self, n): return Point(self.x / n, self.y / n) def __itruediv__(self, n): return self / n def __str__(self): return '%f %f' % (self.x, self.y) def distance(self, pt): return sqrt((self.x - pt.x) ** 2 + (self.y - pt.y) ** 2) def angle(self, pt): det_x = pt.x - self.x det_y = pt.y - self.y if det_x == 0: if det_y > 0: return pi / 2 if det_y < 0: return pi / 2 * 3 if det_y == 0: raise Exception('Fail to find angle between two identical points.') if det_x > 0: return atan(det_y / det_x) if det_x < 0: return atan(det_y / det_x) + pi def get_pt_by_dis_ang(self, dis, ang): return Point(self.x + cos(ang) * dis, self.y + sin(ang) * dis) class Convex_polygon(): def __init__(self): self.points = [] self.core = None self.dis = None self.base_ang = 0 self.ang = None self.pin = [0, 1] def add(self, pt): self.points.append(pt) def update(self): self.update_core() self.update_dis() self.update_ang() def cal_core_of_triangle(self, i, j, k): s = (self.points[j].x - self.points[i].x) * (self.points[k].y - self.points[i].y) s -= (self.points[j].y - self.points[i].y) * (self.points[k].x - self.points[i].x) return s, (self.points[i] + self.points[j] + self.points[k]) / 3 * s def update_core(self): self.core = Point(0, 0) s = 0 for i in range(2, len(P.points)): det_s, det_core = self.cal_core_of_triangle(0, i, i - 1) self.core += det_core s += det_s self.core /= s def update_dis(self): self.dis = [] for pt in self.points: self.dis.append(self.core.distance(pt)) def update_ang(self): self.ang = [] for pt in self.points: self.ang.append(self.core.angle(pt)) def get_pt(self, i): return self.core.get_pt_by_dis_ang(self.dis[i], self.base_ang + self.ang[i]) def __str__(self): return '\n'.join(('points: ' + '[' + ', '.join(str(pt) for pt in self.points) + ']', 'core: ' + str(self.core), 'dis: ' + str(self.dis), 'base_ang ' + str(self.base_ang), 'ang ' + str(self.ang), 'pin ' + str(self.pin))) n, q = rd() P = Convex_polygon() for _ in range(n): P.add(Point(*rd())) P.update() for _ in stdin.readlines(): s = rl(_) if s[0] == 1: __, f, t = s f -= 1 t -= 1 P.pin.remove(f) i = P.pin[0] top_pt = P.get_pt(i) P.core = Point(top_pt.x, top_pt.y - P.dis[i]) P.base_ang = pi / 2 - P.ang[i] P.pin.append(t) else: __, v = s v -= 1 print(P.get_pt(v)) ```
output
1
95,015
23
190,031
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,016
23
190,032
Tags: geometry Correct Solution: ``` from sys import* from math import* def main(): n, q = stdin.readline().split() n = int(n) q = int(q) lines = stdin.readlines() x = [0] * (n + 1) y = [0] * (n + 1) for i in range(n): x[i], y[i] = lines[i].split() x[i] = int(x[i]) y[i] = int(y[i]) x[n] = x[0] y[n] = y[0] a = 0 cx = 0 cy = 0 for i in range(n): ii = i + 1 a += x[i] * y[ii] - x[ii] * y[i] cx += (x[i] + x[ii]) * (x[i] * y[ii] - x[ii] * y[i]) cy += (y[i] + y[ii]) * (x[i] * y[ii] - x[ii] * y[i]) a *= 3 r = [0.0] * n z = [0.0] * n for i in range(n): x[i] = x[i] * a - cx y[i] = y[i] * a - cy r[i] = sqrt(x[i] * x[i] + y[i] * y[i]) / a z[i] = atan2(y[i], x[i]) cx /= a cy /= a p = [0, 1] mt = 0.0 for i in range(q): tmp = lines[n + i].split() ta = int(tmp[0]) tb = int(tmp[1]) if ta == 1: ta = int(tmp[2]) ta -= 1 tb -= 1 if p[0] == tb: p[0], p[1] = p[1], p[0] p[1] = ta tc = z[p[0]] + mt cx += r[p[0]] * cos(tc) cy += r[p[0]] * (sin(tc) - 1) tc = pi / 2 - tc; mt = pi / 2 - z[p[0]]; else: tb -= 1 tc = z[tb] + mt stdout.write('%.10lf %.10lf\n' % (cx + r[tb] * cos(tc), cy + r[tb] * sin(tc))) if __name__ == '__main__': main() ```
output
1
95,016
23
190,033
Provide tags and a correct Python 3 solution for this coding contest problem. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
instruction
0
95,017
23
190,034
Tags: geometry Correct Solution: ``` import math; #Вычисление координаты точки по координатам центра, углу, и начальным относительно центра def getCoordinate(gx, gy, alpha, x, y): x1=gx+x*math.cos(alpha)-y*math.sin(alpha); y1=gy+x*math.sin(alpha)+y*math.cos(alpha); return x1, y1 #Вычисление угла, на который надо повернуть точку с координатами x, y, #чтобы она оказалась прямо над gx, gy def getAngle(gx, gy, x, y): x=x-gx; y=y-gy; cos=x/math.sqrt(x**2+y**2); alpha=math.acos(cos); if y<0: alpha=-alpha; return math.pi/2-alpha; n, q = map(int, input().split(' ')); x=[0]*n; y=[0]*n; for i in range(n): x[i], y[i]=map(int, input().split(' ')); r=[0]*q; f=[0]*q; t=[0]*q; v=[0]*q; for i in range(q): l=list(map(int, input().split(' '))); r[i]=l[0]; if r[i]==1: f[i]=l[1]-1; t[i]=l[2]-1; else: v[i]=l[1]-1; gx=0; gy=0; s=0; for i in range(n): ip=i+1; if ip==n: ip=0; ds=x[i]*y[ip]-x[ip]*y[i]; s+=ds; gx+=(x[i]+x[ip])*ds; gy+=(y[i]+y[ip])*ds; s/=2; gx/=6*s; gy/=6*s; angles=[0]*n; for i in range(n): angles[i]=getAngle(gx, gy, x[i], y[i]); for i in range(n): x[i]-=gx; y[i]-=gy; alpha=0; #print('pos',gx, gy, alpha); #Восстанавливать положение точек будем по центру масс и углу #Угол - поворот против часовой вокруг центра масс fix={0, 1} for i in range(q): if r[i]==2: currX, currY = getCoordinate(gx, gy, alpha, x[v[i]], y[v[i]]); print("%.6f %.6f"%(currX, currY)) else: if len(fix)==2: fix.remove(f[i]); #print('remove',f[i]) #j - единственный элемент в множестве for j in fix: #print(j); currX, currY = getCoordinate(gx, gy, alpha, x[j], y[j]); #print('fix:', currX, currY) #dalpha=getAngle(gx, gy, currX, currY); #alpha+=dalpha; alpha=angles[j]; #Чтобы вычислить новые координаты g, нуно повернуть ее на угол #dalpha относительно currX, currY gx, gy=currX, currY-math.sqrt(x[j]**2+y[j]**2); #print('pos',gx, gy, alpha/math.pi) fix.add(t[i]); ```
output
1
95,017
23
190,035
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him. Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Tue Dec 3 11:47:37 2019 @author: CronuS """ def mult (m1, m2): return [[sum([m1[i][k] * m2[k][l] for k in range(3)]) for l in range(3)] for i in range(len(m1))] def getmatr (xc, yc, xi, yi, matr): xc_ = matr[0][0] * xc + matr[1][0] * yc + matr[2][0] yc_ = matr[0][1] * xc + matr[1][1] * yc + matr[2][1] xi_ = matr[0][0] * xi + matr[1][0] * yi + matr[2][0] yi_ = matr[0][1] * xi + matr[1][1] * yi + matr[2][1] s_ = ((xc_ - xi_) ** 2 + (yc_ - yi_) ** 2) ** 0.5 cosa_ = (yi_ - yc_) / s_ sina_ = -(xi_ - xc_) / s_ matrnew = [[cosa_, -sina_, 0], [sina_, cosa_, 0], [-xi_ * cosa_ - yi_ * sina_ + xi_, xi_ * sina_ + yi_ * cosa_ + yi_, 1]] matrnew = mult(matr, matrnew) return matrnew #print(mult(matr0, matr0)) s = list(map(int, input().split())) n = s[0]; q = s[1] p = [] mx = 0; my = 0; m = 0 k1 = 1; k2 = 2 for i in range(n): p = p + [list(map(int, input().split()))] if (i > 2): mxi = (p[0][0] + p[i - 1][0] + p[i][0]) / 3 myi = (p[0][1] + p[i - 1][1] + p[i][1]) / 3 mi = abs((p[i - 1][0] - p[0][0]) * (p[i][1] - p[0][1]) - (p[i][0] - p[0][0]) * (p[i - 1][1] - p[0][1])) mx = (mx * m + mxi * mi) / (m + mi) my = (my * m + myi * mi) / (m + mi) m = m + mi elif (i == 2): mx = (p[0][0] + p[1][0] + p[2][0]) / 3 my = (p[0][1] + p[1][1] + p[2][1]) / 3 m = abs((p[1][0] - p[0][0]) * (p[2][1] - p[0][1]) - (p[2][0] - p[0][0]) * (p[1][1] - p[0][1])) p1, p2 = 1, 2 matr = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] ii = 0 for i in range(q): z = list(map(int, input().split())) if (z[0] == 1): f = z[1]; t = z[2] if (f == p1): p1 = t ii = p2 - 1 pass else: p2 = t ii = p1 - 1 pass matr = getmatr(mx, my, p[ii][0], p[ii][1], matr) else: ii = z[1] - 1 m = mult([[p[ii][0], p[ii][1], 1]], matr) print(m[0][0], m[0][1]) #print(matr) ```
instruction
0
95,018
23
190,036
No
output
1
95,018
23
190,037
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him. Submitted Solution: ``` from sys import stdin, stdout from math import atan2, sqrt, sin, cos, pi def main(): n, q = stdin.readline().split() n = int(n) q = int(q) lines = stdin.readlines() x = [0] * (n + 1) y = [0] * (n + 1) for i in range(n): x[i], y[i] = lines[i].split() x[i] = int(x[i]) y[i] = int(y[i]) x[n] = x[0] y[n] = y[0] a = 0 cx = 0 cy = 0 for i in range(n): ii = i + 1 a += x[i] * y[ii] - x[ii] * y[i] cx += (x[i] + x[ii]) * (x[i] * y[ii] - x[ii] * y[i]) cy += (y[i] + y[ii]) * (x[i] * y[ii] - x[ii] * y[i]) a *= 3 r = [0.0] * n z = [0.0] * n for i in range(n): x[i] = x[i] * a - cx y[i] = y[i] * a - cy r[i] = sqrt(x[i] * x[i] + y[i] * y[i]) / a z[i] = atan2(y[i], x[i]) cx /= a cy /= a p = [0, 1] mt = 0.0 for i in range(q): tmp = lines[n + i].split() ta = int(tmp[0]) tb = int(tmp[1]) if ta == 1: ta = int(ta) ta -= 1 tb -= 1 if p[0] == tb: p[0], p[1] = p[1], p[0] p[1] = ta tc = z[p[0]] + mt cx += r[p[0]] * cos(tc) cy += r[p[0]] * (sin(tc) - 1) tc = pi / 2 - tc mt += tc else: tb -= 1 tc = z[tb] + mt stdout.write('%.10lf %.10lf\n' % (cx + r[tb] * cos(tc), cy + r[tb] * sin(tc))) if __name__ == '__main__': main() ```
instruction
0
95,019
23
190,038
No
output
1
95,019
23
190,039
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him. Submitted Solution: ``` import math; #Вычисление координаты точки по координатам центра, углу, и начальным относительно центра def getCoordinate(gx, gy, alpha, x, y): x1=gx+x*math.cos(alpha)-y*math.sin(alpha); y1=gy+x*math.sin(alpha)+y*math.cos(alpha); return x1, y1 #Вычисление угла, на который надо повернуть точку с координатами x, y, #чтобы она оказалась прямо над gx, gy def getAngle(gx, gy, x, y): x=x-gx; y=y-gy; cos=x/math.sqrt(x**2+y**2); alpha=math.acos(cos); if y<0: alpha=-alpha; return math.pi/2-alpha; n, q = map(int, input().split(' ')); x=[0]*n; y=[0]*n; for i in range(n): x[i], y[i]=map(int, input().split(' ')); r=[0]*q; f=[0]*q; t=[0]*q; v=[0]*q; for i in range(q): l=list(map(int, input().split(' '))); r[i]=l[0]; if r[i]==1: f[i]=l[1]-1; t[i]=l[2]-1; else: v[i]=l[1]-1; gx=0; gy=0; s=0; for i in range(n): ip=i+1; if ip==n: ip=0; ds=x[i]*y[ip]-x[ip]*y[i]; s+=ds; gx+=(x[i]+x[ip])*ds; gy+=(y[i]+y[ip])*ds; s/=2; gx/=6*s; gy/=6*s; for i in range(n): x[i]-=gx; y[i]-=gy; alpha=0; #print('pos',gx, gy, alpha); #Восстанавливать положение точек будем по центру масс и углу #Угол - поворот против часовой вокруг центра масс fix={0, 1} for i in range(q): if r[i]==2: currX, currY = getCoordinate(gx, gy, alpha, x[v[i]], y[v[i]]); print("%.6f %.6f"%(currX, currY)) else: if len(fix)==2: fix.remove(f[i]); #print('remove',f[i]) #j - единственный элемент в множестве for j in fix: #print(j); currX, currY = getCoordinate(gx, gy, alpha, x[j], y[j]); #print('fix:', currX, currY) dalpha=getAngle(gx, gy, currX, currY); #Чтобы вычислить новые координаты g, нуно повернуть ее на угол #dalpha относительно currX, currY gx, gy=currX, currY-math.sqrt(x[j]**2+y[j]**2); alpha+=dalpha; #print('pos',gx, gy, alpha/math.pi) fix.add(t[i]); ```
instruction
0
95,020
23
190,040
No
output
1
95,020
23
190,041
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance. Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices. Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types. * 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t. * 2 v: answer what are the coordinates of the vertex v. Please help Hag to answer his father's queries. You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again. Input The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries. The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct. The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father. It is guaranteed that there is at least one query of the second type. Output The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4} Examples Input 3 4 0 0 2 0 2 2 1 1 2 2 1 2 2 2 3 Output 3.4142135624 -1.4142135624 2.0000000000 0.0000000000 0.5857864376 -1.4142135624 Input 3 2 -1 1 0 0 1 1 1 1 2 2 1 Output 1.0000000000 -1.0000000000 Note In the first test note the initial and the final state of the wooden polygon. <image> Red Triangle is the initial state and the green one is the triangle after rotation around (2,0). In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him. Submitted Solution: ``` import sys read=lambda:map(int,sys.stdin.buffer.readline().split()) from math import * n,q=read() x,y=[],[] cx,cy=0,0 deg=0 p1,p2=0,1 def pos(i): return (cx+x[i]*cos(deg)-y[i]*sin(deg),cy+y[i]*cos(deg)+x[i]*sin(deg)) for i in range(n): _x,_y=read() cx+=_x;cy+=_y x.append(_x);y.append(_y) cx/=n;cy/=n for i in range(n): x[i]-=cx;y[i]-=cy for i in range(q): v=list(read()) if v[0]==1: if p1==v[1]-1: p1,p2=p2,None else: p1,p2=p1,None cx,cy=pos(p1);cy-=hypot(x[p1],y[p1]) deg=pi/2-atan2(y[p1],x[p1]);p2=v[2]-1 else: print("%.15f %.15f"%pos(v[1]-1)) ```
instruction
0
95,021
23
190,042
No
output
1
95,021
23
190,043
Provide a correct Python 3 solution for this coding contest problem. In the International City of Pipe Construction, it is planned to repair the water pipe at a certain point in the water pipe network. The network consists of water pipe segments, stop valves and source point. A water pipe is represented by a segment on a 2D-plane and intersected pair of water pipe segments are connected at the intersection point. A stop valve, which prevents from water flowing into the repairing point while repairing, is represented by a point on some water pipe segment. In the network, just one source point exists and water is supplied to the network from this point. Of course, while repairing, we have to stop water supply in some areas, but, in order to reduce the risk of riots, the length of water pipes stopping water supply must be minimized. What you have to do is to write a program to minimize the length of water pipes needed to stop water supply when the coordinates of end points of water pipe segments, stop valves, source point and repairing point are given. Input A data set has the following format: > N M > xs1 ys1 xd1 yd1 > ... > xsN ysN xdN ydN > xv1 yv1 > ... > xvM yvM > xb yb > xc yc > The first line of the input contains two integers, N (1 ≤ N ≤ 300) and M (0 ≤ M ≤ 1,000) that indicate the number of water pipe segments and stop valves. The following N lines describe the end points of water pipe segments. The i-th line contains four integers, xsi, ysi, xdi and ydi that indicate the pair of coordinates of end points of i-th water pipe segment. The following M lines describe the points of stop valves. The i-th line contains two integers, xvi and yvi that indicate the coordinate of end points of i-th stop valve. The following line contains two integers, xb and yb that indicate the coordinate of the source point. The last line contains two integers, xc and yc that indicate the coordinate of the repairing point. You may assume that any absolute values of coordinate integers are less than 1,000 (inclusive.) You may also assume each of the stop valves, the source point and the repairing point is always on one of water pipe segments and that that each pair among the stop valves, the source point and the repairing point are different. And, there is not more than one intersection between each pair of water pipe segments. Finally, the water pipe network is connected, that is, all the water pipes are received water supply initially. Output Print the minimal length of water pipes needed to stop water supply in a line. The absolute or relative error should be less than or 10-6. When you cannot stop water supply to the repairing point even though you close all stop valves, print "`-1`" in a line. Examples Input 1 2 0 0 10 0 1 0 9 0 0 0 5 0 Output 9.0 Input 5 3 0 4 2 4 0 2 2 2 0 0 2 0 0 0 0 4 2 0 2 4 0 2 1 0 2 2 1 4 2 1 Output 3.0 Input 2 1 0 0 0 4 0 2 2 2 1 2 0 1 0 3 Output -1
instruction
0
95,236
23
190,472
"Correct Solution: ``` from collections import deque import sys readline = sys.stdin.readline write = sys.stdout.write def dot3(O, A, B): ox, oy = O; ax, ay = A; bx, by = B return (ax - ox) * (bx - ox) + (ay - oy) * (by - oy) def cross3(O, A, B): ox, oy = O; ax, ay = A; bx, by = B return (ax - ox) * (by - oy) - (bx - ox) * (ay - oy) def dist2(A, B): ax, ay = A; bx, by = B return (ax - bx) ** 2 + (ay - by) ** 2 def is_intersection(L1, L2): P0 = L1[:2]; P1 = L1[2:] Q0 = L2[:2]; Q1 = L2[2:] C0 = cross3(P0, P1, Q0) C1 = cross3(P0, P1, Q1) D0 = cross3(Q0, Q1, P0) D1 = cross3(Q0, Q1, P1) if C0 == C1 == 0: return False return C0 * C1 <= 0 and D0 * D1 <= 0 def cross_point(L1, L2): x0, y0, x1, y1 = L1 x2, y2, x3, y3 = L2 dx0 = x1 - x0 dy0 = y1 - y0 dx1 = x3 - x2 dy1 = y3 - y2 s = (y0-y2)*dx1 - (x0-x2)*dy1 sm = dx0*dy1 - dy0*dx1 if s < 0: s = -s sm = -sm if s == 0: x = x0; y = y0 elif s == sm: x = x1; y = y1 else: x = x0 + s*dx0/sm; y = y0 + s*dy0/sm return x, y def solve(): N, M = map(int, readline().split()) ps = set() mp = {} LS = [] for i in range(N): x1, y1, x2, y2 = map(int, readline().split()) mp[x1, y1] = 0 mp[x2, y2] = 0 LS.append((x1, y1, x2, y2)) for i in range(N): L1 = LS[i] for j in range(i+1, N): L2 = LS[j] if is_intersection(L1, L2): x, y = cross_point(L1, L2) mp[x, y] = 0 for i in range(M): x, y = map(int, readline().split()) mp[x, y] = 1 xb, yb = map(int, readline().split()) mp[xb, yb] = 2 xc, yc = map(int, readline().split()) mp[xc, yc] = 2 *ps1, = mp.keys() ps1.sort(key = lambda x: (x[0], x[1])) mv = {e: i for i, e in enumerate(ps1)} *ps2, = mp.keys() ps2.sort(key = lambda x: (x[1], x[0])) ES = [] ms = list(map(mv.__getitem__, ps2)) ks = list(map(mp.__getitem__, ps1)) K = len(ps1) G = [[] for i in range(K)] for x1, y1, x2, y2 in LS: vs = [] if x1 != x2: if not x1 <= x2: x1, y1, x2, y2 = x2, y2, x1, y1 for k, (x, y) in enumerate(ps1): if x1 <= x <= x2 and abs((x - x1)*(y2 - y1) - (y - y1)*(x2 - x1)) < 1e-6: vs.append(k) else: if not y1 <= y2: y1, y2 = y2, y1 for k, (x, y) in zip(ms, ps2): if y1 <= y <= y2 and abs((x - x1)*(y2 - y1) - (y - y1)*(x2 - x1)) < 1e-6: vs.append(k) for i in range(len(vs)-1): k1 = vs[i]; k2 = vs[i+1] G[k1].append(k2) G[k2].append(k1) ES.append((k1, k2) if k1 <= k2 else (k2, k1)) s = mv[xc, yc]; t = mv[xb, yb] que = deque([s]) used = [0]*K used[s] = 1 e_used = set() while que: v = que.popleft() for w in G[v]: if w == t: write("-1\n") return e_used.add((v, w) if v <= w else (w, v)) if not used[w] and ks[w] != 1: que.append(w) used[w] = 1 que.append(t) e_used1 = set() used = [0]*K used[t] = 1 while que: v = que.popleft() for w in G[v]: e = (v, w) if v <= w else (w, v) if e in e_used: continue e_used1.add(e) if not used[w]: que.append(w) used[w] = 1 ans = 0 for k1, k2 in ES: if (k1, k2) in e_used1: continue x1, y1 = ps1[k1]; x2, y2 = ps1[k2] ans += ((x1 - x2)**2 + (y1 - y2)**2)**.5 write("%.16f\n" % ans) solve() ```
output
1
95,236
23
190,473
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,256
23
190,512
"Correct Solution: ``` import cmath import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10 ** 9) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 # MOD = 998244353 PI = cmath.pi TAU = cmath.pi * 2 class Point: EPS = 1e-9 def __init__(self, c: complex): self.c = c @property def x(self): return self.c.real @property def y(self): return self.c.imag @staticmethod def from_rect(x: float, y: float): return Point(complex(x, y)) @staticmethod def from_polar(r: float, phi: float): return Point(cmath.rect(r, phi)) def __add__(self, p): """ :param Point p: """ return Point(self.c + p.c) def __iadd__(self, p): """ :param Point p: """ self.c += p.c return self def __sub__(self, p): """ :param Point p: """ return Point(self.c - p.c) def __isub__(self, p): """ :param Point p: """ self.c -= p.c return self def __mul__(self, f: float): return Point(self.c * f) def __imul__(self, f: float): self.c *= f return self def __truediv__(self, f: float): return Point(self.c / f) def __itruediv__(self, f: float): self.c /= f return self def __repr__(self): return "({}, {})".format(round(self.x, 10), round(self.y, 10)) def __neg__(self): return Point(-self.c) def __eq__(self, p): return abs(self.c - p.c) < self.EPS def __abs__(self): return abs(self.c) def dot(self, p): """ 内積 :param Point p: :rtype: float """ return self.x * p.x + self.y * p.y def det(self, p): """ 外積 :param Point p: :rtype: float """ return self.x * p.y - self.y * p.x def dist(self, p): """ 距離 :param Point p: :rtype: float """ return abs(self.c - p.c) def r(self): """ 原点からの距離 :rtype: float """ return abs(self.c) def phase(self): """ 原点からの角度 :rtype: float """ return cmath.phase(self.c) def angle(self, p, q): """ p に向かってる状態から q まで反時計回りに回転するときの角度 :param Point p: :param Point q: :rtype: float """ return (cmath.phase(q.c - self.c) - cmath.phase(p.c - self.c)) % TAU def area(self, p, q): """ p, q となす三角形の面積 :param Point p: :param Point q: :rtype: float """ return abs((p - self).det(q - self) / 2) def projection_point(self, p, q, allow_outer=False): """ 線分 pq を通る直線上に垂線をおろしたときの足の座標 :param Point p: :param Point q: :param allow_outer: 答えが線分の間になくても OK :rtype: Point|None """ diff_q = q - p # p からの距離 r = (self - p).dot(diff_q) / abs(diff_q) # 線分の角度 phase = diff_q.phase() ret = Point.from_polar(r, phase) + p if allow_outer or (p - ret).dot(q - ret) < self.EPS: return ret return None def on_segment(self, p, q): """ 点が線分 pq の上に乗っているか :param Point p: :param Point q: :rtype: bool """ return abs((p - self).det(q - self)) < self.EPS and (p - self).dot(q - self) < self.EPS p1x, p1y, p2x, p2y = list(map(int, sys.stdin.buffer.readline().split())) Q = int(sys.stdin.buffer.readline()) XY = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(Q)] p1 = Point.from_rect(p1x, p1y) p2 = Point.from_rect(p2x, p2y) for x, y in XY: p = Point.from_rect(x, y) ans = p.projection_point(p1, p2, allow_outer=True) print('{:.10f} {:.10f}'.format(ans.x, ans.y)) ```
output
1
95,256
23
190,513
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,257
23
190,514
"Correct Solution: ``` x1, y1, x2, y2 = map(int, input().split()) q = int(input()) p1p2 = complex(x2 - x1, y2 - y1) d2_p1p2 = abs(p1p2) ** 2 conj_p1p2 = p1p2.conjugate() while q: q -= 1 x0, y0 = map(int, input().split()) p1p0 = complex(x0 - x1, y0 - y1) t = (conj_p1p2 * p1p0).real / d2_p1p2 print(x1 + p1p2.real * t, y1 + p1p2.imag * t) ```
output
1
95,257
23
190,515
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,258
23
190,516
"Correct Solution: ``` #!/usr/bin/env python3 def inner_product(v1, v2): return v1.real * v2.real + v1.imag * v2.imag # ????????????b???????????????a??????????????´????????£?°???±??????????????????????????? def projection(a, b): return a * inner_product(a, b) / (abs(a) ** 2) def solve(p0, p1, p2): a = p1 - p0 b = p2 - p0 pro = projection(a, b) t = p0 + pro return t def main(): x_p0, y_p0, x_p1, y_p1 = map(float, input().split()) p0 = complex(x_p0, y_p0) p1 = complex(x_p1, y_p1) q = int(input()) for _ in range(q): p2 = complex(*map(float, input().split())) t = solve(p0, p1, p2) print("{:.10f} {:.10f}".format(t.real, t.imag)) if __name__ == '__main__': main() ```
output
1
95,258
23
190,517
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,259
23
190,518
"Correct Solution: ``` #!/usr/bin/env python3 # CGL_1_A: Points/Vectors - Projection def proj(p1, p2, p): x1, y1 = p1 x2, y2 = p2 x, y = p if x1 == x2: return x1 * 1.0, y * 1.0 elif y1 == y2: return x * 1.0, y1 * 1.0 else: dx = x1 - x2 dy = y1 - y2 return (((dx*x + dy*y)*dx - (x1*y2 - x2*y1)*dy) / (dx*dx + dy*dy), ((dx*x + dy*y)*dy + (x1*y2 - x2*y1)*dx) / (dx*dx + dy*dy)) def run(): px1, py1, px2, py2 = [int(v) for v in input().split()] q = int(input()) for _ in range(q): x, y = [int(v) for v in input().split()] print("{:.10f} {:.10f}".format(*proj((px1, py1), (px2, py2), (x, y)))) if __name__ == '__main__': run() ```
output
1
95,259
23
190,519
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,260
23
190,520
"Correct Solution: ``` import math class Point: def __init__(self, x, y): self.x = x self.y = y def __sub__(self, other): return Point(self.x - other.x, self.y - other.y) def __repr__(self): return f"({self.x},{self.y})" class Segment: def __init__(self, x: Point, y: Point): self.pt1 = x self.pt2 = y self.vector = self.pt2 - self.pt1 self.norm = pow(self.vector.x, 2) + pow(self.vector.y, 2) self.abs = math.sqrt(self.norm) def dot(self, other): return self.vector.x * other.vector.x + self.vector.y * other.vector.y def cross(self, other): return self.vector.x * other.vector.y - self.vector.y * other.vector.x def projection(self, pt: Point)-> Point: vec_p1_to_pt = Segment(self.pt1, pt) t = self.dot(vec_p1_to_pt) / self.abs x = self.pt1.x + t / self.abs * self.vector.x y = self.pt1.y + t / self.abs * self.vector.y return Point(x, y) def __repr__(self): return f"{self.pt1},{self.pt2},{self.vector}" def main(): p0_x, p0_y, p1_x, p1_y = map(int, input().split()) seg_1 = Segment(Point(p0_x, p0_y), Point(p1_x, p1_y)) num_query = int(input()) for i in range(num_query): pt_x , pt_y = map(int, input().split()) proj = seg_1.projection(Point(pt_x, pt_y)) print("{:.10f} {:.10f}".format(proj.x, proj.y)) return main() ```
output
1
95,260
23
190,521
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,261
23
190,522
"Correct Solution: ``` L1=input().split( ) [xp1,yp1,xp2,yp2]=[int(L1[0]),int(L1[1]),int(L1[2]),int(L1[3])] direction=[xp2-xp1,yp2-yp1] norm_direc=[(xp2-xp1)/((xp2-xp1)**2+(yp2-yp1)**2)**(1/2),(yp2-yp1)/((xp2-xp1)**2+(yp2-yp1)**2)**(1/2)] q=int(input()) L=[] for i in range(q): L2=input().split( ) L.append([int(L2[0]),int(L2[1])]) Point=[] for i in range(q): Point.append([(L[i][0]-xp1),(L[i][1]-yp1)]) Vec=[] for i in range(q): Vec.append([L[i][0]-xp1,L[i][1]-yp1]) Proj=[] for i in range(q): inter=Vec[i][0]*norm_direc[0]+Vec[i][1]*norm_direc[1] Proj.append([norm_direc[0]*inter,norm_direc[1]*inter]) L2=[] for i in range(q): L2.append([xp1+Proj[i][0],yp1+Proj[i][1]]) for i in range(q): print(*L2[i]) ```
output
1
95,261
23
190,523
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,262
23
190,524
"Correct Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 0 0 3 4 1 2 5 output: 3.1200000000 4.1600000000 """ import sys def solve(_prj_info): for point in _prj_info: xp, yp = map(int, point) p = xp + yp * 1j hypo = p - p1 prj = p1 + base * project(base, hypo) print('{0:.10f} {1:.10f}'.format(prj.real, prj.imag)) return _prj_info def dot(a, b): return a.real * b.real + a.imag * b.imag def project(a, b): return dot(a, b) / dot(a, a) if __name__ == '__main__': _input = sys.stdin.readlines() base_info = _input[0].split() q_num = int(_input[1]) prj_points = map(lambda x: x.split(), _input[2:]) x1, y1, x2, y2 = map(int, base_info) p1, p2 = x1 + y1 * 1j, x2 + y2 * 1j base = p2 - p1 res = solve(prj_points) ```
output
1
95,262
23
190,525
Provide a correct Python 3 solution for this coding contest problem. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000
instruction
0
95,263
23
190,526
"Correct Solution: ``` from math import hypot x1, y1, x2, y2 = map(int, input().split()) dx, dy = x2-x1, y2-y1 vector_a = hypot(dx, dy) q = int(input()) for x3, y3 in (map(int, input().split()) for _ in [0]*q): d = ((x3-x1)*dx + (y3-y1)*dy) / (vector_a**2) print(x1+dx*d, y1+dy*d) ```
output
1
95,263
23
190,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p. Output For each query, print the coordinate of the projection point x. The output values should be in a decimal fraction with an error less than 0.00000001. Examples Input 0 0 2 0 3 -1 1 0 1 1 1 Output -1.0000000000 0.0000000000 0.0000000000 0.0000000000 1.0000000000 0.0000000000 Input 0 0 3 4 1 2 5 Output 3.1200000000 4.1600000000 Submitted Solution: ``` from decimal import Decimal as D a,s,d,f=map(D,input().split()) h=[d-a,f-s] leh=((d-a)**2+(f-s)**2)**(D("0.5")) for i in range(int(input())): x,y=map(D,input().split()) co=(x-a)*h[0]/leh+(y-s)*h[1]/leh print(a+h[0]*co/leh,s+h[1]*co/leh) ```
instruction
0
95,264
23
190,528
Yes
output
1
95,264
23
190,529