`
20386053
  • 浏览: 432420 次
文章分类
社区版块
存档分类
最新评论

CodeForces 374 A. Inna and Pink Pony

 
阅读更多

一定要考虑全面啊。。。。。

A. 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 ann × mchessboard, a very tasty candy and two numbersaandb.

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 integersn, 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 tonfrom top to bottom and the columns are numbered from 1 tomfrom left to right. Position(i, j)in the statement is a chessboard cell on the intersection of thei-th row and thej-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!
Note

Note to sample 1:

Inna and the pony can move the candy to position(1 + 2, 3 + 2) = (3, 5), from there they can move it to positions(3 - 2, 5 + 2) = (1, 7)and(3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int INF=0x3f3f3f3f;

int abs(int x)
{
    return x<0?-x:x;
}

int px[4],py[4];

int main()
{
    int n,m,x,y,a,b,dx,dy;
    cin>>n>>m>>x>>y>>a>>b;
    px[0]=1;py[0]=1;px[2]=1;py[2]=m;
    px[1]=n;py[1]=1;px[3]=n;py[3]=m;
    int ans=INF;
    for(int i=0;i<4;i++)
    {
        dx=abs(px[i]-x);dy=abs(py[i]-y);
        if(dx&&dy&&dx%a==0&&dy%b==0&&abs(dx/a-dy/b)%2==0) ans=min(ans,max(dx/a,dy/b));
        else if(dx==0&&dy==0) {ans=0; break;}
        else if(dx==0&&n-1>=a&&dy%b==0&&(dy/b)%2==0) ans=min(ans,dy/b);
        else if(dy==0&&m-1>=b&&dx%a==0&&(dx/a)%2==0) ans=min(ans,dx/a);
    }
    if(ans==INF) puts("Poor Inna and pony!");
    else cout<<ans<<endl;
    return 0;
}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics