CF1607B.Odd Grasshopper

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

The grasshopper is very fond of positive integers, so for each integer ii starting with 11 the following holds: exactly ii minutes after the start he makes a jump with a distance of exactly ii. So, in the first minutes he jumps by 11, then by 22, 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 1818 consecutive jumps he arrives at the point with a coordinate 77, he will jump by a distance of 1919 to the right, since 77 is an odd number, and will end up at a point 7+19=267 + 19 = 26. Since 2626 is an even number, the next jump the grasshopper will make to the left by a distance of 2020, and it will move him to the point 26−20=626 - 20 = 6.

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

蚱蜢位于数轴上坐标为 x0x_0 的点处。

由于无事可做,它开始在数轴上的整数点之间跳跃。从坐标为 xx 的点向左跳跃距离 dd,会使其到达坐标为 x−dx - d 的点;向右跳跃则会使其到达坐标为 x+dx + d 的点。

蚱蜢非常喜爱正整数,因此对每个从 11 开始的整数 ii,均有如下规律:恰好在起跳后第 ii 分钟,它执行一次距离恰好为 ii 的跳跃。也就是说,它在第 11 分钟跳跃距离 11,第 22 分钟跳跃距离 22,依此类推。

每次跳跃的方向按如下规则确定:若蚱蜢在跳跃前所在点的坐标为偶数,则向左跳跃;否则向右跳跃。

例如,若蚱蜢经过连续 1818 次跳跃后到达坐标为 77 的点,则它将在第 1919 分钟向右跳跃距离 1919(因为 77 是奇数),最终到达点 7+19=267 + 19 = 26。由于 2626 是偶数,下一次(即第 2020 次)跳跃将向左进行,距离为 2020,从而到达点 26−20=626 - 20 = 6。

求蚱蜢在恰好完成 nn 次跳跃后所处的坐标。

输入格式

The first line of input contains an integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

Each of the following tt lines contains two integers x0x_0 (−1014≤x0≤1014-10^{14} \leq x_0 \leq 10^{14}) and nn (0≤n≤10140 \leq n \leq 10^{14}) — the coordinate of the grasshopper's initial position and the number of jumps.

输入的第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 测试用例的数量。

接下来的 tt 行,每行包含两个整数 x0x_0(−1014≤x0≤1014-10^{14} \leq x_0 \leq 10^{14})和 nn(0≤n≤10140 \leq n \leq 10^{14})—— 分别表示蚱蜢的初始位置坐标和跳跃次数。

输出格式

Print exactly tt lines. On the ii-th line print one integer — the answer to the ii-th test case — the coordinate of the point the grasshopper will be at after making nn jumps from the point x0x_0.

精确输出 tt 行。第 ii 行输出一个整数——第 ii 个测试用例的答案,即蚱蜢从初始点 x0x_0 出发,经过 nn 次跳跃后所到达的点的坐标。

输入输出样例

  • 输入#1

    9
    0 1
    0 2
    10 10
    10 99
    177 13
    10000000000 987654321
    -433494437 87178291199
    1 0
    -1 1

    输出#1

    -1
    1
    11
    110
    190
    9012345679
    -87611785637
    1
    0

说明/提示

The first two test cases in the example correspond to the first two jumps from the point x0=0x_0 = 0.

Since 00 is an even number, the first jump of length 11 is made to the left, and the grasshopper ends up at the point 0−1=−10 - 1 = -1.

Then, since −1-1 is an odd number, a jump of length 22 is made to the right, bringing the grasshopper to the point with coordinate −1+2=1-1 + 2 = 1.

示例中的前两个测试用例对应于从点 x0=0x_0 = 0 出发的前两次跳跃。

由于 00 是偶数,第一次长度为 11 的跳跃向左进行,蚱蜢最终落在点 0−1=−10 - 1 = -1 处。

接着,由于 −1-1 是奇数,第二次长度为 22 的跳跃向右进行,将蚱蜢带到坐标为 −1+2=1-1 + 2 = 1 的点。

输入解题思路,AI测评打分。不知道怎么写?

首页