Pagini recente » Cod sursa (job #2557601) | Cod sursa (job #669676) | Cod sursa (job #224016) | Cod sursa (job #2333004) | Cod sursa (job #330362)
Cod sursa(job #330362)
#include <stdio.h>
#define dim 401
long long n, m, sol, sqrt[dim*dim];
long long nrsol[dim][dim];
int main() {
long long i, tmp, h, w, a, aa, bb, cc, dd, s1, s2;
freopen("dreptunghiuri.in", "r", stdin);
freopen("dreptunghiuri.out", "w", stdout);
scanf("%lld %lld\n", &n, &m);
for (i=1; i*i<dim*dim; ++i) sqrt[i*i]=i;
sol=(n-1)*(m-1); //nr of rectangles with h=1 and w=1
for (h=1; h<n; ++h)
for (w=1; w<m; ++w)
if (h==1 && w==1) continue;
else if (nrsol[h][w]) sol+=nrsol[h][w]*(n-h)*(m-w);
else if (nrsol[w][h]) sol+=nrsol[w][h]*(n-h)*(m-w);
else {
tmp=1; //temp nr of rect = 1(the rect itself)
for (a=1; a<h; ++a) {
aa=1;
bb=-w;
cc=a*(h-a);
dd=bb*bb-4*aa*cc;
if (dd<0) continue;
if (!dd) ++tmp;
else if (sqrt[dd]) {
s1=(-bb+sqrt[dd])/2*aa;
s2=(-bb-sqrt[dd])/2*aa;
if (s1>0) ++tmp;
if (s2>0) ++tmp;
}
}
tmp*=(n-h)*(m-w);
nrsol[h][w]=nrsol[w][h]=tmp;
sol+=tmp*(n-h)*(m-w);
}
printf("%lld\n", sol);
return 0;
}