Codeforces Round #353 (Div. 2)Restoring Painting

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

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<iostream>
using namespace std;
typedef long long ll;
int main()
{
    ll n,a,b,c,d,s=0;
    cin>>n>>a>>b>>c>>d;

    for(int i=1;i<=n;i++)
    {
        ll q1,q2,q3,q4,q5;
        q1=i;
        q2=q1+b-c;
        q4=q1+a-d;
        q5=q4+b-c;
        int cnt=0;
        if(q1>=1&&q1<=n)cnt++;
        if(q2>=1&&q2<=n)cnt++;
        if(q4>=1&&q4<=n)cnt++;
        if(q5>=1&&q5<=n)cnt++;
            if(cnt==4)
        s++;
    }
        cout<<s*n;
}
时间: 2024-10-06 13:47:12

Codeforces Round #353 (Div. 2)Restoring Painting的相关文章

递归解Codeforces Round #256 (Div. 2)C. Painting Fence

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

Codeforces Round #256 (Div. 2) C. Painting Fence

C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no

Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题

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

Codeforces Round #353 (Div. 2) ABCDE 题解 python

Problems # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring Painting standard input/output 1 s, 256 MB    x2519 C Money Transfers standard input/output 1 s, 256 MB    x724 D Tree Construction standard input/output 2

Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

题目链接:http://codeforces.com/problemset/problem/448/C ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时候查找比a[i]大的数的位置,然后插入,而父亲就是刚好比a[i]小的数或刚好大的数. 然后讨论是哪一个数. 比如给你3 1 2 ,如图 1的父亲是3 ,2的父亲是1. 那我其实只要找左边或右边出现最晚的数就行了,用pair的first表示a[i],second表示出现的顺序i. 1 #include

Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                               D. Painting The Wall User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not

Codeforces Round #353 (Div. 2) C. Money Transfers

题目大意:某个人在N个银行的存款和借款总和为0,这N个银行排成一个圆环,每次转账只能在相邻的两个银行之间进行.问进行多少次转账能使每个银行的存款为0. 题目保证总和一定是0. 有两个理论基础需要先说明. 一个总和为0的长度为N的区间内至多转账N-1次就可以使每个账户存款为0.因此只要找出总和为0的区间个个数最多设为W,那么答案就是N-W 一个区间总和为零的充要条件是,第一个元素之前的前缀和等于最后一个元素截止的前缀和. #include <iostream> #include <map&

Codeforces Round #353 (Div. 2) D. Tree Construction (BST询问父亲节点)

原题请戳这里 题意: 给出n个不同的数,按照给出的顺序构造二叉排序树BST,第1个数为根节点.输出2-n个 节点的父亲节点. 分析: 二叉排序树的平均复杂度是log2n,最坏情况下变成线性的,复杂度为n. 对n个节点的插入操作如果用结构体指针的写法最坏情况下为n2=1010,这样会超时. 开始没有注意这点,TLE.但是正好复习了BST的插入操作递归和非递归的写法. 易知,插入的数的父亲节点是已插入的比它大或者比它小的与其最接近的数之一. 这个题利用set保存输入的数,用两个map分别记录左右孩子