题意:给定一个abs(x) <= a, abs(y) <= b,除了原点之外的整点各有一棵树,可以相互阻挡,求从原点可以看到多少棵树。
析:由于a < b,所以我们可以一列一列的统计,第 x 列可以看到的树的个数就是 0 < y <= b中gcd(x, y) = 1的y的个数。
然后就可以分别统计,时间复杂度为O(a*a)。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++") #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-5; const int maxn = 2000 + 10; const int mod = 1e6; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int phi[maxn]; void phi_table(){ memset(phi, 0, sizeof phi); 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); } } int main(){ phi_table(); while(scanf("%d %d", &n, &m) == 2 && n){ LL ans = 0; for(int i = 1; i <= n; ++i){ ans += m / i * phi[i]; int t = m % i; for(int j = 1; j <= t; ++j) if(gcd(i, j) == 1) ++ans; } ans = ans * 4LL + 4LL; LL tmp = (LL)n * m * 4LL + 2LL * (m+n); double res = (double)ans / ((double)tmp); printf("%.10f \n", res); } return 0; }