Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader jin, PrintWriter jout) { int x1 = jin.int32(); int y1 = jin.int32(); int x2 = jin.int32(); int y2 = jin.int32(); int cnt = 0; int n = jin.int32(); for(int i = 0; i < n; i++) { long a = jin.int32(); long b = jin.int32(); long c = jin.int32(); long v1 = a * x1 + b * y1 + c; long v2 = a * x2 + b * y2 + c; if(Long.signum(v1) * Long.signum(v2) < 0) cnt++; } jout.println(cnt); } } class InputReader { private static final int bufferMaxLength = 1024; private InputStream in; private byte[] buffer; private int currentBufferSize; private int currentBufferTop; private static final String tokenizers = " \t\r\f\n"; public InputReader(InputStream stream) { this.in = stream; buffer = new byte[bufferMaxLength]; currentBufferSize = 0; currentBufferTop = 0; } private boolean refill() { try { this.currentBufferSize = this.in.read(this.buffer); this.currentBufferTop = 0; } catch(Exception e) {} return this.currentBufferSize > 0; } public Byte readChar() { if(currentBufferTop < currentBufferSize) { return this.buffer[this.currentBufferTop++]; } else { if(!this.refill()) { return null; } else { return readChar(); } } } public String token() { StringBuffer tok = new StringBuffer(); Byte first; while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) != -1)); if(first == null) return null; tok.append((char)first.byteValue()); while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) == -1)) { tok.append((char)first.byteValue()); } return tok.toString(); } public String next() { return token(); } public Integer int32() throws NumberFormatException { String tok = token(); return tok == null? null : Integer.parseInt(tok); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
/* * * @author Mukesh Singh * */ /* Finding_the_Minimum_Window_in_S_which_Contains_All_Elements_from_T */ import java.io.*; import java.util.*; import java.text.DecimalFormat; @SuppressWarnings("unchecked") public class AB { //solve test cases void solve() throws Exception { int x1 = in.nextInt(); int y1 = in.nextInt(); int x2 = in.nextInt(); int y2 = in.nextInt(); int n = in.nextInt(); int ans = 0 ; for( int i= 0; i < n ; i++) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); if(f(x1,y1,x2,y2,a,b,c)) ans++; } System.out.println(ans); } boolean f(long x1, long y1 , long x2 , long y2 , long a , long b, long c) { long ss = a*x1+b*y1+c ; long dd = a*x2+b*y2+c ; if((ss< 0 && dd<0)||(ss>0&&dd>0)) return false ; return true ; } //@ main function public static void main(String[] args) throws Exception { new AB(); } InputReader in; PrintStream out ; DecimalFormat df ; AB() { try { File defaultInput = new File("file.in"); if (defaultInput.exists()) in = new InputReader("file.in"); else in = new InputReader(); defaultInput = new File("file.out"); if (defaultInput.exists()) out = new PrintStream(new FileOutputStream("file.out")); else out = new PrintStream(System.out); df = new DecimalFormat("######0.00"); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(261); } } class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader() { reader = new BufferedReader(new InputStreamReader(System.in)); } InputReader(String fileName) throws FileNotFoundException { reader = new BufferedReader(new FileReader(new File(fileName))); } String readLine() throws IOException { return reader.readLine(); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(readLine()); return tokenizer.nextToken(); } boolean hasMoreTokens() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String s = readLine(); if (s == null) return false; tokenizer = new StringTokenizer(s); } return true; } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
// Main Code at the Bottom import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; public class Main { //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=1000000000+7; //Euclidean Algorithm static long gcd(long A,long B){ return (B==0)?A:gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //Modular Inverse static long inverse(long x) { return fastExpo(x,MOD-2); } //Prime Number Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static void reverse(char arr[],int l,int r){ while(l<r) { char tmp=arr[l]; arr[l++]=arr[r]; arr[r--]=tmp; } } //Print array static void print1d(int arr[]) { out.println(Arrays.toString(arr)); } static void print2d(int arr[][]) { for(int a[]: arr) out.println(Arrays.toString(a)); } // Pair static class pair{ int x,y; pair(int a,int b){ this.x=a; this.y=b; } public boolean equals(Object obj) { if(obj == null || obj.getClass()!= this.getClass()) return false; pair p = (pair) obj; return (this.x==p.x && this.y==p.y); } public int hashCode() { return Objects.hash(x,y); } } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Main function(The main code starts from here public static void main (String[] args) throws java.lang.Exception { int test=1; //test=sc.nextInt(); while(test-->0){ long x1=sc.nextInt(),y1=sc.nextInt(),x2=sc.nextInt(),y2=sc.nextInt(); long n=sc.nextInt(),ans=0; for(int i=0;i<n;i++) { int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(); long v1=a*x1+b*y1+c>0?1:-1,v2=a*x2+b*y2+c>0?1:-1; if(v1!=v2) ans++; } out.println(ans); } out.flush(); out.close(); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const int N = 310; long long xh, yh, xu, yu; int n; long long a[N], b[N], c[N]; int main() { scanf("%I64d%I64d", &xh, &yh); scanf("%I64d%I64d", &xu, &yu); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d%I64d%I64d", a + i, b + i, c + i); } int cnt = 0; for (int i = 0; i < n; ++i) { if (a[i] * xu + b[i] * yu + c[i] < 0 && a[i] * xh + b[i] * yh + c[i] > 0) { ++cnt; } else if (a[i] * xu + b[i] * yu + c[i] > 0 && a[i] * xh + b[i] * yh + c[i] < 0) { ++cnt; } } printf("%d\n", cnt); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long int x1, yy1, x2, y2; long long int A, B, C; const double eps = 0.0; int check(long long int a, long long int b, long long int c) { long long int det = A * b - a * B; if (det == 0) return 0; double x = (((double)b) / det) * C - (((double)B) / det) * c; double y = (((double)A) / det) * c - (((double)a) / det) * C; double mx, Mx, my, My; mx = min(x1, x2); Mx = max(x1, x2); my = min(yy1, y2); My = max(yy1, y2); if (x - mx >= eps && Mx - x >= eps && y - my >= eps && My - y >= eps) return 1; return 0; } int main() { scanf("%I64d %I64d", &x1, &yy1); scanf("%I64d %I64d", &x2, &y2); A = y2 - yy1; B = x1 - x2; C = A * x1 + B * yy1; int n, tot = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { long long int a, b, c; scanf("%I64d %I64d %I64d", &a, &b, &c); tot += check(a, b, -c); } printf("%d\n", tot); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1 , y1 = map(int , input().split()) x2 , y2 = map(int , input().split()) n = int(input()) res = 0 for i in range(n): a , b , c = map(int , input().split()) x = (a * x1) + (b * y1) + c y = (a * x2) + (b * y2) + c if ((x < 0) and (y > 0)) or ((x > 0) and (y < 0)): res = res + 1 print(res)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.*; import java.util.*; public class Template implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } class GraphBuilder { int n, m; int[] x, y; int index; int[] size; GraphBuilder(int n, int m) { this.n = n; this.m = m; x = new int[m]; y = new int[m]; size = new int[n]; } void add(int u, int v) { x[index] = u; y[index] = v; size[u]++; size[v]++; index++; } int[][] build() { int[][] graph = new int[n][]; for (int i = 0; i < n; i++) { graph[i] = new int[size[i]]; } for (int i = index - 1; i >= 0; i--) { int u = x[i]; int v = y[i]; graph[u][--size[u]] = v; graph[v][--size[v]] = u; } return graph; } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long[] readLongArray(int size) throws IOException { long[] res = new long[size]; for (int i = 0; i < size; i++) { res[i] = readLong(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } <T> List<T>[] createGraphList(int size) { List<T>[] list = new List[size]; for (int i = 0; i < size; i++) { list[i] = new ArrayList<>(); } return list; } public static void main(String[] args) { new Template().run(); // new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } int simple(String s) { int res = 0; while (true) { String was = s; s = s.replaceFirst("ab", "bba"); if (s.equals(was)) break; res++; } return res; } void solve() throws IOException { int x1 = readInt(); int y1 = readInt(); int x2 = readInt(); int y2 = readInt(); int n = readInt(); int res = 0; for (int i = 0; i < n; i++) { int a = readInt(); int b = readInt(); int c = readInt(); long val1 = (long) a * x1 + (long) b * y1 + c; long val2 = (long) a * x2 + (long) b * y2 + c; if (sgn(val1) * sgn(val2) < 0) { res++; } } out.println(res); } long sgn(long v) { return v == 0 ? 0 : (v > 0 ? 1 : -1); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; typedef struct point { double x, y; point(double x = 0, double y = 0) : x(x), y(y) {} } Vector; point st, en; int n; int s1[311], s2[311]; Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator-(point A, point B) { return Vector(A.x - B.x, A.y - B.y); } double cross(Vector p1, Vector p2) { return p1.x * p2.y - p2.x * p1.y; } int main() { cin >> st.x >> st.y >> en.x >> en.y; scanf("%d", &n); int ans = 0; for (int i = 1; i <= n; i++) { point q1, q2; double a, b, c; cin >> a >> b >> c; if (fabs(a) < 1e-8) { q1.x = 0; q1.y = -c / b; q2.x = 10; q2.y = q1.y; } else if (fabs(b) < 1e-8) { q1.x = -c / a; q1.y = 0; q2.x = q1.x; q2.y = 10; } else { q1.x = 0; q1.y = -c / b; q2.x = 10; q2.y = (-q2.x * a - c) / b; } double res1 = cross(st - q1, q2 - q1); double res2 = cross(en - q1, q2 - q1); if (res1 * res2 < 0) ans++; } printf("%d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long x_1, y_1, x_2, y_2, n, a, b, c, ans; int main() { scanf("%I64d%I64d%I64d%I64d", &x_1, &y_1, &x_2, &y_2); scanf("%I64d", &n); while (n-- > 0) { scanf("%I64d%I64d%I64d", &a, &b, &c); long long D = a * x_1 + b * y_1 + c; long long E = a * x_2 + b * y_2 + c; if ((D > 0 && E < 0) || (D < 0 && E > 0)) ans++; } printf("%I64d", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int sgn(double x) { if (fabs(x) < 1e-6) { return 0; } else if (x < 0) { return -1; } else return 1; } int dcmp(double x, double y) { if (sgn(x - y) == 1) { return 1; } else if (sgn(x - y) == 0) { return 0; } return -1; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} }; typedef Point Vector; Vector operator+(const Vector A, const Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator-(const Vector A, const Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator*(const Vector A, const double p) { return Vector(A.x * p, A.y * p); } Vector operator/(const Vector A, const double p) { return Vector(A.x / p, A.y / p); } double operator*(const Vector A, const Vector B) { return A.x * B.x + A.y * B.y; } double operator^(const Vector A, const Vector B) { return A.x * B.y - A.y * B.x; } bool operator==(const Vector A, const Vector B) { if (dcmp(A.x, B.x) == 0 && dcmp(A.y, B.y) == 0) { return true; } return false; } double Length(const Vector A) { return sqrt(A * A); } double Angle(Vector A, Vector B) { return acos((A * B) / Length(A) / Length(B)); } double ArcToAngle(double x) { return x * 180.0 / pi; } double AngleToArc(double x) { return x / 180 * pi; } double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } const int maxn = 300 + 10; int sx, sy, gx, gy; int n; struct node { int a, b, c; } no[maxn]; int main() { Vector ps, pg; scanf("%lf %lf", &ps.x, &ps.y); scanf("%lf %lf", &pg.x, &pg.y); scanf("%d", &n); int ans = 0; for (int i = 1; i <= n; i++) { double a, b, c; scanf("%lf %lf %lf", &a, &b, &c); double tmp1 = a * ps.x + b * ps.y + c; double tmp2 = a * pg.x + b * pg.y + c; if (sgn(tmp1) == 0 || sgn(tmp2) == 0) { ans++; } else if (sgn(tmp1 * tmp2) <= 0) { ans++; } } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.Scanner; public class Prob284 { /** * @param args */ public static void main(String[] args) { Scanner reader = new Scanner(System.in); long hx = reader.nextLong(); long hy = reader.nextLong(); long ux = reader.nextLong(); long uy = reader.nextLong(); int n = reader.nextInt(); int cnt = 0; for(int i=0; i<n; i++) { long a = reader.nextLong(); long b = reader.nextLong(); long c = reader.nextLong(); long dh = hx*a+hy*b+c; long du = ux*a+uy*b+c; if (Long.signum(dh)*Long.signum(du) < 0) cnt ++; } System.out.println(cnt); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1164777216") using namespace std; template <class T> inline T sqr(T x) { return x * x; } const double pi = acos(-1.0), eps = 1e-15; const int INF = 1000 * 1000 * 1000 + 5, MAXN = 100005, MOD = 1000007; const long long INFL = 1000000000000000005; void prepare(string s) { if ((int)s.size() != 0) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } } long long x, y, X, Y, cnt = 0, n; inline bool check(long long &a, long long &b, long long &c, long long x, long long y) { return (a * x + b * y + c) < 0; } void solve() { cin >> x >> y >> X >> Y >> n; for (int i = 0; i < n; ++i) { long long a, b, c; cin >> a >> b >> c; if (check(a, b, c, x, y) && !check(a, b, c, X, Y)) cnt++; if (!check(a, b, c, x, y) && check(a, b, c, X, Y)) cnt++; } cout << cnt; } int main() { prepare(""); srand(time(NULL)); solve(); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
p1=raw_input() p1=p1.split() x1=int(p1[0]) y1=int(p1[1]) p2=raw_input() p2=p2.split() x2=int(p2[0]) y2=int(p2[1]) n=int(input()) ans = 0 while n>0: p=raw_input() p=p.split() a=int(p[0]) b=int(p[1]) c=int(p[2]) if((a*x1+b*y1+c)*(a*x2+b*y2+c)<=0): ans+=1 n-=1 print ans
PYTHON
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CrazyTown { static final double EPS = 1e-9; public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); Point home = new Point(sc.nextDouble(),sc.nextDouble()); Point uni = new Point(sc.nextDouble(),sc.nextDouble()); int n = sc.nextInt(); int lines = 0; for(int i = 0;i<n;i++) { Line l = new Line(sc.nextDouble(), sc.nextDouble(), sc.nextDouble()); if((calc(home,l) + EPS < 0) != (calc(uni,l) + EPS < 0)) lines++; } System.out.println(lines); } static double calc(Point p,Line l) { return (p.x * l.a) + (p.y * l.b) + (l.c); } static class Point { double x,y; Point(double a,double b) { x = a; y = b; } } static class Line { double a,b,c; Line(double a,double b,double c) { this.a = a; this.b = b; this.c = c; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public long[] nextLongArr(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; pair<int, int> cr[2]; long long n, cnt; int main() { cin >> cr[0].first >> cr[0].second >> cr[1].first >> cr[1].second; cin >> n; long long a, b, c; for (int i = 0; i < n; i++) { cin >> a >> b >> c; long long p = a * cr[0].first + b * cr[0].second + c; long long q = a * cr[1].first + b * cr[1].second + c; if ((p > 0) ^ (q > 0)) cnt++; } cout << cnt << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) k = 0 n = int(input()) for i in range(n): a, b, c = map(int, input().split()) if (a * x1 + b * y1 + c) * (a * x2 + b * y2 + c) < 0: k += 1 print(k)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemACrazyTown solver = new ProblemACrazyTown(); solver.solve(1, in, out); out.close(); } static class ProblemACrazyTown { public void solve(int testNumber, InputReader in, PrintWriter out) { long x1 = in.readInt(); long y1 = in.readInt(); long x2 = in.readInt(); long y2 = in.readInt(); int n = in.readInt(); int ans = 0; for (int i = 0; i < n; i++) { int a = in.readInt(); int b = in.readInt(); int c = in.readInt(); double v1 = a * x1 + b * y1 + c; double v2 = a * x2 + b * y2 + c; if (v1 * v2 < 0) ans++; } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
def main(): rdl = list(map(int,input().split())) x1,y1 = rdl rdl = list(map(int,input().split())) x2,y2 = rdl n = int(input()) Steps = 0 for i in range(n): rdl = list(map(int,input().split())) if rdl[1]*y1+rdl[0]*x1+rdl[2] > 0 and rdl[1]*y2+rdl[0]*x2+rdl[2] < 0: Steps += 1 elif rdl[1]*y1+rdl[0]*x1+rdl[2] < 0 and rdl[1]*y2+rdl[0]*x2+rdl[2] > 0: Steps += 1 print(Steps) main()
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.Scanner; public class CF284C { public static void main(String [] args) { Scanner input = new Scanner(System.in); long x1 = input.nextLong(); long y1 = input.nextLong(); long x2 = input.nextLong(); long y2 = input.nextLong(); long n = input.nextLong(); long ans = 0; for (long i = 0; i < n; i++) { long ai = input.nextLong(); long bi = input.nextLong(); long ci = input.nextLong(); if ((ai * x1 + bi * y1 + ci) > 0 && (ai * x2 + bi * y2 + ci) < 0 || (ai * x1 + bi * y1 + ci) < 0 && (ai * x2 + bi * y2 + ci) > 0) { ans++; } } input.close(); System.out.println(ans); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long hx, hy, sx, sy, n; long long a[305], b[305], c[305]; int main() { cin >> hx >> hy >> sx >> sy >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i] >> c[i]; c[i] = -c[i]; } int cou = 0; for (int i = 0; i < n; i++) { if ((a[i] * hx + b[i] * hy > c[i] && a[i] * sx + b[i] * sy < c[i]) || (a[i] * hx + b[i] * hy < c[i] && a[i] * sx + b[i] * sy > c[i])) cou++; } cout << cou << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long p1, p2; long long q1, q2; scanf("%I64d%I64d%I64d%I64d", &p1, &p2, &q1, &q2); int n; scanf("%d", &n); long long a, b, c; int val1 = 1, val2 = 1; int ans = 0; for (int i = 0; i < n; i++) { scanf("%I64d%I64d%I64d", &a, &b, &c); if (a * p1 + b * p2 + c < 0) val1 = -1; if (a * q1 + b * q2 + c < 0) val2 = -1; if ((val1 < 0 && val2 > 0) || (val1 > 0 && val2 < 0)) { ans += 1; } val1 = val2 = 1; } printf("%d", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } const long long N = ((long long)1e6); long long gcd(long long a, long long b) { if (b > a) { return gcd(b, a); } if (b == 0) { return a; } return gcd(b, a % b); } void swap(long long &x, long long &y) { long long temp = x; x = y; y = temp; } long long mod_add(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } long long mod_mul(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } long long mod_sub(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } long long power(long long x, long long y, long long m) { long long res = 1; while (y > 0) { if (y & 1) res = (res * x) % m; y = y >> 1; x = x * x % m; } return res; } bool check(long long x, long long y) { if (x > 0 and y > 0) { return true; } if (x < 0 and y < 0) { return true; } return false; } void solve() { long long h1, h2; cin >> h1 >> h2; long long u1, u2; cin >> u1 >> u2; long long n; cin >> n; long long ans = 0; for (long long i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; long long x = a * h1 + b * h2 + c; long long y = a * u1 + b * u2 + c; if (!check(x, y)) { ans++; } } cout << ans << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; t = 1; for (long long i = 1; i <= t; i++) { solve(); } return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CFC { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); String[] s=br.readLine().split(" "); int x1=Integer.parseInt(s[0]); int y1=Integer.parseInt(s[1]); String[] ss=br.readLine().split(" "); int x2=Integer.parseInt(ss[0]); int y2=Integer.parseInt(ss[1]); int N=Integer.parseInt(br.readLine()); int count=0; for(int i=0;i<N;i++) { String[] temp=br.readLine().split(" "); double a=Integer.parseInt(temp[0]); double b=Integer.parseInt(temp[1]); double c=Integer.parseInt(temp[2]); double t=(a*x1+b*y1+c)*(a*x2+b*y2+c); //System.out.println(t); if(t<0) { count++; } } System.out.println(count); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
# -*- coding: utf-8 -*- import sys import heapq from collections import defaultdict from collections import OrderedDict import copy from collections import deque class Solver(object): def run(self): p1 = tuple(readarray(int)) p2 = tuple(readarray(int)) n = read(int) lines = [readarray(int) for i in xrange(n)] def line2point(a, b, c): c = float(c) def _workX(x): # a*x + b*y + c = 0 return (-1*c - a*x) / b def _workY(y): # a*x + b*y + c = 0 return (-1*c - b*y) / a if b != 0: return _workX else: return _workY res = 0 for line in lines: points = [] # convert line into points a, b, c = line func = line2point(a, b, c) if b != 0: points.append( (0, func(0)) ) points.append( (1, func(1)) ) else: points.append( (func(0), 0) ) points.append( (func(1), 1) ) if lineIntersect(points[0], points[1], p1, p2): res += 1 print(res) #################################### # ここからは見けゃダパ! def read(foo): return foo(raw_input()) def readarray(foo): return [foo(x) for x in raw_input().split()] def dbg(a): sys.stderr.write(str(a)) def dbgln(a): sys.stderr.write(str(a) + "\n") # https://gist.github.com/kachayev/5910538 def topological(graph): GRAY, BLACK = 0, 1 order, enter, state = deque(), set(graph), {} def dfs(node): state[node] = GRAY for k in graph.get(node, ()): sk = state.get(k, None) if sk == GRAY: raise ValueError("cycle") if sk == BLACK: continue enter.discard(k) dfs(k) order.appendleft(node) state[node] = BLACK while enter: dfs(enter.pop()) return order # l1 to l2 -> line # ls1 to ls2 -> line segment def lineIntersect( l1, l2, ls1, ls2 ): l1x, l1y = l1 l2x, l2y = l2 ls1x, ls1y = ls1 ls2x, ls2y = ls2 if (((l1x - l2x) * (ls1y - l1y) + (l1y - l2y) * (l1x - ls1x)) * ((l1x - l2x) * (ls2y - l1y) + (l1y - l2y) * (l1x - ls2x)) > 0): return False return True # p1 to p2 -> line segment # p3 to p4 -> line segemnt def intersect( p1, p2, p3, p4 ): if lineIntersect(p1, p2, p3, p4) and lineIntersect(p3, p4, p1, p2): return True return False if __name__ == '__main__': Solver().run()
PYTHON
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; long long int C(int n, int k) { long long ans = 1; k = k > n - k ? n - k : k; int j = 1; for (; j <= k; j++, n--) { if (n % j == 0) { ans *= n / j; } else if (ans % j == 0) { ans = ans / j * n; } else { ans = (ans * n) / j; } } return ans; } long long int powmod(long long int a, long long int b) { long long int res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long int gcd(long long int a, long long int b) { if (a == 0) return (b); else return (gcd(b % a, a)); } long long int powmod(long long int a, long long int b, long long int m) { long long int res = 1; a %= m; for (; b; b >>= 1) { if (b & 1) res = res * a % m; a = a * a % m; } return res; } int main() { int h_x, h_y; cin >> h_x >> h_y; int u_x, u_y; cin >> u_x >> u_y; int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; long long int val1 = (long long int)a * h_x + (long long int)b * h_y + (long long int)c; long long int val2 = (long long int)a * u_x + (long long int)b * u_y + (long long int)c; if (val1 > 0 and val2 < 0) ans++; else if (val1 < 0 and val2 > 0) ans++; } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.Scanner; public class CF284A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x1 = in.nextInt(); int y1 = in.nextInt(); int x2 = in.nextInt(); int y2 = in.nextInt(); int n = in.nextInt(); int ans = 0; for (int i = 0; i < n; i++) { long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); long x = -(a * x1 + b * y1); long y = -(a * x2 + b * y2); if ((x < c && y > c) || (x > c && y < c)) ans++; } System.out.println(ans); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long long MX = 1e9; const long long INF = 1e9; void print_a(vector<int> v) { if (v.size()) cout << v[0]; for (int i = 1; i < v.size(); ++i) cout << ' ' << v[i]; cout << '\n'; } vector<vector<int> > init_vvi(int n, int m, int val) { return vector<vector<int> >(n, vector<int>(m, val)); } vector<vector<long long> > init_vvl(int n, int m, long long val) { return vector<vector<long long> >(n, vector<long long>(m, val)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; long long a[n + 1], b[n + 1], c[n + 1]; for (int i = 1; i < n + 1; ++i) cin >> a[i] >> b[i] >> c[i]; int ans = 0; long long s1, s2; for (int i = 1; i < n + 1; ++i) { s1 = (a[i] * x1) + (b[i] * y1) + c[i]; s2 = (a[i] * x2) + (b[i] * y2) + c[i]; if ((s1 >= 0 && s2 < 0) || (s1 < 0 && s2 >= 0)) ++ans; } cout << ans << '\n'; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; long long a, b, c; long long j, k, moves = 0; while (n--) { cin >> a >> b >> c; j = -1 * (y1 * b + c); k = -1 * (y2 * b + c); if ((j > x1 * a && k < x2 * a) || (j < x1 * a && k > x2 * a)) moves++; } cout << moves << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(in, out); out.close(); } } class Solver { int sign(long x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } public void solve(InputReader in, PrintWriter out) { long x1 = in.nextInt(); long y1 = in.nextInt(); long x2 = in.nextInt(); long y2 = in.nextInt(); int ans = 0; int n = in.nextInt(); for (int i = 0; i < n; i++) { long a = in.nextInt(); long b = in.nextInt(); long c = in.nextInt(); if (sign(a * x1 + b * y1 + c) != sign(a * x2 + b * y2 + c)) ans++; } out.println(ans); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } const int N = 1e5 + 7; const int xinc[] = {0, 0, 1, -1}; const int yinc[] = {1, -1, 0, 0}; long long x1, _y1, x2, y2, a, b, c, n, ans; bool same(long long a, long long b) { return ((a > 0 && b > 0) || (a < 0 && b < 0)); } void solve() { cin >> x1 >> _y1 >> x2 >> y2; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b >> c; if (!same(a * x1 + b * _y1 + c, a * x2 + b * y2 + c)) ans++; } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) solve(); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.*; public class crazyTown { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double x1 = sc.nextInt(); double y1 = sc.nextInt(); double x2 = sc.nextInt(); double y2 = sc.nextInt(); int n = sc.nextInt(); int cnt = 0; while(n-->0) { double a = sc.nextInt(); double b = sc.nextInt(); double c = sc.nextInt(); double t1 = a*x1 + b*y1 +c; double t2 = a*x2 + b*y2 +c; if(t1*t2<0) ++cnt; } System.out.println(cnt); }}
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import sys def evaluate(a, b, c, x, y): return a*x + b*y + c start_x, start_y = [int(x) for x in sys.stdin.readline().split()] dest_x, dest_y = [int(x) for x in sys.stdin.readline().split()] n_lines = int(sys.stdin.readline().strip()) lines = [] for i in range(n_lines): a, b, c = [int(x) for x in sys.stdin.readline().split()] lines.append((a, b, c)) t = 0 for line in lines: a, b, c = line[0], line[1], line[2] first = evaluate(a, b, c, start_x, start_y) second = evaluate(a, b, c, dest_x, dest_y) signa = -1 if first < 0 else 1 signb = -1 if second < 0 else 1 if signa != signb: t += 1 print(t)
PYTHON
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.*; import java.util.*; public class CrazyTown { private void run() throws IOException { // put your code here :) double x1 = nextDouble(); double y1 = nextDouble(); double x2 = nextDouble(); double y2 = nextDouble(); int num_roads = nextInt(); double[][] roads = new double[num_roads][3]; for (int i = 0; i < num_roads; i++) { roads[i][0] = nextDouble(); roads[i][1] = nextDouble(); roads[i][2] = nextDouble(); } int step = 0; double m = (y2-y1); if(x2 != x1){ m /= (x2-x1); } double k = y1 - m*x1; // coordinates of line between x1,y1 --- x2,y2 double a1 = -m; double b1 = 1; double c1 = -k; if(x1 == x2){ a1 = 1; b1 = 0; c1 = -x1; } for (int i = 0; i < roads.length; i++) { // equation of each road double a2 = roads[i][0]; double b2 = roads[i][1]; double c2 = roads[i][2]; double x = (b1*c2 - b2*c1)/(b2*a1 - b1*a2); double y = (a1*c2 - c1*a2)/(b1*a2 - b2*a1); if( (x1>=x2 && x<=x1 && x>=x2) || (x1<x2 && x>=x1 && x<=x2) ){ if((y1>=y2 && y<=y1 && y>=y2) || (y1<y2 && y>=y1 && y<=y2)){ // x,y lies between x1,y1 and x2,y2 step ++; } } } System.out.println(step); } private int nextInt() throws IOException { if (input.hasMoreTokens()) return Integer.parseInt(input.nextToken()); input = new StringTokenizer(in.readLine()); return nextInt(); } private double nextDouble() throws IOException { if (input.hasMoreTokens()) return Double.parseDouble(input.nextToken()); input = new StringTokenizer(in.readLine()); return nextDouble(); } private void end() throws IOException { in.close(); out.flush(); out.close(); System.exit(0); } private BufferedReader in; private PrintWriter out; private StringTokenizer input; public CrazyTown() throws IOException { // Input Output for Console to be used for trying the test samples of // the problem in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // ------------------------------------------------------------------------- // Input Output for File to be used for solving the small and large // test files // in = new BufferedReader(new FileReader("UVA.in")); // out = new PrintWriter("UVA.out"); // ------------------------------------------------------------------------- // Initalize Stringtokenizer input input = new StringTokenizer(""); } public static void main(String[] args) throws Exception { // ------------------------------------------------------------------------- // initializing... CrazyTown sol = new CrazyTown(); // ------------------------------------------------------------------------- sol.run(); // ------------------------------------------------------------------------- // closing up sol.end(); // -------------------------------------------------------------------------- } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.Scanner; public class C { public static void main(String args[]){ Scanner in = new Scanner(System.in); long x1 , y1 , x2 , y2; x1 = in.nextLong(); y1 = in.nextLong(); x2 = in.nextLong(); y2 = in.nextLong(); int n = in.nextInt(); long a = 0 , b = 0 , c = 0 ; int count = 0; for(int i = 0 ; i < n ; i++){ a = in.nextLong(); b = in.nextLong(); c = in.nextLong(); if(a*x1+b*y1>-c&&a*x2+b*y2<-c){ count++; }else if(a*x1+b*y1<-c&&a*x2+b*y2>-c){ count++; }else{ } } System.out.println(count); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; int main() { double X1, X2, Y1, Y2; scanf("%lf%lf%lf%lf", &X1, &Y1, &X2, &Y2); int ans = 0; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { double a, b, c; scanf("%lf%lf%lf", &a, &b, &c); if ((a * X1 + b * Y1 + c) * (a * X2 + b * Y2 + c) < eps) { ++ans; } } printf("%d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.*; import java.util.*; public class CF498A { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String t1=br.readLine(); String[] t2=t1.split(" "); long x1=Long.parseLong(t2[0]); long y1=Long.parseLong(t2[1]); String t3=br.readLine(); String[] t4=t3.split(" "); long x2=Long.parseLong(t4[0]); long y2=Long.parseLong(t4[1]); long n=Long.parseLong(br.readLine()); long[][] r=new long[(int)n][3]; long ans=0; for(int i=0;i<n;i++) { String t5=br.readLine(); String[] t6=t5.split(" "); r[i][0]=Long.parseLong(t6[0]); r[i][1]=Long.parseLong(t6[1]); r[i][2]=Long.parseLong(t6[2]); if(((((r[i][0]*x1)+(r[i][1]*y1)+(r[i][2]))>0)&&(((r[i][0]*x2)+(r[i][1]*y2)+(r[i][2]))<0))||((((r[i][0]*x1)+(r[i][1]*y1)+(r[i][2]))<0)&&(((r[i][0]*x2)+(r[i][1]*y2)+(r[i][2]))>0))) { ans++; } } System.out.println(ans); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.*; public class Q1 { public static void main(String args[]){ Scanner sc = new Scanner(System.in); long x1=sc.nextLong(),y1=sc.nextLong(),x2=sc.nextLong(),y2=sc.nextLong(); int n = sc.nextInt(),sum=0; long arr[][]=new long[n][3]; for(int i=0;i<n;i++){ arr[i][0]=sc.nextLong(); arr[i][1]=sc.nextLong(); arr[i][2]=sc.nextLong(); } for(int i=0;i<n;i++){ if(arr[i][0]*x1+arr[i][1]*y1+arr[i][2]>0 && arr[i][0]*x2+arr[i][1]*y2+arr[i][2]<0){ sum++; } if(arr[i][0]*x1+arr[i][1]*y1+arr[i][2]<0 && arr[i][0]*x2+arr[i][1]*y2+arr[i][2]>0){ sum++; } } System.out.println(sum); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.PrintStream; import java.util.Scanner; public class C { static Scanner in = new Scanner(System.in); static PrintStream out = System.out; public static void main(String[] args) { long x1 = in.nextLong(); long y1 = in.nextLong(); long x2 = in.nextLong(); long y2 = in.nextLong(); int n = in.nextInt(); int ans = 0; for (int i = 0; i < n; i++){ long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); long eval1 = a * x1 + b * y1 + c; long eval2 = a * x2 + b * y2 + c; if (eval1 < 0 && eval2 > 0 || eval1 > 0 && eval2 < 0){ ans++; } } out.println(ans); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const int N = 305; long long x1, my_y1; long long x2, y2; int n; long long a[N], b[N], c[N]; int sgn(long long x) { if (x < 0) return -1; if (x == 0) return 0; return 1; } int get_side(int i, long long x, long long y) { return sgn(a[i] * x + b[i] * y + c[i]); } void solve() { scanf("%lld%lld", &x1, &my_y1); scanf("%lld%lld", &x2, &y2); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); int cnt = 0; for (int i = 0; i < n; i++) if (get_side(i, x1, my_y1) != get_side(i, x2, y2)) cnt++; printf("%d\n", cnt); } int main() { solve(); 0; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> int main() { long long x1, y1, x2, y2, n, i, a, b, c, count = 0; long long p, q; scanf("%I64d%I64d", &x1, &y1); scanf("%I64d%I64d", &x2, &y2); scanf("%I64d", &n); for (i = 0; i < n; i++) { scanf("%I64d%I64d%I64d", &a, &b, &c); p = (a * x1) + (b * y1) + c; q = (a * x2) + (b * y2) + c; if ((p < 0 && q > 0) || (p > 0 && q < 0)) count++; } printf("%I64d\n", count); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; void Solve() { long long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; unsigned long long total = 0; for (int i = 0; i < n; ++i) { long long a, b, c; cin >> a >> b >> c; long long r1 = a * x1 + b * y1 + c; long long r2 = a * x2 + b * y2 + c; if ((r1 < 0 && r2 < 0) || (r1 > 0 && r2 > 0)) { } else { total++; } } cout << total << endl; return; ; } int main() { Solve(); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; struct point { double x, y; point(){}; point(double _x, double _y) { x = _x, y = _y; } }; point s, e; double det(double a, double b, double c, double d) { return (a * d - b * c); } point perpotongan(point p1, point p2, point p3, point p4) { point has; has.x = -137; has.y = -1134; double d1 = det(p1.x, p1.y, p2.x, p2.y); double d2 = det(p1.x, 1, p2.x, 1); double d3 = det(p3.x, p3.y, p4.x, p4.y); double d4 = det(p3.x, 1, p4.x, 1); double d5 = det(p1.x, 1, p2.x, 1); double d6 = det(p1.y, 1, p2.y, 1); double d7 = det(p3.x, 1, p4.x, 1); double d8 = det(p3.y, 1, p4.y, 1); double top = det(d1, d2, d3, d4); double bot = det(d5, d6, d7, d8); if (bot == 0) return (has); has.x = top / bot; d2 = det(p1.y, 1, p2.y, 1); d4 = det(p3.y, 1, p4.y, 1); top = det(d1, d2, d3, d4); has.y = top / bot; return (has); } double dist(point p1, point p2) { return hypot(p1.x - p2.x, p1.y - p2.y); } int main() { scanf("%lf %lf", &s.x, &s.y); scanf("%lf %lf", &e.x, &e.y); int n; scanf("%d", &n); double a, b, c; point p, q; int ans = 0; for (int i = 1; i <= n; i++) { scanf("%lf %lf %lf", &a, &b, &c); double kiri = a * s.x + b * s.y + c; double kanan = a * e.x + b * e.y + c; if (((kiri > 0) && (kanan < 0)) || ((kiri < 0) && (kanan > 0))) ++ans; } printf("%d\n", ans); }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) n = int(input()) cost = 0 for i in range(n): a, b, c = map(int, input().split()) d1 = a * x1 + b * y1 + c d2 = a * x2 + b * y2 + c if d1 * d2 < 0: cost += 1 print(cost)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1,y1=map(int,input().split()) x2,y2=map(int,input().split()) n=int(input()) p=0 for i in range(n): a,b,c=map(int,input().split()) zenklas1="" zenklas2="" if a*x1+b*y1+c<0: zenklas1+="-" else: zenklas1+="+" if a*x2+b*y2+c<0: zenklas2+="-" else: zenklas2+="+" if zenklas1!=zenklas2: p+=1 print(p)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static final double EPS = 1e-9; static class Point { double x, y; Point(double a, double b) { x = a; y = b; } boolean between(Point a, Point b) { return x <= Math.max(a.x, b.x) + EPS && x + EPS >= Math.min(a.x, b.x) && y <= Math.max(a.y, b.y) + EPS && y + EPS >= Math.min(a.y, b.y); } public String toString() { return x + " " + y; } } static class Line { double a, b, c; Line(Point p1, Point p2) { if(Math.abs(p1.x - p2.x) < EPS) { a = 1.0; b = 0.0; c = -p1.x; } else { a = (p2.y - p1.y) / (p1.x - p2.x); b = 1.0; c = - a * p1.x - p1.y; } } Line(double x, double y, double z) { a = x; b = y; c = z; } boolean parallel(Line l) { return Math.abs(a - l.a) < EPS && Math.abs(b - l.b) < EPS; } Point intersect(Line l) { if(this.parallel(l)) return null; double x = (l.b * c - b * l.c) / (l.a * b - a * l.b); double y; if(Math.abs(b) > EPS) y = - (a * x + c); else y = - (l.a * x + l.c); return new Point(x, y); } } public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); Point home = new Point(sc.nextInt(), sc.nextInt()); Point uni = new Point(sc.nextInt(), sc.nextInt()); int n = sc.nextInt(); Line[] lines = new Line[n]; for(int i = 0; i < n; i++) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(); if(b == 0) lines[i] = new Line(a, 0, c); else lines[i] = new Line(a * 1.0 / b, 1.0, c * 1.0 / b); } Line cons = new Line(home, uni); int ans = 0; for(int i = 0; i < n; i++) { Point inter = cons.intersect(lines[i]); if(inter != null && inter.between(home, uni)) ans++; } System.out.println(ans); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.rmi.activation.ActivationSystem; import java.util.*; /** * Created by hp on 5/18/2017. */ public class toto { public static PrintWriter out; public static InputReader in; public static void main(String[] args) { out= new PrintWriter(System.out); in=new InputReader(System.in); long x1=in.nextLong(); long y1=in.nextLong(); long x2=in.nextLong(); long y2=in.nextLong(); long k=in.nextLong(); int cnt=0; while (k-->0) { long a=in.nextLong(); long b=in.nextLong(); long c=in.nextLong(); long temp1=a*x1+b*y1+c; long temp2=a*x2+b*y2+c; if(temp1>0 && temp2<0)cnt++; else if(temp1<0 && temp2>0)cnt++; } out.println(cnt); out.close(); } public static int bsearch(ArrayList<choco> app,int target) { int low=0; int high=app.size(); int ans=0; while(low<=high) { int mid=low+(high-low)/2; if(app.get(mid).time<target) { ans=app.get(mid).time; low=mid+1; }else high=mid-1; } return ans; } static class choco { int value; int time; public choco(int value, int time) { this.value = value; this.time = time; } } public static long mod = 1000000007, inf = 100000000000000000l; public static long fac[],inv[]; public static void cal() { fac = new long[1000005]; inv = new long[1000005]; fac[0]=1; inv[0]=1; for(int i=1; i<=1000000; i++) { fac[i]=(fac[i-1]*i)%mod; inv[i]=(inv[i-1]*modPow(i,mod-2,mod))%mod; } } public static long ncr(int n, int r) { return (((fac[n]*inv[r])%mod)*inv[n-r])%mod; } public static long modPow(long base, long exp, long mod) { base = base % mod; long result =1; while(exp > 0) { if(exp % 2== 1) { result = (result * base) % mod; exp --; } else { base = (base * base) % mod; exp = exp >> 1; } } return result; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.Arrays; import java.util.Scanner; public class MainC { MyScanner sc = new MyScanner(); Scanner sc2 = new Scanner(System.in); long start = System.currentTimeMillis(); long fin = System.currentTimeMillis(); final int MOD = 1000000007; int[] dx = { 1, 0, 0, -1 }; int[] dy = { 0, 1, -1, 0 }; int M = 1000000; double EPS = 1e-8; void run() { double x1 = sc.nextDouble(); double y1 = sc.nextDouble(); double x2 = sc.nextDouble(); double y2 = sc.nextDouble(); int n = sc.nextInt(); int cnt = 0; for (int i = 0; i < n; i++) { double x = sc.nextDouble(); double y = sc.nextDouble(); double d = sc.nextDouble(); double xp = (-y - d) / x; double yp = (-(x*xp) - d) / y; double xpp = (-y * 2 - d) / x; double ypp = (-(x*xpp) - d) / y; if (x == 0) { xp = 1; yp = -d / y; xpp = 2; ypp = -d / y; } else if (y == 0) { xp = -d / x; yp = 1; xpp = -d / x; ypp = 2; } if (lineCross(xp, yp, xpp, ypp, x1, y1, x2, y2)) { cnt++; } } System.out.println(cnt); } boolean lineCross(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y, double p4x, double p4y) { double a = (p1x - p2x) * (p3y - p1y) + (p1y - p2y) * (p1x - p3x); double b = (p1x - p2x) * (p4y - p1y) + (p1y - p2y) * (p1x - p4x); return a * b <= 0; } public static void main(String[] args) { new MainC().run(); } void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void debug2(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } boolean inner(int h, int w, int H, int W) { return 0 <= h && h < H && 0 <= w && w < W; } void swap(int[] x, int a, int b) { int tmp = x[a]; x[a] = x[b]; x[b] = tmp; } // find minimum i (a[i] >= border) int lower_bound(int a[], int border) { int l = -1; int r = a.length; while (r - l > 1) { int mid = (l + r) / 2; if (border <= a[mid]) { r = mid; } else { l = mid; } } // r = l + 1 return r; } class MyScanner { int nextInt() { try { int c = System.in.read(); while (c != '-' && (c < '0' || '9' < c)) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; do { res *= 10; res += c - '0'; c = System.in.read(); } while ('0' <= c && c <= '9'); return res; } catch (Exception e) { return -1; } } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String next() { try { StringBuilder res = new StringBuilder(""); int c = System.in.read(); while (Character.isWhitespace(c)) c = System.in.read(); do { res.append((char) c); } while (!Character.isWhitespace(c = System.in.read())); return res.toString(); } catch (Exception e) { return null; } } int[] nextIntArray(int n) { int[] in = new int[n]; for (int i = 0; i < n; i++) { in[i] = nextInt(); } return in; } int[][] nextInt2dArray(int n, int m) { int[][] in = new int[n][m]; for (int i = 0; i < n; i++) { in[i] = nextIntArray(m); } return in; } double[][] nextDouble2dArray(int n, int m) { double[][] in = new double[n][m]; for (int i = 0; i < n; i++) { in[i] = nextDoubleArray(m); } return in; } double[] nextDoubleArray(int n) { double[] in = new double[n]; for (int i = 0; i < n; i++) { in[i] = nextDouble(); } return in; } long[] nextLongArray(int n) { long[] in = new long[n]; for (int i = 0; i < n; i++) { in[i] = nextLong(); } return in; } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.*; import java.io.*; public class Main { InputReader ir; int hx ; int hy ; int cx ; int cy ; int check1(int a, int b, int c) { long one = a*1l*hx + b*1l*hy + c; return one < 0 ? -1 : 1; } int check2(int a, int b, int c) { long two = a*1l*cx + b*1l*cy + c; return two < 0 ? -1 : 1; } void solve() { ir = new InputReader(System.in); hx = ir.readInt(); hy = ir.readInt(); cx = ir.readInt(); int ans = 0; cy = ir.readInt(); int n = ir.readInt(); for(int i = 0; i < n ; i++) { int a = ir.readInt(); int b = ir.readInt(); int c = ir.readInt(); if(check1(a, b, c) * check2(a, b, c) < 0) { ans++; } } System.out.println(ans); } public static void main(String[] args) { new Main().solve(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public final int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public double readDouble() { return Double.parseDouble(readString()); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const long long N = 300 + 10; long long sx, sy, ex, ey; long long n; struct code { long long a, b, c; } p[N]; long long calca(long long i) { return (long long)p[i].a * sx + (long long)p[i].b * sy + (long long)p[i].c < 0; } long long calcb(long long i) { return (long long)p[i].a * ex + (long long)p[i].b * ey + (long long)p[i].c < 0; } int main() { long long ans = 0; scanf("%lld%lld%lld%lld", &sx, &sy, &ex, &ey); scanf("%lld", &n); for (long long i = 1; i <= n; i++) { scanf("%lld", &p[i].a); scanf("%lld", &p[i].b); scanf("%lld", &p[i].c); if (calca(i) != calcb(i)) { ans++; } } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.Scanner; public class Main { public static Scanner cin = new Scanner(System.in) ; private static class Point{ long x , y ; } private static class Line{ long a , b , c ; } public static void main(String[] args) throws Exception{ Point p1 = new Point() ; p1.x = cin.nextLong() ; p1.y = cin.nextLong() ; Point p2 = new Point() ; p2.x = cin.nextLong() ; p2.y = cin.nextLong() ; int N = cin.nextInt() ; Line[] l = new Line[N] ; for(int i = 0 ; i < N ; i ++){ l[i] = new Line() ; } ; for(int i = 0 ; i < N ; i ++){ l[i].a = cin.nextLong() ; l[i].b = cin.nextLong() ; l[i].c = cin.nextLong() ; } long cnt = 0 ; for(int i = 0 ; i < N ; i ++){ if( judge(p1 , p2 , l[i]) == true){ cnt ++ ; } } System.out.println(cnt); } private static boolean judge(Point p1, Point p2, Line l) { boolean a = l.a * p1.x + l.b * p1.y + l.c > 0 ? true : false; boolean b = l.a * p2.x + l.b * p2.y + l.c > 0 ? true : false; return a ^ b ; } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; scanf("%d", &n); int ans = 0; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; if ((a * x1 + b * y1 + c) > 0 && (a * x2 + b * y2 + c) < 0) ans++; else if ((a * x1 + b * y1 + c) < 0 && (a * x2 + b * y2 + c) > 0) ans++; } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265359; const long double EPS = 1e-9; long double ax, ay, bx, by, n; int eq(long double x, long double y) { if (x - y < EPS && x - y > EPS) return 1; return 0; } int main() { cin >> ax >> ay >> bx >> by >> n; int cnt = 0; for (int(i) = (0); (i) < (n); (i)++) { long double a, b, c; cin >> a >> b >> c; long double p1x, p1y, p2x, p2y; if (a == 0) { p1x = 0.0; p1y = -c / b; p2x = 1.0; p2y = -c / b; } else if (b == 0) { p1x = -c / a; p1y = 0.0; p2x = -c / a; p2y = 1.0; } else if (c == 0) { p1x = 1.0; p1y = -a / b; p2x = -2.0 * b / a; p2y = 2.0; } else { p1x = 0.0; p1y = -c / b; p2x = -c / a; p2y = 0.0; } long double den = (ax - bx) * (p1y - p2y) - (ay - by) * (p1x - p2x); if (eq(0.0, den)) { continue; } else { long double x, y; x = (ax * by - ay * bx) * (p1x - p2x) - (ax - bx) * (p1x * p2y - p1y * p2x); x /= den; y = (ax * by - ay * bx) * (p1y - p2y) - (ay - by) * (p1x * p2y - p1y * p2x); y /= den; if (x < max(ax, bx) + EPS && x > min(ax, bx) - EPS && y < max(ay, by) + EPS && y > min(ay, by) - EPS) { cnt++; } } } cout << cnt << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.HashMap; import java.util.Scanner; public class Up { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); long x1 = scanner.nextInt(), y1 = scanner.nextInt(), x2 = scanner.nextInt(), y2 = scanner.nextInt(); int n = scanner.nextInt(); int count = 0; for(int i = 0; i < n; i++){ long a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt(); if( b != 0 ){ if((-c - a*x1 > y1*b && -c - a*x2 < y2*b) || (-c - a*x1 < y1*b && -c - a*x2 > y2*b)){ count++; } } else { if((-c - b*y1 > x1*a && -c - b*y2 < x2*a) || (-c - b*y1 < x1*a && -c - b*y2 > x2*a)){ count++; } } } System.out.println(count); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long Ax, Ay, Bx, By, a, b, c; int n, ans = 0; bool Cal() { long long t, tt; t = Ax * a + Ay * b + c; tt = Bx * a + By * b + c; if ((tt < 0 && t > 0) || (tt > 0 && t < 0)) return 1; return 0; } int main() { int i; cin >> Ax >> Ay >> Bx >> By; cin >> n; for (i = 1; i <= n; i++) { cin >> a >> b >> c; if (Cal()) ans++; } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import sys import time from functools import total_ordering from sys import stderr from typing import Union INF = 10 ** 18 + 3 EPS = 1e-10 MAX_CACHE = 10 ** 9 # Decorators def print_to_file(function, file=stderr): def wrapped(*args, **kwargs): res = function(*args, **kwargs) print(res, file=file) file.flush() return res return wrapped def time_it(function, output=stderr): def wrapped(*args, **kwargs): start = time.time() res = function(*args, **kwargs) elapsed_time = time.time() - start print('"%s" took %f ms' % (function.__name__, elapsed_time * 1000), file=output) return res return wrapped @total_ordering class Point(object): def __init__(self, x, y): self.x = x self.y = y def __le__(self, other): return (self.x, self.y) < (other.x, other.y) def __eq__(self, other): return (self.x, self.y) == (other.x, other.y) class Line(object): def __init__(self, *args): if len(args) == 2: point1, point2 = args self.a = point2.y - point1.y self.b = point1.x - point2.x self.c = - (point1.x * self.a + point1.y * self.b) else: self.a, self.b, self.c = args class Segment(Line): def __init__(self, point1: Point, point2: Point): super().__init__(point1, point2) self.least, self.greatest = sorted((point1, point2)) def find_point_of_intersection_with(self, line: Line) -> Union[Point, None]: if self.a * line.b == line.a * self.b: return None res_y = ((line.a * self.c - line.c * self.a) / (self.a * line.b - line.a * self.b)) res_x = ((line.b * self.c - line.c * self.b) / (self.b * line.a - line.b * self.a)) res_point = Point(res_x, res_y) if not (self.least <= res_point <= self.greatest): return None return res_point @time_it def main(): x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) segment = Segment(Point(x1, y1), Point(x2, y2)) n = int(input()) res = 0 for _ in range(n): a, b, c = map(int, input().split()) res += segment.find_point_of_intersection_with(Line(a, b, c)) is not None print(res) # Auxiliary functions @print_to_file def range_of_len(start, length, step=1): return range(start, start + length * step, step) # IO reassignment def set_input(file): global input input = lambda: file.readline().strip() def set_output(file): global print local_print = print def print(*args, **kwargs): kwargs["file"] = kwargs.get("file", file) return local_print(*args, **kwargs) if __name__ == '__main__': set_input(open("input.txt", "r") if "MINE" in sys.argv else sys.stdin) # set_output(open("sum.out", "w")) main()
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1,y1=map(int, input().split()) x2,y2=map(int, input().split()) n=int(input()) ans=0 for i in range(n): a,b,c = map(int, input().split()) if (a*x1+b*y1+c)*(a*x2+b*y2+c)<0: ans=ans+1 print(ans)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
EPS = 0.00000001 xx1, yy1 = (int(x) for x in input().split()) xx2, yy2 = (int(x) for x in input().split()) n = int(input()) def intersect_between_dots(x1, y1, x2, y2, line): liline = Line() liline.build_by_dots(x1, y1, x2, y2) under_under = liline.a * line.b - line.a * liline.b if abs(under_under) > EPS: x = - (liline.c * line.b - liline.b * line.c) / under_under y = - (liline.a * line.c - line.a * liline.c) / under_under if x >= min(x1, x2) and x <= max(x1, x2) and y >= min(y1, y2) and y <= max(y1, y2): return True return False class Line: def build_by_dots(self, x1, y1, x2, y2): self.a = y1 - y2 self.b = x2 - x1 self.c = -self.a * x1 - self.b * y1 def build_by_abc(self, a, b, c): self.a = a self.b = b self.c = c res = 0 for i in range(n): line = Line() line.build_by_abc(*tuple(int(x) for x in input().split())) if intersect_between_dots(xx1, yy1, xx2, yy2, line): res += 1 print(res)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
//package round284; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class A { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long x1 = nl(), y1 = nl(); long x2 = nl(), y2 = nl(); int n = ni(); int ct = 0; for(int i = 0;i < n;i++){ long a = nl(), b = nl(), c = nl(); int sig1 = Long.signum(a*x1+b*y1+c); int sig2 = Long.signum(a*x2+b*y2+c); if(sig1 != sig2){ ct++; } } out.println(ct); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new A().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class zizo { public static void main(String[]args) throws IOException { Scanner zizo=new Scanner(System.in); PrintWriter wr=new PrintWriter(System.out); long x1=zizo.nextLong(); long y1=zizo.nextLong(); long x2=zizo.nextLong(); long y2=zizo.nextLong(); long n=zizo.nextLong(); long r=0; for(int i=0;i<n;i++) { long a=zizo.nextLong(); long b=zizo.nextLong(); long c=zizo.nextLong()*-1; long c2=a*x1+b*y1; long c3=a*x2+b*y2; if((c2<c && c3<c)||(c2>c && c3>c)) r++; }wr.println(n-r); wr.close(); } } class line{ static final double INF = 1e9, EPS = 1e-9; int a, b, c; line(int a,int b,int c){ this.a=a;this.b=b;this.c=c; } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine(), ",| "); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const int N = 305; long long x, y, X, Y, a, b, c; int n, i, ans; bool check() { long long z1, z2; z1 = a * x + b * y + c; z2 = a * X + b * Y + c; if (z1 < 0 && z2 > 0) return 1; if (z1 > 0 && z2 < 0) return 1; return 0; } int main() { cin >> x >> y; cin >> X >> Y; cin >> n; for (i = 1; i <= n; i++) { cin >> a >> b >> c; if (check()) ans++; } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.lang.*; import java.io.*; import java.util.*; public class Town { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in, out); out.close(); } } class TaskA { public void solve(InputReader in, PrintWriter out) { long x1 = in.nextLong(), y1 = in.nextLong(); long x2 = in.nextLong(), y2 = in.nextLong(); long a, b, c; long tmp1, tmp2; int ans = 0; int t = in.nextInt(); while (t-- > 0) { a = in.nextLong(); b = in.nextLong(); c = in.nextLong(); tmp1 = a * x1 + b * y1 + c; tmp2 = a * x2 + b * y2 + c; if ((tmp1>0) != (tmp2>0)) ++ans; } out.println(ans); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, z, t; cin >> x >> y >> z >> t; long long n; cin >> n; long long s, d, f, counter = 0; for (int i = 0; i < n; i++) { cin >> s >> d >> f; if ((x * s + y * d + f < 0 && z * s + t * d + f > 0) || (x * s + y * d + f > 0 && z * s + t * d + f < 0)) counter++; } cout << counter; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int n, i, ans; long double x1, x2, x, y, a2, b2, c2, hdwjshf, y2, a1, b1, c1; long double dist(long double a, long double b, long double c, long double d) { return sqrt((a - c) * (a - c + 0.) + (b - d) * (b - d + 0.)); } int main() { cin >> x1 >> hdwjshf; cin >> x2 >> y2; a1 = y2 - hdwjshf; b1 = x1 - x2; c1 = -a1 * x1 - b1 * hdwjshf; cin >> n; for (i = 1; i <= n; ++i) { cin >> a2 >> b2 >> c2; x = a1 * b2 - a2 * b1; if (x == 0) continue; x = -(c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1); y = -(a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1); if (x >= min(x1, x2) && x <= max(x1, x2) && y >= min(hdwjshf, y2) && y <= max(hdwjshf, y2)) ans++; } cout << ans; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long x1, y1, x2, y2; scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2); int n, ans = 0; scanf("%d", &n); while (n--) { long long a, b, c; scanf("%I64d%I64d%I64d", &a, &b, &c); long long s1 = a * x1 + b * y1 + c, s2 = a * x2 + b * y2 + c; if (s1 > 0 && s2 < 0 || s1 < 0 && s2 > 0) { ans++; } } printf("%d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.*; /** * Created by DELL on 14-Nov-15. */ public class CrazyTown { public static void main(String arg[]) { InputStream inputStream = System.in; OutputStream outputStream = System.out; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); PrintWriter out = new PrintWriter(outputStream); CrazyTown demo = new CrazyTown(); demo.taskA(in, out); out.close(); } public void taskA(BufferedReader in, PrintWriter out) { try { String tokens[] = in.readLine().split(" "); double sourceX = Integer.parseInt(tokens[0]); double sourceY = Integer.parseInt(tokens[1]); tokens = in.readLine().split(" "); double targetX = Integer.parseInt(tokens[0]); double targetY = Integer.parseInt(tokens[1]); int n = Integer.parseInt(in.readLine()); int count = 0; for (int i = 0; i < n; i++) { tokens = in.readLine().split(" "); double a = Integer.parseInt(tokens[0]); double b = Integer.parseInt(tokens[1]); double c = Integer.parseInt(tokens[2]); double sourceResult = (a * sourceX) + (b * sourceY) + c; double targetResult = (a * targetX) + (b * targetY) + c; if (sourceResult * targetResult < 0) { count++; } } out.println(count); } catch (IOException e) { e.printStackTrace(); } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; struct Poi { double x, y; Poi() {} Poi(double x, double y) : x(x), y(y) {} Poi operator-(const Poi& rhs) const { return Poi(x - rhs.x, y - rhs.y); } }; struct Line { Poi p; Poi v; Line() {} Line(const Poi& p, const Poi& v) : p(p), v(v) {} }; Line toline(double a, double b, double c) { Poi v(b, -a); Poi p; if (fabs(a) > fabs(b)) { p = Poi(-c / a, 0); } else { p = Poi(0, -c / b); } return Line(p, v); } inline double cross(const Poi& a, const Poi& b) { return a.x * b.y - a.y * b.x; } inline bool onleft(const Poi& p, const Line& L) { return cross(L.v, p - L.p) > 0; } inline Poi gPoi() { int x, y; scanf("%d%d", &x, &y); return Poi(x, y); } int n; int main() { Poi P = gPoi(), Q = gPoi(); scanf("%d", &n); int ans = 0; for (int i = 0, a, b, c; i < n; i++) { scanf("%d%d%d", &a, &b, &c); Line L = toline(a, b, c); if (onleft(P, L) != onleft(Q, L)) { ans++; } } printf("%d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long n; pair<long long, long long> a, b; pair<pair<long long, long long>, long long> ar[10100]; long long A[10100], B[10100], ans; int main() { scanf("%I64d%I64d", &a.first, &a.second); scanf("%I64d%I64d", &b.first, &b.second); scanf("%I64d", &n); for (long long i = 0; i < n; i++) scanf("%I64d%I64d%I64d", &ar[i].first.first, &ar[i].first.second, &ar[i].second); memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B)); ans = 0; for (long long i = 0; i < n; i++) { long long x, y; long long aa, bb, cc; aa = ar[i].first.first; bb = ar[i].first.second; cc = ar[i].second; x = a.first; y = a.second; A[i] = (aa * x + bb * y + cc > 0); x = b.first; y = b.second; B[i] = (aa * x + bb * y + cc > 0); if (A[i] != B[i]) ans++; } printf("%I64d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long p1, p2, x1, y1, x2, y2, a, b, c, n, cnt = 0; cin >> x1 >> y1 >> x2 >> y2 >> n; while (n--) { cin >> a >> b >> c; p1 = x1 * a + y1 * b + c; p2 = x2 * a + y2 * b + c; if (p1 > 0 && p2 < 0) cnt++; if (p1 < 0 && p2 > 0) cnt++; } cout << cnt; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
""" Codeforces Contest 284 Div 1 Problem A Author : chaotic_iak Language: Python 3.4.2 """ ################################################### SOLUTION def main(): x1,y1 = read() x2,y2 = read() n, = read() ct = 0 for i in range(n): a,b,c = read() if (a*x1+b*y1+c)*(a*x2+b*y2+c) < 0: ct += 1 print(ct) #################################################### HELPERS def read(mode=2): # 0: String # 1: List of strings # 2: List of integers inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main())
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by Rupesh92 on 12/24/2014. */ public class Codeforces_284_3 { public static void main(String args[]) throws IOException { InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); String token[] = br.readLine().split(" "); long x1 = Long.parseLong(token[0]); long y1 = Long.parseLong(token[1]); String token2[] = br.readLine().split(" "); long x2 = Long.parseLong(token2[0]); long y2 = Long.parseLong(token2[1]); int n = Integer.parseInt(br.readLine()); int count = 0; for(int i = 0 ; i < n ; i++){ String token3[] = br.readLine().split(" "); long a = Long.parseLong(token3[0]); long b = Long.parseLong(token3[1]); long c = Long.parseLong(token3[2]); if((((a*x1) + (b*y1) + c)> 0) && (((a*x2) + (b*y2) + c) < 0) || ((((a*x1) + (b*y1) + c)< 0) && (((a*x2) + (b*y2) + c) > 0))) count++; } System.out.println(count); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 301; const double eps = 1e-8; struct Segment { double a, b, c; } seg[maxn], sta; struct Point { double x, y; } you, uni; bool calcross(Segment l, Segment sa, Point &ans) { double delta = l.a * sa.b - l.b * sa.a; if (abs(delta) < eps) return false; ans.x = (-l.c * sa.b) - (-sa.c * l.b); ans.y = l.a * (-sa.c) - sa.a * (-l.c); ans.x /= delta; ans.y /= delta; return true; } int main() { int i, n, con = 0; double x, y; Point ans; scanf("%lf %lf", &you.x, &you.y); scanf("%lf %lf", &uni.x, &uni.y); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf %lf %lf", &seg[i].a, &seg[i].b, &seg[i].c); } if (you.x > uni.x) swap(you, uni); x = uni.x - you.x; y = uni.y - you.y; sta.a = -y; sta.b = x; sta.c = you.x * y - you.y * x; for (i = 0; i < n; i++) if (calcross(sta, seg[i], ans)) { if ((ans.x - you.x) * (ans.x - uni.x) < 0 || (ans.y - you.y) * (ans.y - uni.y) < 0) con++; } printf("%d\n", con); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.util.*; public class crazyTown{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int x1 = sc.nextInt(); int y1 = sc.nextInt(); int x2 = sc.nextInt(); int y2 = sc.nextInt(); int n = sc.nextInt(); int a = 0; int b = 0; int c = 0; double a1,a2; int ans = 0; for (int i = 0;i<n ;i++ ) { a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); a1 = (double)((double)a*(double)x1)+((double)b*(double)y1)+(double)c; a2 = (double)((double)a*(double)x2)+((double)b*(double)y2)+(double)c; //System.out.println("a1 "+a1+" a2 "+a2+" sum"+((double)a1*a2)); if ((double)a1*(double)a2 < 0) { //System.out.println("try "+i+"ans "+((a * x1 + b * y1 + c) * (a * x2 + b * y2 + c))); ans+=1; } } System.out.println(""+ans); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long int x, y, n, m, i, j, a, b, c, r, a1, a2, ans = 0; cin >> x >> y >> n >> m >> r; for (i = 0; i < r; i++) { cin >> a >> b >> c; a1 = (x * a) + (y * b) + c; a2 = (n * a) + (m * b) + c; if (a1 < 0) { if (a2 > 0) ans++; } if (a1 > 0) { if (a2 < 0) ans++; } } cout << ans; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.awt.Point; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Currency; import java.util.HashSet; import java.util.List; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; /** * @author Jens Staahl */ public class Prac { // some local config // static boolean test = false ; private boolean test = System.getProperty("ONLINE_JUDGE") == null; static String testDataFile = "testdata.txt"; // static String testDataFile = "testdata.txt"; private static String ENDL = "\n"; // Just solves the acutal kattis-problem ZKattio io; class Point{ double x, y; public Point(double x, double y) { super(); this.x = x; this.y = y; } } private void solve() throws Throwable { io = new ZKattio(stream); double x0 = io.getInt(), y0 =io.getInt(), x1 = io.getInt(), y1 = io.getInt(); int cnt = 0; int n = io.getInt(); for (int i = 0; i < n; i++) { double ai = io.getInt(); double bi = io.getInt(); double ci = io.getInt(); double fst = ai * x0 + bi * y0 + ci; double snd = ai * x1 + bi * y1 + ci; if(Math.signum(fst) != Math.signum(snd)) { cnt ++; } } System.out.println(cnt); } public static void main(String[] args) throws Throwable { new Prac().solve(); } public Prac() throws Throwable { if (test) { stream = new FileInputStream(testDataFile); } } InputStream stream = System.in; PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));// outStream public class ZKattio extends PrintWriter { public ZKattio(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); } public ZKattio(InputStream i, OutputStream o) { super(new BufferedOutputStream(o)); r = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public int getInt() { return Integer.parseInt(nextToken()); } public double getDouble() { return Double.parseDouble(nextToken()); } public long getLong() { return Long.parseLong(nextToken()); } public String getWord() { return nextToken(); } private BufferedReader r; private String line; private StringTokenizer st; private String token; private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } // System.out; }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF498A { static double EPS = 1e-9; public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); Point home = new Point(sc.nextInt(), sc.nextInt()); Point uni = new Point(sc.nextInt(), sc.nextInt()); int n = sc.nextInt(); int ans = 0; while(n-- > 0) { Line l = new Line(sc.nextInt(), sc.nextInt(), sc.nextInt()); if(!l.onSameSide(home, uni)) ans++; } System.out.println(ans); } static class Point { double x, y; Point(double x, double y) {this.x = x; this.y = y; } } static class Line { double a, b, c; Line(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } Line(Point p, Point q) { if(p.x == q.x) {a = 1.0; b = 0.0; c = -p.x; } else { a = (p.y - q.y) / (q.x - p.x); b = 1.0; c = - (a * p.x + p.y); } } boolean onSameSide(Point p, Point q) { boolean posP = a * p.x + b* p.y + c > 0.0; boolean posQ = a * q.x + b * q.y + c > 0.0; return !(posP ^ posQ); } } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } boolean ready() throws IOException { return br.ready(); } } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; int qf, qb, q[1111111]; int calc(long long a, long long b, long long c, long long x, long long y) { long long res = a * x + b * y + c; if (res > 0) return 1; return -1; } int main() { int n, res, i; long long x, y, a, b, c, xx, yy; scanf("%I64d%I64d%I64d%I64d", &x, &y, &xx, &yy); res = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%I64d%I64d%I64d", &a, &b, &c); if (calc(a, b, c, x, y) != calc(a, b, c, xx, yy)) res++; } printf("%d\n", res); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; double x1, x2, y1, y2, a, b, c; cin >> x1 >> y1 >> x2 >> y2; cin >> n; int t = 0; while (n--) { cin >> a >> b >> c; if ((a * x1 + b * y1 + c) * (a * x2 + b * y2 + c) < 0) t++; } cout << t << endl; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return SQR(POW(B, P / 2)); } template <typename T> inline T BigMod(T b, T p, T m) { if (p == 0) return 1; if (p % 2 == 0) { T s = BigMod(b, p / 2, m); return ((s % m) * (s % m)) % m; } return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; } template <typename T> inline T ModInv(T b, T m) { return BigMod(b, m - 2, m); } template <typename T> inline T ABS(T a) { if (a < 0) return -a; else return a; } template <typename T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <typename T> inline T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } const double eps = 1e-8; const double inf = 1e20; const int maxp = 1111; int dblcmp(double d) { if (fabs(d) < eps) return 0; return d > eps ? 1 : -1; } inline double sqr(double x) { return x * x; } struct point { double x, y; point() {} point(double _x, double _y) { x = _x; y = _y; } void input() { scanf("%lf%lf", &x, &y); } void output() { printf("%.2f %.2f\n", x, y); } bool operator==(point a) const { return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0; } bool operator<(point a) const { return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) < 0 : x < a.x; } point operator-(point a) const { return point(x - a.x, y - a.y); } double len() { return hypot(x, y); } double len2() { return x * x + y * y; } double distance(point p) { return hypot(x - p.x, y - p.y); } point add(point p) { return point(x + p.x, y + p.y); } point sub(point p) { return point(x - p.x, y - p.y); } point mul(double b) { return point(x * b, y * b); } point div(double b) { return point(x / b, y / b); } double dot(point p) { return x * p.x + y * p.y; } double det(point p) { return x * p.y - y * p.x; } double rad(point a, point b) { point p = *this; return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p)))); } point trunc(double r) { double l = len(); if (!dblcmp(l)) return *this; r /= l; return point(x * r, y * r); } point rotleft() { return point(-y, x); } point rotright() { return point(y, -x); } point rotate(point p, double angle) { point v = this->sub(p); double c = cos(angle), s = sin(angle); return point(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c); } }; struct line { point a, b; line() {} line(point _a, point _b) { a = _a; b = _b; } bool operator==(line v) { return (a == v.a) && (b == v.b); } line(point p, double angle) { a = p; if (dblcmp(angle - (2.0 * acos(0.0)) / 2) == 0) { b = a.add(point(0, 1)); } else { b = a.add(point(1, tan(angle))); } } line(double _a, double _b, double _c) { if (dblcmp(_a) == 0) { a = point(0, -_c / _b); b = point(1, -_c / _b); } else if (dblcmp(_b) == 0) { a = point(-_c / _a, 0); b = point(-_c / _a, 1); } else { a = point(0, -_c / _b); b = point(1, (-_c - _a) / _b); } } void input() { a.input(); b.input(); } void adjust() { if (b < a) swap(a, b); } double length() { return a.distance(b); } double angle() { double k = atan2(b.y - a.y, b.x - a.x); if (dblcmp(k) < 0) k += (2.0 * acos(0.0)); if (dblcmp(k - (2.0 * acos(0.0))) == 0) k -= (2.0 * acos(0.0)); return k; } int relation(point p) { int c = dblcmp(p.sub(a).det(b.sub(a))); if (c < 0) return 1; if (c > 0) return 2; return 3; } bool pointonseg(point p) { return dblcmp(p.sub(a).det(b.sub(a))) == 0 && dblcmp(p.sub(a).dot(p.sub(b))) <= 0; } bool parallel(line v) { return dblcmp(b.sub(a).det(v.b.sub(v.a))) == 0; } int segcrossseg(line v) { int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a))); int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a))); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2; return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0 || d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0 || d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0 || d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0); } int segcrossseg_inside(line v) { if (v.pointonseg(a) || v.pointonseg(b) || pointonseg(v.a) || pointonseg(v.b)) return 0; int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a))); int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a))); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 1; return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0 || d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0 || d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0 || d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0); } int linecrossseg(line v) { int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); if ((d1 ^ d2) == -2) return 2; return (d1 == 0 || d2 == 0); } int linecrossline(line v) { if ((*this).parallel(v)) { return v.relation(a) == 3; } return 2; } point crosspoint(line v) { double a1 = v.b.sub(v.a).det(a.sub(v.a)); double a2 = v.b.sub(v.a).det(b.sub(v.a)); return point((a.x * a2 - b.x * a1) / (a2 - a1), (a.y * a2 - b.y * a1) / (a2 - a1)); } double dispointtoline(point p) { return fabs(p.sub(a).det(b.sub(a))) / length(); } double dispointtoseg(point p) { if (dblcmp(p.sub(b).dot(a.sub(b))) < 0 || dblcmp(p.sub(a).dot(b.sub(a))) < 0) { return min(p.distance(a), p.distance(b)); } return dispointtoline(p); } point lineprog(point p) { return a.add(b.sub(a).mul(b.sub(a).dot(p.sub(a)) / b.sub(a).len2())); } point symmetrypoint(point p) { point q = lineprog(p); return point(2 * q.x - p.x, 2 * q.y - p.y); } }; int main() { double x1, x2, y1, y2, a, b, c, d, e, f, mnx, mmx, mny, mmy; cin >> x1 >> y1 >> x2 >> y2; mnx = min(x1, x2); mmx = max(x1, x2); mny = min(y1, y2); mmy = max(y1, y2); point p1, p2; p1 = point(x1, y1); p2 = point(x2, y2); line ll, tl; ll = line(p1, p2); int n; cin >> n; int cnt = 0; for (int i = 1; i <= n; i++) { cin >> a >> b >> c; tl = line(a, b, c); point qq = ll.crosspoint(tl); if (qq.x >= mnx && qq.x <= mmx && qq.y >= mny && qq.y <= mmy) cnt++; } cout << cnt << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { static BufferedReader in; static PrintWriter out; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); String[] tok = rst(); long x1 = Long.parseLong(tok[0]); long y1 = Long.parseLong(tok[1]); tok = rst(); long x2 = Long.parseLong(tok[0]); long y2 = Long.parseLong(tok[1]); int n = ri(); long[] a = new long[n]; long[] b = new long[n]; long[] c = new long[n]; long total = 0; for (int i = 0; i < n; i++) { tok = rst(); a[i] = Long.parseLong(tok[0]); b[i] = Long.parseLong(tok[1]); c[i] = Long.parseLong(tok[2]); if (Long.signum(a[i]*x1+b[i]*y1+c[i]) != Long.signum(a[i]*x2+b[i]*y2+c[i])) total++; } out.println(total); in.close(); out.close(); } public static int ri() throws IOException { return Integer.parseInt(in.readLine()); } public static long rl() throws IOException { return Long.parseLong(in.readLine()); } public static String rs() throws IOException { return in.readLine(); } public static String[] rst() throws IOException { return in.readLine().split(" "); } public static int[] rit() throws IOException { String[] tok = in.readLine().split(" "); int[] res = new int[tok.length]; for (int i = 0; i < tok.length; i++) { res[i] = Integer.parseInt(tok[i]); } return res; } public static long[] rlt() throws IOException { String[] tok = in.readLine().split(" "); long[] res = new long[tok.length]; for (int i = 0; i < tok.length; i++) { res[i] = Long.parseLong(tok[i]); } return res; } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long int calc(long long int a, long long int b, long long int c, long long int x, long long int y) { return a * x + b * y + c; } int main() { long long int x1; cin >> x1; ; long long int y1; cin >> y1; ; long long int x2; cin >> x2; ; long long int y2; cin >> y2; ; long long int n; cin >> n; ; int r = 0; while (n--) { long long int a; cin >> a; ; long long int b; cin >> b; ; long long int c; cin >> c; ; if ((calc(a, b, c, x1, y1) > 0 && calc(a, b, c, x2, y2) < 0) || (calc(a, b, c, x1, y1) < 0 && calc(a, b, c, x2, y2) > 0)) r++; } cout << r << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long int x1, y1, x2, y2, n; cin >> x1 >> y1 >> x2 >> y2 >> n; int r = 0; for (int i = 0; i < n; i++) { long long int a, b, c; cin >> a >> b >> c; if ((a * x1 + b * y1 + c < 0) ^ (a * x2 + b * y2 + c < 0)) r++; } cout << r; return (0); }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
homex,homey = (int(x) for x in input().split(" ")) univx,univy = (int(x) for x in input().split(" ")) n = int(input()) step = 0 for i in range(0,n): a,b,c = (int(x) for x in input().split(" ")) homearea = ((a*homex + b*homey + c) < 0) univarea = ((a*univx + b*univy + c) < 0) if homearea != univarea: step += 1 print(step)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> int a, b, c, n, x[2], y[2], ans; bool f(int i) { return 1LL * a * x[i] + 1LL * b * y[i] + c > 0; } int main() { scanf("%d%d%d%d%d", x, y, x + 1, y + 1, &n); for (int i = 0; i < n; i++) { scanf("%d%d%d", &a, &b, &c); if (f(0) != f(1)) ans++; } printf("%d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> int MOD = 1000000007; using namespace std; int i, j, n, m, k, p, q, l; int a, b; int main() { long long xa, ya, xb, yb; cin >> xa >> ya >> xb >> yb; cin >> n; int ans = 0; for (i = 0; i < n; i++) { long long A, B, C; cin >> A >> B >> C; long long x = A * xa + B * ya + C; long long y = A * xb + B * yb + C; if (x > 0 && y < 0 || x < 0 && y > 0) ans++; } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; struct pt { long double x, y; pt() {} pt(long double _x, long double _y) { x = _x; y = _y; } pt(const pt& a, const pt& b) { x = b.x - a.x; y = b.y - a.y; } }; long double cross(const pt& a, const pt& b) { return a.x * b.y - a.y * b.x; } int main() { pt s, f; cin >> s.x >> s.y; cin >> f.x >> f.y; int n; cin >> n; long double a, b, c; int ans = 0; for (int i = 0; i < n; ++i) { cin >> a >> b >> c; pt p1, p2; if (b == 0) { p1.x = p2.x = -c / a; p1.y = 0; p2.y = 100; } else if (a == 0) { p1.y = p2.y = -c / b; p1.x = 0; p2.x = 100; } else { p1 = pt(0.0, -c / b); p2 = pt(-c / a, 0.0); if (c == 0) { p2 = pt(1.0, (-c - a) / b); } } pt p = pt(p1, p2); if (cross(p, pt(p1, s)) * cross(p, pt(p1, f)) < -0.00000001) ++ans; } cout << ans; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) n = int(input()) data = [] ans = 0 for i in range(n): a, b, c = map(int, input().split()) one = (a * x1 + b * y1 + c) two = (a * x2 + b * y2 + c) if one // abs(one) != two // abs(two): ans += 1 print(ans)
PYTHON3
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; pair<double, double> A, B; pair<double, double> Array[605]; int N; void Read() { cin >> A.first >> A.second; cin >> B.first >> B.second; cin >> N; for (int i = 1; i <= N; i++) { double a, b, c; cin >> a >> b >> c; if (a == 0) { Array[2 * i - 1] = make_pair(0, (-c / b)); Array[2 * i] = make_pair(1, (-c / b)); continue; } if (b == 0) { Array[2 * i - 1] = make_pair(-(c / a), 1); Array[2 * i] = make_pair(-(c / a), 0); continue; } if (b != 0) Array[2 * i - 1] = make_pair(0, -(c / b)); if (a != 0) Array[2 * i] = make_pair(-(c / a), 0); if (Array[2 * i - 1] == Array[2 * i]) { Array[2 * i - 1] = make_pair(1, (-(a + c)) / b); } } } inline bool Area(pair<double, double> a, pair<double, double> b, pair<double, double> c) { return a.first * b.second + b.first * c.second + c.first * a.second - a.second * b.first - b.second * c.first - c.second * a.first > 0; } int main() { int ans = 0; Read(); for (int i = 1; i <= N; i++) if (Area(Array[2 * i - 1], Array[2 * i], A) != Area(Array[2 * i - 1], Array[2 * i], B)) ++ans; cout << ans << "\n"; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.InputStream; import java.util.NoSuchElementException; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * @author Egor Kulikov (egor@egork.net) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { Point origin = Point.readPoint(in); Point destination = Point.readPoint(in); int count = in.readInt(); int answer = 0; for (int i = 0; i < count; i++) { int a = in.readInt(); int b = in.readInt(); int c = in.readInt(); Line line = new Line(a, b, c); if (line.value(origin) * line.value(destination) < 0) { answer++; } } out.printLine(answer); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } class Point { public final double x; public final double y; public String toString() { return "(" + x + ", " + y + ")"; } public Point(double x, double y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return Math.abs(x - point.x) <= GeometryUtils.epsilon && Math.abs(y - point.y) <= GeometryUtils.epsilon; } public int hashCode() { int result; long temp; temp = x != +0.0d ? Double.doubleToLongBits(x) : 0L; result = (int) (temp ^ (temp >>> 32)); temp = y != +0.0d ? Double.doubleToLongBits(y) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } public static Point readPoint(InputReader in) { double x = in.readDouble(); double y = in.readDouble(); return new Point(x, y); } } class Line { public final double a; public final double b; public final double c; public Line(double a, double b, double c) { double h = GeometryUtils.fastHypot(a, b); this.a = a / h; this.b = b / h; this.c = c / h; } public boolean parallel(Line other) { return Math.abs(a * other.b - b * other.a) < GeometryUtils.epsilon; } public double value(Point point) { return a * point.x + b * point.y + c; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Line line = (Line) o; if (!parallel(line)) return false; if (Math.abs(a * line.c - c * line.a) > GeometryUtils.epsilon || Math.abs(b * line.c - c * line.b) > GeometryUtils.epsilon) return false; return true; } } class GeometryUtils { public static double epsilon = 1e-8; public static double fastHypot(double x, double y) { return Math.sqrt(x * x + y * y); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int32_t main() { long long int x, y; cin >> x >> y; long long int x1, y1; cin >> x1 >> y1; long long int n; cin >> n; long long int ans = 0; for (long long int i = 0; i < n; i++) { long long int a, b, c; cin >> a >> b >> c; long long int val1 = a * x + b * y + c; long long int val2 = a * x1 + b * y1 + c; if ((val1 > 0 && val2 < 0) || (val1 < 0 && val2 > 0)) { ++ans; } } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
x1, y1 = map(int, raw_input().split()) x2, y2 = map(int, raw_input().split()) n = input() s=0 for i in range(n): a, b, c = map(int, raw_input().split()) if((a*x1+b*y1+c)*(a*x2+b*y2+c)<0): s+=1 print s
PYTHON
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; }; struct stline { Point a, b; }; int dblcmp(double a, double b) { if (fabs(a - b) <= 1E-6) return 0; if (a > b) return 1; else return -1; } double dot(double x1, double y1, double x2, double y2) { return x1 * x2 + y1 * y2; } int point_on_line(Point a, Point b, Point c) { return dblcmp(dot(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y), 0); } double cross(double x1, double y1, double x2, double y2) { return x1 * y2 - x2 * y1; } double ab_cross_ac(Point a, Point b, Point c) { return cross(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y); } int ab_cross_cd(Point a, Point b, Point c, Point d) { double s1, s2, s3, s4; int d1, d2, d3, d4; Point p; d1 = dblcmp(s1 = ab_cross_ac(a, b, c), 0); d2 = dblcmp(s2 = ab_cross_ac(a, b, d), 0); d3 = dblcmp(s3 = ab_cross_ac(c, d, a), 0); d4 = dblcmp(s4 = ab_cross_ac(c, d, b), 0); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) { p.x = (c.x * s2 - d.x * s1) / (s2 - s1); p.y = (c.y * s2 - d.y * s1) / (s2 - s1); return 1; } if (d1 == 0 && point_on_line(c, a, b) <= 0) { p = c; return 0; } if (d2 == 0 && point_on_line(d, a, b) <= 0) { p = d; return 0; } if (d3 == 0 && point_on_line(a, c, d) <= 0) { p = a; return 0; } if (d4 == 0 && point_on_line(b, c, d) <= 0) { p = b; return 0; } return -1; } int main() { Point a, b, c, d; scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y); int n, ans = 0; double aa, bb, cc; scanf("%d", &n); while (n--) { scanf("%lf%lf%lf", &aa, &bb, &cc); c.x = -(1000000.0 * bb + cc) / aa; c.y = 1000000.0; d.x = (1000000.0 * bb - cc) / aa; d.y = -1000000.0; if (aa == 0.0) { double t = -cc / bb; if ((a.y < t && b.y > t) || (a.y > t && b.y < t)) ++ans; } else if (ab_cross_cd(a, b, c, d) != -1) ++ans; } printf("%d\n", ans); return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
import java.math.*; import java.util.*; public class Main{ public static void main(String args[]){ BigInteger x1,y1,x2,y2; int n,cnt = 0; Scanner cin = new Scanner(System.in); x1 = cin.nextBigInteger();y1 = cin.nextBigInteger(); x2 = cin.nextBigInteger();y2 = cin.nextBigInteger(); n = cin.nextInt(); while(n-- > 0){ BigInteger a,b,c; a = cin.nextBigInteger();b = cin.nextBigInteger();c = cin.nextBigInteger(); BigInteger t1 = a.multiply(x1).add(b.multiply(y1).add(c)),t2 = a.multiply(x2).add(b.multiply(y2).add(c)); if((t1.multiply(t2)).compareTo(BigInteger.valueOf(0)) < 0) cnt++; } System.out.println(cnt); } }
JAVA
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> #pragma GCC optimize "Ofast" #pragma GCC target "avx2" using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; long long n; cin >> n; long long x, y; long long ans = 0; for (int i = 0; i < n; i++) { long long z; cin >> x >> y >> z; long long t = x * a + y * b + z; long long tt = x * c + y * d + z; t = -t > t ? 1 : 0; tt = -tt > tt ? 1 : 0; if (t != tt) ans++; } cout << ans; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } using namespace std; long long xa, ya, xb, yb, a, b, c, con = 0; bool test1, test2; int n; int main() { cin >> xa >> ya >> xb >> yb >> n; for (int i = 0; i < n; i++) { cin >> a >> b >> c; test1 = (0 < a * xa + c + b * ya); test2 = (0 < a * xb + c + b * yb); if (test1 ^ test2) con++; } cout << con; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, xx, yy; cin >> x >> y >> xx >> yy; int t; cin >> t; long long ans = 0; while (t--) { long long a, b, c; cin >> a >> b >> c; long long s1 = a * x + b * y + c; long long s2 = a * xx + b * yy + c; if ((s1 > 0 && s2 < 0) || (s1 < 0 && s2 > 0)) ans++; } cout << ans << endl; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } const int maxn = 2020; int n, x; long long a, b, c; void solve() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; cin >> n; for (int i = 0; i < n; ++i) { cin >> a >> b >> c; x += (a * x1 + b * y1 + c > 0 && a * x2 + b * y2 + c < 0 || a * x1 + b * y1 + c < 0 && a * x2 + b * y2 + c > 0); } cout << x; } int main() { int t = 1; while (t--) { solve(); } }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; template <typename t> t abs(t a) { if (a >= 0) return a; return -a; } vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long int x1, y1, x2, y2, n, a, b, c, tmp1, tmp2, ans = 0; cin >> x1 >> y1 >> x2 >> y2; cin >> n; while (n--) { cin >> a >> b >> c; tmp1 = (a * x1) + (b * y1) + c; tmp2 = (a * x2) + (b * y2) + c; if ((tmp1 < 0 && tmp2 > 0) || tmp1 > 0 && tmp2 < 0) { ans++; } } cout << ans << endl; return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b, long long int m) { if (b == 0) return (1); long long int sol = power(a, b / 2, m); sol = (sol * sol) % m; if (b % 2 == 1) sol = (sol * a) % m; return (sol); } long long int gcd(long long int a, long long int b) { if (b == 0) return a; else { return gcd(b, a % b); } } int a[100005], pre[100005], suf[100005]; void solve() { int ans = 0; long long int n, x1, x2, y1, y2; cin >> x1 >> y1; cin >> x2 >> y2; cin >> n; for (long long int i = 0; i < n; i++) { long long int a, b, c; cin >> a >> b >> c; long long int sol1, sol2; sol1 = a * x1 + b * y1 + c; sol2 = a * x2 + b * y2 + c; if ((sol1 < 0 && sol2 > 0) || (sol1 > 0 && sol2 < 0)) { ans++; } } cout << ans; } int main() { int t = 1; while (t--) { solve(); } return 0; }
CPP
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
home = [int(_) for _ in raw_input().split()] uni = [int(_) for _ in raw_input().split()] n = input() lines = [] for i in range(n): lines.append([int(_) for _ in raw_input().split()]) # print lines ans = 0 def where(point, line): s = line[0] * point[0] + line[1] * point[1] + line[2] if s > 0: return 1 else: return -1 for line in lines: if where(home, line) != where(uni, line): ans += 1 print ans
PYTHON
498_A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect. Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step). Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road. Input The first line contains two space-separated integers x1, y1 ( - 106 ≀ x1, y1 ≀ 106) β€” the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≀ x2, y2 ≀ 106) β€” the coordinates of the university you are studying at. The third line contains an integer n (1 ≀ n ≀ 300) β€” the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≀ ai, bi, ci ≀ 106; |ai| + |bi| > 0) β€” the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines). Output Output the answer to the problem. Examples Input 1 1 -1 -1 2 0 1 0 1 0 0 Output 2 Input 1 1 -1 -1 3 1 0 0 0 1 0 1 1 -3 Output 2 Note Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): <image> <image>
2
7
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int counter = 0; int x1 = sc.nextInt(); int y1 = sc.nextInt(); int x2 = sc.nextInt(); int y2 = sc.nextInt(); int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; int c[] = new int[n]; for(int i = 0;i<n;i++){ a[i]=sc.nextInt(); b[i]=sc.nextInt(); c[i]=sc.nextInt(); } for(int i = 0;i<n;i++){ long first = (long)a[i]*(long)x1+(long)b[i]*(long)y1+(long)c[i]; long second = (long)a[i]*(long)x2+(long)b[i]*(long)y2+(long)c[i]; if(first>0&&second<0){ counter++; } if(first<0&&second>0){ counter++; } } System.out.println(counter); } }
JAVA