2021 ICPC新疆省赛 G - cocktail with snake (思维+模拟)

The favorite game of Mr.Cocktail is dota2. Serpentine walking is a useful skill to dodge the enemy’s attack. Serpentine walking refers to
the path of movement that keeps swing direction like a snake. Suppose
the map is a n*m two-dimensional plane. Mr.Cocktail at the point (1,1)
in initial.

He will first go to the right to the end, that is, point (n,1). Then turn and go one step upwards to point (n,2). Go to the left again
to the end, that is, point (1,2),. Then go one step up, and go to the
right again to the end.
The picture above is the action line when n=3 and m=4.

Now the map size is n*m . The number of steps k of Cocktail’s movement are known. Find the Manhattan distance from the position
after k steps to the starting point according to the rule.

Manhattan distance Definition: The distance between two points measured along axes at right angles. In a plane with p1 at (x1, y1)
and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|. Input

The first line contains a positive integer T, which represents the number of groups to be tested. (1≤T≤104)

Next contains T lines, each row contains three integers
n,m,k(1≤n,m≤1018,0≤k≤min(n∗m−1,1018))

Output

For each test data, output a line of one coordinate to represent the answer. (A positive integer separated by two spaces represents the
x and y coordinates respectively) Example
Input

5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5

Output

1
2
3
2
1

思路

首先无论如何,x坐标永远都是k/n。

接下来推公式,可以分两种情况讨论

分奇数行和偶数行。偶数行从左往右,奇数行从右往左。然后输出行号+列号就没了。

#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,k;
        cin>>n>>m>>k;
        ll x,y;
        if(k<=n-1)
        {
            x=k+1;
            y=1;
        }
        else
        {
            ll temp=(k-(n-1));
            if(temp%n==0)
            {
                y=1+temp/n;
                if(temp/n%2==1)
                {
                    x=1;
                }
                else
                {
                    x=n;
                }
            }
            else
            {
                y=2+temp/n;
                if(temp/n%2==0)
                {
                    x=n-temp%n+1;
                }
                else
                {
                    x=temp%n;
                }
               
            }
        }
        //cout<<x<<" "<<y<<endl;
        ll ans;
        ans=(y-1)+(x-1);
        cout<<ans<<endl;
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值