Problem description

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 x1y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home.

The second line contains two integers separated by a space x2y2 ( - 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):

解题思路:题目的意思就是有n条直线(它们互不重合且其方程满足ax+by+c=0)把一个平面分成很多块,家和学校这两个坐标点必在其中一个块中,规定从一个块到与之相邻的另一个块所走步数为1(不能走没有相邻的块),求从家到学校走的最小步数。分析一下走法可知,从家到学校路途中,跨过相邻的块必经过一条直线,且这条直线在A、B两点之间,因此可以得出最小步数为A、B两点之间直线的条数。怎么判断条数呢?回想一下,高中学过的线性规划,在判断可行域的时候,通常以某个点例如(0,0)来判断可行域属于直线上方还是下方,上方为正,下方为负。因此这道题刚好就运用到这一知识点:把A(x1,y1),B(x2,y2)分别代到n个方程中,只要每个方程的两个结果满足一正一负(直线的两侧),就将步数加1,最后即为最小步数。注意本题判断方法中的相乘可能导致数据溢出,因此将所有数据类型定义为long long,避免出错。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
LL x1,y1,x2,y2,n,a,b,c,step=;
cin>>x1>>y1>>x2>>y2>>n;
while(n--){
cin>>a>>b>>c;
if((a*x1+b*y1+c>)&&(a*x2+b*y2+c<))step++;
if((a*x1+b*y1+c<)&&(a*x2+b*y2+c>))step++;
}
cout<<step<<endl;
return ;
}

C - Crazy Town的更多相关文章

  1. #284 div.2 C.Crazy Town

    C. Crazy Town   Crazy Town is a plane on which there are n infinite line roads. Each road is defined ...

  2. Codeforces Round #284 (Div. 1) A. Crazy Town 计算几何

    A. Crazy Town 题目连接: http://codeforces.com/contest/498/problem/A Description Crazy Town is a plane on ...

  3. Codeforces 498A Crazy Town

    C. Crazy Town time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. A. Crazy Town

    Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation ...

  5. Codeforces_499C:Crazy Town(计算几何)

    题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...

  6. Codeforces 499C:Crazy Town(计算几何)

    题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...

  7. 【codeforces 499C】Crazy Town

    [题目链接]:http://codeforces.com/problemset/problem/499/C [题意] 一个平面,被n条直线分成若干个块; 你在其中的某一块,然后你想要要到的终点在另外一 ...

  8. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #284 (Div. 1)

    A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...

随机推荐

  1. 使用原生JS的AJAX读取json全过程

    首先ajax(async javascript and xml)是用于前端与后端文件比如xml或者json之间的交互.他是一种异步加载技术,意味着你点击某个加载事件是再也不用刷新整个页面,而是发送局部 ...

  2. css的基础知识1

    总结:css引用:1内联:在标签中加style属性,<标签名 style="样式1:样式值1:样式2:样式值2"> </标签名>.2.内嵌:在head标签中 ...

  3. 【Flutter学习】基本组件之AppBar顶部导航栏

    一,概述 AppBar 显示在app的顶部.AppBar包含5大部分,如下图: 二,构造函数及参数含义 构造函数 AppBar({ Key key, this.leading, //在标题前面显示的一 ...

  4. c#用控制台程序安装启动停止卸载服务

    第一步:新建控制台项目  第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上   第四步 编写代码 双击打开 using System; using Syst ...

  5. 洛谷 1003 NOIP2011 D1T1 铺地毯

    [题解] 因为只询问一个点,所以记录地毯信息,倒着找第一个符合条件的地毯就是在最上面的. #include<cstdio> #include<algorithm> #defin ...

  6. 【codeforces 796D】Police Stations

    [题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...

  7. Beetl学习总结(2)——基本用法

    2.1. 安装 如果使用maven,使用如下坐标 <dependency> <groupId>com.ibeetl</groupId> <artifactId ...

  8. 洛谷 1373 dp 小a和uim之大逃离 良心题解

    洛谷 1373 dp 这题还不算太难,,当初看的时候不是很理解题意,以为他们会选择两条不同的路径,导致整体思路混乱 传送门 其实理解题意和思路之后还是敲了不短的时间,一部分身体原因再加上中午休息不太好 ...

  9. Spring MVC-处理程序映射(Handler Mapping)-简单的Url处理程序映射(Simple Url Handler Mapping)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_simpleurlhandlermapping.htm 说明:示例基于Spring ...

  10. IDFTP连不上FTP服务器的解决方法

    使用IDFTP连不上FTP服务端. 原来是IDFTP有2种模式:主动模式和被动模式. IDFTP默认是使用主动模式,如果FTP服务端设置使用被动模式,使用IDFTP连不上FTP服务端的. 解决方法: ...