Codeforces353-div2B Restoring Painting

Problem - B - Codeforces

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.

 のマスで が与えられる. の マスの和が全て等しくなるような ?を埋める 数字の組はいくつあるか?

 ?のマスをそれぞれ と置くと,それぞれ つの正方形の和は

となり,全て が入っていて,の値は関係ないことがわかる.なのでの組の倍でいい.
まず, の値を全探索する. と置くと,

と決まるので,その値が の間に収まっていれば,正しい組となる.その組の場合に を足していった(は何でも良い).

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
  ll n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;

  ll ans = 0;

  REP(i, 1, n + 1) {
      ll x = i;
      ll y = b + x - c;
      ll z = a + x - d;
      ll h = a + y - d;

      if(1 <= y && y <= n && 1 <= z && z <= n && 1 <= h && h <= n) {
          ans += n;
      }
  }

  cout << ans << endl;

  return 0;
}
May 17th, 2016