Cod sursa(job #1723476)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 30 iunie 2016 18:49:03
Problema Dreptunghiuri Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <vector>

using namespace std;

const int N_MAX = 405;
const int INF = 0x3f3f3f3f;

int N, M;
int Roots[N_MAX];
int64_t Answer;

void getRoots() {
  for(int i = 1; i < N_MAX; i++) {
    if(!Roots[i]) Roots[i] = INF;
    if(i * i < N_MAX) Roots[i * i] = i;
  }
}

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 and D < N_MAX and Roots[D] != INF) {
      D = Roots[D];
      Count += (Width - D >= 0 and (Width - D) % 2 == 0);
      Count += (Width + D <= 2 * Width and (Width + D) % 2 == 0);
      if(!D) Count--;
    }
  }
  return Count + 1;
}

int main() {
  ifstream f("dreptunghiuri.in");
  ofstream g("dreptunghiuri.out");
  
  f >> N >> M;
  
  getRoots();
  
  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;
}