Codeforces 1459 B. Move and Turn(有意思的数学题)

A robot is standing at the origin of the infinite two-dimensional plane. Each second the robot moves exactly 1 meter in one of the four cardinal directions: north, south, west, and east. For the first step the robot can choose any of the four directions, but then at the end of every second it has to turn 90 degrees left or right with respect to the direction it just moved in. For example, if the robot has just moved north or south, the next step it takes has to be either west or east, and vice versa.

The robot makes exactly 𝑛 steps from its starting position according to the rules above. How many different points can the robot arrive to at the end? The final orientation of the robot can be ignored.

Input
The only line contains a single integer 𝑛 (1≤𝑛≤1000) — the number of steps the robot makes.

Output
Print a single integer — the number of different possible locations after exactly 𝑛 steps.

Examples
inputCopy
1
outputCopy
4
inputCopy
2
outputCopy
4
inputCopy
3
outputCopy
12
Note
In the first sample case, the robot will end up 1 meter north, south, west, or east depending on its initial direction.

In the second sample case, the robot will always end up 2‾√ meters north-west, north-east, south-west, or south-east.

题意:
走n步,上一步是左右,下一步就必须得是上下,反之亦然。求最终点可能的方案数。

思路:
比赛的时候是打表过的,但是这是可以用数学方法推出来的。

  • 假设 n = 2 k n=2k n=2k,那么水平和垂直方向都移动了 k k k步,水平坐标可能的值就是 k , k − 2 , k − 4... − k k,k-2,k-4...-k k,k2,k4...k,一共有 k + 1 k+1 k+1个,竖直方向同理。所以最终方案数就是水平坐标方案数乘以垂直坐标方案数为 ( n / 2 + 1 ) ∗ ( n / 2 + 1 ) (n/2+1)*(n/2+1) (n/2+1)(n/2+1)
  • 假设 n = 2 k + 1 n=2k+1 n=2k+1,和上面的分析方法一样,有两种可能:要么水平方向移动了 k k k步且竖直方向移动了 k + 1 k+1 k+1步,要么水平方向移动了 k + 1 k+1 k+1步且竖直方向移动了 k k k步;两种可能的方案数都是 ( k + 1 ) ∗ ( k + 2 ) (k+1)*(k+2) (k+1)(k+2),所以最终结果就是 2 ∗ ( k + 1 ) ∗ ( k + 2 ) 2*(k+1)*(k+2) 2(k+1)(k+2),也就是 2 ∗ ( n / 2 + 1 ) ∗ ( n / 2 + 2 ) 2*(n/2+1)*(n/2+2) 2(n/2+1)(n/2+2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;

int main() {
    int n;scanf("%d",&n);
    if(!(n & 1)) {
        printf("%d\n",(n / 2 + 1) * (n / 2 + 1));
    } else {
        printf("%d\n",2 * (n / 2 + 1) * (n / 2 + 2));
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值