2019 Multi-University Training Contest 6 F - Faraway

题面

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

题意

简单来说就是给定n组x,y,k,t,找有多少对(xe,ye)能够满足这n组约束,公式如下
( ∣ x i − x e ∣ + ∣ y i − y e ∣ )   m o d   k i = t i (|x_i-x_e|+|y_i-y_e|)\ mod\ k_i=t_i (xixe+yiye) mod ki=ti
如果将绝对值去掉,一个点(x,y)会将所在二维平面分割成四个平面,如下

2019 Multi-University Training Contest 6 F - Faraway_第1张图片
可以看出,分割后的每个区域上的点(xe,ye),对于(xi-xe),(yi-ye)的正负性是一样的,所以可以分区域枚举符合条件的点,因为k的取值只有2,3,4,5,所以在枚举xe,ye的时候,可以对其取模lcm(2,3,4,5)=60,即xe mod 60,ye mod 60,然后直接计算在该区域内有多少相同的xe,ye。
就是在计算过程中要注意不要重复计算边界上的点,方法就是计算区域内的点时,区域区间为左开右闭。

代码:

#include
using namespace std;
typedef long long ll;
const int N=1e2+10;
int x[N],y[N],k[N],t[N];
int da[N],db[N],top,n;
int judge(int xe,int ye)
{
    for(int i=0; i<n; i++)
        if((abs(x[i]-xe)+abs(y[i]-ye))%k[i]!=t[i])return 0;
    return 1;

}
int cal(int l,int r)
{
    if(r<=l)return 0;
    return (r-l-1)/60+1;
}
int main()
{

    int T,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        top=2;
        da[0]=db[0]=0;
        da[1]=db[1]=m+1;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d%d",&x[i],&y[i],&k[i],&t[i]);
            da[top]=x[i];
            db[top++]=y[i];
        }
        ll res=0;
        sort(da,da+top);
        sort(db,db+top);
        for(int i=0; i<top; i++)
            if(da[i]!=da[i+1])
                for(int j=0; j<top; j++)
                    if(db[j]!=db[j+1])
                        for(int x=0; x<60; x++)
                            for(int y=0; y<60; y++)
                                if(judge(da[i]+x,db[j]+y))
                                    res+=1ll*cal(da[i]+x,da[i+1])*cal(db[j]+y,db[j+1]);

        printf("%lld\n",res);
    }

    return 0;
}


你可能感兴趣的:(题目)