A. Inna and Pink Pony----暴力
2017-08-20 14:41
645 查看
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
output
input
output
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.
题目链接:http://codeforces.com/contest/374/problem/A
这个题的题意真是绝了。
题意(转):有一个n*m的方格,位于(i,j)位置上,每次能同时向上下方向移动a并且向左右方向移动b(必须同时向两个方向移动),求到达任意一个角上所需要的最小移动次数((1,1),(1,m),(n,1),(n,m)中的任意一个),当然,任何时候不可以移动到方格外面
直观的想法是求起始点到四个点的坐标偏差,并计算x/y方向的运动次数,倘若两者运动次数一样,则可作为一个解,
若运动次数奇偶性不同,则无法修正。遍历四个点,取最小即可。
代码:
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.
题目链接:http://codeforces.com/contest/374/problem/A
这个题的题意真是绝了。
题意(转):有一个n*m的方格,位于(i,j)位置上,每次能同时向上下方向移动a并且向左右方向移动b(必须同时向两个方向移动),求到达任意一个角上所需要的最小移动次数((1,1),(1,m),(n,1),(n,m)中的任意一个),当然,任何时候不可以移动到方格外面
直观的想法是求起始点到四个点的坐标偏差,并计算x/y方向的运动次数,倘若两者运动次数一样,则可作为一个解,
若运动次数奇偶性不同,则无法修正。遍历四个点,取最小即可。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int main(){ int n,m,i,j,a,b,ans=1000000,x,y,ta,tb,sx[2],sy[2]; scanf("%d%d%d%d%d%d",&n,&m,&i,&j,&a,&b); sx[0]=1,sx[1]=n,sy[0]=1,sy[1]=m; if((i==1||i==n)&&(j==1||j==m)) ans=0; for(int p=0;p<2;p++){ for(int q=0;q<2;q++){ x=abs(i-sx[p]); y=abs(j-sy[q]); if(x%a==0&&y%b==0){ ta=x/a; tb=y/b; if(ta%2==tb%2){ if(ta==0&&(abs(i-1)<a)&&(abs(i-n)<a)) continue; if(tb==0&&(abs(j-1)<b)&&(abs(j-m)<b)) continue; ans=min(ans,max(ta,tb)); } } } } if(ans!=1000000) printf("%d\n",ans); else printf("Poor Inna and pony!\n"); return 0; }
相关文章推荐
- CF:374A - Inna and Pink Pony(思想题)
- 【打CF,学算法——二星级】CodeForces 374A Inna and Pink Pony (水题/坑题)
- CodeForces 374 A. Inna and Pink Pony
- CodeForces 374A Inna and Pink Pony
- A. Inna and Pink Pony
- Codeforces 374A - Inna and Pink Pony
- codeforces 374A Inna and Pink Pony(数学)
- 220 DIV2 A. Inna and Pink Pony
- CF #220 Inna and Pink Pony
- cf A. Inna and Pink Pony(思维题)
- 220 DIV2 A. Inna and Pink Pony
- Codeforces 400E Inna and Binary Logic(位运算+暴力)
- Codeforces 400A Inna and Choose Options(暴力)
- codeforces400A - Inna and Choose Options 暴力
- CodeForces 374 B.Inna and Nine(组合数学)
- CodeForces 558 C. Amr and Chemistry && 51NOD 1483 化学变换(暴力 + 贪心)
- Codeforces Round #332 (Div. 2)D. Spongebob and Squares(数学方程,暴力)
- CF #312.div2 -Amr and Chemistry-暴力+位运算
- uva 10825 - Anagram and Multiplication(暴力)
- CF #234 A. Inna and Choose Options