其他分享
首页 > 其他分享> > codeforces B. Odd Grasshopper

codeforces B. Odd Grasshopper

作者:互联网

B. Odd Grasshopper

The grasshopper is located on the numeric axis at the point with
coordinate x0.

Having nothing else to do he starts jumping between integer points on
the axis. Making a jump from a point with coordinate x with a distance
d to the left moves the grasshopper to a point with a coordinate x−d,
while jumping to the right moves him to a point with a coordinate x+d.

The grasshopper is very fond of positive integers, so for each integer
i starting with 1 the following holds: exactly i minutes after the
start he makes a jump with a distance of exactly i. So, in the first
minutes he jumps by 1, then by 2, and so on.

The direction of a jump is determined as follows: if the point where
the grasshopper was before the jump has an even coordinate, the
grasshopper jumps to the left, otherwise he jumps to the right.

For example, if after 18 consecutive jumps he arrives at the point
with a coordinate 7, he will jump by a distance of 19 to the right,
since 7 is an odd number, and will end up at a point 7+19=26. Since 26
is an even number, the next jump the grasshopper will make to the left
by a distance of 20, and it will move him to the point 26−20=6.

Find exactly which point the grasshopper will be at after exactly n
jumps.

题意:奇数跳后,偶数跳前,且跳的值都是当前第n次的n,求从a开始跳n次的坐标在哪里

思路: 演练一遍就会发现,这道题完全就是一个以4为一次循环而不断递增(至今不懂为什么这道题我做了一个小时 屮)

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10,maxn=32;
string a1,a2;
map<char,int>s;

int main()
{
    int T;cin>>T;
    while(T--)
    {
        ll x,y;cin>>x>>y;
        ll s,w;
        if(abs(x)&1)
        {
            s=y%4;
            if(s==0)cout<<x<<endl;
            if(s==1)cout<<x+y<<endl;
            if(s==2)cout<<x-1<<endl;
            if(s==3)cout<<x-y-1<<endl;

        }
        else
        {
            s=y%4;
            if(s==0)cout<<x<<endl;
            if(s==1)cout<<x-y<<endl;
            if(s==2)cout<<x+1<<endl;
            if(s==3)cout<<x+y+1<<endl;
        }
    }

    return 0;
}

标签:Grasshopper,point,jumps,grasshopper,codeforces,jump,coordinate,Odd,he
来源: https://blog.csdn.net/Excers/article/details/121293035