2019HDU多校第六场——HDU6639 Faraway【思维/曼哈顿距离】

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Problem Description

n soldiers are dispatched to somewhere in Byteland. These soldiers are going to set off now, but the target location is not so clear.

Assume the target location is at (xe,ye), it is clear that xe,ye are both non-negative integers within [0,m]. For the i-th soldier, the only thing he knows is that (|xi−xe|+|yi−ye|)modki=ti.

To find the correct target location, these soldiers are working on the information they have now. Please write a program to figure out the number of possible target locations.

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, there are two integers n,m(1≤n≤10,1≤m≤109) in the first line, denoting the number of soldiers and the upper bound of xe,ye.

For the next n lines, each line contains four integers xi,yi,ki,ti(0≤xi,yi≤m,2≤ki≤5,0≤ti

Output

For each test case, print a single line containing an integer, denoting the number of possible target locations

Sample Input

2

2 5

1 2 4 2

3 1 2 1

2 5

1 2 4 2

1 2 4 3

Sample Output

10

0

题意:

令(x,y)与每一对(xi,yi)的曼哈顿距离%ki==ti,求这样的(x,y)的个数

分析:

场上觉得情况太多实在想不来,没想到这么巧妙。

【官方题解】将 |xi −xe|+|yi −ye| 的绝对值拆掉,则每个点 (xi,yi) 会将平面分割成 4 个部分,每个部 分里距离的表达式没有绝对值符号,一共 O(n2) 个这样的区域。 枚举每个区域,计算该区域中可能的终点数量。注意到 lcm(2,3,4,5) = 60,所以只需要枚 举 xe 和 ye 模 60 的余数,O(n) 判断是否可行,然后 O(1) 计算该区域中有多少这样的点即可。 时间复杂度为 O(602n3)。

#include
#define ll long long
using namespace std;
int n,m,tot;
int a[20],b[20];
struct node{int x,y,k,t;} p[20];
bool check(int x,int y)
{
    for (int i=0;i

 

你可能感兴趣的:(思维,2019HDU多校)