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

本文探讨了一个机器人在无限二维平面上的行走策略。机器人从原点出发,每秒移动1米,且每次移动后必须改变方向90度。文章提供了一种算法来计算经过n步后,机器人可能达到的不同位置数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 n 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 n (1≤n≤1000) — the number of steps the robot makes.

Output
Print a single integer — the number of different possible locations after exactly n 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.
分奇偶看,如果是偶数那么就可以水平垂直走相同的数字,如果是奇数则有一个方向会多走一步。

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
#include<assert.h>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int a[maxn],b[maxn];
vector<int>l,r;
struct node{
	int l,r;
}n1[maxn];
void solve(){
	int n;
	cin>>n;
	if(n%2==1){
		int cnt =(n+1)/2;
		int ans=(cnt+1)*(cnt)/2;
		cout<<ans*4<<endl;
	}
	else{
		cout<<((n+2)/2)*((n+2)/2)<<endl;
	}
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值