CodeForces - 304D Rectangle Puzzle II

You are given a rectangle grid. That grid's size is n × m. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates — a pair of integers (x, y) (0 ≤ x ≤ n, 0 ≤ y ≤ m).

Your task is to find a maximum sub-rectangle on the grid (x1, y1, x2, y2) so that it contains the given point (x, y), and its length-width ratio is exactly (a, b). In other words the following conditions must hold: 0 ≤ x1 ≤ x ≤ x2 ≤ n, 0 ≤ y1 ≤ y ≤ y2 ≤ m, .

The sides of this sub-rectangle should be parallel to the axes. And values x1, y1, x2, y2 should be integers.

If there are multiple solutions, find the rectangle which is closest to (x, y). Here "closest" means the Euclid distance between (x, y) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here "lexicographically minimum" means that we should consider the sub-rectangle as sequence of integers (x1, y1, x2, y2), so we can choose the lexicographically minimum one.

Input

The first line contains six integers n, m, x, y, a, b (1 ≤ n, m ≤ 109, 0 ≤ x ≤ n, 0 ≤ y ≤ m, 1 ≤ a ≤ n, 1 ≤ b ≤ m).

Output

Print four integers x1, y1, x2, y2, which represent the founded sub-rectangle whose left-bottom point is (x1, y1) and right-up point is (x2, y2).

Examples

Input

9 9 5 5 2 1

Output

1 3 9 7

Input

100 100 52 50 46 56

Output

17 8 86 92

题意:给出n, m, x, y, a, b(1 ≤ n, m ≤ 109, 0 ≤ x ≤ n, 0 ≤ y ≤ m, 1 ≤ a ≤ n, 1 ≤ b ≤ m)。在范围是(0,0)<-->(n,m)的矩形原区域找出包含(x,y)的(x1,y1)<-->(x2,y2)矩形满足:首先矩形最大,然后中心最靠近(x,y),最后字典序最小

题解:先确定最大矩形(x1,y1)-(x2,y2),然后根据矩形的中心 确定最靠近中心,若中心不为整点,注意字典序最小的条件

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,m,x,y,a,b;
int main()
{
    while(~scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&a,&b))
    {
        int x1=0,x2=0,y1=0,y2=0,x3,y3;
        int d=__gcd(a,b);
        a=a/d,b=b/d;
        d=min(n/a,m/b);
        x2=d*a,y2=d*b;
        x3=x2,y3=y2;
        if(x>x2/2) x3=min(n,(x-x2/2-(x2&1))+x2);// 中心若不是整数,多减1,保证字典序小
        if(y>y2/2) y3=min(m,(y-y2/2-(y2&1))+y2);
        printf("%d %d %d %d\n",x1+(x3-x2),y1+(y3-y2),x3,y3);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值