uva10214 Trees in a Wood.

2023-11-07 20:58
文章标签 trees wood uva10214

本文主要是介绍uva10214 Trees in a Wood.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

The saying “You can’t see the wood for the trees” is not only a
cliche, but is also incorrect. The real problem is that you can’t see
the trees for the wood. If you stand in the middle of a wood, the
trees tend to obscure each other and the number of distinct trees you
can actually see is quite small. This is especially true if the trees
are planted in rows and columns, because they tend to line up. The
purpose of this problem is to find how many distinct trees one can see
if one were standing on the position of a tree in the middle of the
wood. For a mathematically more precise description we assume that you
are situated at the origin of a coordinate system in the plane. Trees
are planted at all positions (x,y) ∈ ZZ×ZZ{(0,0)}, with |x|≤ a and
|y|≤ b. A tree at position (x,y) can be seen from the origin if there
are no other trees on the straight line from (0,0) to (x,y). Find the
number K of all the trees in the wood that can be seen from the origin
and the number N of all the trees to compute the fraction K N . Hint:
The Euler phi function φ(n) is defined to be the number of integers m
in the range 1 ≤ m ≤ n relatively prime to n: φ(n) = #{m | 1 ≤ m ≤ n
and gcd(m,n) = 1} (gcd = greatest common divisor) Instead of counting
(an adequate method for small n!) you could as well use the following
identity: φ(n) = n ∏ p∈P,p|n(1− 1 p), P = {p ∈ IN|p prime} Hint:
Remember that gcd(m,n) = gcd(m + n,n) = gcd(m + 2n,n) = gcd(m + in,i)
You might be surprised that the fraction K N converges to 6 π2 ≈
0.607927 for an infinitely largewood. Input Each scenario consists of a line with the two numbers a and b (separated by a white space), with 1
≤ a ≤ 2000 and 1 ≤ b ≤ 2000000. Input is terminated by a line with two
zeros. Output For each scenario print a line containing the fraction
with a precision of 7 digits after the decimal point. Error less than
2e-7 or 2∗10−7 will be tolerated.

求出所有互质的数对(x,y)(1<=x<=a,1<=y<=b)的个数ans,那么答案就是(4 * ans+4)/((2 * a+1) * (2 * b+1)-1)。
因为a比较小,b比较大,所以枚举i=1..a,计算1..b中与i互质的数。
不难发现,在区间[1..i],[i+1..2*i],…中各有phi(i)个满足条件的数,最后一段长度不正好为i的O(i)暴力即可。

#include<cstdio>
#include<cstring>
#define LL long long
int phi[2010],gcd[2010][2010];
int find(int a,int b)
{if (b==0) return a;if (gcd[a][b]) return gcd[a][b];return gcd[a][b]=find(b,a%b);
}
int main()
{int i,j,k,p,q,a,b;LL x,y,z,ans;phi[1]=1;for (i=2;i<=2000;i++)if (!phi[i])for (j=i;j<=2000;j+=i){if (!phi[j]) phi[j]=j;phi[j]=phi[j]/i*(i-1);}for (i=1;i<=2000;i++)for (j=1;j<=2000;j++)gcd[i][j]=find(i,j);while (scanf("%d%d",&a,&b)&&a){ans=0;for (i=1;i<=a;i++){ans+=(LL)phi[i]*(b/i);for (j=1;j<=b%i;j++)if (gcd[i][j]==1)ans++;}printf("%.7f\n",((double)ans*4+4)/((2*(double)a+1)*(2*b+1)-1));}
}

这篇关于uva10214 Trees in a Wood.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/366243

相关文章

【LeetCode】Unique Binary Search Trees I II

1、Unique Binary Search Trees  Total Accepted: 11405 Total Submissions: 32197 My Submissions Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example

leetcode-Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 去网上搜n个二叉搜索树的递推公式或者Catalan数,可以由h(n)=C(

Unique Binary Search Trees II问题及解法

问题描述: Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. 示例: Given n = 3, your program should return all 5 unique BST's shown below. 1

Unique Binary Search Trees问题及解法

问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 示例: Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1\

Codeforces Round #236 (Div. 2) B. Trees in a Row

B. Trees in a Row time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The Queen of England has n trees growing in a row i

HDU 2841 Visible Trees 容斥

题意(一开始也没怎么看懂): 一个人站在(0,0)处,树从(1,1)点开始排,共有m*n棵。如果两棵树在同一视线上(意思是两个点和(0,0)的斜率相同),则只看到前面一棵树,问你那个人能看到几棵树。 思路: 容斥。 简单分析之后,其实本质就是让你求gcd(x,y)=1有几组。(x,y)和(y,x)算两种。 这题和HDU 1695差不多,只不过那题(x,y)和(y,x)算一种。

在obspy中获得Wood-Anderson仪器振幅

在obspy中获得Wood-Anderson仪器振幅 在地震学学习中,有时候需要对地震的震级大小进行确定,这个时候可能需要将原始波形进行转换,得到Wood-Anderson仪器振幅。这里简单举个例子介绍一下如何通过obspy获得Wood-Anderson仪器振幅。 导入需要的包: from obspy import readfrom obspy.io.sac import sacpz.at

UVA 10303 How Many Trees?

题意:设f[i]表示一共有i个元素时二叉搜索树的个数,那么依次取1~n-1作为根节点,那么左右子树元素的个数就分别是(0,n-1),......,所以f[n] = f[0]*f[n-1]+f[1]*f[n-2]...+f[n-1]f[0],其实也就是Catalan数,高精度的计算,递推公式是f[n]=(4n-2)/(n+1)*f[n-1] #include <iostream>#include

HDU - 3015 Disharmony Trees

题意:给你n棵树的坐标x,高度h,让你分别按坐标,高度排序后,得到一新的序列,也可以理解为一颗组合成的新树,这棵树的坐标,高度都是排序来的,看了别人的解释,还是理解了老半天,然后就是求花费了,每任意两颗树的花费为 min(h[i],h[j])*abs(x[i]-x[j]),求所有组合的花费 思路:经过排序的处理后,就是仿着POJ-1990 的思想来做 了,也可以简化成:按高度排序后,然后按每棵

uva 10303 - How Many Trees?(卡特兰数)

题目链接:uva 10303 - How Many Trees? 卡特兰数,公式num[i + 1] = num[i] * (4 * i - 6) / i ( i ≥ 3)。 #include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int N = 6005;str