CodeForces 374 A. Inna and Pink Pony

本文介绍了一个有趣的编程问题:如何在遵循特定移动规则的情况下,将放置于棋盘上的糖果移动到角落,最少需要多少步。文章提供了详细的输入输出样例及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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;
}




### Codeforces 1732A Bestie 题目解析 对于给定的整数数组 \(a\) 和查询次数 \(q\),每次查询给出两个索引 \(l, r\),需要计算子数组 \([l,r]\) 的最大公约数(GCD)。如果 GCD 结果为 1,则返回 "YES";否则返回 "NO"[^4]。 #### 解决方案概述 为了高效解决这个问题,可以预先处理数据以便快速响应多个查询。具体方法如下: - **预处理阶段**:构建辅助结构来存储每一对可能区间的 GCD 值。 - **查询阶段**:利用已有的辅助结构,在常量时间内完成每个查询。 然而,考虑到内存限制以及效率问题,直接保存所有区间的结果并不现实。因此采用更优化的方法——稀疏表(Sparse Table),它允许 O(1) 时间内求任意连续子序列的最大值/最小值/GCD等问题,并且支持静态RMQ(Range Minimum Query)/RANGE_GCD等操作。 #### 实现细节 ##### 构建稀疏表 通过动态规划的方式填充二维表格 `st`,其中 `st[i][j]` 表示从位置 i 开始长度为 \(2^j\) 的子串的最大公约数值。初始化时只需考虑单元素情况即 j=0 的情形,之后逐步扩展至更大的范围直到覆盖整个输入序列。 ```cpp const int MAXN = 2e5 + 5; int st[MAXN][20]; // Sparse table for storing precomputed results. vector<int> nums; void build_sparse_table() { memset(st,-1,sizeof(st)); // Initialize the base case where interval length is one element only. for(int i = 0 ;i < nums.size(); ++i){ st[i][0]=nums[i]; } // Fill up sparse table using previously computed values. for (int j = 1;(1 << j)<=nums.size();++j){ for (int i = 0;i+(1<<j)-1<nums.size();++i){ if(i==0 || st[i][j-1]!=-1 && st[i+(1<<(j-1))][j-1]!=-1) st[i][j]=__gcd(st[i][j-1],st[i+(1<<(j-1))][j-1]); } } } ``` ##### 处理查询请求 当接收到具体的 l 和 r 参数后,可以通过查找对应的 log₂(r-l+1) 来定位合适的跳跃步长 k ,进而组合得到最终答案。 ```cpp string query(int L,int R){ int K=(int)(log2(R-L+1)); return __gcd(st[L][K],st[R-(1<<K)+1][K])==1?"YES":"NO"; } ``` 这种方法能在较短时间内完成大量查询任务的同时保持较低的空间开销,非常适合本题设定下的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值