题意:给定一个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;
}

UVa 10214 Trees in a Wood. (数论-欧拉函数)的更多相关文章

  1. UVa 10214 - Trees in a Wood.(欧拉函数)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVA 10214 Trees in a Wood(欧拉函数)

    题意:给你a.b(a<=2000,b<=2000000),问你从原点可以看到范围在(-a<=x<=a,-b<=y<=b)内整数点的个数 题解:首先只需要计算第一象限 ...

  3. 数论-欧拉函数-LightOJ - 1370

    我是知道φ(n)=n-1,n为质数  的,然后给的样例在纸上一算,嗯,好像是找往上最近的质数就行了,而且有些合数的欧拉函数值还会比比它小一点的质数的欧拉函数值要小,所以坚定了往上找最近的质数的决心—— ...

  4. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

  5. UVA 10214 Trees in a Wood

    https://vjudge.net/problem/UVA-10214 题意:你站在原点,每个坐标位置有一棵高度相同的树,问能看到多少棵树 ans=Σ gcd(x,y)=1 欧拉函数搞搞 #incl ...

  6. UVa 11440 Help Tomisu (数论欧拉函数)

    题意:给一个 n,m,统计 2 和 n!之间有多少个整数x,使得x的所有素因子都大于M. 析:首先我们能知道的是 所有素数因子都大于 m 造价于 和m!互质,然后能得到 gcd(k mod m!, m ...

  7. BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)

    今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...

  8. Codeforces_776E: The Holmes Children (数论 欧拉函数)

    题目链接 先看题目中给的函数f(n)和g(n) 对于f(n),若自然数对(x,y)满足 x+y=n,且gcd(x,y)=1,则这样的数对对数为f(n) 证明f(n)=phi(n) 设有命题 对任意自然 ...

  9. Codeforces 776E: The Holmes Children (数论 欧拉函数)

    题目链接 先看题目中给的函数f(n)和g(n) 对于f(n),若自然数对(x,y)满足 x+y=n,且gcd(x,y)=1,则这样的数对对数为f(n) 证明f(n)=phi(n) 设有命题 对任意自然 ...

随机推荐

  1. EasyDarwin开源流媒体server将select改为epoll的方法

    本文来自EasyDarwin团队Fantasy(fantasy(at)easydarwin.org) 一. EasyDarwin网络模型介绍 EventContext负责监听全部网络读写事件.Even ...

  2. 【BZOJ4950】lydsy七月月赛 C 二分图最大匹配

    [BZOJ4950]lydsy七月月赛 C 题面 题解:比较直接的想法就是:每行,每列的最大值都留下,剩下的格子都变成1.但是如果一个格子既是行的最大值又是列的最大值,那么我们只需要把它留下即可.这就 ...

  3. Sping中Bean配置的深入探讨

    一.p命名空间的使用 Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性.使用 p 命名空间后,基于 XML 的配 ...

  4. Unable to start adb server: adb server version (32) doesn't match this client (39); killing...

    关于Android studio 连接不上adb问题,有人说重启机器,有人说重启工具,也有人说adb kill-server.然后我都尝试过依然没有解决.通过各种查询.最终成功的解决!!! adb n ...

  5. "php"正则表达式使用总结

    一直对php的正则表达式的理解不是很深刻,而且是很一知半解,所以把自己用的的正则表达式总结下,以后方便查阅,以后遇到正则表达式的时候然后再追加到该文档的后面: /** * php常用正则表达式 * p ...

  6. Linux下MySQL、Apache、PHP源码安装全程实录(CentOS 6.4)

    转自http://www.zjmainstay.cn/lamp-config 本文记录了我自己配置LAMP的全过程,借此记录一下,同时希望能够帮助一下需要帮助的LINUX新人,跟我一起学习,一起进步. ...

  7. [RK3288][Android6.0] 调试笔记 --- 移除uboot和kernel开机logo【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/71600690 Platform: RockchipOS: Android 6.0Kernel ...

  8. hadoop 添加,删除节点

    http://www.cnblogs.com/tommyli/p/3418273.html

  9. phpcms v9中的$CATEGORYS栏目数组

    首先 如果不能用$CATEGORYS这个数组或掉不出来内容应加入 $CATEGORYS = getcache('category_content_1','commons'); 1.用途 $CATEGO ...

  10. the art of seo(chapter three)

    SEO Planning: Customizing Your Strategy ***Developing an SEO Plan Prior to Site Development***Determ ...