Restoring Painting

来源:互联网 发布:ubuntu命令行运行软件 编辑:程序博客网 时间:2024/05/29 03:02

Restoring Painting

CodeForces - 675B

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 from1 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 square2 × 2.
  • Four elements a, b, c 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 to0, 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 n,a, b,c 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.

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.

思路:观察发现中间那个数无论是几都无所谓,四四方格若相等中间的数都会被抵消。暴力枚举最左上的数,则另三个角的数是固定的,判断这三个数是否符合条件。


#include<stdio.h>int main(){int a,b,c,d,x,y,z,i;long long ans,n;while(~scanf("%lld%d%d%d%d",&n,&a,&b,&c,&d)){ans=0;for(i=1;i<=n;i++){x=b+i-c;y=i+a-d;z=b+i+a-c-d;if(x>=1&&x<=n&&y>=1&&y<=n&&z>=1&&z<=n)ans++;}printf("%lld\n",ans*n);}return 0;}





原创粉丝点击