Hackerland is a happy democratic country with m×n cities, arranged in
a rectangular m by n grid and connected by m roads in the east-west
direction and n roads in the north-south direction. By public demand,
this orthogonal road system is to be supplemented by a system of
highways in sucha way that there will be a direct connection between
any pair of cities. Each highway is a straight line going through two
or more cities. If two cities lie on the same highway, then they are
directly connected.If two cities are in the same row or column, then
they are already connected by the existing orthogonal road system
(each east-west road connects all the m cities in that row and each
north-south road connects all the n cities in that column), thus no
new highway is needed to connect them. Your task is to count the
number of highway that has to be built (a highway that goes through
several cities on a straight line is counted as a single highway).
Input The input contains several blocks of test cases. Each test case
consists of a single line containing two integers 1 ≤ n, m ≤ 300,
specifying the number of cities. The input is terminated by a test
case with n = m = 0. Output For each test case, output one line
containing a single integer, the number of highways that must be
built.
考虑在一个m行n列的矩形中找长x宽y的小矩形,则这个小矩形要满足如下条件:
1.x,y互质。否则重复计算。
2.左上角不能放下相同的矩形,否则重复计算。
这样对于一对互质的(i,j),答案为(m-i+1)* (n-j+1)-max(0,m-2* i+1)* max(0,n-2* j+1)【总数减去不合法(左上角有)的个数】
最后答案乘2(\和/)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int gcd[310][310];
int find(int a,int b)
{
if (b==0) return a;
if (gcd[a][b]) return gcd[a][b];
return gcd[a][b]=find(b,a%b);
}
int main()
{
int i,j,k,m,n,p,q,x,y,z,ans;
for (i=1;i<=300;i++)
for (j=1;j<=300;j++)
gcd[i][j]=find(i,j);
while (scanf("%d%d",&m,&n)&&n)
{
ans=0;
m--;
n--;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
if (gcd[i][j]==1)
ans+=(m-i+1)*(n-j+1)-max(0,m-2*i+1)*max(0,n-2*j+1);
printf("%d\n",ans*2);
}
}