【CodeForces】[675B]Restoring Painting

Problem Description

Loading...

Problem Analysis

At first I didn’t understand why the bottom-left and top-right blocks were constrained
until I realized
it means every 2×2 must equal the top-left 2×2

In the 3×3 grid
x a x
b x c
x d x
the middle cell is shared by all four 2×2 blocks and does not need to be considered
so we can enumerate the number in the top-left block
and calculate how many possibilities each of the other three blocks has

It cannot be calculated directly because
some numbers may make the conditions impossible to satisfy
(adding 1 to the other cells still gives a sum larger than the top-left one)
(adding n to the other cells still does not give a sum larger than the top-left one)

Also note the data range
__int64 is required

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
int main() {
	int n,a,b,c,d;
	while(scanf("%d %d %d %d %d",&n,&a,&b,&c,&d)!=EOF) {
		int cnt=0,t1=a+b,t2=b+d,t3=d+c,t4=a+c;
		for(int i=1; i<=n; i++) {
			int t=i+t1;
			if(t>t2&&t>t3&&t>t4&&t<=t2+n&&t<=t3+n&&t<=t4+n) {
				cnt++;
			}
		}
		printf("%I64d\n",(__int64)cnt*n);
	}
	return 0;
}

Problem address: 【CodeForces】[675B]Restoring Painting

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...