思路:對於座標爲的樹,需要滿足這棵樹纔不會被阻擋。(同一條直線上的樹從第二顆起會被遮擋)。
根據對稱性,設第一象限的答案爲,則四個象限的答案爲,再加上,每個座標軸上能看見一棵樹,答案爲。
由於列比較小,考慮枚舉列,對於列x,,素數的數量爲。爲歐拉函數。
根據題目提示,那麼得。
那麼執行次轉換有:
對於區間,直接統計即可。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int MAXN = 2000;
int phi[MAXN + 1];
void InitPhi() {
phi[1] = 1;
for (int i = 2; i <= MAXN; ++i) {
if (!phi[i]) {
for (int j = i; j <= MAXN; j += i) {
if (!phi[j]) {
phi[j] = j;
}
phi[j] = phi[j] / i * (i - 1);
}
}
}
}
//gcd(a,b)=gcd(b,a%b)
//gcd(a,0)=a
int gcd(int a, int b) {
while (b) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
int main(){
InitPhi();
int a, b;
while (cin >> a >> b && a && b) {
long long K = 0;
for (int raw = 1; raw <= a; ++raw) {
const long long k = b / raw;
K += k * phi[raw];
for (int y = k * raw + 1; y <= b; ++y) {
if (gcd(y, raw) == 1) {
++K;
}
}
}
const long long N = static_cast<long long>(2 * a + 1) * static_cast<long long>(2 * b + 1) - 1ll;
K *= 4;
K += 4;
printf("%.7lf\n", static_cast<double>(K) / static_cast<double>(N));
}
return 0;
}