您的位置:首页 > 其它

codeforces 374A Inna and Pink Pony(数学)

2015-01-09 20:16 323 查看
题目链接

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: