K - *Restoring Painting(思维)

19 篇文章 0 订阅
17 篇文章 0 订阅

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 0001 ≤ 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.

Example
Input
2 1 1 1 2
Output
2
Input
3 3 1 2 3
Output
6
Note

Below are all the possible paintings for the first sample.  

In the second sample, only paintings displayed below satisfy all the rules.      

题意:有一个3*3的九宫格,给你a,b,c,d四个数,其余不知道,在这个九宫格中,每一个田字格的中的数字和是相同的,每个未知的格子中数字范围是1~n;问你有多少种这样的九宫格;

思路:由于a,b,c,d的值已经给你了,那么我们枚举左上角的格子的值,就可以算出其余三个角的值,如果这三个边角的格子的值在1~n之间,那么就符合要求;至于正中间的那个格子无所谓,从1~n的值中其余四角的情况都相同,所以只要遍历一遍左上角的值求出情况总数再乘以n即可;

下面附上代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{	
	long long n,a,b,c,d,q,w,e;
	while(~scanf("%lld %lld %lld %lld %lld",&n,&a,&b,&c,&d))
	{
		long long sum=0;
		for(long long i=1;i<=n;i++)
		{	
				int flag1=0,flag2=0,flag3=0;
				q=i+b-c;
				if(q>0&&q<=n) flag1=1;
				w=i+a-d;
				if(w>0&&w<=n) flag2=1;
				e=i+a+b-d-c;
				if(e>0&&e<=n) flag3=1;
				if(flag1&&flag2&&flag3)
					sum++;
			}
			printf("%lld\n",sum*n);
		}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值