2017暑期集训一Restoring Painting(思维)

来源:互联网 发布:java和php哪个前景好 编辑:程序博客网 时间:2024/06/14 13:40

K - *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 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.

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.  

    

思路:根据题目信息四个2x2的区域之和相等,正中间小方块存在于四个区域,故其值没有影响不用考虑,

然后设左上角为x1,右上角为x2,左下角为x3,右下角为x4,因为x1+a+b=x2+a+c=x3+b+d=x4+c+d,所以x2=x1+b-c,x3=x1+a-d,x4=x1+a+b-c-d;题目要求,x1,x2,x3,x4都要在1和n之间,所以根据四者之间的关系,令x1从1到n循环,并判断其余三者是不是同时都在1到n的范围内,在就计数,而且最中间数字可以使1到n的任意一个。

#include<cstdio>int main(){int a,b,c,d;long long n;while(scanf("%lld%d%d%d%d",&n,&a,&b,&c,&d)!=EOF){long long num=0;for(int x1=1;x1<=n;x1++){int x2=x1+b-c;int x3=x1+a-d;int x4=x1+a+b-c-d;if((x2>=1&&x2<=n)&&(x4>=1&&x4<=n)&&(x3>=1&&x3<=n))num++;}long long k=num*n;printf("%lld\n",k);}return 0; }