Cod sursa(job #945407)

Utilizator ZeusCatalin Tiseanu Zeus Data 1 mai 2013 21:11:23
Problema Dreptunghiuri Scor 100
Compilator cpp Status done
Runda Lista lui wefgef Marime 3.66 kb
/*
 Catalin-Stefan Tiseanu
 
 Pre-written code is assembled from various sources found online.
 Big thanks to the community for that !
 */

// Pre-written code below

using namespace std;

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DEBUG

#ifdef DEBUG
#define debug(args...)            {dbg,args; cerr<<endl;}
#else
#define debug(args...)              // Just strip off all debug tokens
#endif

struct debugger
{
  template<typename T> debugger& operator , (const T& v)
  {
  cerr<<v<<" ";
  return *this;
  }
} dbg;

// templates
template<typename T> int size(const T& c) { return int(c.size()); }
template<typename T> T abs(T x) { return x < 0 ? -x : x; }
template<typename T> T sqr(T x) { return x*x; }
template<typename T> bool remin(T& x, T y) { if (x <= y) return false; x = y; return true; }
template<typename T> bool remax(T& x, T y) { if (x >= y) return false; x = y; return true; }

// misc
#define EPSILON              1e-7

// types
typedef long long            int64;
typedef unsigned long long   uint64;

// shortcuts
#define all(_xx)             _xx.begin(), _xx.end()

#define pb                   push_back
#define vi                   vector<int>
#define vpii                 vector<pair<int,int> >
#define vpdd                 vector<paid<double,double> >

#define pii                  pair<int,int>
#define pdd                  pair<double, double>
#define mp(XX, YY)           make_pair(XX, YY)
#define fi                   first
#define se                   second

#define ll                   long long
#define SS                   stringstream

// for loops
#define re(II, NN)           for (int II(0), _NN(NN); (II) < (NN); ++(II))
#define fod(II, XX, YY)      for (int II(XX), _YY(YY); (II) >= (_YY); --(II))
#define fo(II, XX, YY)       for (int II(XX), _YY(YY); (II) <= (_YY); ++(II))
#define foreach(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)

// Useful hardware instructions
#define bitcount             __builtin_popcount
#define gcd                  __gcd

// Useful all around
#define checkbit(n,b)        ( (n >> b) & 1)
#define DREP(a)              sort(all(a));a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())

// Code written during the competition below

int main() {
  freopen("dreptunghiuri.in", "r", stdin);
  freopen("dreptunghiuri.out", "w", stdout);

  int N, M;
  ll res = 0;
  
  cin >> N >> M;
  N--, M--;
  
  pii v[4];
  v[0] = mp(0,0);
  
  fo (i, 0, N) fo (j, 1, M) {
    v[1] = mp(i , j);
    int my_factor = j / gcd(i, j);
    for(int kp = 1; kp * my_factor + i <= N &&
                   (kp * i * my_factor) / j <= M; ++kp) {
      // 0 = i * k + j * o;
      if (true) {
        int o = (-kp * i * my_factor) / j;
        int k = kp * my_factor;
        v[2] = mp(i + k, j + o);
        v[3] = mp(v[2].fi - i, v[2].se - j);
      
        //assert(v[3].fi - k == 0);
        //assert(v[3].se - o == 0);
      
        int h = v[1].se - v[3].se;
        int l = v[2].fi - v[0].fi;
      
        //debug(v[0].fi, v[0].se, v[1].fi, v[1].se, v[2].fi, v[2].se);
        //debug(i, j, " -> ", l, h, " = ", (N-l+1) * (M-h+1));
      
        if (l <= N && h <= M)
          res += (N-l+1) * (M-h+1);
      }
    }
  }
  
  cout << res << endl;
  
  return 0;
}