UVa 10214 Trees in a Wood. (数论-欧拉函数)
2017-02-19 16:28
197 查看
题意:给定一个abs(x) <= a, abs(y) <= b,除了原点之外的整点各有一棵树,可以相互阻挡,求从原点可以看到多少棵树。
析:由于a < b,所以我们可以一列一列的统计,第 x 列可以看到的树的个数就是 0 < y <= b中gcd(x, y) = 1的y的个数。
然后就可以分别统计,时间复杂度为O(a*a)。
代码如下:
析:由于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; }
相关文章推荐
- 20140925 【 初等数论--欧拉函数 】 UVa 10214 - Trees in a Wood
- UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
- GCD - Extreme (II) UVA - 11426 (数论,gcd,欧拉函数)题解
- Trees in a Wood. UVA - 10214 欧拉函数表
- UVA 11440 Help Tomisu 数论+欧拉函数
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
- UVa 11440 Help Tomisu (数论欧拉函数)
- UVALive 5964 LCM Extreme(数论 欧拉函数)
- UVA 10820 - Send a Table 数论 (欧拉函数)
- POJ2407 ZOJ1906 UVA10229 Relatives【欧拉函数+数论】
- 数论:欧拉函数 uva 10820
- UVA10179 Irreducable Basic Fractions【欧拉函数+数论】
- UVA 10214 Trees in a Wood. (欧拉函数)
- uva 11424 && 11426 GCD - Extreme(数论:欧拉函数)
- UVA 10820 - Send a Table 数论 (欧拉函数)
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
- UVA 10214 Trees in a Wood(欧拉函数)
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
- UVA 10820 - Send a Table 数论 (欧拉函数)
- UVa10214 - Trees in a Wood.(数论)