Trees in a Wood. UVA - 10214

根据对称性求解出第一象限,然后乘以4加上四个坐标轴上的量即可得到最终的答案。根据几何图形的分析可以发现,假设从原点可以看见的树的坐标为(x,y),仅仅只有当gcd(x,y)==1的时候才不会被遮挡。所以问题也就变成了对于坐标x,首先求解小于等于x,并且和x互质的数的个数。同时我们可以发现,如果有gcd(x,y)==k那么肯定也就有gcd(x,y+x)==k,那么利用这条性质在纵坐标上就很好求解了,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

int a, b;

long long res;
long long amount;

long long getPhi(int t){
	long long ans = t;
	int temp = t;
	int up = floor(sqrt(temp) + 0.5);
	for (int i = 2; i <= up; i++){
		if (temp%i == 0){
			ans = ans/ i*(i-1);
			while (temp%i == 0) temp = temp / i;
		}
	}
	if (temp > 1) ans = ans / temp*(temp - 1);
	return ans;
}

int gcd(int a, int b){
	if (!b) return a;
	return gcd(b, a%b);
}

long long cal(){
	
	long long ans = 0;
	for (int i = 1; i <= a; i++){
        int k = b / i;
		long long phi = getPhi(i);
		ans += phi*k;
		for (int j = k*i + 1; j <= b; j++) {
			if (gcd(j, i)==1)
				ans++;
		}
	}
	return ans * 4 + 4;
}

int main(){
	while (cin >> a >> b){
		if (a == 0) break;
		long long amount = cal();
		long long total= (long long)(2 * a + 1)*(2 * b + 1) - 1;
		cout << fixed << setprecision(7) << (double)amount / total << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值