Panasonic Programming Contest 2024(AtCoder Beginner Contest 354)D题

题目来源:D - AtCoder Wallpaper

写在前面:当时打虚拟重现赛时这题卡了我1个小时都没写出来,赛后看题解看了半天才理解(本人蒟蒻) 

回归正题-----

题意:求点(a,b)到点(c,d)这个范围内所有黑色区域的面积,并输出该面积的两倍。

思路:由图像可知x的周期为4,y的周期为2,我们将2,3,4象限的a,b,c,d全部移到第一象限,既然求点(a,b)到点(c,d)黑色区域的面积,那么我们以原点为参照,点(c,d)的面积减去点(a,d)的面积减去点(c,b)的面积加上点(a,b)的面积即是我们要求的区域面积。如下图:

x以4个周期,y以2个周期,那么可以算出一个总周期面积为8,以下图为例:

2个总周期面积就为16。

剩下多余的面积就要进行讨论,x可能的情况为0,1,2,3,y可能的情况为0,1,那么总共有8种可能,一一罗列即可。

实现代码:

#include<bits/stdc++.h>
#define int long long 
#define endl "\n"
#define fi first
#define se second
#define PII pair<int,int> 
using namespace std;
const int N=1e6+5;
int s[2][4]={{2,1,0,1},{1,2,1,0}};
int asd(int x,int y){
	int u=x%4,x1=x/4;
	int v=y%2,y1=y/2;
	int ans=0;
	ans+=8*x1*y1;//求有几个周期总面积
	if (u==0&&v==1){//分类讨论
        ans+=4*x1;
    }
    else if (u==1&&v==1){
        ans+=4*x1+3*y1+2;
    }
    else if (u==2&&v==1){
        ans+=4*x1+6*y1+3;
    }
    else if (u==3&&v==1){
        ans+=4*x1+7*y1+3;
    }
    else if (u==1&&v==0){
        ans+=3*y1;
    }
    else if (u==2&&v==0){
        ans+=6*y1;
    }
    else if (u==3&&v==0){
        ans+=7*y1;
    }
    return ans;
}
void solve(){
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	if(a<0){//a小于0移动到第一象限
		int t=4-(abs(a)%4);
		c+=(t-a); 
		a=t;
	} 
	if(b<0){//b小于0移动到第一象限
		int t=4-(abs(b)%4);
		d+=(t-b);
		b=t;
	}
	int ans=asd(c,d)-asd(a,d)-asd(c,b)+asd(a,b);//求面积的公式
	cout << ans << endl;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	//cin >> t;
	while(t--) solve();
	return 0;
}

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值