UVa 10214 Trees in a Wood. (数论-欧拉函数)
函数 in UVa 欧拉 数论 Trees
2023-09-11 14:17:18 时间
题意:给定一个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; }
相关文章
- np.diff函数
- 训练图像预处理函数功能(paddle)
- taro 不支持render中,使用函数多条件渲染
- 第117章 SQL函数 REPLICATE
- 第123章 Caché 函数大全 $ZWUNPACK,$ZWBUNPACK函数
- 指针数组 数组指针 函数指针 指针函数
- C++第12周项目2——分段函数
- 码栈开发手册(四)---编码方式开发(文件相关函数)
- mfc 创建线程函数AfxBeginThread,线程中访问mfc控件
- 【C++】函数对象
- C++编程规范之20:避免函数过长,避免嵌套过深
- 用函数式编程对JavaScript进行断舍离
- hdu 1171 Big Event in HDU (01背包, 母函数)
- C++-浅谈逆向——32位逆向分析技术(1.函数)
- [Mysql] FIND_IN_SET函数