codeforces 374A Inna and Pink Pony(数学)

题目链接

Inna and Pink Pony
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n × m chessboard, a very tasty candy and two numbers a and b.

Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:

  • move the candy from position (x, y) on the board to position (x - a, y - b);
  • move the candy from position (x, y) on the board to position (x + a, y - b);
  • move the candy from position (x, y) on the board to position (x - a, y + b);
  • move the candy from position (x, y) on the board to position (x + a, y + b).

Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.

Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!

Input

The first line of the input contains six integers n, m, i, j, a, b (1 ≤ n, m ≤ 106; 1 ≤ i ≤ n; 1 ≤ j ≤ m; 1 ≤ a, b ≤ 106).

You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).

Output

In a single line print a single integer — the minimum number of moves needed to get the candy.

If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.

Sample test(s)
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!

题意:n*m的棋盘,糖的初始位置在(i,j),把糖移动到四个角落之一就可以得到糖,问得到糖的最小步数?

假设当前的点为(x,y),则下一步可以一定到:(x-a,y-b),(x-a,y+b),(x+a,y-b),(x+a,y+b)

题解:先分别考虑横坐标走到目的横坐标的最小步骤,和轴坐标走到目的纵坐标的最小步骤,如果两则的差为偶数则可以走到(注意:特判越界的情况)。

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
#include<set>
#include<map>
#define nn 1100
#define inff 0x3fffffff
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n,m,i,j,a,b;
int solve(int x,int y)
{
    x=abs(x-i),y=abs(y-j);
    if(x%a||y%b)
        return inff;
    x/=a,y/=b;
    if(x==y)
        return x;
    if(x>y)
    {
        if((x-y)%2)
            return inff;
        if(j+b>m&&j-b<1)
            return inff;
        return x;
    }
    if((y-x)%2)
        return inff;
    if(i-a<1&&i+a>n)
        return inff;
    return y;
}
int main()
{
    while(scanf("%d%d%d%d%d%d",&n,&m,&i,&j,&a,&b)!=EOF)
    {
        int ans=inff;
        ans=min(ans,solve(1,1));
        ans=min(ans,solve(1,m));
        ans=min(ans,solve(n,1));
        ans=min(ans,solve(n,m));
        if(ans==inff)
            puts("Poor Inna and pony!");
        else
            printf("%d\n",ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值