Codeforces Round #691 (Div. 2) B. Move and Turn

链接

https://codeforces.com/contest/1459/problem/B

题意

一个机器人可以水平或垂直移动,不能连续水平或连续垂直移动,若移动 n n n 步,能到达多少个不同的终点?

思路

  • n n n 为偶数时:

水平移动 n / 2 n/2 n/2 步,在水平方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点;

垂直移动 n / 2 n/2 n/2 步,在垂直方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点。

组合后共有 ( n / 2 + 1 ) ( n / 2 + 1 ) (n/2+1)(n/2+1) (n/2+1)(n/2+1) 个点。

  • n n n 为奇数时:

水平移动 n / 2 + 1 n/2+1 n/2+1 步,在水平方向上最多可能到达 n / 2 + 2 n/2+2 n/2+2 个终点;

垂直移动 n / 2 n/2 n/2 步,在垂直方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点。

组合后共有 ( n / 2 + 1 ) ( n / 2 + 2 ) (n/2+1)(n/2+2) (n/2+1)(n/2+2) 个点。

垂直移动 n / 2 + 1 n/2+1 n/2+1 步,在垂直方向上最多可能到达 n / 2 + 2 n/2+2 n/2+2 个终点;

水平移动 n / 2 n/2 n/2 步,在水平方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点。

组合后共有 ( n / 2 + 1 ) ( n / 2 + 2 ) (n/2+1)(n/2+2) (n/2+1)(n/2+2) 个点。

这些点是不重复的,所以共有 2 ( n / 2 + 1 ) ( n / 2 + 2 ) 2(n/2+1)(n/2+2) 2(n/2+1)(n/2+2) 个点。

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
int main() {
    //freopen("E:/OneDrive/IO/in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    int k=n/2;
    if(n&1) cout<<2*(k+1)*(k+2)<<'\n';
    else cout<<(k+1)*(k+1)<<'\n';
    return 0;
}

水平移动 n / 2 n/2 n/2 步,在水平方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点

垂直移动 n / 2 n/2 n/2 步,在垂直方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点

组合后共有 ( n / 2 + 1 ) ( n / 2 + 1 ) (n/2+1)(n/2+1) (n/2+1)(n/2+1) 个点

  • n n n 为奇数时

水平移动 n / 2 + 1 n/2+1 n/2+1 步,在水平方向上最多可能到达 n / 2 + 2 n/2+2 n/2+2 个终点

垂直移动 n / 2 n/2 n/2 步,在垂直方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点

组合后共有 ( n / 2 + 1 ) ( n / 2 + 2 ) (n/2+1)(n/2+2) (n/2+1)(n/2+2) 个点

垂直移动 n / 2 + 1 n/2+1 n/2+1 步,在垂直方向上最多可能到达 n / 2 + 2 n/2+2 n/2+2 个终点

水平移动 n / 2 n/2 n/2 步,在水平方向上最多可能到达 n / 2 + 1 n/2+1 n/2+1 个终点

组合后共有 ( n / 2 + 1 ) ( n / 2 + 2 ) (n/2+1)(n/2+2) (n/2+1)(n/2+2) 个点

这些点是不重复的,所以共有 2 ( n / 2 + 1 ) ( n / 2 + 2 ) 2(n/2+1)(n/2+2) 2(n/2+1)(n/2+2) 个点

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
int main() {
    //freopen("E:/OneDrive/IO/in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    int k=n/2;
    if(n&1) cout<<2*(k+1)*(k+2)<<'\n';
    else cout<<(k+1)*(k+1)<<'\n';
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值