CF 1459B Move and Turn 题解(思维)

题目链接

题目大意

你现在原点((0,0))你可以向四个方向走(n(nle1e3))步,但是前提是你你必须和上次走的方向夹角为(90)

题目思路

(p=frac{n}{2})

若在一个方向走(p)

那么可以到达的点为(-p,-p+2,-p+4,...0...,p-4,p-2,p)

总共(p+1)个点

  • (n)为偶数,那么可以横向走(p)步,竖向走(p)步,答案为(p imes(p+1))
  • (n)为奇数,那么可以横向走(p+1)步,竖向走(p)步,或相反,答案为(2 imes (p+1) imes(p+2))

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=1e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,ans;
signed main(){
    cin>>n;
    int p=n/2;
    if(n%2==0){
        ans=(p+1)*(p+1);
    }else{
        ans=2*(p+1)*(p+2);
    }
    printf("%d
",ans);
    return 0;
}

卷也卷不过,躺又躺不平
原文地址:https://www.cnblogs.com/hunxuewangzi/p/14679631.html