codeforces 675B B. 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 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
题意:
给出a,b,c,d,其中所有2*2的格子的和与左上角的2*2的和相等,且满足每个格子的数字在[1,n];问有多少种不同的方案;
思路:
枚举左上角的那个数,然后看有多少数可以填在那,最后再乘上n,因为最中间的那个选什么数字没有影响;
AC代码:
#include <bits/stdc++.h> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=1e4+25; int n,a,b,c,d,sum; int main() { scanf("%d%d%d%d%d",&n,&a,&b,&c,&d); LL ans=0; int temp=0; for(int i=1;i<=n;i++) { sum=a+b+i; if(sum-b-d<1||sum-b-d>n)continue; if(sum-a-c<1||sum-a-c>n)continue; if(sum-c-d<1||sum-c-d>n)continue; temp++; } ans=(LL)n*(LL)temp; cout<<ans<<"\n"; return 0; }