Codeforces Round #220 (Div. 2) A. Inna and Pink Pony 这个题目不简单的

来源:互联网 发布:广州密室逃脱 知乎 编辑:程序博客网 时间:2024/04/30 00:52
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 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!
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.

       
/*这个题目是个简单题目吗,其实想想好像是蛮简单的,但是想要做出来肯定是不容易的这个题目的意思是说有n*m的棋盘,一个东西在(x,y)处,移动是x+a,x-a,y+b,y-b等等的操作,问到四个角种某一个最少需要几步。我本来是想要用BFS来暴力搜一下的,但是当我看到n,m的大小,觉得不能用这个开个数组当地图,会RE的,那么就必须直接去求这个是多少,其实如果想要走到四个角,需要满足起点距离四个角某一点的垂直距离是可以整除b的,而且是可以水平距离必须是整除a的,这样以来的话就可以水平距离la=a*t,,hb=b*e的,那么需要这时候非常必然是t和e的最大值,但是一个非常容易忽略的问题是既然t和e的值不一样,那么少的那个是咋弄的,很简单,因为有一方向我们在+a或者-a,或者是+b或者-b,某一个方向这样的运动重复了偶数次所以才会大小之差,故而是t与e的奇偶行必定一样了。*/#include<iostream>#include<cstdio>#include<math.h>#include<cstring>using namespace std;int M=99999999;int main(){    int i,j,k;    int n,m;    int a,b;    int x,y;    while(scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&a,&b)!=EOF)    {       if((((x-1)%a!=0)&&((n-x)%a!=0))||(((y-1)%b!=0)&&((m-y)%b!= 0)))       {            printf("Poor Inna and pony!\n");            return 0;       }       if(((x==1)||(x==n))&&((y==1)||(y==m)))       {            printf("0\n");            return 0;       }        int ax=M;        if((((x+a<=n)||(x-a>0))&&((y+b<=m)||(y-b>0)))&&(((x-1)%a==0)&&((y-1)%b==0))&&((((x-1)/a)&1)==(((y-1)/b)&1)))        {            ax=min(ax,max((x-1)/a, (y-1)/ b));        }        if( (((x + a <= n) || (x - a > 0)) && ((y + b <= m) || (y - b > 0))) && (((x - 1) % a == 0) && ((m - y) % b == 0)) && ((((x - 1) / a) & 1) == (((m - y) / b) & 1)))        {            ax = min(ax, max((x - 1) / a, (m - y) / b));        }        if( (((x + a <= n) || (x - a > 0)) && ((y + b <= m) || (y - b > 0))) && (((n - x) % a == 0) && ((y - 1) % b == 0)) && ((((n - x) / a) & 1) == (((y - 1) / b) & 1)))        {            ax = min(ax, max((n - x) / a, (y - 1) / b));        }        if( (((x + a <= n) || (x - a > 0)) && ((y + b <= m) || (y - b > 0))) && (((n - x) % a == 0) && ((m - y) % b == 0)) && ((((n - x) / a) & 1) == (((m - y) / b) & 1)))        {            ax = min(ax, max((n - x) / a, (m - y) / b));        }        if(ax==M)        {            printf("Poor Inna and pony!\n");        }        else        {            printf("%d\n", ax);        }    }    return 0;}

0 0
原创粉丝点击