To enumerate each column, just find all y in g (x, y) = 1.
Because in 1 ~ x, if there are i coprime with x, then there are i in [1 + x, 2x], because each of the i coprime is added with x, and the common factor is still 1.
1≤y≤x There are phi (x)
x + 1≤y≤x also has phi (x)
2x + 1≤y≤x There are also phi (x)
.....
kx + 1≤y≤b .... direct statistics
#include<bits/stdc++.h>
using namespace std;
int phi[2005];
void euler_phi(int n){
for(int i=2;i<=n;i++) phi[i]=0;
phi[1]=1;
for(int i=2;i<=n;i++)if(!phi[n]){
for(int j=i;j<=n;j+=i){
if(!phi[j])
phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
}
bool gcd(int a,int b){
return b==0 ? a:gcd(b,a%b);
}
int main(){
euler_phi(2000);
int a,b;
while(scanf("%d%d", &a, &b) == 2 && a){
long long sum=(long long)4*a*b+(long long)2*(a+b);
int trees=0;
for(int x=1;x<=a;x++){
int k=b/x;
trees+=(long long)phi[x]*k;
for(int y=k*x+1;y<=b;y++){
if(gcd(x,y)) trees++;
}
}
trees *= 4; trees += 4;
double ans = (double)trees / sum;
printf("%.7lf\n", ans);
}
}