CodeForces 374A 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 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.

Examples
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.



思路:

最近状态不行啊,各种被水题坑。。
各种坑的模拟题,这种题目还是画画图比较清楚。
因为x轴只有一个dx,y轴也只有一个dy,所以走法基本上是可以确定的(也就是说如果x的水平距离不是dx的倍数,那么肯定走不到,y轴类似)

坑点1:起点和终点的x相同或y相同(在同一点也算)
对于这种情况,要考虑是否能够往外走,因为不能沿着直线只能斜线,对于特殊情况不小心就走到外面去了
坑点2:神奇的走法
要怎么走呢,除了可以直接沿着斜线走过去以外,还有先碰到墙再弹回来的情况(如果y轴所需步数比x轴多,那么x轴走到边上后(例如左边),采取右上,左上的迂回走法)。如果两个坐标轴的步数不同,那么他们的差值必须是偶数(保证一个坐标不变另一个变化)

说多了都是泪啊,直接贴代码吧
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<b;i++)
#define debug(a) printf("a =: %d\n",a);
const int INF=0x3f3f3f3f;
const int maxn=1e6+50;
const int Mod=1e9+7;
typedef long long ll;
using namespace std;


int n,m,sx,sy,dx,dy;

int get(int sx,int sy,int ex,int ey){
  int cx=abs(sx-ex),cy=abs(sy-ey);
  if (cx==0 && cy==0 ) return 0;
 // if (!cx || !cy) return INF;
  if (cx%dx!=0 || cy%dy!=0) return INF;
  if (sx==ex){
    if (!(sx-dx>=1 || sx+dx<=n)) return INF;
  }else if (sy==ey){
    if (!(sy-dy>=1 || sy+dy<=m)) return INF;
  }
  int t1=cx/dx,t2=cy/dy;
 // printf("%d %d\n",ey,sy);
  int cha=abs(t1-t2);
  if (cha&1) return INF;
  return max(t1,t2);
}
int main()
{
   #ifndef ONLINE_JUDGE
       freopen("in.txt","r",stdin);
   //    freopen("out.txt","w",stdout);
   #endif


   while(scanf("%d %d %d %d %d %d",&n,&m,&sx,&sy,&dx,&dy)!=EOF){
        int ans=INF;
        ans=min(get(sx,sy,1,1),ans);
        ans=min(get(sx,sy,1,m),ans);
        ans=min(get(sx,sy,n,1),ans);
        ans=min(get(sx,sy,n,m),ans);
        if (ans==INF) 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、付费专栏及课程。

余额充值