Cordefoces 675B - Restoring Painting 题解

这篇博客介绍了Codeforces上的一道题目675B - Restoring Painting,Vasya需要恢复一幅3x3的方格画,其中一些数字已知,要求找到满足条件的不同填数方案的数量。博客提供了问题描述、输入输出格式、示例以及解题思路和AC代码。
摘要由CSDN通过智能技术生成

Problem - 675B - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/problemset/problem/675/B

B. Restoring Painting

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.

  • The painting is a square 3 × 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.
  • The sum of integers in each of four squares 2 × 2 is equal to the sum of integers in the top left square 2 × 2.
  • Four elements abc and d are known and are located as shown on the picture below.

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.

Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.

Input

The first line of the input contains five integers nabc and d (1 ≤ n ≤ 100 000, 1 ≤ a, b, c, d ≤ n) — maximum possible value of an integer in the cell and four integers that Vasya remembers.

Output

Print one integer — the number of distinct valid squares.

Examples

input

Copy

2 1 1 1 2

output

Copy

2

input

Copy

3 3 1 2 3

output

Copy

6

Note

Below are all the possible paintings for the first sample.

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Apr/09/2022 12:14:00UTC+8 (h1).

Desktop version, switch to mobile version.

Privacy Policy

Supported by

翻译
小 V 在玩填数游戏,游戏在 3×3 的正方形中进行。

9 个格子中的数字都为 1∼n 的整数,不同格子中的数字可以相同。
正方形可以划分出 4 个 2×2 的小正方形,这四个正方形中的数之和相同。
如图所示,a, b, c, d 的值是已知的。

 
现在 小 V 想知道,剩下的 5 个数一共有多少种填法。

题意及思路
可以很容易得到,四周数字的填写与中间数字无关,因而我们枚举中间数 e 为1~n

所以可以知道每次枚举e时,a,b,c,d,e是固定值,因而四周数的相对差是相同的

所以根据n的大小得出四周数的范围,因为四个方块的和相同,则与:a+b, a+c, b+d, c+d有关

经过分析,四周数的范围与其中的 最大值max 与 最小值min 有关

范围大小是最大和减最小和加一,可以得出每次枚举的四周数范围为n-(max-min+1)+1

因为我们知道当差值过大以至于大于n时没有答案,所以每次枚举结果为max(n-(max-min+1)+1, 0)

因为枚举中间数, 所以次数即为n,答案为 n * max(n-(max-min+1)+1, 0)
 

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define sz(_s) (int)_s.size()
using namespace std;
int main(){
	long long a,b,c,d,n;
	cin>>n>>a>>b>>c>>d;
	int mx=max(max(a+b,a+c),max(d+b,d+c));
	int mn=min(min(a+b,a+c),min(d+b,d+c));
	long long ans=n*(n-mx+mn);
	if(ans<0) cout<<0;
	else cout<<ans;
                                                                                                                                                                                                                                                                                  
    return 0;
}
//ACplease!!!


/*  printf("                                                                \n");
	printf("                                                                \n");
	printf("       * * *               * * *             * * *             * * *            \n");
	printf("     *       *           *       *         *      *          *       *         \n");
	printf("    *        *          *         *       *        *        *         *        \n");
	printf("            *           *         *                *                  *      \n");
	printf("           *            *         *               *                  *     \n");
	printf("          *             *         *              *                  *       \n");
	printf("         *              *         *             *                  *            \n");
	printf("        *               *         *           *                  *            \n");
	printf("      *                  *       *          *                  *           \n");
	printf("    * * * * * * *          * * *          * * * * * * *      * * * * * * *                           \n");
*/    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值