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 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 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.
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.
Print one integer — the number of distinct valid squares.
2 1 1 1 2
2
3 3 1 2 3
6
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;
}