A. Forked!

time limit per test

2 seconds

memory limit per test

256 megabytes

Lunchbox is done with playing chess! His queen and king just got forked again!

In chess, a fork is when a knight attacks two pieces of higher value, commonly the king and the queen. Lunchbox knows that knights can be tricky, and in the version of chess that he is playing, knights are even trickier: instead of moving 1 tile in one direction and 2 tiles in the other, knights in Lunchbox's modified game move a tiles in one direction and b tiles in the other.

Lunchbox is playing chess on an infinite chessboard which contains all cells (x,y) where x and y are (possibly negative) integers. Lunchbox's king and queen are placed on cells (xK,yK) and (xQ,yQ) respectively. Find the number of positions such that if a knight was placed on that cell, it would attack both the king and queen.

Input

Each test contains multiple test cases. The first line contains an integer t (1≤t≤1000) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers a and b (1≤a,b≤108) --- describing the possible moves of the knight.

The second line of each test case contains two integers xK and yK (0≤xK,yK≤108) --- the position of Lunchbox's king.

The third line in a test case contains xQ and yQ (0≤xQ,yQ≤108) --- the position of Lunchbox's queen.

It is guaranteed that Lunchbox's queen and king will occupy different cells. That is, (xK,yK)≠(xQ,yQ).

Output

For each test case, output the number of positions on an infinite chessboard such that a knight can attack both the king and the queen.

Example

Input

Copy

复制代码

4

2 1

0 0

3 3

1 1

3 1

1 3

4 4

0 0

8 0

4 2

1 4

3 4

Output

Copy

复制代码
2
1
2
0

Note

In the first test case, the knight can move 2 squares in one direction and 1 square in the other (it is essentially the same as the knight in standard chess). A knight placed on (2,1) or (1,2) would attack both the king and queen.

Example of a knight placement that forks the queen and king in the first test case. The squares that the knight attacks are highlighted in red.

In the second test case, a knight placed on (2,2) would attack both the king and queen.

Example of a knight placement that does not fork the queen and king in the second test case. The knight attacks the king but not the queen.

In the third test case, a knight placed on (4,4) or (4,−4) would attack both the king and queen.

In the fourth test case, there are no positions where the knight can attack both the king and the queen.

(Credits to EnDeRBeaT for the nice images)

解题说明:此题是一道几何题,根据题目意思,判断图中的一个点能否通过移动a和b到达其他两个点。这里直接遍历进行求解,用另外两个点作为基准,判断能否得到一个共同点,找出所有这种情况。注意a和b相等的情况,此时需要除以4。

cpp 复制代码
#include<bits/stdc++.h>
#include<iostream>
using namespace std; 
int T, a, b, kx, ky, qx, qy, t;
int main() 
{
	cin >> T; 
	while (T--) 
	{
		cin >> a >> b >> kx >> ky >> qx >> qy; 
		t = 0;
		int dx[8] = { -a,-a,-b,-b,a,a,b,b }, dy[8] = { b,-b,a,-a,b,-b,a,-a };
		for (int i = 0; i < 8; ++i) 
		{
			for (int j = 0; j < 8; ++j)
			{
				if (kx + dx[i] == qx + dx[j] && ky + dy[i] == qy + dy[j]) 
				{
					++t;
				}
			}
		}
		if (a == b)
		{
			t /= 4;
		}
		cout << t << endl;
	}
	return 0;
}
相关推荐
_OP_CHEN3 分钟前
【算法基础篇】(五十三)隔板法指南:从 “分球入盒” 到不定方程,组合计数的万能解题模板
算法·蓝桥杯·c/c++·组合数学·隔板法·acm/icpc
近津薪荼6 分钟前
优选算法——滑动窗口3(子数组)
c++·学习·算法
遨游xyz7 分钟前
数据结构-栈
java·数据结构·算法
ghie909011 分钟前
基于动态规划算法的混合动力汽车能量管理建模与计算
算法·汽车·动态规划
蓝海星梦11 分钟前
GRPO 算法演进——裁剪机制篇
论文阅读·人工智能·深度学习·算法·自然语言处理·强化学习
小O的算法实验室12 分钟前
2025年SEVC SCI2区,结合低差异序列和共轭梯度法的新型异构综合学习粒子群算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
会叫的恐龙21 分钟前
C++ 核心知识点汇总(第四日)(循环结构)
c++·算法·循环结构
sin_hielo26 分钟前
leetcode 3637
数据结构·算法·leetcode
仍然.27 分钟前
算法题目---双指针算法
数据结构·算法·排序算法
2401_8414956427 分钟前
【LeetCode刷题】翻转二叉树
python·算法·leetcode··递归·节点·翻转二叉树