Cod sursa(job #1723484)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 30 iunie 2016 18:59:07
Problema Dreptunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>

using namespace std;

const int N_MAX = 405;

int N, M;
int Roots[N_MAX * N_MAX + 1];
int64_t Answer;

int getInscribedRectangles(int Height, int Width) {
  int Count = 0;
  for(int i = 1; i < Height; i++) {
    int D = Width * Width - 4 * i * (Height - i);
    if(D >= 0) {
      int R = Roots[D];
      if(R * R == D and ((Width - R) & 1) == 0) {
        Count += (R == 0 ? 1 : 2);
      }
    }
  }
  return Count + 1;
}

int main() {
  ifstream f("dreptunghiuri.in");
  ofstream g("dreptunghiuri.out");
  
  f >> N >> M;
  
  for(int i = 1; i <= N_MAX; i++)
    Roots[i] = i * i;
  
  Answer = 0;
  for(int i = 1; i < N; i++) {
    for(int j = 1; j < M; j++) {
      Answer += 1LL * getInscribedRectangles(i, j) * (N - i) * (M - j);
    }
  }
  
  g << Answer << "\n";
  return 0;
}