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<ki), denoting what each soldier knows.

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<bits/stdc++.h>
#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<n;i++)
        if((abs(x-p[i].x)+abs(y-p[i].y))%p[i].k!=p[i].t) return false;
    return true;
}
int cal(int l,int r)
{
    r=r-(l+1);
    if(r<0) return 0;
    else return r/60+1;
}
void rua()
{
    scanf("%d%d",&n,&m);
    tot=1;
    a[1]=b[1]=m+1;
    for (int i=0;i<n;i++)
    {
        scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].k,&p[i].t);
        a[++tot]=p[i].x;b[tot]=p[i].y;
    }
    ll ans=0;
    sort(a+1,a+1+tot);sort(b+1,b+1+tot);
    for(int i=0;i<tot;i++) if(a[i]<a[i+1])
        for(int j=0;j<tot;j++) if(b[j]<b[j+1])
            for(int x=0;x<60;x++) for(int y=0;y<60;y++)
                if(check(a[i]+x,b[j]+y)) ans+=1ll*cal(a[i]+x,a[i+1])*cal(b[j]+y,b[j+1]);
    printf("%lld\n",ans);
}
int main()
{
    int t;scanf("%d",&t);
    while (t--) rua();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值